httpup/configparser.cpp

80 lines
2.3 KiB
C++
Raw Normal View History

2005-11-09 21:44:34 +00:00
////////////////////////////////////////////////////////////////////////
// FILE: configparser.cpp
// AUTHOR: Johannes Winkelmann, jw@tks6.net
// COPYRIGHT: (c) 2002-2005 by Johannes Winkelmann
// ---------------------------------------------------------------------
2006-02-23 14:18:42 +00:00
// 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.
2005-11-09 21:44:34 +00:00
////////////////////////////////////////////////////////////////////////
#include <iostream>
2009-05-13 18:45:47 +02:00
#include <cstdio>
#include <cstring>
2005-11-09 21:44:34 +00:00
#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;
}
2006-02-23 14:18:42 +00:00
2005-11-09 21:44:34 +00:00
char line[512];
string s;
while (fgets(line, 512, fp)) {
if (line[strlen(line)-1] == '\n') {
line[strlen(line)-1] = '\0';
2006-02-23 14:18:42 +00:00
}
2005-11-09 21:44:34 +00:00
s = line;
2006-02-23 14:18:42 +00:00
// strip comments
2005-11-09 21:44:34 +00:00
string::size_type pos = s.find("#");
if (pos != string::npos) {
s = s.substr(0, pos);
}
2006-02-23 14:18:42 +00:00
// 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));
2005-11-09 21:44:34 +00:00
if (key == "proxy_host") {
config.proxyHost = val;
2006-02-23 14:18:42 +00:00
} else if (key == "proxy_port") {
2005-11-09 21:44:34 +00:00
config.proxyPort = val;
2006-02-23 14:18:42 +00:00
} else if (key == "proxy_user") {
2005-11-09 21:44:34 +00:00
config.proxyUser = val;
2006-02-23 14:18:42 +00:00
} else if (key == "proxy_pass") {
2005-11-09 21:44:34 +00:00
config.proxyPassword = val;
2006-02-23 14:18:42 +00:00
} else if (key == "operation_timeout") {
config.operationTimeout = val;
2005-11-09 21:44:34 +00:00
}
}
}
2006-02-23 14:18:42 +00:00
2005-11-09 21:44:34 +00:00
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);
}
2006-02-23 14:18:42 +00:00
2005-11-09 21:44:34 +00:00
return output;
}