Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "options.hxx"
21 :
22 : #include "osl/diagnose.h"
23 :
24 : #include <stdio.h>
25 : #include <string.h>
26 :
27 : namespace registry
28 : {
29 : namespace tools
30 : {
31 :
32 14 : Options::Options (char const * program)
33 14 : : m_program (program)
34 14 : {}
35 :
36 14 : Options::~Options()
37 14 : {}
38 :
39 : // static
40 5241 : bool Options::checkArgument(std::vector< std::string> & rArgs, char const * arg, size_t len)
41 : {
42 5241 : bool result = ((arg != 0) && (len > 0));
43 : OSL_PRECOND(result, "registry::tools::Options::checkArgument(): invalid arguments");
44 5241 : if (result)
45 : {
46 : OSL_TRACE("registry::tools:Options::checkArgument(): \"%s\"", arg);
47 5241 : switch (arg[0])
48 : {
49 : case '@':
50 13 : if ((result = (len > 1)) == true)
51 : {
52 : // "@<cmdfile>"
53 13 : result = Options::checkCommandFile(rArgs, &(arg[1]));
54 : }
55 13 : break;
56 : case '-':
57 4 : if ((result = (len > 1)) == true)
58 : {
59 : // "-<option>"
60 4 : std::string option (&(arg[0]), 2);
61 4 : rArgs.push_back(option);
62 4 : if (len > 2)
63 : {
64 : // "-<option><param>"
65 2 : std::string param(&(arg[2]), len - 2);
66 2 : rArgs.push_back(param);
67 4 : }
68 : }
69 4 : break;
70 : default:
71 5224 : rArgs.push_back(std::string(arg, len));
72 5224 : break;
73 : }
74 : }
75 5241 : return (result);
76 : }
77 :
78 : // static
79 13 : bool Options::checkCommandFile(std::vector< std::string > & rArgs, char const * filename)
80 : {
81 13 : FILE * fp = fopen(filename, "r");
82 13 : if (fp == 0)
83 : {
84 0 : fprintf(stderr, "ERROR: Can't open command file \"%s\"\n", filename);
85 0 : return (false);
86 : }
87 :
88 13 : std::string buffer;
89 13 : buffer.reserve(256);
90 :
91 13 : bool quoted = false;
92 13 : int c = EOF;
93 587371 : while ((c = fgetc(fp)) != EOF)
94 : {
95 587345 : switch(c)
96 : {
97 : case '\"':
98 0 : quoted = !quoted;
99 0 : break;
100 : case ' ':
101 : case '\t':
102 : case '\r':
103 : case '\n':
104 5247 : if (!quoted)
105 : {
106 5247 : if (!buffer.empty())
107 : {
108 5222 : if (!checkArgument(rArgs, buffer.c_str(), buffer.size()))
109 : {
110 : // failure.
111 0 : (void) fclose(fp);
112 0 : return false;
113 : }
114 5222 : buffer.clear();
115 : }
116 5247 : break;
117 : }
118 : default:
119 : // quoted white-space fall through
120 582098 : buffer.push_back(sal::static_int_cast<char>(c));
121 582098 : break;
122 : }
123 : }
124 13 : return (fclose(fp) == 0);
125 : }
126 :
127 14 : bool Options::initOptions (std::vector< std::string > & rArgs)
128 : {
129 14 : return initOptions_Impl (rArgs);
130 : }
131 :
132 0 : bool Options::badOption (char const * reason, char const * option) const
133 : {
134 0 : (void) fprintf(stderr, "%s: %s option '%s'\n", m_program.c_str(), reason, option);
135 0 : return printUsage();
136 : }
137 :
138 0 : bool Options::printUsage() const
139 : {
140 0 : printUsage_Impl();
141 0 : return false;
142 : }
143 :
144 : } // namespace tools
145 : } // namespace registry
146 :
147 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|