/home/vlg/develop/libASSA/libassa/assa/IniFile.cpp

Go to the documentation of this file.
00001 // -*- c++ -*-
00002 //------------------------------------------------------------------------------
00003 // $Id: IniFile.cpp,v 1.7 2005/08/19 03:18:24 vlg Exp $
00004 //------------------------------------------------------------------------------
00005 //                             IniFile.cpp
00006 //------------------------------------------------------------------------------
00007 //  Copyright (C) 2003,2004  Vladislav Grinchenko <vlg@users.sourceforge.net>
00008 //
00009 //  This library is free software; you can redistribute it and/or
00010 //  modify it under the terms of the GNU Library General Public
00011 //  License as published by the Free Software Foundation; either
00012 //  version 2 of the License, or (at your option) any later version.
00013 //
00014 //  Date: Sat Sep  6 00:17:52 EDT 2003
00015 //------------------------------------------------------------------------------
00016 
00017 #include <assa/Assure.h>
00018 #include <assa/IniFile.h>
00019 #include <assa/CommonUtils.h>
00020 using namespace ASSA;
00021 
00022 IniFile::
00023 IniFile (const string& fname_) 
00024     :
00025     m_fname (fname_),
00026     m_section_pttrn ("\\[[a-zA-Z0-9]+.*] *$"),
00027     m_tuple_pttrn ("^[ \t]*[a-zA-Z0-9]+.* *= *.*"),
00028     m_comment_pttrn ("^#.*$")
00029 {
00030     trace_with_mask ("IniFile::IniFile", INIFILE);
00031 }
00032     
00033 IniFile::
00034 ~IniFile ()
00035 {
00036     trace_with_mask ("IniFile::~IniFile", INIFILE);
00037     m_config.clear ();
00038 }
00039 
00040 int 
00041 IniFile::
00042 load ()
00043 {
00044     trace_with_mask ("IniFile::load", INIFILE);
00045     
00046     const int size = 1024;
00047     char inbuf [size];
00048 
00049     string line;
00050     string name;
00051     string value;
00052     int ret = -1;
00053 
00054     m_stream.open (m_fname.c_str (), std::ios::in);
00055     if (!m_stream) {
00056         goto done;
00057     }
00058 
00059     while (m_stream) {
00060         m_stream.getline (inbuf, size, '\n');
00061         DL((INIFILE,"Input: \"%s\"\n", inbuf));
00062 
00063         if (::strlen (inbuf) == 0 || m_comment_pttrn.match (inbuf) == 0) {
00064             continue;
00065         }
00066         line = inbuf;
00067         if (m_section_pttrn.match (inbuf) == 0) {
00068             if (trim_section_name (line) < 0) {
00069                 goto done;
00070             }
00071             m_config.push_back (sect_type (line, std::list<tuple_type> ()));
00072         }
00073         else if (m_tuple_pttrn.match (inbuf) == 0) {
00074             if (Utils::split_pair (line, '=', name, value) < 0) {
00075                 goto done;
00076             }
00077             Utils::trim_sides (name);
00078             Utils::trim_sides (value);
00079             m_config.back ().second.push_back (tuple_type (name, value));
00080         }
00081         else {
00082             goto done;
00083         }
00084     }
00097     ret = 0;
00098 
00099  done:
00100     if (ret < 0) {
00101         DL((INIFILE, "Parse error: illegal syntax!\n"));
00102     }
00103     m_stream.clear ();
00104     m_stream.close ();
00105     return ret;
00106 }
00107 
00108 int 
00109 IniFile::
00110 sync (const string& fname_)
00111 {
00112     trace_with_mask ("IniFile::sync(fname)", INIFILE);
00113 
00114     ::unlink (fname_.c_str ());
00115     m_stream.open (fname_.c_str (), std::ios::app | std::ios::out);
00116     if (!m_stream) {
00117         EL((INIFILE,"Failed to open(\"%s\", app|out)\n", fname_.c_str ()));
00118         return -1;
00119     }
00120     const_config_iterator i = m_config.begin ();
00121     const_tuple_iterator j;
00122 
00123     while (i != m_config.end ()) {
00124         m_stream << "[" << (*i).first << "]\n";
00125         j = (*i).second.begin ();
00126 
00127         while (j != (*i).second.end ()) {
00128             m_stream << (*j).first << "=" << (*j).second << "\n";
00129             j++;
00130         }
00131         m_stream << "\n";
00132         i++;
00133     }
00134     m_stream.clear ();
00135     m_stream.close ();
00136     return 0;
00137 }
00138 
00139 void 
00140 IniFile::
00141 dump () const
00142 {
00143     trace_with_mask ("IniFile::dump", INIFILE);
00144 
00145     const_config_iterator i = m_config.begin ();
00146     const_tuple_iterator j;
00147 
00148     DL((INIFILE,"============= Start =================\n"));
00149 
00150     while (i != m_config.end ()) {
00151         DL((INIFILE, "[%s]\n", (*i).first.c_str ()));
00152         j = (*i).second.begin ();
00153         while (j != (*i).second.end ()) {
00154             DL((INIFILE, "  %s=\%s\n",
00155                 (*j).first.c_str (), (*j).second.c_str ()));
00156             j++;
00157         }
00158         i++;
00159     }
00160     DL((INIFILE,"============== End  =================\n"));
00161 }
00162 
00163 string
00164 IniFile::
00165 get_value (const string& section_, const string& name_) const
00166 {
00167     const_config_iterator i = m_config.begin ();
00168     const_tuple_iterator j;
00169     string ret ("");
00170 
00171     while (i != m_config.end ()) {
00172         if ((*i).first == section_) {
00173             j = (*i).second.begin ();
00174             while (j != (*i).second.end ()) {
00175                 if ((*j).first == name_) {
00176                     ret = (*j).second;
00177                     break;
00178                 }
00179                 j++;
00180             }
00181         }
00182         i++;
00183     }
00184     return ret;
00185 }
00186 
00187 void
00188 IniFile::
00189 add_section (const string& section_)
00190 {
00191     const_config_iterator i = find_section (section_);
00192     if (i == m_config.end ()) {
00193         m_config.push_back (sect_type (section_, std::list<tuple_type> ()));
00194     }
00195 }
00196 
00197 IniFile::config_iterator 
00198 IniFile::
00199 find_section (const string& section_)
00200 {
00201     config_iterator i = m_config.begin ();
00202 
00203     while (i != m_config.end ()) {
00204         if ((*i).first == section_) {
00205             return (i);
00206         }
00207         i++;
00208     }
00209     return m_config.end ();
00210 }
00211 
00212 IniFile::const_config_iterator 
00213 IniFile::
00214 find_section (const string& section_) const
00215 {
00216     const_config_iterator i = m_config.begin ();
00217 
00218     while (i != m_config.end ()) {
00219         if ((*i).first == section_) {
00220             return (i);
00221         }
00222         i++;
00223     }
00224     return m_config.end ();
00225 }
00226 
00227 int
00228 IniFile::
00229 set_pair (const string& section_, const tuple_type& newkey_)
00230 {
00231     trace_with_mask ("IniFile::set_pair", INIFILE);
00232 
00233     config_iterator i = find_section (section_);
00234     if (i == sect_end ()) {
00235         DL((INIFILE,"Section [%s] is not found!\n", section_.c_str ()));
00236         return -1;
00237     }
00238     tuple_iterator j = (*i).second.begin ();
00239     while (j != (*i).second.end ()) {
00240         if ((*j).first == newkey_.first) {
00241             (*j).second = newkey_.second;
00242             return 0;
00243         }
00244         j++;
00245     }
00246     (*i).second.push_back (newkey_);
00247     return 0;
00248 }
00249 
00250 int
00251 IniFile::
00252 drop_section (const string& section_)
00253 {
00254     trace_with_mask ("IniFile::drop_section", INIFILE);
00255 
00256     config_iterator i = find_section (section_);
00257     if (i == sect_end ()) {
00258         DL((INIFILE,"Section [%s] is not found!\n", section_.c_str ()));
00259         return -1;
00260     }
00261     m_config.erase (i);
00262     return 0;
00263 }
00264 
00265 int
00266 IniFile::
00267 drop_pair (const string& section_, const string& name_)
00268 {
00269     trace_with_mask ("IniFile::drop_pair", INIFILE);
00270 
00271     config_iterator i = find_section (section_);
00272     if (i == sect_end ()) {
00273         DL((INIFILE,"Section [%s] is not found!\n", section_.c_str ()));
00274         return -1;
00275     }
00276 
00277     tuple_iterator j = (*i).second.begin ();
00278     while (j != (*i).second.end ()) {
00279         if ((*j).first == name_) {
00280             (*i).second.erase (j);
00281             return 0;
00282         }
00283         j++;
00284     }
00285     return -1;
00286 }

Generated on Sun Aug 13 15:08:00 2006 for libassa by  doxygen 1.4.6