xref: /aoo42x/main/registry/tools/options.cxx (revision 51134e9e)
1*51134e9eSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*51134e9eSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*51134e9eSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*51134e9eSAndrew Rist  * distributed with this work for additional information
6*51134e9eSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*51134e9eSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*51134e9eSAndrew Rist  * "License"); you may not use this file except in compliance
9*51134e9eSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*51134e9eSAndrew Rist  *
11*51134e9eSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*51134e9eSAndrew Rist  *
13*51134e9eSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*51134e9eSAndrew Rist  * software distributed under the License is distributed on an
15*51134e9eSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*51134e9eSAndrew Rist  * KIND, either express or implied.  See the License for the
17*51134e9eSAndrew Rist  * specific language governing permissions and limitations
18*51134e9eSAndrew Rist  * under the License.
19*51134e9eSAndrew Rist  *
20*51134e9eSAndrew Rist  *************************************************************/
21*51134e9eSAndrew Rist 
22*51134e9eSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "options.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include "osl/diagnose.h"
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <stdio.h>
29cdf0e10cSrcweir #include <string.h>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir namespace registry
32cdf0e10cSrcweir {
33cdf0e10cSrcweir namespace tools
34cdf0e10cSrcweir {
35cdf0e10cSrcweir 
Options(char const * program)36cdf0e10cSrcweir Options::Options (char const * program)
37cdf0e10cSrcweir     : m_program (program)
38cdf0e10cSrcweir {}
39cdf0e10cSrcweir 
~Options()40cdf0e10cSrcweir Options::~Options()
41cdf0e10cSrcweir {}
42cdf0e10cSrcweir 
43cdf0e10cSrcweir // static
checkArgument(std::vector<std::string> & rArgs,char const * arg,size_t len)44cdf0e10cSrcweir bool Options::checkArgument(std::vector< std::string> & rArgs, char const * arg, size_t len)
45cdf0e10cSrcweir {
46cdf0e10cSrcweir     bool result = ((arg != 0) && (len > 0));
47cdf0e10cSrcweir     OSL_PRECOND(result, "registry::tools::Options::checkArgument(): invalid arguments");
48cdf0e10cSrcweir     if (result)
49cdf0e10cSrcweir     {
50cdf0e10cSrcweir         OSL_TRACE("registry::tools:Options::checkArgument(): \"%s\"", arg);
51cdf0e10cSrcweir         switch (arg[0])
52cdf0e10cSrcweir         {
53cdf0e10cSrcweir         case '@':
54cdf0e10cSrcweir             if ((result = (len > 1)) == true)
55cdf0e10cSrcweir             {
56cdf0e10cSrcweir                 // "@<cmdfile>"
57cdf0e10cSrcweir                 result = Options::checkCommandFile(rArgs, &(arg[1]));
58cdf0e10cSrcweir             }
59cdf0e10cSrcweir             break;
60cdf0e10cSrcweir         case '-':
61cdf0e10cSrcweir             if ((result = (len > 1)) == true)
62cdf0e10cSrcweir             {
63cdf0e10cSrcweir                 // "-<option>"
64cdf0e10cSrcweir                 std::string option (&(arg[0]), 2);
65cdf0e10cSrcweir                 rArgs.push_back(option);
66cdf0e10cSrcweir                 if (len > 2)
67cdf0e10cSrcweir                 {
68cdf0e10cSrcweir                     // "-<option><param>"
69cdf0e10cSrcweir                     std::string param(&(arg[2]), len - 2);
70cdf0e10cSrcweir                     rArgs.push_back(param);
71cdf0e10cSrcweir                 }
72cdf0e10cSrcweir             }
73cdf0e10cSrcweir             break;
74cdf0e10cSrcweir         default:
75cdf0e10cSrcweir             rArgs.push_back(std::string(arg, len));
76cdf0e10cSrcweir             break;
77cdf0e10cSrcweir         }
78cdf0e10cSrcweir     }
79cdf0e10cSrcweir     return (result);
80cdf0e10cSrcweir }
81cdf0e10cSrcweir 
82cdf0e10cSrcweir // static
checkCommandFile(std::vector<std::string> & rArgs,char const * filename)83cdf0e10cSrcweir bool Options::checkCommandFile(std::vector< std::string > & rArgs, char const * filename)
84cdf0e10cSrcweir {
85cdf0e10cSrcweir     FILE * fp = fopen(filename, "r");
86cdf0e10cSrcweir     if (fp == 0)
87cdf0e10cSrcweir     {
88cdf0e10cSrcweir         fprintf(stderr, "ERROR: Can't open command file \"%s\"\n", filename);
89cdf0e10cSrcweir         return (false);
90cdf0e10cSrcweir     }
91cdf0e10cSrcweir 
92cdf0e10cSrcweir     std::string buffer;
93cdf0e10cSrcweir     buffer.reserve(256);
94cdf0e10cSrcweir 
95cdf0e10cSrcweir     bool quoted = false;
96cdf0e10cSrcweir     int  c = EOF;
97cdf0e10cSrcweir     while ((c = fgetc(fp)) != EOF)
98cdf0e10cSrcweir     {
99cdf0e10cSrcweir         switch(c)
100cdf0e10cSrcweir         {
101cdf0e10cSrcweir         case '\"':
102cdf0e10cSrcweir             quoted = !quoted;
103cdf0e10cSrcweir             break;
104cdf0e10cSrcweir         case ' ':
105cdf0e10cSrcweir         case '\t':
106cdf0e10cSrcweir         case '\r':
107cdf0e10cSrcweir         case '\n':
108cdf0e10cSrcweir             if (!quoted)
109cdf0e10cSrcweir             {
110cdf0e10cSrcweir                 if (!buffer.empty())
111cdf0e10cSrcweir                 {
112cdf0e10cSrcweir                     if (!checkArgument(rArgs, buffer.c_str(), buffer.size()))
113cdf0e10cSrcweir                     {
114cdf0e10cSrcweir                         // failure.
115cdf0e10cSrcweir                         (void) fclose(fp);
116cdf0e10cSrcweir                         return false;
117cdf0e10cSrcweir                     }
118cdf0e10cSrcweir                     buffer.clear();
119cdf0e10cSrcweir                 }
120cdf0e10cSrcweir                 break;
121cdf0e10cSrcweir             }
122cdf0e10cSrcweir         default:
123cdf0e10cSrcweir             // quoted white-space fall through
124cdf0e10cSrcweir             buffer.push_back(sal::static_int_cast<char>(c));
125cdf0e10cSrcweir             break;
126cdf0e10cSrcweir         }
127cdf0e10cSrcweir     }
128cdf0e10cSrcweir     return (fclose(fp) == 0);
129cdf0e10cSrcweir }
130cdf0e10cSrcweir 
initOptions(std::vector<std::string> & rArgs)131cdf0e10cSrcweir bool Options::initOptions (std::vector< std::string > & rArgs)
132cdf0e10cSrcweir {
133cdf0e10cSrcweir     return initOptions_Impl (rArgs);
134cdf0e10cSrcweir }
135cdf0e10cSrcweir 
badOption(char const * reason,char const * option) const136cdf0e10cSrcweir bool Options::badOption (char const * reason, char const * option) const
137cdf0e10cSrcweir {
138cdf0e10cSrcweir     (void) fprintf(stderr, "%s: %s option '%s'\n", m_program.c_str(), reason, option);
139cdf0e10cSrcweir     return printUsage();
140cdf0e10cSrcweir }
141cdf0e10cSrcweir 
printUsage() const142cdf0e10cSrcweir bool Options::printUsage() const
143cdf0e10cSrcweir {
144cdf0e10cSrcweir     printUsage_Impl();
145cdf0e10cSrcweir     return false;
146cdf0e10cSrcweir }
147cdf0e10cSrcweir 
148cdf0e10cSrcweir } // namespace tools
149cdf0e10cSrcweir } // namespace registry
150