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 #ifndef UAPBasicUtilities_hpp
00027 #define UAPBasicUtilities_hpp 1
00028
00029 #include <string>
00030 #include <map>
00031 #include <list>
00032 #include <set>
00033 #include <iostream>
00034 #include <fstream>
00035 #include <vector>
00036 #include <valarray>
00037
00038 typedef std::string Str;
00039
00040 typedef std::list<std::string> StrList;
00041 typedef std::list<std::string>::iterator StrListIter;
00042 typedef std::list<std::string>::const_iterator StrListCIter;
00043
00044 typedef std::map<std::string, std::string> StrMap;
00045 typedef std::map<std::string, std::string>::iterator StrMapIter;
00046 typedef std::map<std::string, std::string>::const_iterator StrMapCIter;
00047
00048 typedef std::map<std::string, int> IntMap;
00049 typedef std::map<std::string, int>::iterator IntMapIter;
00050 typedef std::map<std::string, int>::const_iterator IntMapCIter;
00051
00052 typedef std::vector<std::string> StrVec;
00053 typedef std::vector<std::string>::iterator StrVecIter;
00054 typedef std::vector<std::string>::const_iterator StrVecCIter;
00055
00056 typedef std::map<std::string, StrVec> StrVecMap;
00057 typedef std::map<std::string, StrVec>::iterator StrVecMapIter;
00058 typedef std::map<std::string, StrVec>::const_iterator StrVecMapCIter;
00059
00060 typedef std::valarray<int> IntArray;
00061 typedef std::vector<int> IntVec;
00062 typedef std::list<int> IntList;
00063
00064 typedef std::vector<double> DoubleVec;
00065
00066
00067 StrList& operator<< (StrList& s_list, const std::string& str);
00068 StrList& operator<< (StrList& list_out, StrList list_in);
00069
00070 StrVec& operator<< (StrVec& s_vector, const std::string& str);
00071 StrVec& operator<< (StrVec& vector_out, StrVec list_in);
00072
00077 namespace BasicUtilities {
00078
00079 const Str ERROR_STR = "!ERROR";
00080
00085 Str trim(Str str);
00086
00094 void split_file_name (Str file_name_in, Str& dir, Str& file_name, bool& is_relative);
00095 void split_file_name (Str file_name_in, Str& dir, Str& file_name);
00096
00103 int string_to_int (const Str& str, bool& ok);
00104 int string_to_int (const Str& str);
00105
00111 bool is_int (const Str& str);
00112
00119 double string_to_double (const Str& str, bool& ok);
00120 double string_to_double (const Str& str);
00121
00127 bool is_double (const Str& str);
00128
00135 bool string_to_bool (const Str& str, bool& ok);
00136 bool string_to_bool (const Str& str);
00137
00143 bool is_bool (const Str& str);
00144
00149 Str bool_to_string (bool this_bool);
00150
00156 Str int_to_string (const int i, bool& ok);
00157 Str int_to_string (const int i);
00158
00164 Str double_to_string (const double r, bool& ok);
00165 Str double_to_string (const double r);
00166
00171 Str str_to_upper (const Str& str_in);
00172
00177 Str str_to_lower (const Str& str_in);
00178
00186 Str add_parens (const Str& sub_expression, const Str& expression, int i0);
00187
00204 Str add_parens (const Str& sub_expression, int op_level);
00205
00212 Str to_xml_string (Str str_in);
00213
00219 Str from_xml_string (Str str_in);
00220
00225 Str str_pop (StrList& str_list);
00226
00232 Str element_val(StrList& s_list, int index);
00233
00239 bool found(StrList& s_list, const Str& item);
00240 bool found(StrVec& s_vector, const Str& item);
00241 bool found(Str& str, const Str& item);
00242 template <class T> inline bool found(std::map<Str, T>& this_map, const Str& item)
00243 {
00244 return (this_map.find(item) != this_map.end());
00245 }
00246
00252 StrListIter find(StrList& s_list, const Str& item);
00253 StrListIter rfind(StrList& s_list, const Str& item);
00254
00260 StrList list_copy(StrListIter begin, StrListIter end);
00261
00267 bool found_value(StrMap& string_map, const Str& item);
00268
00274 bool get_taylor_exponents (const Str& exp_str, IntVec& exp_vec);
00275
00286 bool splitXMLName (const Str& _name, Str& uri, Str& prefix,
00287 Str& loc_name);
00288 bool splitXMLName (const Str& _name, Str& uri, Str& prefix,
00289 Str& loc_name, bool& prefix_found);
00290
00291 }
00292
00293
00294
00295 struct FileStackElement {
00296 bool splitName (const Str& file_name_in);
00297 Str this_name;
00298 Str this_dir;
00299 Str this_file_no_dir;
00300 Str full_dir;
00301 Str full_name;
00302 };
00303
00304 struct FileStackList {
00305 void addFile (const Str& file_name);
00306 std::list<FileStackElement> file_list;
00307 };
00308
00309
00310
00311
00312
00313
00314
00315 struct EndStruct {};
00316
00317 class StreamStruct {
00318
00319 public:
00320
00321 StreamStruct(Str file_name = "");
00322
00323 Str continuation_char;
00324 int n_indent;
00325 int max_len;
00326
00327 void close_file();
00328 void open_file(const Str& file_name);
00329 bool is_open();
00330 StreamStruct& operator<< (const Str& str);
00331 StreamStruct& operator<< (const int i);
00332 StreamStruct& operator<< (EndStruct& s);
00333 StreamStruct& operator= (StreamStruct& s);
00334
00335 private:
00336
00337 int ix_line;
00338 std::ofstream *out_file;
00339
00340 };
00341
00342
00343
00344
00345
00346
00347 class PrintInfoStruct {
00348
00349 public:
00350
00351 void error (Str line1, Str line2 = "", Str line3 = "");
00352 void warning (Str line1, Str line2 = "", Str line3 = "");
00353
00354 static Str parsing_status;
00355 static Str file_name;
00356 static Str statement;
00357 static int ix_line;
00358 static bool everything_ok;
00359
00360 private:
00361
00362 void print (Str& line2, Str& line3);
00363
00364 };
00365
00366
00367 #endif