blob: f6af60054b8df3a6642f88fbc01c36dfb7a57af5 (
plain)
1 ////////////////////////////////////////////////////////////////////////
2 // FILE: configparser.cpp
3 // AUTHOR: Johannes Winkelmann, jw@tks6.net
4 // COPYRIGHT: (c) 2002-2005 by Johannes Winkelmann
5 // ---------------------------------------------------------------------
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 ////////////////////////////////////////////////////////////////////////
11
12 #include <iostream>
13 #include "configparser.h"
14
15 using namespace std;
16
17 int ConfigParser::parseConfig(const std::string& fileName,
18 Config& config)
19 {
20 FILE* fp = fopen(fileName.c_str(), "r");
21 if (!fp) {
22 return -1;
23 }
24
25 char line[512];
26 string s;
27 while (fgets(line, 512, fp)) {
28 if (line[strlen(line)-1] == '\n') {
29 line[strlen(line)-1] = '\0';
30 }
31 s = line;
32
33 // strip comments
34 string::size_type pos = s.find("#");
35 if (pos != string::npos) {
36 s = s.substr(0, pos);
37 }
38
39 // whitespace separates
40 pos = s.find(' ');
41 if (pos == string::npos) {
42 pos = s.find('\t');
43 }
44 if (pos != string::npos) {
45 string key = s.substr(0, pos);
46 string val = stripWhiteSpace(s.substr(pos));
47
48 if (key == "proxy_host") {
49 config.proxyHost = val;
50 } else if (key == "proxy_port") {
51 config.proxyPort = val;
52 } else if (key == "proxy_user") {
53 config.proxyUser = val;
54 } else if (key == "proxy_pass") {
55 config.proxyPassword = val;
56 } else if (key == "operation_timeout") {
57 config.operationTimeout = val;
58 }
59 }
60 }
61
62 fclose(fp);
63 return 0;
64 }
65
66 string ConfigParser::stripWhiteSpace(const string& input)
67 {
68 string output = input;
69 while (isspace(output[0])) {
70 output = output.substr(1);
71 }
72 while (isspace(output[output.length()-1])) {
73 output = output.substr(0, output.length()-1);
74 }
75
76 return output;
77 }
|