Branch libreoffice-24-8-3
[LibreOffice.git] / codemaker / source / cppumaker / cppuoptions.cxx
blobd16c7dacecba6a3181ad6b89d623f63ea52a7788
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"
25 #ifdef SAL_UNX
26 #define SEPARATOR '/'
27 #else
28 #define SEPARATOR '\\'
29 #endif
31 bool CppuOptions::initOptions(int ac, char* av[], bool bCmdFile)
33 bool ret = true;
34 int i=0;
36 if (!bCmdFile)
38 bCmdFile = true;
40 OString name(av[0]);
41 sal_Int32 index = name.lastIndexOf(SEPARATOR);
42 m_program = name.copy(index > 0 ? index+1 : 0);
44 if (ac < 2)
46 fprintf(stderr, "%s", prepareHelp().getStr());
47 ret = false;
50 i = 1;
52 else
54 i = 0;
57 char *s=nullptr;
58 for( ; i < ac; i++)
60 if (av[i][0] == '-')
62 switch (av[i][1])
64 case 'O':
65 if (av[i][2] == '\0')
67 if (i < ac - 1 && av[i+1][0] != '-')
69 i++;
70 s = av[i];
72 else
74 OString tmp("'-O', please check"_ostr);
75 if (i <= ac - 1)
77 tmp += OString::Concat(" your input '") + av[i+1] + "'";
80 throw IllegalArgument(tmp);
83 else
85 s = av[i] + 2;
88 m_options["-O"_ostr] = OString(s);
89 break;
90 case 'n':
91 if (av[i][2] != 'D' || av[i][3] != '\0')
93 OString tmp = OString::Concat("'-nD', please check your input '") + av[i] + "'";
94 throw IllegalArgument(tmp);
97 m_options["-nD"_ostr] = OString();
98 break;
99 case 'T':
100 if (av[i][2] == '\0')
102 if (i < ac - 1 && av[i+1][0] != '-')
104 i++;
105 s = av[i];
107 else
109 OString tmp("'-T', please check"_ostr);
110 if (i <= ac - 1)
112 tmp += OString::Concat(" your input '") + av[i+1] + "'";
115 throw IllegalArgument(tmp);
118 else
120 s = av[i] + 2;
123 if (m_options.count("-T"_ostr) > 0)
125 OString tmp = m_options["-T"_ostr] + ";" + s;
126 m_options["-T"_ostr] = tmp;
128 else
130 m_options["-T"_ostr] = OString(s);
132 break;
133 case 'L':
134 if (av[i][2] != '\0')
136 OString tmp("'-L', please check"_ostr);
137 if (i <= ac - 1)
139 tmp += OString::Concat(" your input '") + av[i] + "'";
142 throw IllegalArgument(tmp);
145 if (isValid("-C"_ostr) || isValid("-CS"_ostr))
147 throw IllegalArgument("'-L' could not be combined with '-C' or '-CS' option"_ostr);
149 m_options["-L"_ostr] = OString();
150 break;
151 case 'C':
152 if (av[i][2] == 'S')
154 if (av[i][3] != '\0')
156 OString tmp("'-CS', please check"_ostr);
157 if (i <= ac - 1)
159 tmp += OString::Concat(" your input '") + av[i] + "'";
162 throw IllegalArgument(tmp);
165 if (isValid("-L"_ostr) || isValid("-C"_ostr))
167 throw IllegalArgument("'-CS' could not be combined with '-L' or '-C' option"_ostr);
169 m_options["-CS"_ostr] = OString();
170 break;
172 else if (av[i][2] != '\0')
174 OString tmp("'-C', please check"_ostr);
175 if (i <= ac - 1)
177 tmp += OString::Concat(" your input '") + av[i] + "'";
180 throw IllegalArgument(tmp);
183 if (isValid("-L"_ostr) || isValid("-CS"_ostr))
185 throw IllegalArgument("'-C' could not be combined with '-L' or '-CS' option"_ostr);
187 m_options["-C"_ostr] = OString();
188 break;
189 case 'G':
190 if (av[i][2] == 'c')
192 if (av[i][3] != '\0')
194 OString tmp("'-Gc', please check"_ostr);
195 if (i <= ac - 1)
197 tmp += OString::Concat(" your input '") + av[i] + "'";
200 throw IllegalArgument(tmp);
203 m_options["-Gc"_ostr] = OString();
204 break;
206 else if (av[i][2] != '\0')
208 OString tmp("'-G', please check"_ostr);
209 if (i <= ac - 1)
211 tmp += OString::Concat(" your input '") + av[i] + "'";
214 throw IllegalArgument(tmp);
217 m_options["-G"_ostr] = OString();
218 break;
219 case 'X': // support for eXtra type rdbs
221 if (av[i][2] == '\0')
223 if (i < ac - 1 && av[i+1][0] != '-')
225 i++;
226 s = av[i];
228 else
230 OString tmp("'-X', please check"_ostr);
231 if (i <= ac - 1)
233 tmp += OString::Concat(" your input '") + av[i+1] + "'";
236 throw IllegalArgument(tmp);
239 else
241 s = av[i] + 2;
244 m_extra_input_files.emplace_back(s );
245 break;
248 default:
249 throw IllegalArgument(OString::Concat("the option is unknown") + av[i]);
251 } else
253 if (av[i][0] == '@')
255 FILE* cmdFile = fopen(av[i]+1, "r");
256 if( cmdFile == nullptr )
258 fprintf(stderr, "%s", prepareHelp().getStr());
259 ret = false;
261 else
263 int rargc=0;
264 char* rargv[512];
265 char buffer[512];
267 while (fscanf(cmdFile, "%511s", buffer) != EOF && rargc < 512)
269 rargv[rargc]= strdup(buffer);
270 rargc++;
272 fclose(cmdFile);
274 ret = initOptions(rargc, rargv, bCmdFile);
276 for (int j=0; j < rargc; j++)
278 free(rargv[j]);
282 else
284 m_inputFiles.emplace_back(av[i]);
289 return ret;
292 OString CppuOptions::prepareHelp()
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" +
313 prepareVersion();
315 return help;
318 OString CppuOptions::prepareVersion() const
320 OString version = m_program + " Version 2.0\n\n";
321 return version;
325 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */