bump product version to 5.0.4.1
[LibreOffice.git] / registry / tools / options.cxx
blob30d58d50d04471b5a4e0ca30476637f1fd5493de
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>
25 #include <string.h>
27 namespace registry
29 namespace tools
32 Options::Options (char const * program)
33 : m_program (program)
36 Options::~Options()
39 // static
40 bool Options::checkArgument(std::vector< std::string> & rArgs, char const * arg, size_t len)
42 bool result = ((arg != 0) && (len > 0));
43 OSL_PRECOND(result, "registry::tools::Options::checkArgument(): invalid arguments");
44 if (result)
46 OSL_TRACE("registry::tools:Options::checkArgument(): \"%s\"", arg);
47 switch (arg[0])
49 case '@':
50 result = len > 1;
51 if (result)
53 // "@<cmdfile>"
54 result = Options::checkCommandFile(rArgs, &(arg[1]));
56 break;
57 case '-':
58 result = len > 1;
59 if (result)
61 // "-<option>"
62 std::string option (&(arg[0]), 2);
63 rArgs.push_back(option);
64 if (len > 2)
66 // "-<option><param>"
67 std::string param(&(arg[2]), len - 2);
68 rArgs.push_back(param);
71 break;
72 default:
73 rArgs.push_back(std::string(arg, len));
74 break;
77 return result;
80 // static
81 bool Options::checkCommandFile(std::vector< std::string > & rArgs, char const * filename)
83 FILE * fp = fopen(filename, "r");
84 if (fp == 0)
86 fprintf(stderr, "ERROR: Can't open command file \"%s\"\n", filename);
87 return false;
90 std::string buffer;
91 buffer.reserve(256);
93 bool quoted = false;
94 int c = EOF;
95 while ((c = fgetc(fp)) != EOF)
97 switch(c)
99 case '\"':
100 quoted = !quoted;
101 break;
102 case ' ':
103 case '\t':
104 case '\r':
105 case '\n':
106 if (!quoted)
108 if (!buffer.empty())
110 if (!checkArgument(rArgs, buffer.c_str(), buffer.size()))
112 // failure.
113 (void) fclose(fp);
114 return false;
116 buffer.clear();
118 break;
120 default:
121 // quoted white-space fall through
122 buffer.push_back(sal::static_int_cast<char>(c));
123 break;
126 return fclose(fp) == 0;
129 bool Options::initOptions (std::vector< std::string > & rArgs)
131 return initOptions_Impl (rArgs);
134 bool Options::badOption (char const * reason, char const * option) const
136 (void) fprintf(stderr, "%s: %s option '%s'\n", m_program.c_str(), reason, option);
137 return printUsage();
140 bool Options::printUsage() const
142 printUsage_Impl();
143 return false;
146 } // namespace tools
147 } // namespace registry
149 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */