summaryrefslogtreecommitdiff
path: root/configparser.cpp
blob: 5ed1c95a56501cdba8982cb2eb97f832b2837148 (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 <cstdio>
   14 #include <cstring>
   15 #include "configparser.h"
   16 
   17 using namespace std;
   18 
   19 int ConfigParser::parseConfig(const std::string& fileName,
   20                               Config& config)
   21 {
   22     FILE* fp = fopen(fileName.c_str(), "r");
   23     if (!fp) {
   24         return -1;
   25     }
   26 
   27     char line[512];
   28     string s;
   29     while (fgets(line, 512, fp)) {
   30         if (line[strlen(line)-1] == '\n') {
   31             line[strlen(line)-1] = '\0';
   32         }
   33         s = line;
   34 
   35         // strip comments
   36         string::size_type pos = s.find("#");
   37         if (pos != string::npos) {
   38             s = s.substr(0, pos);
   39         }
   40 
   41         // whitespace separates
   42         pos = s.find(' ');
   43         if (pos == string::npos) {
   44             pos = s.find('\t');
   45         }
   46         if (pos != string::npos) {
   47             string key = s.substr(0, pos);
   48             string val = stripWhiteSpace(s.substr(pos));
   49         
   50             if (key == "proxy_host") {
   51                 config.proxyHost = val;
   52             } else if (key == "proxy_port") {
   53                 config.proxyPort = val;
   54             } else if (key ==  "proxy_user") {
   55                 config.proxyUser = val;
   56             } else if (key ==  "proxy_pass") {
   57                 config.proxyPassword = val;
   58             } else if (key == "operation_timeout") {
   59                 config.operationTimeout = val;
   60             }
   61         }
   62     }
   63 
   64     fclose(fp);
   65     return 0;
   66 }
   67 
   68 string ConfigParser::stripWhiteSpace(const string& input)
   69 {
   70     string output = input;
   71     while (isspace(output[0])) {
   72         output = output.substr(1);
   73     }
   74     while (isspace(output[output.length()-1])) {
   75         output = output.substr(0, output.length()-1);
   76     }
   77 
   78     return output;
   79 }

Generated by cgit