Branch data 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 : 22 : Options::Options (char const * program)
33 [ + - ]: 22 : : m_program (program)
34 : 22 : {}
35 : :
36 : 22 : Options::~Options()
37 [ - + ]: 22 : {}
38 : :
39 : : // static
40 : 9545 : bool Options::checkArgument(std::vector< std::string> & rArgs, char const * arg, size_t len)
41 : : {
42 [ + - ][ + - ]: 9545 : bool result = ((arg != 0) && (len > 0));
43 : : OSL_PRECOND(result, "registry::tools::Options::checkArgument(): invalid arguments");
44 [ + - ]: 9545 : if (result)
45 : : {
46 : : OSL_TRACE("registry::tools:Options::checkArgument(): \"%s\"", arg);
47 [ + + + ]: 9545 : switch (arg[0])
48 : : {
49 : : case '@':
50 [ + - ]: 19 : if ((result = (len > 1)) == true)
51 : : {
52 : : // "@<cmdfile>"
53 : 19 : result = Options::checkCommandFile(rArgs, &(arg[1]));
54 : : }
55 : 19 : break;
56 : : case '-':
57 [ + - ]: 8 : if ((result = (len > 1)) == true)
58 : : {
59 : : // "-<option>"
60 [ + - ]: 8 : std::string option (&(arg[0]), 2);
61 [ + - ]: 8 : rArgs.push_back(option);
62 [ + + ]: 8 : if (len > 2)
63 : : {
64 : : // "-<option><param>"
65 [ + - ]: 4 : std::string param(&(arg[2]), len - 2);
66 [ + - ]: 4 : rArgs.push_back(param);
67 : 8 : }
68 : : }
69 : 8 : break;
70 : : default:
71 [ + - ][ + - ]: 9518 : rArgs.push_back(std::string(arg, len));
72 : 9545 : break;
73 : : }
74 : : }
75 : 9545 : return (result);
76 : : }
77 : :
78 : : // static
79 : 19 : bool Options::checkCommandFile(std::vector< std::string > & rArgs, char const * filename)
80 : : {
81 [ + - ]: 19 : FILE * fp = fopen(filename, "r");
82 [ - + ]: 19 : if (fp == 0)
83 : : {
84 [ # # ]: 0 : fprintf(stderr, "ERROR: Can't open command file \"%s\"\n", filename);
85 : 0 : return (false);
86 : : }
87 : :
88 [ + - ]: 19 : std::string buffer;
89 [ + - ]: 19 : buffer.reserve(256);
90 : :
91 : 19 : bool quoted = false;
92 : 19 : int c = EOF;
93 [ + - ][ + + ]: 1075476 : while ((c = fgetc(fp)) != EOF)
94 : : {
95 [ - + + ]: 1075457 : switch(c)
96 : : {
97 : : case '\"':
98 : 0 : quoted = !quoted;
99 : 0 : break;
100 : : case ' ':
101 : : case '\t':
102 : : case '\r':
103 : : case '\n':
104 [ + - ]: 9549 : if (!quoted)
105 : : {
106 [ + + ]: 9549 : if (!buffer.empty())
107 : : {
108 [ + - ][ - + ]: 9511 : if (!checkArgument(rArgs, buffer.c_str(), buffer.size()))
109 : : {
110 : : // failure.
111 [ # # ]: 0 : (void) fclose(fp);
112 : 0 : return false;
113 : : }
114 : 9511 : buffer.clear();
115 : : }
116 : 9549 : break;
117 : : }
118 : : default:
119 : : // quoted white-space fall through
120 [ + - ]: 1065908 : buffer.push_back(sal::static_int_cast<char>(c));
121 : 1065908 : break;
122 : : }
123 : : }
124 [ + - ]: 19 : return (fclose(fp) == 0);
125 : : }
126 : :
127 : 22 : bool Options::initOptions (std::vector< std::string > & rArgs)
128 : : {
129 : 22 : 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: */
|