update dev300-m58
[ooovba.git] / idlc / source / options.cxx
blobefe97bb84f1faac5e8d890335bbe127e6c04c17c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: options.cxx,v $
10 * $Revision: 1.18 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_idlc.hxx"
34 #include <stdio.h>
35 #include /*MSVC trouble: <cstring>*/ <string.h>
36 #include <idlc/options.hxx>
38 using namespace rtl;
40 Options::Options(): m_stdin(false)
44 Options::~Options()
48 sal_Bool Options::initOptions(int ac, char* av[], sal_Bool bCmdFile)
49 throw( IllegalArgument )
51 sal_Bool ret = sal_True;
52 sal_uInt16 j=0;
54 if (!bCmdFile)
56 bCmdFile = sal_True;
58 m_program = av[0];
60 if (ac < 2)
62 fprintf(stderr, "%s", prepareHelp().getStr());
63 ret = sal_False;
66 j = 1;
67 } else
69 j = 0;
72 char *s=NULL;
73 for (; j < ac; j++)
75 if (av[j][0] == '-')
77 switch (av[j][1])
79 case 'O':
80 if (av[j][2] == '\0')
82 if (j < ac - 1 && av[j+1][0] != '-')
84 j++;
85 s = av[j];
86 } else
88 OString tmp("'-O', please check");
89 if (j <= ac - 1)
91 tmp += " your input '" + OString(av[j+1]) + "'";
94 throw IllegalArgument(tmp);
96 } else
98 s = av[j] + 2;
101 m_options["-O"] = OString(s);
102 break;
103 case 'I':
105 if (av[j][2] == '\0')
107 if (j < ac - 1 && av[j+1][0] != '-')
109 j++;
110 s = av[j];
111 } else
113 OString tmp("'-I', please check");
114 if (j <= ac - 1)
116 tmp += " your input '" + OString(av[j+1]) + "'";
119 throw IllegalArgument(tmp);
121 } else
123 s = av[j] + 2;
126 OString inc(s);
127 if ( inc.indexOf(';') > 0 )
129 OString tmp(s);
130 sal_Int32 nIndex = 0;
131 inc = OString();
132 do inc = inc + " -I\"" + tmp.getToken( 0, ';', nIndex ) +"\""; while( nIndex != -1 );
133 } else
134 inc = OString("-I\"") + s + "\"";
136 if (m_options.count("-I") > 0)
138 OString tmp(m_options["-I"]);
139 tmp = tmp + " " + inc;
140 m_options["-I"] = tmp;
141 } else
143 m_options["-I"] = inc;
146 break;
147 case 'D':
148 if (av[j][2] == '\0')
150 if (j < ac - 1 && av[j+1][0] != '-')
152 j++;
153 s = av[j];
154 } else
156 OString tmp("'-D', please check");
157 if (j <= ac - 1)
159 tmp += " your input '" + OString(av[j+1]) + "'";
162 throw IllegalArgument(tmp);
164 } else
166 s = av[j];
169 if (m_options.count("-D") > 0)
171 OString tmp(m_options["-D"]);
172 tmp = tmp + " " + s;
173 m_options["-D"] = tmp;
174 } else
175 m_options["-D"] = OString(s);
176 break;
177 case 'C':
178 if (av[j][2] != '\0')
180 throw IllegalArgument(OString(av[j]) + ", please check your input");
182 if (m_options.count("-C") == 0)
183 m_options["-C"] = OString(av[j]);
184 break;
185 case 'c':
186 if (av[j][2] == 'i' && av[j][3] == 'd' && av[j][4] == '\0')
188 if (m_options.count("-cid") == 0)
189 m_options["-cid"] = OString(av[j]);
190 } else
191 throw IllegalArgument(OString(av[j]) + ", please check your input");
192 break;
193 case 'w':
194 if (av[j][2] == 'e' && av[j][3] == '\0') {
195 if (m_options.count("-we") == 0)
196 m_options["-we"] = OString(av[j]);
197 } else {
198 if (av[j][2] == '\0') {
199 if (m_options.count("-w") == 0)
200 m_options["-w"] = OString(av[j]);
201 } else
202 throw IllegalArgument(OString(av[j]) + ", please check your input");
204 break;
205 case 'h':
206 case '?':
207 if (av[j][2] != '\0')
209 throw IllegalArgument(OString(av[j]) + ", please check your input");
210 } else
212 fprintf(stdout, "%s", prepareHelp().getStr());
213 exit(0);
215 case 's':
216 if (/*MSVC trouble: std::*/strcmp(&av[j][2], "tdin") == 0)
218 m_stdin = true;
219 break;
221 // fall through
222 default:
223 throw IllegalArgument("the option is unknown" + OString(av[j]));
225 } else
227 if (av[j][0] == '@')
229 FILE* cmdFile = fopen(av[j]+1, "r");
230 if( cmdFile == NULL )
232 fprintf(stderr, "%s", prepareHelp().getStr());
233 ret = sal_False;
234 } else
236 int rargc=0;
237 char* rargv[512];
238 char buffer[512]="";
240 int i=0;
241 int found = 0;
242 char c;
243 while ( fscanf(cmdFile, "%c", &c) != EOF )
245 if (c=='\"') {
246 if (found) {
247 found=0;
248 } else {
249 found=1;
250 continue;
252 } else {
253 if (c!=13 && c!=10) {
254 if (found || c!=' ') {
255 buffer[i++]=c;
256 continue;
259 if (i==0)
260 continue;
262 buffer[i]='\0';
263 found=0;
264 i=0;
265 rargv[rargc]= strdup(buffer);
266 rargc++;
267 buffer[0]='\0';
269 if (buffer[0] != '\0') {
270 buffer[i]='\0';
271 rargv[rargc]= strdup(buffer);
272 rargc++;
274 fclose(cmdFile);
276 ret = initOptions(rargc, rargv, bCmdFile);
278 long ii = 0;
279 for (ii=0; ii < rargc; ii++)
281 free(rargv[ii]);
284 } else
286 OString name(av[j]);
287 name = name.toAsciiLowerCase();
288 if ( name.lastIndexOf(".idl") != (name.getLength() - 4) )
290 throw IllegalArgument("'" + OString(av[j]) +
291 "' is not a valid input file, only '*.idl' files will be accepted");
293 m_inputFiles.push_back(av[j]);
298 return ret;
301 OString Options::prepareHelp()
303 OString help("\nusing: ");
304 help += m_program
305 + " [-options] <file_1> ... <file_n> | @<filename> | -stdin\n";
306 help += " <file_n> = file_n specifies one or more idl files.\n";
307 help += " Only files with the extension '.idl' are valid.\n";
308 help += " @<filename> = filename specifies the name of a command file.\n";
309 help += " -stdin = read idl file from standard input.\n";
310 help += " Options:\n";
311 help += " -O<path> = path specifies the output directory.\n";
312 help += " The generated output is a registry file with\n";
313 help += " the same name as the idl input file (or 'stdin'\n";
314 help += " for -stdin).\n";
315 help += " -I<path> = path specifies a directory where include\n";
316 help += " files will be searched by the preprocessor.\n";
317 help += " Multiple directories can be combined with ';'.\n";
318 help += " -D<name> = name defines a macro for the preprocessor.\n";
319 help += " -C = generate complete type information, including\n";
320 help += " documentation.\n";
321 help += " -cid = check if identifiers fulfill the UNO naming\n";
322 help += " requirements.\n";
323 help += " -w = display warning messages.\n";
324 help += " -we = treat warnings as errors.\n";
325 help += " -h|-? = print this help message and exit.\n";
326 help += prepareVersion();
328 return help;
331 OString Options::prepareVersion()
333 OString version("\nSun Microsystems (R) ");
334 version += m_program + " Version 1.1\n\n";
335 return version;
338 const OString& Options::getProgramName() const
340 return m_program;
343 sal_Bool Options::isValid(const OString& option)
345 return (m_options.count(option) > 0);
348 const OString Options::getOption(const OString& option)
349 throw( IllegalArgument )
351 if (m_options.count(option) > 0)
353 return m_options[option];
354 } else
356 throw IllegalArgument("Option is not valid or currently not set.");