LibreOffice Module codemaker (master) 1
cppuoptions.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <stdio.h>
21#include <string.h>
22
23#include "cppuoptions.hxx"
24
25#ifdef SAL_UNX
26#define SEPARATOR '/'
27#else
28#define SEPARATOR '\\'
29#endif
30
31bool CppuOptions::initOptions(int ac, char* av[], bool bCmdFile)
32{
33 bool ret = true;
34 int i=0;
35
36 if (!bCmdFile)
37 {
38 bCmdFile = true;
39
40 OString name(av[0]);
41 sal_Int32 index = name.lastIndexOf(SEPARATOR);
42 m_program = name.copy(index > 0 ? index+1 : 0);
43
44 if (ac < 2)
45 {
46 fprintf(stderr, "%s", prepareHelp().getStr());
47 ret = false;
48 }
49
50 i = 1;
51 }
52 else
53 {
54 i = 0;
55 }
56
57 char *s=nullptr;
58 for( ; i < ac; i++)
59 {
60 if (av[i][0] == '-')
61 {
62 switch (av[i][1])
63 {
64 case 'O':
65 if (av[i][2] == '\0')
66 {
67 if (i < ac - 1 && av[i+1][0] != '-')
68 {
69 i++;
70 s = av[i];
71 }
72 else
73 {
74 OString tmp("'-O', please check");
75 if (i <= ac - 1)
76 {
77 tmp += OString::Concat(" your input '") + av[i+1] + "'";
78 }
79
80 throw IllegalArgument(tmp);
81 }
82 }
83 else
84 {
85 s = av[i] + 2;
86 }
87
88 m_options["-O"] = OString(s);
89 break;
90 case 'n':
91 if (av[i][2] != 'D' || av[i][3] != '\0')
92 {
93 OString tmp = OString::Concat("'-nD', please check your input '") + av[i] + "'";
94 throw IllegalArgument(tmp);
95 }
96
97 m_options["-nD"] = OString();
98 break;
99 case 'T':
100 if (av[i][2] == '\0')
101 {
102 if (i < ac - 1 && av[i+1][0] != '-')
103 {
104 i++;
105 s = av[i];
106 }
107 else
108 {
109 OString tmp("'-T', please check");
110 if (i <= ac - 1)
111 {
112 tmp += OString::Concat(" your input '") + av[i+1] + "'";
113 }
114
115 throw IllegalArgument(tmp);
116 }
117 }
118 else
119 {
120 s = av[i] + 2;
121 }
122
123 if (m_options.count("-T") > 0)
124 {
125 OString tmp = m_options["-T"] + ";" + s;
126 m_options["-T"] = tmp;
127 }
128 else
129 {
130 m_options["-T"] = OString(s);
131 }
132 break;
133 case 'L':
134 if (av[i][2] != '\0')
135 {
136 OString tmp("'-L', please check");
137 if (i <= ac - 1)
138 {
139 tmp += OString::Concat(" your input '") + av[i] + "'";
140 }
141
142 throw IllegalArgument(tmp);
143 }
144
145 if (isValid("-C") || isValid("-CS"))
146 {
147 throw IllegalArgument("'-L' could not be combined with '-C' or '-CS' option");
148 }
149 m_options["-L"] = OString();
150 break;
151 case 'C':
152 if (av[i][2] == 'S')
153 {
154 if (av[i][3] != '\0')
155 {
156 OString tmp("'-CS', please check");
157 if (i <= ac - 1)
158 {
159 tmp += OString::Concat(" your input '") + av[i] + "'";
160 }
161
162 throw IllegalArgument(tmp);
163 }
164
165 if (isValid("-L") || isValid("-C"))
166 {
167 throw IllegalArgument("'-CS' could not be combined with '-L' or '-C' option");
168 }
169 m_options["-CS"] = OString();
170 break;
171 }
172 else if (av[i][2] != '\0')
173 {
174 OString tmp("'-C', please check");
175 if (i <= ac - 1)
176 {
177 tmp += OString::Concat(" your input '") + av[i] + "'";
178 }
179
180 throw IllegalArgument(tmp);
181 }
182
183 if (isValid("-L") || isValid("-CS"))
184 {
185 throw IllegalArgument("'-C' could not be combined with '-L' or '-CS' option");
186 }
187 m_options["-C"] = OString();
188 break;
189 case 'G':
190 if (av[i][2] == 'c')
191 {
192 if (av[i][3] != '\0')
193 {
194 OString tmp("'-Gc', please check");
195 if (i <= ac - 1)
196 {
197 tmp += OString::Concat(" your input '") + av[i] + "'";
198 }
199
200 throw IllegalArgument(tmp);
201 }
202
203 m_options["-Gc"] = OString();
204 break;
205 }
206 else if (av[i][2] != '\0')
207 {
208 OString tmp("'-G', please check");
209 if (i <= ac - 1)
210 {
211 tmp += OString::Concat(" your input '") + av[i] + "'";
212 }
213
214 throw IllegalArgument(tmp);
215 }
216
217 m_options["-G"] = OString();
218 break;
219 case 'X': // support for eXtra type rdbs
220 {
221 if (av[i][2] == '\0')
222 {
223 if (i < ac - 1 && av[i+1][0] != '-')
224 {
225 i++;
226 s = av[i];
227 }
228 else
229 {
230 OString tmp("'-X', please check");
231 if (i <= ac - 1)
232 {
233 tmp += OString::Concat(" your input '") + av[i+1] + "'";
234 }
235
236 throw IllegalArgument(tmp);
237 }
238 }
239 else
240 {
241 s = av[i] + 2;
242 }
243
244 m_extra_input_files.emplace_back(s );
245 break;
246 }
247
248 default:
249 throw IllegalArgument(OString::Concat("the option is unknown") + av[i]);
250 }
251 } else
252 {
253 if (av[i][0] == '@')
254 {
255 FILE* cmdFile = fopen(av[i]+1, "r");
256 if( cmdFile == nullptr )
257 {
258 fprintf(stderr, "%s", prepareHelp().getStr());
259 ret = false;
260 }
261 else
262 {
263 int rargc=0;
264 char* rargv[512];
265 char buffer[512];
266
267 while (fscanf(cmdFile, "%511s", buffer) != EOF && rargc < 512)
268 {
269 rargv[rargc]= strdup(buffer);
270 rargc++;
271 }
272 fclose(cmdFile);
273
274 ret = initOptions(rargc, rargv, bCmdFile);
275
276 for (int j=0; j < rargc; j++)
277 {
278 free(rargv[j]);
279 }
280 }
281 }
282 else
283 {
284 m_inputFiles.emplace_back(av[i]);
285 }
286 }
287 }
288
289 return ret;
290}
291
293{
294 OString help = "\nusing: " +
295 m_program + " [-options] file_1 ... file_n\nOptions:\n"
296 " -O<path> = path describes the root directory for the generated output.\n"
297 " The output directory tree is generated under this directory.\n"
298 " -T<name> = name specifies a type or a list of types. The output for this\n"
299 " [t1;...] type is generated. If no '-T' option is specified,\n"
300 " then output for all types is generated.\n"
301 " Example: 'com.sun.star.uno.XInterface' is a valid type.\n"
302 " -L = UNO type functions are generated lightweight, that means only\n"
303 " the name and typeclass are given and everything else is retrieved\n"
304 " from the type library dynamically. The default is that UNO type\n"
305 " functions provides enough type information for bootstrapping C++.\n"
306 " '-L' should be the default for external components.\n"
307 " -C = UNO type functions are generated comprehensive that means all\n"
308 " necessary information is available for bridging the type in UNO.\n"
309 " -nD = no dependent types are generated.\n"
310 " -G = generate only target files which does not exists.\n"
311 " -Gc = generate only target files which content will be changed.\n"
312 " -X<file> = extra types which will not be taken into account for generation.\n\n" +
314
315 return help;
316}
317
319{
320 OString version = m_program + " Version 2.0\n\n";
321 return version;
322}
323
324
325/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OString prepareVersion() const
bool initOptions(int ac, char *av[], bool bCmdFile=false) override
Definition: cppuoptions.cxx:31
OString prepareHelp() override
StringVector m_inputFiles
Definition: options.hxx:65
OptionMap m_options
Definition: options.hxx:67
StringVector m_extra_input_files
Definition: options.hxx:66
bool isValid(const ::rtl::OString &option) const
Definition: options.cxx:26
::rtl::OString m_program
Definition: options.hxx:64
#define SEPARATOR
Definition: global.cxx:46
const char * name
int i
index
help