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 operator int()11ValType::operator int() 12 { 13 used = true; 14 return get_int(str); 15 } 16 operator vector<double>()17ValType::operator vector<double>() 18 { 19 used = true; 20 return get_vector(str); 21 } 22 operator vector<int>()23ValType::operator vector<int>() 24 { 25 used = true; 26 return get_ivector(str); 27 } 28 operator double()29ValType::operator double() 30 { 31 used = true; 32 return get_double(str); 33 } 34 operator double*()35ValType::operator double*() 36 { 37 used = true; 38 return get_double_array(str); 39 } 40 operator string()41ValType::operator string() 42 { 43 used = true; 44 return get_string(str); 45 } 46 47 // 48 // function implementations for type specific conversions 49 // 50 get_int(string str)51int ValType::get_int(string str) 52 { 53 int i = atoi(str.c_str()); 54 return i; 55 } 56 get_double(string str)57double ValType::get_double(string str) 58 { 59 double x = atof(str.c_str()); 60 return x; 61 } 62 get_double_array(string str)63double *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 get_vector(string str)80vector<double> ValType::get_vector(string str) 81 { 82 char* s = (char*) malloc(str.size()+1); 83 strcpy(s,str.c_str()); 84 char *strTok = strtok(s," "); 85 vector<double> vec; 86 while(strTok) { 87 vec.push_back(atof(strTok)); 88 strTok = strtok(NULL," "); 89 } 90 free(s); 91 return vec; 92 } 93 get_ivector(string str)94vector<int> ValType::get_ivector(string str) 95 { 96 char* s = (char*) malloc(str.size()+1); 97 strcpy(s,str.c_str()); 98 char *strTok = strtok(s," "); 99 vector<int> vec; 100 while(strTok) { 101 vec.push_back(atoi(strTok)); 102 strTok = strtok(NULL," "); 103 } 104 free(s); 105 return vec; 106 } 107 get_string(string str)108string ValType::get_string(string str) 109 { 110 return str; 111 } 112