This repository has been archived by the owner on Jan 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoption.cpp
98 lines (77 loc) · 3.32 KB
/
option.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/***************************************************************************
* Copyright (C) 2013 by Torsten Flammiger *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "option.h"
Option::Option(int argc, char **argv, Settings *cfg)
{
_argc = argc;
_argv = argv;
_cfg = *cfg;
}
bool Option::isVerbose() {
char* env = getenv("SEARCH_VERBOSE");
if(! env) return false;
if( strlen(env) == 1 && env[0] == '1') return true;
return false;
}
bool Option::hasOption(const std::string &option) {
for(int i=1; i<_argc; i++) {
std::string argument(_argv[i]);
if(argument == option)
return true;
}
return false;
}
std::string Option::getOption(const std::string &option) {
for(int i=1; i<_argc; i++) {
std::string argument(_argv[i]);
int position = argument.find('=');
std::string left = argument.substr(0, position);
std::string right = argument.substr(position + 1);
// sanitize me!
if(left == option) return right;
}
return "";
}
bool Option::doConfig() {
std::string tmp = this->getOption("--name");
if(tmp.length() == 0) return false;
tmp = this->getOption("--server");
if(tmp.length() == 0) return false;
tmp = this->getOption("--username");
if(tmp.length() == 0) return false;
tmp = this->getOption("--password");
if(tmp.length() == 0) return false;
return true;
}
void Option::configure() {
// for the name option set the section!
std::string section = this->getOption("--name");
_cfg.setSection(section);
// for the rest call setProperty
std::string tmp = this->getOption("--server");
_cfg.setProperty(section, "server", tmp);
tmp = this->getOption("--username");
_cfg.setProperty(section, "username", tmp);
tmp = this->getOption("--password");
_cfg.setProperty(section, "password", tmp);
// chmod go-a to the config file, ignore the results
chmod(_cfg.getConfigDir().c_str(), S_IRUSR | S_IWUSR | S_IXUSR);
chmod(_cfg.getConfigFile().c_str(), S_IRUSR | S_IWUSR);
}