Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / registry / tools / options.cxx
blobe9b9c23950bfbf8ef0e049ddfbabd2ca084b47a4
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 "options.hxx"
22 #include <osl/diagnose.h>
24 #include <stdio.h>
26 namespace registry
28 namespace tools
31 Options::Options (char const * program)
32 : m_program (program)
35 Options::~Options()
38 // static
39 bool Options::checkArgument(std::vector< std::string> & rArgs, char const * arg, size_t len)
41 bool result = ((arg != nullptr) && (len > 0));
42 OSL_PRECOND(result, "registry::tools::Options::checkArgument(): invalid arguments");
43 if (result)
45 switch (arg[0])
47 case '@':
48 result = len > 1;
49 if (result)
51 // "@<cmdfile>"
52 result = Options::checkCommandFile(rArgs, &(arg[1]));
54 break;
55 case '-':
56 result = len > 1;
57 if (result)
59 // "-<option>"
60 std::string option (&(arg[0]), 2);
61 rArgs.push_back(option);
62 if (len > 2)
64 // "-<option><param>"
65 std::string param(&(arg[2]), len - 2);
66 rArgs.push_back(param);
69 break;
70 default:
71 rArgs.push_back(std::string(arg, len));
72 break;
75 return result;
78 // static
79 bool Options::checkCommandFile(std::vector< std::string > & rArgs, char const * filename)
81 FILE * fp = fopen(filename, "r");
82 if (fp == nullptr)
84 fprintf(stderr, "ERROR: Can't open command file \"%s\"\n", filename);
85 return false;
88 std::string buffer;
89 buffer.reserve(256);
91 bool quoted = false;
92 int c = EOF;
93 while ((c = fgetc(fp)) != EOF)
95 switch(c)
97 case '\"':
98 quoted = !quoted;
99 break;
100 case ' ':
101 case '\t':
102 case '\r':
103 case '\n':
104 if (!quoted)
106 if (!buffer.empty())
108 if (!checkArgument(rArgs, buffer.c_str(), buffer.size()))
110 // failure.
111 (void) fclose(fp);
112 return false;
114 buffer.clear();
116 break;
118 [[fallthrough]];
119 default:
120 buffer.push_back(sal::static_int_cast<char>(c));
121 break;
124 return fclose(fp) == 0;
127 bool Options::initOptions (std::vector< std::string > & rArgs)
129 return initOptions_Impl (rArgs);
132 bool Options::badOption (char const * reason, char const * option) const
134 (void) fprintf(stderr, "%s: %s option '%s'\n", m_program.c_str(), reason, option);
135 return printUsage();
138 bool Options::printUsage() const
140 printUsage_Impl();
141 return false;
144 } // namespace tools
145 } // namespace registry
147 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */