Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / codemaker / source / cppumaker / cppuoptions.cxx
blob54bfec3393f94fc09606c6542bc8035a9d392253
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <stdio.h>
21 #include <string.h>
23 #include "cppuoptions.hxx"
24 #include <osl/thread.h>
25 #include <osl/process.h>
27 using ::rtl::OString;
29 #ifdef SAL_UNX
30 #define SEPARATOR '/'
31 #else
32 #define SEPARATOR '\\'
33 #endif
35 bool CppuOptions::initOptions(int ac, char* av[], bool bCmdFile)
37 bool ret = true;
38 int i=0;
40 if (!bCmdFile)
42 bCmdFile = true;
44 OString name(av[0]);
45 sal_Int32 index = name.lastIndexOf(SEPARATOR);
46 m_program = name.copy(index > 0 ? index+1 : 0);
48 if (ac < 2)
50 fprintf(stderr, "%s", prepareHelp().getStr());
51 ret = false;
54 i = 1;
56 else
58 i = 0;
61 char *s=nullptr;
62 for( ; i < ac; i++)
64 if (av[i][0] == '-')
66 switch (av[i][1])
68 case 'O':
69 if (av[i][2] == '\0')
71 if (i < ac - 1 && av[i+1][0] != '-')
73 i++;
74 s = av[i];
76 else
78 OString tmp("'-O', please check");
79 if (i <= ac - 1)
81 tmp += " your input '" + OString(av[i+1]) + "'";
84 throw IllegalArgument(tmp);
87 else
89 s = av[i] + 2;
92 m_options["-O"] = OString(s);
93 break;
94 case 'n':
95 if (av[i][2] != 'D' || av[i][3] != '\0')
97 OString tmp = "'-nD', please check your input '" + OString(av[i]) + "'";
98 throw IllegalArgument(tmp);
101 m_options["-nD"] = OString();
102 break;
103 case 'T':
104 if (av[i][2] == '\0')
106 if (i < ac - 1 && av[i+1][0] != '-')
108 i++;
109 s = av[i];
111 else
113 OString tmp("'-T', please check");
114 if (i <= ac - 1)
116 tmp += " your input '" + OString(av[i+1]) + "'";
119 throw IllegalArgument(tmp);
122 else
124 s = av[i] + 2;
127 if (m_options.count("-T") > 0)
129 OString tmp(m_options["-T"]);
130 tmp = tmp + ";" + s;
131 m_options["-T"] = tmp;
133 else
135 m_options["-T"] = OString(s);
137 break;
138 case 'L':
139 if (av[i][2] != '\0')
141 OString tmp("'-L', please check");
142 if (i <= ac - 1)
144 tmp += " your input '" + OString(av[i]) + "'";
147 throw IllegalArgument(tmp);
150 if (isValid("-C") || isValid("-CS"))
152 throw IllegalArgument("'-L' could not be combined with '-C' or '-CS' option");
154 m_options["-L"] = OString();
155 break;
156 case 'C':
157 if (av[i][2] == 'S')
159 if (av[i][3] != '\0')
161 OString tmp("'-CS', please check");
162 if (i <= ac - 1)
164 tmp += " your input '" + OString(av[i]) + "'";
167 throw IllegalArgument(tmp);
170 if (isValid("-L") || isValid("-C"))
172 throw IllegalArgument("'-CS' could not be combined with '-L' or '-C' option");
174 m_options["-CS"] = OString();
175 break;
177 else if (av[i][2] != '\0')
179 OString tmp("'-C', please check");
180 if (i <= ac - 1)
182 tmp += " your input '" + OString(av[i]) + "'";
185 throw IllegalArgument(tmp);
188 if (isValid("-L") || isValid("-CS"))
190 throw IllegalArgument("'-C' could not be combined with '-L' or '-CS' option");
192 m_options["-C"] = OString();
193 break;
194 case 'G':
195 if (av[i][2] == 'c')
197 if (av[i][3] != '\0')
199 OString tmp("'-Gc', please check");
200 if (i <= ac - 1)
202 tmp += " your input '" + OString(av[i]) + "'";
205 throw IllegalArgument(tmp);
208 m_options["-Gc"] = OString();
209 break;
211 else if (av[i][2] != '\0')
213 OString tmp("'-G', please check");
214 if (i <= ac - 1)
216 tmp += " your input '" + OString(av[i]) + "'";
219 throw IllegalArgument(tmp);
222 m_options["-G"] = OString();
223 break;
224 case 'X': // support for eXtra type rdbs
226 if (av[i][2] == '\0')
228 if (i < ac - 1 && av[i+1][0] != '-')
230 i++;
231 s = av[i];
233 else
235 OString tmp("'-X', please check");
236 if (i <= ac - 1)
238 tmp += " your input '" + OString(av[i+1]) + "'";
241 throw IllegalArgument(tmp);
244 else
246 s = av[i] + 2;
249 m_extra_input_files.emplace_back(s );
250 break;
253 default:
254 throw IllegalArgument("the option is unknown" + OString(av[i]));
256 } else
258 if (av[i][0] == '@')
260 FILE* cmdFile = fopen(av[i]+1, "r");
261 if( cmdFile == nullptr )
263 fprintf(stderr, "%s", prepareHelp().getStr());
264 ret = false;
266 else
268 int rargc=0;
269 char* rargv[512];
270 char buffer[512];
272 while (fscanf(cmdFile, "%511s", buffer) != EOF && rargc < 512)
274 rargv[rargc]= strdup(buffer);
275 rargc++;
277 fclose(cmdFile);
279 ret = initOptions(rargc, rargv, bCmdFile);
281 for (int j=0; j < rargc; j++)
283 free(rargv[j]);
287 else
289 m_inputFiles.emplace_back(av[i]);
294 return ret;
297 OString CppuOptions::prepareHelp()
299 OString help("\nusing: ");
300 help += m_program + " [-options] file_1 ... file_n\nOptions:\n"
301 " -O<path> = path describes the root directory for the generated output.\n"
302 " The output directory tree is generated under this directory.\n"
303 " -T<name> = name specifies a type or a list of types. The output for this\n"
304 " [t1;...] type is generated. If no '-T' option is specified,\n"
305 " then output for all types is generated.\n"
306 " Example: 'com.sun.star.uno.XInterface' is a valid type.\n"
307 " -L = UNO type functions are generated lightweight, that means only\n"
308 " the name and typeclass are given and everything else is retrieved\n"
309 " from the type library dynamically. The default is that UNO type\n"
310 " functions provides enough type information for bootstrapping C++.\n"
311 " '-L' should be the default for external components.\n"
312 " -C = UNO type functions are generated comprehensive that means all\n"
313 " necessary information is available for bridging the type in UNO.\n"
314 " -nD = no dependent types are generated.\n"
315 " -G = generate only target files which does not exists.\n"
316 " -Gc = generate only target files which content will be changed.\n"
317 " -X<file> = extra types which will not be taken into account for generation.\n\n";
318 help += prepareVersion();
320 return help;
323 OString CppuOptions::prepareVersion() const
325 OString version = m_program + " Version 2.0\n\n";
326 return version;
330 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */