sc: copy cache values when clone color conditional format
[LibreOffice.git] / idlc / source / options.cxx
blob2418e13afdf056a831acc12f3b6f73c3d2fc1855
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 <sal/config.h>
22 #include <string_view>
24 #include <options.hxx>
26 #include <osl/diagnose.h>
27 #include <rtl/string.hxx>
28 #include <rtl/strbuf.hxx>
30 #include <rtl/ustring.hxx>
31 #include <osl/file.hxx>
32 #include <o3tl/char16_t2wchar_t.hxx>
34 #ifdef _WIN32
35 # if !defined WIN32_LEAN_AND_MEAN
36 # define WIN32_LEAN_AND_MEAN
37 # endif
38 # include <windows.h>
39 #endif
41 #include <stdio.h>
42 #include <string.h>
45 Options::Options(char const * progname)
46 : m_program(progname), m_stdin(false), m_verbose(false), m_quiet(false)
50 Options::~Options()
54 // static
55 bool Options::checkArgument (std::vector< std::string > & rArgs, char const * arg, size_t len)
57 bool result = ((arg != nullptr) && (len > 0));
58 OSL_PRECOND(result, "idlc::Options::checkArgument(): invalid arguments");
59 if (result)
61 switch(arg[0])
63 case '@':
64 result = len > 1;
65 if (result)
67 // "@<cmdfile>"
68 result = Options::checkCommandFile (rArgs, &(arg[1]));
70 break;
71 case '-':
72 result = len > 1;
73 if (result)
75 // "-<option>"
76 switch (arg[1])
78 case 'O':
79 case 'M':
80 case 'I':
81 case 'D':
83 // "-<option>[<param>]
84 std::string option(&(arg[0]), 2);
85 rArgs.push_back(option);
86 if (len > 2)
88 // "-<option><param>"
89 std::string param(&(arg[2]), len - 2);
90 rArgs.push_back(param);
92 break;
94 default:
95 // "-<option>" ([long] option, w/o param)
96 rArgs.emplace_back(arg, len);
97 break;
100 break;
101 default:
102 // "<param>"
103 rArgs.emplace_back(arg, len);
104 break;
107 return result;
110 // static
111 bool Options::checkCommandFile (std::vector< std::string > & rArgs, char const * filename)
113 FILE * fp = fopen(filename, "r");
114 if (fp == nullptr)
116 fprintf(stderr, "ERROR: can't open command file \"%s\"\n", filename);
117 return false;
120 std::string buffer;
121 buffer.reserve(256);
123 bool quoted = false;
124 int c = EOF;
125 while ((c = fgetc(fp)) != EOF)
127 switch(c)
129 case '\"':
130 quoted = !quoted;
131 break;
132 case ' ':
133 case '\t':
134 case '\r':
135 case '\n':
136 if (!quoted)
138 if (!buffer.empty())
140 // append current argument.
141 if (!Options::checkArgument(rArgs, buffer.c_str(), buffer.size()))
143 (void) fclose(fp);
144 return false;
146 buffer.clear();
148 break;
150 [[fallthrough]];
151 default:
152 buffer.push_back(sal::static_int_cast<char>(c));
153 break;
156 if (!buffer.empty())
158 // append unterminated argument.
159 if (!Options::checkArgument(rArgs, buffer.c_str(), buffer.size()))
161 (void) fclose(fp);
162 return false;
164 buffer.clear();
166 return (fclose(fp) == 0);
169 bool Options::badOption(char const * reason, std::string const & rArg)
171 if (reason != nullptr)
173 OString message = OString::Concat(reason) + " option '" + rArg.c_str() + "'";
174 throw IllegalArgument(message);
176 return false;
179 bool Options::setOption(char const * option, std::string const & rArg)
181 bool result = (0 == strcmp(option, rArg.c_str()));
182 if (result)
183 m_options[rArg.c_str()] = OString(rArg.c_str(), rArg.size());
184 return result;
187 #ifdef _WIN32
188 /* Helper function to convert windows paths including spaces, brackets etc. into
189 a windows short Url. The ucpp preprocessor has problems with such paths and returns
190 with error.
192 static OString convertIncPathtoShortWindowsPath(const OString& incPath) {
193 OUString path = OStringToOUString(incPath, RTL_TEXTENCODING_UTF8);
195 std::vector<sal_Unicode> vec(path.getLength() + 1);
196 //GetShortPathNameW only works if the file can be found!
197 const DWORD len = GetShortPathNameW(
198 o3tl::toW(path.getStr()), o3tl::toW(vec.data()), path.getLength() + 1);
200 if (len > 0)
202 OUString ret(vec.data(), len);
203 return OUStringToOString(ret, RTL_TEXTENCODING_UTF8);
206 return incPath;
208 #endif
210 bool Options::initOptions(std::vector< std::string > & rArgs)
212 std::vector< std::string >::const_iterator first = rArgs.begin(), last = rArgs.end();
213 for (; first != last; ++first)
215 if ((*first)[0] != '-')
217 OString filename((*first).c_str(), (*first).size());
218 OString tmp(filename.toAsciiLowerCase());
219 if (tmp.lastIndexOf(".idl") != (tmp.getLength() - 4))
221 throw IllegalArgument("'" + filename + "' is not a valid input file, only '*.idl' files will be accepted");
223 m_inputFiles.push_back(filename);
224 continue;
227 std::string const option(*first);
228 switch((*first)[1])
230 case 'O':
232 if ((++first == last) || ((*first)[0] == '-'))
234 return badOption("invalid", option);
236 OString param((*first).c_str(), (*first).size());
237 m_options["-O"] = param;
238 break;
240 case 'M':
242 if ((++first == last) || ((*first)[0] == '-'))
244 return badOption("invalid", option);
246 OString param((*first).c_str(), (*first).size());
247 m_options["-M"] = param;
248 break;
250 case 'I':
252 if ((++first == last) || ((*first)[0] == '-'))
254 return badOption("invalid", option);
256 OString param((*first).c_str(), (*first).size());
258 // quote param token(s).
259 OStringBuffer buffer;
260 sal_Int32 k = 0;
263 if (!buffer.isEmpty())
264 buffer.append(' ');
265 // buffer.append("-I\"");
266 #ifdef _WIN32
267 OString incpath = convertIncPathtoShortWindowsPath(param.getToken(0, ';', k));
268 #else
269 OString incpath = param.getToken(0, ';', k);
270 #endif
271 buffer.append(incpath);
272 // buffer.append("\"");
273 } while (k != -1);
274 param = buffer.makeStringAndClear();
276 if (m_options.count("-I") > 0)
278 // append param.
279 param = m_options["-I"] + " " + param;
281 m_options["-I"] = param;
282 break;
284 case 'D':
286 if ((++first == last) || ((*first)[0] == '-'))
288 return badOption("invalid", option);
290 OString param = OString::Concat("-D") + std::string_view((*first).c_str(), (*first).size());
291 if (m_options.count("-D") > 0)
293 param = m_options["-D"] + " " + param;
295 m_options["-D"] = param;
296 break;
298 case 'C':
300 if (!setOption("-C", option))
302 return badOption("invalid", option);
304 break;
306 case 'c':
308 if (!setOption("-cid", option))
310 return badOption("invalid", option);
312 break;
314 case 'q':
316 if (!setOption("-quiet", option))
318 return badOption("invalid", option);
320 m_quiet = true;
321 break;
323 case 'v':
325 if (!setOption("-verbose", option))
327 return badOption("invalid", option);
329 m_verbose = true;
330 break;
332 case 'w':
334 if (!(setOption("-w", option) || setOption("-we", option)))
336 return badOption("invalid", option);
338 break;
340 case 'h':
341 case '?':
343 if (!(setOption("-h", option) || setOption("-?", option)))
345 return badOption("invalid", option);
348 (void) fprintf(stdout, "%s", prepareHelp().getStr());
349 return false;
351 // break; // Unreachable
353 case 's':
355 if (!setOption("-stdin", option))
357 return badOption("invalid", option);
359 m_stdin = true;
360 break;
362 default:
363 return badOption("unknown", option);
366 return true;
369 OString Options::prepareHelp() const
371 OString help = "\nusing: " +
372 m_program + " [-options] <file_1> ... <file_n> | @<filename> | -stdin\n"
373 " <file_n> = file_n specifies one or more idl files.\n"
374 " Only files with the extension '.idl' are valid.\n"
375 " @<filename> = filename specifies the name of a command file.\n"
376 " -stdin = read idl file from standard input.\n"
377 " Options:\n"
378 " -O<path> = path specifies the output directory.\n"
379 " The generated output is a registry file with\n"
380 " the same name as the idl input file (or 'stdin'\n"
381 " for -stdin).\n"
382 " -M<path> = path specifies the output directory for deps.\n"
383 " Generate GNU make dependency files with the\n"
384 " same name as the idl input file.\n"
385 " -I<path> = path specifies a directory where include\n"
386 " files will be searched by the preprocessor.\n"
387 " Multiple directories can be combined with ';'.\n"
388 " -D<name> = name defines a macro for the preprocessor.\n"
389 " -C = generate complete type information, including\n"
390 " documentation.\n"
391 " -cid = check if identifiers fulfill the UNO naming\n"
392 " requirements.\n"
393 " -quiet = no output.\n"
394 " -verbose = verbose output.\n"
395 " -w = display warning messages.\n"
396 " -we = treat warnings as errors.\n"
397 " -h|-? = print this help message and exit.\n\n" +
398 prepareVersion();
400 return help;
403 OString Options::prepareVersion() const
405 return m_program + " Version 1.1\n\n";
409 bool Options::isValid(const OString& option) const
411 return (m_options.count(option) > 0);
414 const OString& Options::getOption(const OString& option)
416 if (!isValid(option))
418 throw IllegalArgument("Option is not valid or currently not set.");
420 return m_options[option];
423 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */