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