Remove pointless macro
[LibreOffice.git] / registry / tools / options.cxx
blob81d1131f75c2353b1772e8d44228d6cdd99ae7b1
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::tools
29 Options::Options (char const * program)
30 : m_program (program)
33 Options::~Options()
36 // static
37 bool Options::checkArgument(std::vector< std::string> & rArgs, char const * arg, size_t len)
39 bool result = ((arg != nullptr) && (len > 0));
40 OSL_PRECOND(result, "registry::tools::Options::checkArgument(): invalid arguments");
41 if (result)
43 switch (arg[0])
45 case '@':
46 result = len > 1;
47 if (result)
49 // "@<cmdfile>"
50 result = Options::checkCommandFile(rArgs, &(arg[1]));
52 break;
53 case '-':
54 result = len > 1;
55 if (result)
57 // "-<option>"
58 std::string option (&(arg[0]), 2);
59 rArgs.push_back(option);
60 if (len > 2)
62 // "-<option><param>"
63 std::string param(&(arg[2]), len - 2);
64 rArgs.push_back(param);
67 break;
68 default:
69 rArgs.push_back(std::string(arg, len));
70 break;
73 return result;
76 // static
77 bool Options::checkCommandFile(std::vector< std::string > & rArgs, char const * filename)
79 FILE * fp = fopen(filename, "r");
80 if (fp == nullptr)
82 fprintf(stderr, "ERROR: Can't open command file \"%s\"\n", filename);
83 return false;
86 std::string buffer;
87 buffer.reserve(256);
89 bool quoted = false;
90 int c = EOF;
91 while ((c = fgetc(fp)) != EOF)
93 switch(c)
95 case '\"':
96 quoted = !quoted;
97 break;
98 case ' ':
99 case '\t':
100 case '\r':
101 case '\n':
102 if (!quoted)
104 if (!buffer.empty())
106 if (!checkArgument(rArgs, buffer.c_str(), buffer.size()))
108 // failure.
109 (void) fclose(fp);
110 return false;
112 buffer.clear();
114 break;
116 [[fallthrough]];
117 default:
118 buffer.push_back(sal::static_int_cast<char>(c));
119 break;
122 return fclose(fp) == 0;
125 bool Options::initOptions (std::vector< std::string > & rArgs)
127 return initOptions_Impl (rArgs);
130 bool Options::badOption (char const * reason, char const * option) const
132 (void) fprintf(stderr, "%s: %s option '%s'\n", m_program.c_str(), reason, option);
133 return printUsage();
136 bool Options::printUsage() const
138 printUsage_Impl();
139 return false;
142 } // namespace registry::tools
144 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */