1 #include <string> 2 #include <vector> 3 //MR CHANGE 4 #include <cstring> 5 #include <cmath> 6 #include <cstdlib> 7 //MR CHANGE END 8 9 #include "ValType.h" 10 11 ValType::operator int() 12 { 13 used = true; 14 return get_int(str); 15 } 16 17 ValType::operator vector<double>() 18 { 19 used = true; 20 return get_vector(str); 21 } 22 23 ValType::operator vector<int>() 24 { 25 used = true; 26 return get_ivector(str); 27 } 28 29 ValType::operator double() 30 { 31 used = true; 32 return get_double(str); 33 } 34 35 ValType::operator double*() 36 { 37 used = true; 38 return get_double_array(str); 39 } 40 41 ValType::operator string() 42 { 43 used = true; 44 return get_string(str); 45 } 46 47 // 48 // function implementations for type specific conversions 49 // 50 51 int ValType::get_int(string str) 52 { 53 int i = atoi(str.c_str()); 54 return i; 55 } 56 57 double ValType::get_double(string str) 58 { 59 double x = atof(str.c_str()); 60 return x; 61 } 62 63 double *ValType::get_double_array(string str) 64 { 65 //istrstream ist(str.c_str(),str.length()); 66 //vector<double> vec; 67 //double v; 68 //while ( ist >> v ) { 69 // vec.push_back(v); 70 //} 71 vector<double> vec = get_vector(str); 72 int n = vec.size(); 73 double *x = new double[n]; 74 for (int i=0; i < n; i++) { 75 x[i] = vec[i]; 76 } 77 return x; 78 } 79 80 vector<double> ValType::get_vector(string str) 81 { 82 char *strTok = strtok((char *)str.c_str()," "); 83 vector<double> vec; 84 while(strTok) { 85 vec.push_back(atof(strTok)); 86 strTok = strtok(NULL," "); 87 } 88 89 //istrstream ist(str.c_str(),str.length()); 90 //vector<double> vec; 91 //double v; 92 //while ( ist >> v ) { 93 // vec.push_back(v); 94 //} 95 return vec; 96 } 97 98 vector<int> ValType::get_ivector(string str) 99 { 100 char *strTok = strtok((char *)str.c_str()," "); 101 vector<int> vec; 102 while(strTok) { 103 vec.push_back(atoi(strTok)); 104 strTok = strtok(NULL," "); 105 } 106 107 //istrstream ist(str.c_str(),str.length()); 108 //vector<int> vec; 109 //double v; 110 //while ( ist >> v ) { 111 // vec.push_back(v); 112 //} 113 return vec; 114 } 115 116 string ValType::get_string(string str) 117 { 118 return str; 119 } 120