Impress: Implement getPartName.
[LibreOffice.git] / idlc / source / options.cxx
blobdfdb9bd291a9deea72664415ae5698f81c9f3ff6
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 .
21 #include "idlc/options.hxx"
23 #include <osl/diagnose.h>
24 #include <rtl/string.hxx>
25 #include <rtl/strbuf.hxx>
27 #include "rtl/ustring.hxx"
28 #include "osl/file.hxx"
30 #ifdef WNT
31 # include <windows.h>
32 #endif
35 #ifndef WIN32_LEAN_AND_MEAN
36 # define WIN32_LEAN_AND_MEAN
37 # ifdef _MSC_VER
38 # pragma warning(push,1)
39 # endif
40 # include <windows.h>
41 # ifdef _MSC_VER
42 # pragma warning(pop)
43 # endif
44 # include <tchar.h>
45 # undef WIN32_LEAN_AND_MEAN
46 #endif
49 #include <stdio.h>
50 #include <string.h>
53 Options::Options(char const * progname)
54 : m_program(progname), m_stdin(false), m_verbose(false), m_quiet(false)
58 Options::~Options()
62 // static
63 bool Options::checkArgument (std::vector< std::string > & rArgs, char const * arg, size_t len)
65 bool result = ((arg != 0) && (len > 0));
66 OSL_PRECOND(result, "idlc::Options::checkArgument(): invalid arguments");
67 if (result)
69 switch(arg[0])
71 case '@':
72 if ((result = (len > 1)) == true)
74 // "@<cmdfile>"
75 result = Options::checkCommandFile (rArgs, &(arg[1]));
77 break;
78 case '-':
79 if ((result = (len > 1)) == true)
81 // "-<option>"
82 switch (arg[1])
84 case 'O':
85 case 'M':
86 case 'I':
87 case 'D':
89 // "-<option>[<param>]
90 std::string option(&(arg[0]), 2);
91 rArgs.push_back(option);
92 if (len > 2)
94 // "-<option><param>"
95 std::string param(&(arg[2]), len - 2);
96 rArgs.push_back(param);
98 break;
100 default:
101 // "-<option>" ([long] option, w/o param)
102 rArgs.push_back(std::string(arg, len));
103 break;
106 break;
107 default:
108 // "<param>"
109 rArgs.push_back(std::string(arg, len));
110 break;
113 return (result);
116 // static
117 bool Options::checkCommandFile (std::vector< std::string > & rArgs, char const * filename)
119 FILE * fp = fopen(filename, "r");
120 if (fp == 0)
122 fprintf(stderr, "ERROR: can't open command file \"%s\"\n", filename);
123 return (false);
126 std::string buffer;
127 buffer.reserve(256);
129 bool quoted = false;
130 int c = EOF;
131 while ((c = fgetc(fp)) != EOF)
133 switch(c)
135 case '\"':
136 quoted = !quoted;
137 break;
138 case ' ':
139 case '\t':
140 case '\r':
141 case '\n':
142 if (!quoted)
144 if (!buffer.empty())
146 // append current argument.
147 if (!Options::checkArgument(rArgs, buffer.c_str(), buffer.size()))
149 (void) fclose(fp);
150 return (false);
152 buffer.clear();
154 break;
156 default:
157 // quoted white-space fall through
158 buffer.push_back(sal::static_int_cast<char>(c));
159 break;
162 if (!buffer.empty())
164 // append unterminated argument.
165 if (!Options::checkArgument(rArgs, buffer.c_str(), buffer.size()))
167 (void) fclose(fp);
168 return (false);
170 buffer.clear();
172 return (fclose(fp) == 0);
175 bool Options::badOption(char const * reason, std::string const & rArg) throw(IllegalArgument)
177 OStringBuffer message;
178 if (reason != 0)
180 message.append(reason); message.append(" option '"); message.append(rArg.c_str()); message.append("'");
181 throw IllegalArgument(message.makeStringAndClear());
183 return false;
186 bool Options::setOption(char const * option, std::string const & rArg)
188 bool result = (0 == strcmp(option, rArg.c_str()));
189 if (result)
190 m_options[rArg.c_str()] = OString(rArg.c_str(), rArg.size());
191 return (result);
194 #ifdef WNT
195 /* Helper functiopn to convert windows paths including spaces, brackets etc. into
196 a windows short Url. The ucpp preprocessor has problems with such paths and returns
197 with error.
199 OString convertIncPathtoShortWindowsPath(const OString& incPath) {
200 OUString path = OStringToOUString(incPath, RTL_TEXTENCODING_UTF8);
202 std::vector<sal_Unicode> vec(path.getLength() + 1);
203 //GetShortPathNameW only works if the file can be found!
204 const DWORD len = GetShortPathNameW(
205 reinterpret_cast<LPCWSTR>(path.getStr()), reinterpret_cast<LPWSTR>(&vec[0]), path.getLength() + 1);
207 if (len > 0)
209 OUString ret(&vec[0], len);
210 return OUStringToOString(ret, RTL_TEXTENCODING_UTF8);
213 return incPath;
215 #endif
217 bool Options::initOptions(std::vector< std::string > & rArgs) throw(IllegalArgument)
219 std::vector< std::string >::const_iterator first = rArgs.begin(), last = rArgs.end();
220 for (; first != last; ++first)
222 if ((*first)[0] != '-')
224 OString filename((*first).c_str(), (*first).size());
225 OString tmp(filename.toAsciiLowerCase());
226 if (tmp.lastIndexOf(".idl") != (tmp.getLength() - 4))
228 throw IllegalArgument("'" + filename + "' is not a valid input file, only '*.idl' files will be accepted");
230 m_inputFiles.push_back(filename);
231 continue;
234 std::string const option(*first);
235 switch((*first)[1])
237 case 'O':
239 if (!((++first != last) && ((*first)[0] != '-')))
241 return badOption("invalid", option);
243 OString param((*first).c_str(), (*first).size());
244 m_options["-O"] = param;
245 break;
247 case 'M':
249 if (!((++first != last) && ((*first)[0] != '-')))
251 return badOption("invalid", option);
253 OString param((*first).c_str(), (*first).size());
254 m_options["-M"] = param;
255 break;
257 case 'I':
259 if (!((++first != last) && ((*first)[0] != '-')))
261 return badOption("invalid", option);
263 OString param((*first).c_str(), (*first).size());
265 // quote param token(s).
266 OStringBuffer buffer;
267 sal_Int32 k = 0;
270 if (!buffer.isEmpty())
271 buffer.append(' ');
272 // buffer.append("-I\"");
273 #ifdef WNT
274 OString incpath = convertIncPathtoShortWindowsPath(param.getToken(0, ';', k));
275 #else
276 OString incpath = param.getToken(0, ';', k);
277 #endif
278 buffer.append(incpath);
279 // buffer.append("\"");
280 } while (k != -1);
281 param = buffer.makeStringAndClear();
283 if (m_options.count("-I") > 0)
285 // append param.
286 OStringBuffer buffer(m_options["-I"]);
287 buffer.append(' '); buffer.append(param);
288 param = buffer.makeStringAndClear();
290 m_options["-I"] = param;
291 break;
293 case 'D':
295 if (!((++first != last) && ((*first)[0] != '-')))
297 return badOption("invalid", option);
299 OString param("-D"); param += OString((*first).c_str(), (*first).size());
300 if (m_options.count("-D") > 0)
302 OStringBuffer buffer(m_options["-D"]);
303 buffer.append(' '); buffer.append(param);
304 param = buffer.makeStringAndClear();
306 m_options["-D"] = param;
307 break;
309 case 'C':
311 if (!setOption("-C", option))
313 return badOption("invalid", option);
315 break;
317 case 'c':
319 if (!setOption("-cid", option))
321 return badOption("invalid", option);
323 break;
325 case 'q':
327 if (!setOption("-quiet", option))
329 return badOption("invalid", option);
331 m_quiet = true;
332 break;
334 case 'v':
336 if (!setOption("-verbose", option))
338 return badOption("invalid", option);
340 m_verbose = true;
341 break;
343 case 'w':
345 if (!(setOption("-w", option) || setOption("-we", option)))
347 return badOption("invalid", option);
349 break;
351 case 'h':
352 case '?':
354 if (!(setOption("-h", option) || setOption("-?", option)))
356 return badOption("invalid", option);
359 (void) fprintf(stdout, "%s", prepareHelp().getStr());
360 return (false);
362 // break; // Unreachable
364 case 's':
366 if (!setOption("-stdin", option))
368 return badOption("invalid", option);
370 m_stdin = true;
371 break;
373 default:
374 return badOption("unknown", option);
377 return (true);
380 OString Options::prepareHelp()
382 OString help("\nusing: ");
383 help += m_program + " [-options] <file_1> ... <file_n> | @<filename> | -stdin\n";
384 help += " <file_n> = file_n specifies one or more idl files.\n";
385 help += " Only files with the extension '.idl' are valid.\n";
386 help += " @<filename> = filename specifies the name of a command file.\n";
387 help += " -stdin = read idl file from standard input.\n";
388 help += " Options:\n";
389 help += " -O<path> = path specifies the output directory.\n";
390 help += " The generated output is a registry file with\n";
391 help += " the same name as the idl input file (or 'stdin'\n";
392 help += " for -stdin).\n";
393 help += " -M<path> = path specifies the output directory for deps.\n";
394 help += " Generate GNU make dependency files with the\n";
395 help += " same name as the idl input file.\n";
396 help += " -I<path> = path specifies a directory where include\n";
397 help += " files will be searched by the preprocessor.\n";
398 help += " Multiple directories can be combined with ';'.\n";
399 help += " -D<name> = name defines a macro for the preprocessor.\n";
400 help += " -C = generate complete type information, including\n";
401 help += " documentation.\n";
402 help += " -cid = check if identifiers fulfill the UNO naming\n";
403 help += " requirements.\n";
404 help += " -quiet = no output.\n";
405 help += " -verbose = verbose output.\n";
406 help += " -w = display warning messages.\n";
407 help += " -we = treat warnings as errors.\n";
408 help += " -h|-? = print this help message and exit.\n\n";
409 help += prepareVersion();
411 return help;
414 OString Options::prepareVersion()
416 OString version(m_program);
417 version += " Version 1.1\n\n";
418 return version;
422 bool Options::isValid(const OString& option)
424 return (m_options.count(option) > 0);
427 const OString& Options::getOption(const OString& option)
428 throw( IllegalArgument )
430 if (!isValid(option))
432 throw IllegalArgument("Option is not valid or currently not set.");
434 return m_options[option];
437 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */