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