Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
00033 CdcString buff;
00034 BuildParamBuffer (buff, CdcFileUtil::GetProgramPath (), file_ext);
00035
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
00042 while (start != -1)
00043 {
00044 end = buff.Find ('>', start);
00045 if (end == -1)
00046 throw (CdcExceptionFile ("Bad Parameters file format (Cannot find '>')",
00047 DC_FILE_EXP));
00048
00049 field = buff.Mid (start + 1, end - start -1);
00050
00051 int tmp = field.FindOneOf (" \t\r\n");
00052 if (tmp > 0)
00053 field = field.Left (tmp);
00054
00055 start = buff.Find ('<', end);
00056
00057 if (field[0] == '/')
00058 {
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 {
00073 if (start == -1)
00074 throw (CdcExceptionFile (
00075 "Bad Parameters file format (File does not end properly)",
00076 DC_FILE_EXP));
00077
00078
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)
00096 cur_node->value = val;
00097 mParam [name] = cur_node;
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 {
00134 if (node == NULL)
00135 return false;
00136
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
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
00155 return false;
00156 }
00157
00158 bool CdcParameters::IsUnder (const CdcString& name, const CdcString& value,
00159 bool partly)
00160 {
00161
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
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 {
00184 if (node == NULL) return EMPTY_STR;
00185 if (node->name == name) return name;
00186
00187
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
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
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
00233
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