xref: /phasta/phSolver/common/ValType.cc (revision 1e99f302ca5103688ae35115c2fefb7cfa6714f1)
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* 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 
94 vector<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 
108 string ValType::get_string(string str)
109 {
110   return str;
111 }
112