xref: /trunk/main/registry/tools/options.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "options.hxx"
29 
30 #include "osl/diagnose.h"
31 
32 #include <stdio.h>
33 #include <string.h>
34 
35 namespace registry
36 {
37 namespace tools
38 {
39 
40 Options::Options (char const * program)
41     : m_program (program)
42 {}
43 
44 Options::~Options()
45 {}
46 
47 // static
48 bool Options::checkArgument(std::vector< std::string> & rArgs, char const * arg, size_t len)
49 {
50     bool result = ((arg != 0) && (len > 0));
51     OSL_PRECOND(result, "registry::tools::Options::checkArgument(): invalid arguments");
52     if (result)
53     {
54         OSL_TRACE("registry::tools:Options::checkArgument(): \"%s\"", arg);
55         switch (arg[0])
56         {
57         case '@':
58             if ((result = (len > 1)) == true)
59             {
60                 // "@<cmdfile>"
61                 result = Options::checkCommandFile(rArgs, &(arg[1]));
62             }
63             break;
64         case '-':
65             if ((result = (len > 1)) == true)
66             {
67                 // "-<option>"
68                 std::string option (&(arg[0]), 2);
69                 rArgs.push_back(option);
70                 if (len > 2)
71                 {
72                     // "-<option><param>"
73                     std::string param(&(arg[2]), len - 2);
74                     rArgs.push_back(param);
75                 }
76             }
77             break;
78         default:
79             rArgs.push_back(std::string(arg, len));
80             break;
81         }
82     }
83     return (result);
84 }
85 
86 // static
87 bool Options::checkCommandFile(std::vector< std::string > & rArgs, char const * filename)
88 {
89     FILE * fp = fopen(filename, "r");
90     if (fp == 0)
91     {
92         fprintf(stderr, "ERROR: Can't open command file \"%s\"\n", filename);
93         return (false);
94     }
95 
96     std::string buffer;
97     buffer.reserve(256);
98 
99     bool quoted = false;
100     int  c = EOF;
101     while ((c = fgetc(fp)) != EOF)
102     {
103         switch(c)
104         {
105         case '\"':
106             quoted = !quoted;
107             break;
108         case ' ':
109         case '\t':
110         case '\r':
111         case '\n':
112             if (!quoted)
113             {
114                 if (!buffer.empty())
115                 {
116                     if (!checkArgument(rArgs, buffer.c_str(), buffer.size()))
117                     {
118                         // failure.
119                         (void) fclose(fp);
120                         return false;
121                     }
122                     buffer.clear();
123                 }
124                 break;
125             }
126         default:
127             // quoted white-space fall through
128             buffer.push_back(sal::static_int_cast<char>(c));
129             break;
130         }
131     }
132     return (fclose(fp) == 0);
133 }
134 
135 bool Options::initOptions (std::vector< std::string > & rArgs)
136 {
137     return initOptions_Impl (rArgs);
138 }
139 
140 bool Options::badOption (char const * reason, char const * option) const
141 {
142     (void) fprintf(stderr, "%s: %s option '%s'\n", m_program.c_str(), reason, option);
143     return printUsage();
144 }
145 
146 bool Options::printUsage() const
147 {
148     printUsage_Impl();
149     return false;
150 }
151 
152 } // namespace tools
153 } // namespace registry
154