]> Creatis software - bbtk.git/blob - packages/vtk/src/bbtkSimpleUtilities.cxx
549a56ab4867734bdb2fed0edc8f53d5a213344e
[bbtk.git] / packages / vtk / src / bbtkSimpleUtilities.cxx
1
2 #include "bbtkSimpleUtilities.h"
3 #define LOG_FILE_NAME "log.txt"
4
5 namespace bbtk
6 {
7
8
9 //template <typename T>
10
11
12
13
14
15 /*
16 namespace what
17 {
18            std::string kind(int*);
19            std::string is(int*, bool withkind=false);
20            std::string kind(double* a);
21            std::string is(double* a, bool withkind=false);
22            std::string kind(float* a);
23            std::string is(float* a, bool withkind=false);
24            std::string kind(char* a);
25            std::string is(char* a, bool withkind=false);
26            std::string kind(long* a);
27            std::string is(long* a, bool withkind=false);
28            std::string kind(short* a);
29            std::string is(short* a, bool withkind=false);
30            std::string kind(bool* a);
31            std::string is(bool* a, bool withkind=false);
32            std::string kind(void* a);
33            std::string is(void* a, bool withkind=false);
34
35            std::string kind(unsigned int* a);
36            std::string is(unsigned int* a, bool withkind=false);
37            std::string kind(unsigned char* a);
38            std::string is(unsigned char* a, bool withkind=false);
39            std::string kind(unsigned long* a);
40            std::string is(unsigned long* a, bool withkind=false);
41            std::string kind(unsigned short* a);
42            std::string is(unsigned short* a, bool withkind=false);
43            std::string kind(__int64* a);
44            std::string is(__int64* a, bool withkind=false);
45            std::string kind(unsigned __int64* a);
46            std::string is(unsigned __int64* a, bool withkind=false);
47            std::string kind(long double* a);
48            std::string is(long double* a, bool withkind=false);
49
50            std::string kind(int a);
51            std::string is(int a, bool withkind=false);
52            std::string kind(double a);
53            std::string is(double a, bool withkind=false);
54            std::string kind(float a);
55            std::string is(float a, bool withkind=false);
56            std::string kind(short a);
57            std::string is(short a, bool withkind=false);
58            std::string kind(char a);
59            std::string is(char a, bool withkind=false);
60            std::string kind(long a);
61            std::string is(long a, bool withkind=false);
62            std::string kind(bool a);
63            std::string is(bool a, bool withkind=false);
64            std::string kind(unsigned int a);
65            std::string is(unsigned int a, bool withkind=false);
66            std::string kind(unsigned char a);
67            std::string is(unsigned char a, bool withkind=false);
68            std::string kind(unsigned long a);
69            std::string is(unsigned long a, bool withkind=false);
70            std::string kind(unsigned short a);
71            std::string is(unsigned short a, bool withkind=false);
72            std::string kind(__int64 a);
73            std::string is(__int64 a, bool withkind=false);
74            std::string kind(unsigned __int64 a);
75            std::string is(unsigned __int64 a, bool withkind=false);
76            std::string kind(long double a);
77            std::string is(long double a, bool withkind=false);
78
79            std::string kind(std::vector<int> a);
80            std::string is(std::vector<int> a, bool withkind=false);
81            std::string kind(std::vector<double> a);
82            std::string is(std::vector<double> a, bool withkind=false);
83            std::string kind(std::vector<float> a);
84            std::string is(std::vector<float> a, bool withkind=false);
85            std::string kind(std::vector<short> a);
86            std::string is(std::vector<short> a, bool withkind=false);
87            std::string kind(std::vector<char> a);
88            std::string is(std::vector<char> a, bool withkind=false);
89            std::string kind(std::vector<long> a);
90            std::string is(std::vector<long> a, bool withkind=false);
91            std::string kind(std::vector<bool> a);
92            std::string is(std::vector<bool> a, bool withkind=false);
93            std::string kind(std::vector<__int64> a);
94            std::string is(std::vector<__int64> a, bool withkind=false);
95            std::string kind(std::vector<unsigned int> a);
96            std::string is(std::vector<unsigned int> a, bool withkind=false);
97            std::string kind(std::vector<unsigned char> a);
98            std::string is(std::vector<unsigned char> a, bool withkind=false);
99            std::string kind(std::vector<unsigned long> a);
100            std::string is(std::vector<unsigned long> a, bool withkind=false);
101            std::string kind(std::vector<unsigned short> a);
102            std::string is(std::vector<unsigned short> a, bool withkind=false);
103            std::string kind(std::vector<long double> a);
104            std::string is(std::vector<long double> a, bool withkind=false);
105
106                    std::string is(double* a,int len=1, bool withkind=false);
107                    std::string is(int* a,int len=1, bool withkind=false);
108                    std::string is(float* a,int len=1, bool withkind=false);
109                    std::string is(char* a,int len=1, bool withkind=false);
110
111 }
112 */
113
114
115
116         bool persistence::writeFile(std::string stm, std::string content)
117         {
118                         std::ofstream myfile (stm.c_str());
119                         if (myfile.is_open())
120                         {
121                                 myfile << content;
122                                 myfile.close();
123                                 return true;
124                         }
125                         else
126                         {
127                                 return false;
128                         }
129         }
130
131         std::string persistence::readFile(std::string path)
132         {
133                 std::string line;
134                 std::stringstream stm;
135                 std::ifstream myfile (path.c_str());
136                 if (myfile.is_open())
137                 {
138                         while (! myfile.eof() )
139                         {
140                                 getline (myfile,line);
141                                 stm << line << std::endl;
142                         }
143                         myfile.close();
144                 }
145                 else
146                 {
147
148                 }
149                 return stm.str();
150         }
151
152
153
154
155         std::string logging::fecha()
156         {
157                 time_t seconds;
158                 seconds = time (NULL);
159
160                 //char timeStr [9];
161                 std::stringstream out;
162                 out << seconds;
163                 std::string la_fecha = out.str();
164                 //_strtime( timeStr );
165                 //std::string la_fecha(timeStr);
166                 return "["+la_fecha+"] ";
167         }
168
169         void logging::out(std::string texto)
170         {
171                 std::ofstream outFile;
172                 outFile.open(LOG_FILE_NAME, std::ios::app);
173                 if (outFile)
174                 {
175                         outFile << fecha() << texto << std::endl;
176                 }
177         }
178
179         void logging::erase()
180         {
181                 std::ofstream outFile;
182                 outFile.open(LOG_FILE_NAME, std::ios::out);
183                 if (outFile)
184                 {
185                         outFile << fecha() << "INICIO" << std::endl;
186                 }
187         }
188
189
190         std::vector<std::string> translate::StringSplit(std::string str, std::string delim)
191         {
192                 std::vector<std::string> results;
193                 int cutAt;
194                 while( (cutAt = (int)str.find_first_of(delim)) != str.npos )
195                 {
196                         if(cutAt > 0)
197                         {
198                                 results.push_back(str.substr(0,cutAt));
199                         }
200                                 str = str.substr(cutAt+1);
201                         }
202                         if(str.length() > 0)
203                         {
204                                 results.push_back(str);
205                         }
206                 return results;
207         }
208
209         std::vector<double> translate::stringTovector(std::string texto, std::string start, std::string end)
210         {
211                 std::vector<double> el_vector;
212                 std::vector<std::string> partes;
213
214
215                 int inicial = (int)texto.find( start, 0 );
216                 int final = (int)texto.find( end, 0 );
217
218                 if (inicial == std::string::npos || final == std::string::npos)
219                         return el_vector;
220                 if (final<=inicial)
221                         return el_vector;
222
223                 std::string contenido = texto.substr(inicial+1, final-inicial-1);
224
225                 partes = StringSplit(contenido, ",");
226                 if (partes.size() != -1){
227                         for (int i=0; i<(int)partes.size(); i++)
228                         {
229                                 if (partes.at(i).size() > 0)
230                                 {
231                                         double uno = atof(partes.at(i).data());
232                                         el_vector.push_back(uno);
233                                 }
234                         }
235                 }
236                 return el_vector;
237         }
238
239         std::vector<double> translate::stringTovectorDelimited(std::string texto, std::string delimitador)
240         {
241                 std::vector<double> el_vector;
242                 std::vector<std::string> partes;
243
244                 std::string contenido = texto;
245
246                 partes = StringSplit(contenido, delimitador);
247                 if (partes.size() != -1){
248                         for (int i=0; i<(int)partes.size(); i++)
249                         {
250                                 double uno = atof(partes.at(i).data());
251                                 el_vector.push_back(uno);
252                         }
253                 }
254                 return el_vector;
255         }
256
257     double* translate::stringToArray(std::string texto)
258         {
259         std::vector<double> el_vector = stringTovector(texto);
260         double* array = new double[el_vector.size()];
261         for (int i=0; i<(int)el_vector.size(); i++)
262         {
263             array[i]=el_vector[i];
264         }
265                 return array;
266         }
267
268         std::string translate::getInnerInfo(std::string texto)
269         {
270                 int first_par = (int)texto.find("[", 0);
271                 int last_par = (int)texto.rfind("]", texto.size());
272
273                 return texto.substr(first_par+1,last_par-first_par-1);
274         }
275
276         std::vector<double*> translate::stringToVectorArray(std::string texto)
277         {
278                 std::vector<double*> el_vector;
279                 std::string innerInfo = getInnerInfo(texto);
280
281                 int first_par = 0;
282                 int last_par = 0;
283                 int lastComma = 0;
284
285                 while (lastComma != std::string::npos)
286                 {
287                         first_par = (int)innerInfo.find("[", lastComma);
288                         last_par = (int)innerInfo.find("]", lastComma);
289                         lastComma = (int)innerInfo.find(",", last_par);
290                         if (first_par == std::string::npos || last_par == std::string::npos)
291                                 break;
292                         std::string subArreglo = innerInfo.substr(first_par,last_par-first_par+1);
293                         double* subArray = stringToArray(subArreglo);
294                         el_vector.push_back(subArray);
295                 }
296                 return el_vector;
297         }
298
299         std::string translate::vectorToStringDelimited(std::vector<double> a, std::string delim)
300         {
301                 std::stringstream out;
302
303         unsigned int i;
304         for (i = 0; i < a.size(); i++)
305         {
306             out << a.at(i);
307             if (i != a.size()-1)
308                 out << delim;
309         }
310         return out.str();
311         }
312
313
314
315 //Recorre el vector de manera ciclica
316 double vectores::vectorInfinite(std::vector<double> in, int index)
317 {
318     //std::cout << "index = " << index;
319     int value;
320         int tamanio = in.size();
321         bool inverso = false;
322         if (tamanio == 0)
323                 return -1;
324
325         if (index < 0)
326         {
327                 inverso = true;
328                 index = abs(index);
329                 value = tamanio - index%tamanio;
330                 if (value == tamanio)
331                    value = 0;
332         }else{
333                 value = index%tamanio;
334         }
335         //std::cout << ", value=" << value << std::endl;
336         //return 0;
337     return in.at(value);
338 }
339
340
341
342         /**
343         Ordenamiento busbuja, array[0] es el maximo
344         http://mathbits.com/mathbits/compsci/Arrays/Bubble.htm
345         **/
346         void sorts::bubble(std::vector<double> &array)
347         {
348                 double i, j, flag = 1;    // set flag to 1 to begin initial pass
349                 double temp;             // holding variable
350                 double arrayLength = array.size();
351                 for(i = 1; (i <= arrayLength) && flag; i++)
352                 {
353                         flag = 0;
354                         for (j=0; j < (arrayLength -1); j++)
355                         {
356                                 if (array[(int)(j+1)] > array[(int)j])      // ascending order simply changes to <
357                                 {
358                                                 temp = array[(int)j];             // swap elements
359                                                 array[(int)j] = array[(int)(j+1)];
360                                                 array[(int)(j+1)] = temp;
361                                                 flag = 1;               // indicates that a swap occurred.
362                                 }
363                         }
364                 }
365         }
366
367 };
368
369