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 <iostream>
00027 #include <algorithm>
00028 #include <sstream>
00029
00030 #include "UAP/BasicUtilities.hpp"
00031
00032 namespace BU = BasicUtilities;
00033 using namespace std;
00034
00035
00036
00037
00038 string PrintInfoStruct::parsing_status;
00039 string PrintInfoStruct::file_name;
00040 string PrintInfoStruct::statement;
00041 int PrintInfoStruct::ix_line;
00042 bool PrintInfoStruct::everything_ok;
00043
00044
00045
00046
00047 StrList& operator<< (StrList& s_list, const string& str) {
00048 s_list.push_back(str);
00049 return s_list;
00050 }
00051
00052 StrList& operator<< (StrList& list_out, StrList list_in) {
00053 list_out.merge(list_in);
00054 return list_out;
00055 }
00056
00057
00058
00059
00060 StrVec& operator<< (StrVec& s_vector, const string& str) {
00061 s_vector.push_back(str);
00062 return s_vector;
00063 }
00064
00065 StrVec& operator<< (StrVec& vector_out, StrVec vector_in) {
00066 vector_out.insert(vector_out.end(), vector_in.begin(), vector_in.end());
00067 return vector_out;
00068 }
00069
00070
00071
00072
00073 std::string BU::element_val(StrList& s_list, int index) {
00074 if (index > s_list.size()-1) return "";
00075 StrListIter is = s_list.begin();
00076 for (int i = 0; i < index; i++)
00077 is++;
00078 return *is;
00079 }
00080
00081
00082
00083
00084 string BU::trim(string str) {
00085
00086
00087 const string delims = " \t\r\n";
00088
00089 size_t ix = str.find_first_not_of(delims);
00090 if (ix == string::npos) return "";
00091
00092 str.erase(0, ix);
00093 ix = str.find_last_not_of(delims);
00094 str.erase(ix+1);
00095
00096 return str;
00097
00098 }
00099
00100
00101
00102
00103 void BU::split_file_name (string file_name_in, string& dir, string& base, bool& is_relative) {
00104
00105 split_file_name (file_name_in, dir, base);
00106
00107 if (dir == "") {
00108 is_relative = true;
00109 return;
00110 }
00111
00112 char dir0 = dir[0];
00113
00114 if (dir0 == '/' || dir0 == '\\' || dir0 == '~') {
00115 is_relative = false;
00116 } else if (dir0 == '.') {
00117 is_relative = true;
00118 } else if (isalpha(dir0) || isdigit(dir0)) {
00119 is_relative = true;
00120 } else {
00121 is_relative = false;
00122 }
00123
00124 return;
00125
00126 }
00127
00128
00129
00130
00131 void BU::split_file_name (string file_name_in, string& dir, string& base) {
00132
00133
00134
00135 int ix_dir = -1;
00136
00137 int i = file_name_in.rfind("]");
00138 if (i != string::npos && i > ix_dir) ix_dir = i;
00139
00140 i = file_name_in.rfind(":");
00141 if (i != string::npos && i > ix_dir) ix_dir = i;
00142
00143 i = file_name_in.rfind("/");
00144 if (i != string::npos && i > ix_dir) ix_dir = i;
00145
00146 i = file_name_in.rfind("\\");
00147 if (i != string::npos && i > ix_dir) ix_dir = i;
00148
00149 if (ix_dir == -1) {
00150 base = file_name_in;
00151 dir = "";
00152 return;
00153 }
00154
00155 dir = file_name_in.substr(0, ix_dir+1);
00156 base = file_name_in.substr(ix_dir+1);
00157
00158 }
00159
00160
00161
00162
00163 int BU::string_to_int (const string& str, bool& ok) {
00164 ok = false;
00165 if (str == "") return 0;
00166 istringstream iss(str);
00167 int value;
00168 ok = !(iss >> dec >> value).fail();
00169 return value;
00170 }
00171
00172
00173
00174
00175 int BU::string_to_int (const string& str) {
00176 if (str == "") throw;
00177 istringstream iss(str);
00178 int value;
00179 if ((iss >> dec >> value).fail()) throw;
00180 return value;
00181 }
00182
00183
00184
00185
00186 bool BU::is_int (const string& str) {
00187 if (str.length() == 0) return false;
00188 for(unsigned int i=0; i < str.length(); i++) {
00189 if (i == 0 && (str[i] == '-' || str[i] == '+')) {
00190 if (str.length() == 1) return false;
00191 continue;
00192 }
00193 if (!isdigit(str[i])) return false;
00194 }
00195 return true;
00196 }
00197
00198
00199
00200
00201 double BU::string_to_double (const string& str, bool& ok) {
00202 ok = false;
00203 if (str == "") return 0;
00204 istringstream iss(str);
00205 double value;
00206 ok = !(iss >> dec >> value).fail();
00207 return value;
00208 }
00209
00210
00211
00212
00213 double BU::string_to_double (const string& str) {
00214 if (str == "") throw;
00215 istringstream iss(str);
00216 double value;
00217 if ((iss >> dec >> value).fail()) throw;
00218 return value;
00219 }
00220
00221
00222
00223
00224 bool BU::is_double (const string& str) {
00225
00226 if (str.length() == 0) return false;
00227
00228 double d;
00229 istringstream i(str);
00230
00231 if (i >> d)
00232 return true;
00233 else
00234 return false;
00235
00236 }
00237
00238
00239
00240
00241 bool BU::string_to_bool (const string& str, bool& ok) {
00242 ok = false;
00243 if (str == "") return false;
00244 istringstream iss(str);
00245 bool value;
00246 ok = !(iss >> boolalpha >> value).fail();
00247 return value;
00248 }
00249
00250
00251
00252
00253 bool BU::string_to_bool (const string& str) {
00254 if (str == "") throw;
00255 istringstream iss(str);
00256 bool value;
00257 if ((iss >> boolalpha >> value).fail()) throw;
00258 return value;
00259 }
00260
00261
00262
00263
00264 string BU::bool_to_string (bool this_bool) {
00265 if (this_bool) return "TRUE";
00266 return "FALSE";
00267 }
00268
00269
00270
00271
00272 bool BU::is_bool (const string& str) {
00273
00274 if (str.length() == 0) return false;
00275
00276 bool b;
00277 istringstream i(str);
00278
00279 if (i >> b)
00280 return true;
00281 else
00282 return false;
00283
00284 }
00285
00286
00287
00288
00289 string BU::int_to_string (const int i, bool& ok) {
00290 ostringstream oss;
00291 string str;
00292 if (!(oss << i)) {
00293 ok = false;
00294 return "";
00295 }
00296 ok = true;
00297 return oss.str();
00298 }
00299
00300
00301
00302
00303 string BU::int_to_string (const int i) {
00304 ostringstream oss;
00305 string str;
00306 if (!(oss << i)) throw;
00307 return oss.str();
00308 }
00309
00310
00311
00312
00313 string BU::double_to_string (const double r, bool& ok) {
00314 ostringstream oss;
00315 if (!(oss << r)) {
00316 ok = false;
00317 return "";
00318 }
00319 ok = true;
00320 return oss.str();
00321 }
00322
00323
00324
00325
00326 string BU::double_to_string (const double r) {
00327 ostringstream oss;
00328 if (!(oss << r)) throw;
00329 return oss.str();
00330 }
00331
00332
00333
00334
00335 string BU::str_to_lower(const string& str_in) {
00336 string str_out = str_in;
00337 for(unsigned int i=0; i < str_in.length(); i++) {
00338 str_out[i] = tolower(str_in[i]);
00339 }
00340 return str_out;
00341 }
00342
00343
00344
00345
00346 string BU::add_parens (const string& sub_expression, const string& expression, int i0) {
00347
00348
00349
00350
00351
00352
00353 size_t ix_fwd = expression.substr(i0).find_first_of("-*+-/^)]");
00354 size_t ix_bck = expression.substr(0,i0).find_last_of("*+-/^([");
00355 char c_fwd = ' ', c_bck = ' ';
00356 if (ix_fwd != string::npos) c_fwd = expression[i0+ix_fwd];
00357 if (ix_bck != string::npos) c_bck = expression[ix_bck];
00358
00359 if (c_fwd == '^' || c_bck == '^' || c_bck == '/') {
00360 return add_parens(sub_expression, 2);
00361 }
00362
00363 if (c_fwd == '*' || c_fwd == '/' || c_bck == '-' || c_bck == '*') {
00364 return add_parens(sub_expression, 1);
00365 }
00366
00367 return sub_expression;
00368
00369 }
00370
00371
00372
00373
00374 string BU::add_parens (const string& sub_expression, int op_level) {
00375
00376 int np = 0;
00377
00378 for (int i = 0; i < sub_expression.size(); i++) {
00379 char ch = sub_expression[i];
00380 if (ch == '(') np++;
00381 if (ch == ')') np--;
00382 if (np != 0) continue;
00383
00384 if (i != sub_expression.size() - 1) {
00385 if (ch == '-' && sub_expression[i+1] == '>') continue;
00386 }
00387 if (ch == '+' || ch == '-') return "(" + sub_expression + ")";
00388 if (op_level > 1 && (ch == '/' || ch == '*')) return "(" + sub_expression + ")";
00389 }
00390
00391 return sub_expression;
00392
00393 }
00394
00395
00396
00397
00398 string BU::str_to_upper(const string& str_in) {
00399 string str_out = str_in;
00400 for(unsigned int i=0; i < str_in.length(); i++) {
00401 str_out[i] = toupper(str_in[i]);
00402 }
00403 return str_out;
00404 }
00405
00406
00407
00408
00409 string BU::to_xml_string (string str_in) {
00410
00411 int i = -1;
00412
00413 while ((i = str_in.find("&", i+1)) != string::npos)
00414 str_in.replace(i, 1, "&");
00415
00416 while ((i = str_in.find("<")) != string::npos)
00417 str_in.replace(i, 1, "<");
00418
00419 while ((i = str_in.find(">")) != string::npos)
00420 str_in.replace(i, 1, ">");
00421
00422 while ((i = str_in.find("\"")) != string::npos)
00423 str_in.replace(i, 1, ""e;");
00424
00425 return str_in;
00426
00427 }
00428
00429
00430
00431
00432 string BU::from_xml_string (string str_in) {
00433
00434 size_t i;
00435
00436 while ((i = str_in.find("&")) != string::npos)
00437 str_in.replace(i, 5, "&");
00438
00439 while ((i = str_in.find("<")) != string::npos)
00440 str_in.replace(i, 4, "<");
00441
00442 while ((i = str_in.find(">")) != string::npos)
00443 str_in.replace(i, 4, ">");
00444
00445 while ((i = str_in.find(""e;")) != string::npos)
00446 str_in.replace(i, 7, "\"");
00447
00448 while ((i = str_in.find("’")) != string::npos)
00449 str_in.replace(i, 7, "'");
00450
00451 return str_in;
00452
00453 }
00454
00455
00456
00457
00458 bool BU::found(StrList& s_list, const string& item) {
00459 return (find(s_list.begin(), s_list.end(), item) != s_list.end());
00460 }
00461
00462 bool BU::found(StrVec& s_vector, const string& item) {
00463 return (find(s_vector.begin(), s_vector.end(), item) != s_vector.end());
00464 }
00465
00466 bool BU::found(string& str, const string& item) {
00467 return (str.find(item) != string::npos);
00468 }
00469
00470 bool BU::found_value(StrMap& string_map, const string& item) {
00471 for (StrMapCIter im = string_map.begin(); im != string_map.end(); im++) {
00472 if (im->second == item) return true;
00473 }
00474 return false;
00475 }
00476
00477 StrListIter BU::find(StrList& s_list, const string& item) {
00478 return find(s_list.begin(), s_list.end(), item);
00479 }
00480
00481 StrListIter BU::rfind(StrList& s_list, const string& item) {
00482 StrListIter is_item = s_list.end();
00483 for (StrListIter is = s_list.begin(); is != s_list.end(); is++)
00484 if (*is == item) is_item = is;
00485 return is_item;
00486 }
00487
00488 StrList BU::list_copy(StrListIter begin, StrListIter end) {
00489 StrList str_list;
00490 for (StrListIter is = begin; is != end; is++)
00491 str_list.push_back(*is);
00492 return str_list;
00493 }
00494
00495
00496
00497
00498 string BU::str_pop (StrList& str_list) {
00499
00500 string front = str_list.front();
00501 str_list.pop_front();
00502 return front;
00503
00504 }
00505
00506
00507
00508
00509 bool BU::get_taylor_exponents (const string& exp_str, IntVec& exp_vec) {
00510
00511 if (exp_vec.size() != 6) exp_vec.resize(6);
00512
00513 bool ok;
00514 int i = 0;
00515
00516 for (size_t j = 0; j < exp_str.size(); j++) {
00517 if (exp_str.substr(j,1) == " ") continue;
00518 size_t j2 = exp_str.find(" ", j);
00519 if (j2 == string::npos) j2 = exp_str.size();
00520 if (i == 6) return false;
00521 exp_vec[i] = string_to_int(exp_str.substr(j, j2-j), ok);
00522 if (exp_vec[i++] < 0) return false;
00523 if (!ok) return false;
00524 j = j2;
00525 if (j == exp_str.size()) break;
00526 }
00527
00528 if (i < 6) return false;
00529 return true;
00530
00531 }
00532
00533
00534
00535 bool BU::splitXMLName (const string& _name, string& uri, string& prefix, string& loc_name) {
00536 bool prefix_found;
00537 return splitXMLName(_name, uri, prefix, loc_name, prefix_found);
00538 }
00539
00540
00541
00542 bool BU::splitXMLName (const string& _name, string& uri, string& prefix,
00543 string& loc_name, bool& prefix_found) {
00544
00545 uri = "";
00546 prefix = "";
00547 loc_name = _name;
00548 prefix_found = false;
00549
00550 if (_name.size() == 0) return true;
00551
00552
00553
00554 if (_name[0] == '{') {
00555 size_t ix = _name.find("}");
00556 if (ix == string::npos) return false;
00557 uri = _name.substr(1, ix-1);
00558 if (uri == "") {
00559 cout << "UAPNode::splitXMLName: Blank URIs not allowed: " << _name << endl;
00560 throw;
00561 }
00562 loc_name = _name.substr(ix+1);
00563 }
00564
00565
00566
00567 size_t ix = loc_name.find(":");
00568 if (ix != string::npos) {
00569 prefix = loc_name.substr(0, ix);
00570 loc_name = loc_name.substr(ix+1);
00571 prefix_found = true;
00572 } else {
00573 prefix_found = false;
00574 }
00575
00576 return true;
00577
00578 }
00579
00580
00581
00582
00583 void PrintInfoStruct::error (string line1, string line2, string line3) {
00584 cout << endl;
00585 cout << "ERROR: " << line1 << endl;
00586 print (line2, line3);
00587 everything_ok = false;
00588 return;
00589 }
00590
00591 void PrintInfoStruct::warning (string line1, string line2, string line3) {
00592 cout << endl;
00593 cout << "Warning: " << line1 << endl;
00594 print (line2, line3);
00595 return;
00596 }
00597
00598 void PrintInfoStruct::print (string& line2, string& line3) {
00599
00600
00601
00602
00603 string space_str = string(5, ' ');
00604
00605 if (line2 != "") {
00606 line2 += space_str;
00607 while (true) {
00608 size_t ix = line2.find("\n");
00609 if (ix == string::npos) {
00610 cout << space_str << line2 << endl;
00611 break;
00612 } else {
00613 cout << space_str << line2.substr(0,ix+1);
00614 line2.erase(0,ix+1);
00615 }
00616 }
00617 }
00618
00619 if (line3 != "") {
00620 line3 += space_str;
00621 while (true) {
00622 size_t ix = line3.find("\n");
00623 if (ix == string::npos) {
00624 cout << space_str << line3 << endl;
00625 break;
00626 } else {
00627 cout << space_str << line3.substr(0,ix+1);
00628 line3.erase(0,ix+1);
00629 }
00630 }
00631 }
00632
00633 cout << space_str << "While: " << parsing_status << endl;
00634 if (file_name != "") cout << " IN FILE: " << file_name << endl;
00635 if (ix_line > 0) cout << " AT OR BEFORE LINE: " << ix_line << endl;
00636 if (statement != "") {
00637 cout << " IN STATEMENT: " << BU::trim(statement) << endl;
00638 }
00639 everything_ok = false;
00640 return;
00641
00642 }
00643
00644
00645
00646
00647 bool FileStackElement::splitName (const string& file_name_in) {
00648
00649 this_name = file_name_in;
00650 bool is_relative;
00651 BU::split_file_name (file_name_in, this_dir, this_file_no_dir, is_relative);
00652 return is_relative;
00653
00654 }
00655
00656
00657
00658
00659 void FileStackList::addFile (const string& file_name) {
00660
00661 FileStackElement this_file;
00662 bool relative_path = this_file.splitName(file_name);
00663
00664 if (!relative_path || file_list.size() == 0) {
00665 this_file.full_dir = this_file.this_dir;
00666 this_file.full_name = this_file.this_name;
00667 } else {
00668 FileStackElement prev_file = file_list.back();
00669 this_file.full_dir = prev_file.full_dir + this_file.this_dir;
00670 this_file.full_name = prev_file.full_dir + this_file.this_name;
00671 }
00672
00673 file_list.push_back(this_file);
00674 return;
00675
00676 }
00677
00678
00679
00680
00681 StreamStruct::StreamStruct(string file_name) {
00682 ix_line = 0;
00683 continuation_char = "";
00684 n_indent = 8;
00685 max_len = 98;
00686 if (file_name == "") out_file = NULL;
00687 else out_file = new ofstream(file_name.c_str());
00688 }
00689
00690 void StreamStruct::close_file() {
00691 if (!out_file) return;
00692 if (out_file->is_open()) {
00693 out_file->close();
00694 delete out_file;
00695 }
00696 }
00697
00698 void StreamStruct::open_file(const string& file_name) {
00699 out_file = new ofstream(file_name.c_str());
00700 }
00701
00702 bool StreamStruct::is_open() {
00703 if (out_file) return out_file->is_open();
00704 return false;
00705 }
00706
00707 StreamStruct& StreamStruct::operator<< (const string& str) {
00708
00709 if (str == "") return *this;
00710
00711 int len = ix_line + str.length();
00712 if (len < max_len) {
00713 *out_file << str;
00714 ix_line = len;
00715 } else {
00716 if (ix_line != 0) {
00717 *out_file << " " << continuation_char << endl;
00718 *out_file << string(n_indent, ' ');
00719 ix_line = n_indent;
00720 }
00721 *out_file << str;
00722 ix_line += str.length();
00723 }
00724 return *this;
00725 }
00726
00727 StreamStruct& StreamStruct::operator<< (const int i) {
00728 bool ok;
00729 return (*this << BU::int_to_string(i, ok));
00730 }
00731
00732 StreamStruct& StreamStruct::operator<< (EndStruct& s) {
00733 *out_file << endl;
00734 ix_line = 0;
00735 return *this;
00736 }
00737
00738 StreamStruct& StreamStruct::operator= (StreamStruct& s) {
00739 out_file = s.out_file;
00740 continuation_char = s.continuation_char;
00741 n_indent = s.n_indent;
00742 return *this;
00743 }
00744