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