forked from winkj/httpup
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfigparser.cpp
79 lines (69 loc) · 2.27 KB
/
configparser.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
////////////////////////////////////////////////////////////////////////
// FILE: configparser.cpp
// AUTHOR: Johannes Winkelmann, [email protected]
// COPYRIGHT: (c) 2002-2005 by Johannes Winkelmann
// ---------------------------------------------------------------------
// 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.
////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <cstdio>
#include <cstring>
#include "configparser.h"
using namespace std;
int ConfigParser::parseConfig(const std::string& fileName,
Config& config)
{
FILE* fp = fopen(fileName.c_str(), "r");
if (!fp) {
return -1;
}
char line[512];
string s;
while (fgets(line, 512, fp)) {
if (line[strlen(line)-1] == '\n') {
line[strlen(line)-1] = '\0';
}
s = line;
// strip comments
string::size_type pos = s.find("#");
if (pos != string::npos) {
s = s.substr(0, pos);
}
// whitespace separates
pos = s.find(' ');
if (pos == string::npos) {
pos = s.find('\t');
}
if (pos != string::npos) {
string key = s.substr(0, pos);
string val = stripWhiteSpace(s.substr(pos));
if (key == "proxy_host") {
config.proxyHost = val;
} else if (key == "proxy_port") {
config.proxyPort = val;
} else if (key == "proxy_user") {
config.proxyUser = val;
} else if (key == "proxy_pass") {
config.proxyPassword = val;
} else if (key == "operation_timeout") {
config.operationTimeout = val;
}
}
}
fclose(fp);
return 0;
}
string ConfigParser::stripWhiteSpace(const string& input)
{
string output = input;
while (isspace(output[0])) {
output = output.substr(1);
}
while (isspace(output[output.length()-1])) {
output = output.substr(0, output.length()-1);
}
return output;
}