read.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2004 by Michael Moritz                                  *
00003  *   mimo@restoel.net                                                      *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #ifdef HAVE_CONFIG_H
00022 #include <config.h>
00023 #endif
00024 #include "read.h"
00025 #include <iostream>
00026 #include <string>
00027 #include "cfg.h"
00028 
00029 using namespace std;
00030 
00031 const string Reader::_s_notfound = "key not found";
00032 
00033 int Reader::parse(const Reader::AttributeSet& attr, istream &is) 
00034 {
00035         while(!is.eof()) {
00036                 string line;
00037                 getline(is,line);       
00038                 if(line.length() > 0) {
00039                         if( g_getCfg().isVerbose() ) // only build inpu string if in verbose mode
00040                                 _input += line + " ";
00041                         string::size_type eqpos = line.find('=');
00042                         if(eqpos != string::npos) {
00043                                 const string key = line.substr(0,eqpos);
00044                                 Reader::AttributeSet::const_iterator itr = attr.find(key);
00045                                 if( itr != attr.end() ) {
00046                                         const string val = line.substr(eqpos+1,string::npos);
00047                                         _map.insert(AttributeMap::value_type(key,val));
00048                                         //cout << "key: " << key << " val: "<<val<< endl;
00049                                 }
00050                         }
00051                 } else {
00052                         return _map.size();
00053                 }
00054         }
00055         return -1;
00056 }
00057 bool Reader::haveAllRequired(const Reader::AttributeSet& attr) const
00058 {
00059         AttributeSet::const_iterator itr;
00060         for(itr=attr.begin(); itr != attr.end(); ++itr) {
00061                 if( ! itr->second) 
00062                         continue;
00063                 if( _map.find(itr->first) == _map.end() )
00064                         return false;
00065         }
00066         return true;
00067 }
00068 
00069 
00070 
00071 /*
00072 int main()
00073 {
00074         Reader::AttributeSet attrSet;
00075         attrSet.insert("client_address");
00076         attrSet.insert("sender");
00077         attrSet.insert("recipient");
00078 
00079         Reader testReader(attrSet,cin);
00080 }
00081 */

Generated on Tue Jul 24 16:36:53 2007 for gps by  doxygen 1.5.1