• Main Page
  • Classes
  • Files
  • File List
  • File Members

Beesnest/DcParameters.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright 2007 Erez Bibi (erezbibi@users.sourceforge.net)
00003 This file is part of Beesnest.
00004 
00005 Beesnest 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 Beesnest 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 Beesnest; if not, write to the Free Software
00017 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 */
00019 
00020 
00021 /**
00022  * DcParameters.cpp: Implementation of CdcParameters class.
00023  * for documentation see the header file.
00024  */
00025 
00026 #include "DcParameters.h"
00027 
00028 CdcParameters::t_obj_map CdcParameters::mParam;
00029 
00030 bool CdcParameters::ReadParams (const char* file_ext) throw (CdcException)
00031 {
00032     /* Build a CdcString buffer from the parameters files in the working directory. */
00033     CdcString buff;
00034     BuildParamBuffer (buff, CdcFileUtil::GetProgramPath (), file_ext);
00035     /* Parse the buffer. */
00036     CdcString name, val, field;
00037     int start, end;
00038     bool is_ok = true;
00039     CdcParameters::CdcNode* cur_node = NULL;
00040     start = buff.Find ('<');
00041     /* While you have new <field> */
00042     while (start != -1)
00043     {   /* Find the end of field name. */
00044         end = buff.Find ('>', start);
00045         if (end == -1)
00046             throw (CdcExceptionFile ("Bad Parameters file format (Cannot find '>')",
00047             DC_FILE_EXP));
00048         /* Get the field name. */
00049         field = buff.Mid (start + 1, end - start -1);
00050         /* Ignore attributes. */
00051         int tmp = field.FindOneOf (" \t\r\n");
00052         if (tmp > 0)
00053             field = field.Left (tmp);
00054         /* Find next field start. */
00055         start = buff.Find ('<', end);
00056         /* If this is closing field. */
00057         if (field[0] == '/')
00058         {   /* Erase the filed name from the 'name' string. */
00059             field = field.Mid (1);
00060             if (cur_node == NULL || field != cur_node->name)
00061             {
00062                 CdcString temp (
00063                 "Bad Parameters file format - Cannot close field with \"" + field + "\"");
00064                 throw (CdcExceptionFile (temp.GetBuffer (), DC_FILE_EXP));
00065             }
00066             name = name.Left (name.GetLength () - field.GetLength ());
00067             if (name.IsEmpty () == false)
00068                 name = name.Left (name.GetLength () - PARM_SEP_LEN);
00069             cur_node = cur_node->up;
00070         }
00071         else
00072         {   /* Proper Parameters file will not end here. */
00073             if (start == -1)
00074                 throw (CdcExceptionFile (
00075                 "Bad Parameters file format (File does not end properly)",
00076                 DC_FILE_EXP));
00077             /* If this is an opening field name, add the field name into the
00078             'name' string, read the value, and insert to the map under 'name'. */
00079             if (name.IsEmpty () == false)
00080                 name += PARM_SEP;
00081             name += field;
00082             CdcNode* temp_node = mParam [name];
00083             if (temp_node == NULL)
00084             {
00085                 temp_node = new CdcNode (field, cur_node);
00086                 if (temp_node == NULL)
00087                     throw (CdcExceptionFile ("Out of memory", DC_GENERAL_EXP));
00088                 if (cur_node != NULL)
00089                     cur_node->oNodes.push_back (temp_node);
00090             }
00091             cur_node = temp_node;
00092             val = buff.Mid (end + 1, start - end -1);
00093             val.TrimLeft ();
00094             val.TrimRight ();
00095             if (val.IsEmpty () == false)    /* Insert to the map if not empty. */
00096                 cur_node->value = val;
00097             mParam [name] = cur_node; /* maybe just replace... */
00098         }
00099     }
00100     return is_ok;
00101 }
00102 
00103 
00104 CdcString CdcParameters::GetParam (const CdcString& name)
00105 {
00106     CdcNode* val = NULL;
00107     val = mParam [name];
00108     if (val == NULL)
00109         return EMPTY_STR;
00110     return val->value;
00111 }
00112 
00113 
00114 bool ExtGetParam (const char* name, char* val, int max_len)
00115 {
00116     CdcString sval = CdcParameters::GetParam (name);
00117     if (sval.IsEmpty ())
00118         return false;
00119     strncpy (val, sval.GetBuffer (), max_len);
00120     return true;
00121 }
00122 
00123 
00124 CdcParameters::CdcNode* CdcParameters::GetNodePointer (const char* name)
00125 {
00126     CdcNode* val = NULL;
00127     val = mParam [name];
00128     return val;
00129 }
00130 
00131 bool CdcParameters::IsUnder (CdcParameters::CdcNode* node, const CdcString& value,
00132                              bool partly)
00133 {   /* Base case. */
00134     if (node == NULL)
00135         return false;
00136     /* Compare this node. */
00137     if (partly)
00138     {
00139         if (node->value.Find (value) != -1)
00140             return true;
00141     }
00142     else
00143     {
00144         if (node->value == value)
00145             return true;
00146     }
00147     /* Check the child of this node (recursive call). */
00148     for (CdcParameters::CdcNode::t_node_list::const_iterator
00149         pos = node->oNodes.begin ();
00150         pos != node->oNodes.end (); ++pos)
00151 
00152         if (IsUnder ((CdcNode*)(*pos), value, partly) == true)
00153             return true;
00154     /* Other base case. */
00155     return false;
00156 }
00157 
00158 bool CdcParameters::IsUnder (const CdcString& name, const CdcString& value,
00159                              bool partly)
00160 {
00161     /* Get the starting node and search from it. */
00162     CdcNode* node = NULL;
00163     node = mParam [name];
00164     return IsUnder (node, value, partly);
00165 }
00166 
00167 
00168 CdcString CdcParameters::GetFullName (const CdcString& name,
00169                                     const CdcString& start_at)
00170 {
00171     /* Get the starting node and search from it. */
00172     CdcNode* node = NULL;
00173     node = mParam [start_at];
00174     CdcString temp = FindName (name, node, false);
00175     if (temp.IsEmpty ()) return temp;
00176     return start_at + PARM_SEP + temp;
00177 }
00178 
00179 
00180 CdcString CdcParameters::FindName (const CdcString& name,
00181                                  CdcParameters::CdcNode* node,
00182                                  bool append_name)
00183 {   /* Base case. */
00184     if (node == NULL) return EMPTY_STR;
00185     if (node->name == name) return name;
00186 
00187     /* Check the child of this node (recursive call). */
00188     for (CdcParameters::CdcNode::t_node_list::const_iterator
00189         pos = node->oNodes.begin ();
00190         pos != node->oNodes.end (); pos++)
00191     {
00192         CdcString temp = FindName (name,
00193             (CdcNode*)(*pos), true);
00194         if (temp.IsEmpty () == false)
00195             if (append_name)
00196                 return node->name + PARM_SEP + temp;
00197             else
00198                 return temp;
00199     }
00200     /* Other base case. */
00201     return EMPTY_STR;
00202 }
00203 
00204 
00205 int CdcParameters::ToInt (const CdcString& str)
00206 {
00207     CdcString temp = str;
00208     return atoi (temp.GetBuffer ());
00209 }
00210 
00211 void CdcParameters::BuildParamBuffer (CdcString& buff,
00212                                       const CdcString& path,
00213                                       const CdcString& ext)
00214 {
00215     t_files_vector vect = CdcFileUtil::GetAllFilesInDir
00216         (path, ext);
00217     t_files_vector::iterator itr;
00218     for (itr = vect.begin(); itr != vect.end(); ++itr)
00219         ReadParamFile (buff, *itr);
00220 }
00221 
00222 
00223 void CdcParameters::ReadParamFile (CdcString& buff,
00224                                    const CdcString& file_name)
00225 {
00226     /* Open the parameters file. */
00227     ifstream file (file_name.GetBuffer ());
00228     if (file.is_open () == 0)
00229         throw (CdcExceptionFile ("Cannot open Parameters file",
00230             DC_FILE_EXP));
00231 
00232     /* Read the entire file (w/o lines that start with '#' or empty lines) into
00233     the CdcString buffer. */
00234     char temp [BUFF_LEN];
00235     while (file.eof () == 0)
00236     {
00237         file.getline (temp, BUFF_LEN, '\n');
00238         CdcString str = temp;
00239         str.Trim ();
00240         int place = str.Find ('#');
00241         if (place > -1)
00242             str = str.Left (place);
00243         if (str.IsEmpty ()) continue;
00244         buff += str + ' ';
00245     }
00246 }
00247 

Generated on Mon Oct 11 2010 16:23:24 for Beesnest by  doxygen 1.7.2