2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
10 # This software is governed by the CeCILL-B license under French law and
11 # abiding by the rules of distribution of free software. You can use,
12 # modify and/ or redistribute the software under the terms of the CeCILL-B
13 # license as circulated by CEA, CNRS and INRIA at the following URL
14 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15 # or in the file LICENSE.txt.
17 # As a counterpart to the access to the source code and rights to copy,
18 # modify and redistribute granted by the license, users are provided only
19 # with a limited warranty and the software's author, the holder of the
20 # economic rights, and the successive licensors have only limited
23 # The fact that you are presently reading this means that you have had
24 # knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------ */
29 #include "bbtkSimpleUtilities.h"
30 #define LOG_FILE_NAME "log.txt"
36 //template <typename T>
45 std::string kind(int*);
46 std::string is(int*, bool withkind=false);
47 std::string kind(double* a);
48 std::string is(double* a, bool withkind=false);
49 std::string kind(float* a);
50 std::string is(float* a, bool withkind=false);
51 std::string kind(char* a);
52 std::string is(char* a, bool withkind=false);
53 std::string kind(long* a);
54 std::string is(long* a, bool withkind=false);
55 std::string kind(short* a);
56 std::string is(short* a, bool withkind=false);
57 std::string kind(bool* a);
58 std::string is(bool* a, bool withkind=false);
59 std::string kind(void* a);
60 std::string is(void* a, bool withkind=false);
62 std::string kind(unsigned int* a);
63 std::string is(unsigned int* a, bool withkind=false);
64 std::string kind(unsigned char* a);
65 std::string is(unsigned char* a, bool withkind=false);
66 std::string kind(unsigned long* a);
67 std::string is(unsigned long* a, bool withkind=false);
68 std::string kind(unsigned short* a);
69 std::string is(unsigned short* a, bool withkind=false);
70 std::string kind(__int64* a);
71 std::string is(__int64* a, bool withkind=false);
72 std::string kind(unsigned __int64* a);
73 std::string is(unsigned __int64* a, bool withkind=false);
74 std::string kind(long double* a);
75 std::string is(long double* a, bool withkind=false);
77 std::string kind(int a);
78 std::string is(int a, bool withkind=false);
79 std::string kind(double a);
80 std::string is(double a, bool withkind=false);
81 std::string kind(float a);
82 std::string is(float a, bool withkind=false);
83 std::string kind(short a);
84 std::string is(short a, bool withkind=false);
85 std::string kind(char a);
86 std::string is(char a, bool withkind=false);
87 std::string kind(long a);
88 std::string is(long a, bool withkind=false);
89 std::string kind(bool a);
90 std::string is(bool a, bool withkind=false);
91 std::string kind(unsigned int a);
92 std::string is(unsigned int a, bool withkind=false);
93 std::string kind(unsigned char a);
94 std::string is(unsigned char a, bool withkind=false);
95 std::string kind(unsigned long a);
96 std::string is(unsigned long a, bool withkind=false);
97 std::string kind(unsigned short a);
98 std::string is(unsigned short a, bool withkind=false);
99 std::string kind(__int64 a);
100 std::string is(__int64 a, bool withkind=false);
101 std::string kind(unsigned __int64 a);
102 std::string is(unsigned __int64 a, bool withkind=false);
103 std::string kind(long double a);
104 std::string is(long double a, bool withkind=false);
106 std::string kind(std::vector<int> a);
107 std::string is(std::vector<int> a, bool withkind=false);
108 std::string kind(std::vector<double> a);
109 std::string is(std::vector<double> a, bool withkind=false);
110 std::string kind(std::vector<float> a);
111 std::string is(std::vector<float> a, bool withkind=false);
112 std::string kind(std::vector<short> a);
113 std::string is(std::vector<short> a, bool withkind=false);
114 std::string kind(std::vector<char> a);
115 std::string is(std::vector<char> a, bool withkind=false);
116 std::string kind(std::vector<long> a);
117 std::string is(std::vector<long> a, bool withkind=false);
118 std::string kind(std::vector<bool> a);
119 std::string is(std::vector<bool> a, bool withkind=false);
120 std::string kind(std::vector<__int64> a);
121 std::string is(std::vector<__int64> a, bool withkind=false);
122 std::string kind(std::vector<unsigned int> a);
123 std::string is(std::vector<unsigned int> a, bool withkind=false);
124 std::string kind(std::vector<unsigned char> a);
125 std::string is(std::vector<unsigned char> a, bool withkind=false);
126 std::string kind(std::vector<unsigned long> a);
127 std::string is(std::vector<unsigned long> a, bool withkind=false);
128 std::string kind(std::vector<unsigned short> a);
129 std::string is(std::vector<unsigned short> a, bool withkind=false);
130 std::string kind(std::vector<long double> a);
131 std::string is(std::vector<long double> a, bool withkind=false);
133 std::string is(double* a,int len=1, bool withkind=false);
134 std::string is(int* a,int len=1, bool withkind=false);
135 std::string is(float* a,int len=1, bool withkind=false);
136 std::string is(char* a,int len=1, bool withkind=false);
143 bool persistence::writeFile(std::string stm, std::string content)
145 std::ofstream myfile (stm.c_str());
146 if (myfile.is_open())
158 std::string persistence::readFile(std::string path)
161 std::stringstream stm;
162 std::ifstream myfile (path.c_str());
163 if (myfile.is_open())
165 while (! myfile.eof() )
167 getline (myfile,line);
168 stm << line << std::endl;
182 std::string logging::fecha()
185 seconds = time (NULL);
188 std::stringstream out;
190 std::string la_fecha = out.str();
191 //_strtime( timeStr );
192 //std::string la_fecha(timeStr);
193 return "["+la_fecha+"] ";
196 void logging::out(std::string texto)
198 std::ofstream outFile;
199 outFile.open(LOG_FILE_NAME, std::ios::app);
202 outFile << fecha() << texto << std::endl;
206 void logging::erase()
208 std::ofstream outFile;
209 outFile.open(LOG_FILE_NAME, std::ios::out);
212 outFile << fecha() << "INICIO" << std::endl;
217 std::vector<std::string> translate::StringSplit(std::string str, std::string delim)
219 std::vector<std::string> results;
221 while( (cutAt = (int)str.find_first_of(delim)) != str.npos )
225 results.push_back(str.substr(0,cutAt));
227 str = str.substr(cutAt+1);
231 results.push_back(str);
236 std::vector<double> translate::stringTovector(std::string texto, std::string start, std::string end)
238 std::vector<double> el_vector;
239 std::vector<std::string> partes;
242 int inicial = (int)texto.find( start, 0 );
243 int final = (int)texto.find( end, 0 );
245 if (inicial == std::string::npos || final == std::string::npos)
250 std::string contenido = texto.substr(inicial+1, final-inicial-1);
252 partes = StringSplit(contenido, ",");
253 if (partes.size() != -1){
254 for (int i=0; i<(int)partes.size(); i++)
256 if (partes.at(i).size() > 0)
258 double uno = atof(partes.at(i).data());
259 el_vector.push_back(uno);
266 std::vector<double> translate::stringTovectorDelimited(std::string texto, std::string delimitador)
268 std::vector<double> el_vector;
269 std::vector<std::string> partes;
271 std::string contenido = texto;
273 partes = StringSplit(contenido, delimitador);
274 if (partes.size() != -1){
275 for (int i=0; i<(int)partes.size(); i++)
277 double uno = atof(partes.at(i).data());
278 el_vector.push_back(uno);
284 double* translate::stringToArray(std::string texto)
286 std::vector<double> el_vector = stringTovector(texto);
287 double* array = new double[el_vector.size()];
288 for (int i=0; i<(int)el_vector.size(); i++)
290 array[i]=el_vector[i];
295 std::string translate::getInnerInfo(std::string texto)
297 int first_par = (int)texto.find("[", 0);
298 int last_par = (int)texto.rfind("]", texto.size());
300 return texto.substr(first_par+1,last_par-first_par-1);
303 std::vector<double*> translate::stringToVectorArray(std::string texto)
305 std::vector<double*> el_vector;
306 std::string innerInfo = getInnerInfo(texto);
312 while (lastComma != std::string::npos)
314 first_par = (int)innerInfo.find("[", lastComma);
315 last_par = (int)innerInfo.find("]", lastComma);
316 lastComma = (int)innerInfo.find(",", last_par);
317 if (first_par == std::string::npos || last_par == std::string::npos)
319 std::string subArreglo = innerInfo.substr(first_par,last_par-first_par+1);
320 double* subArray = stringToArray(subArreglo);
321 el_vector.push_back(subArray);
326 std::string translate::vectorToStringDelimited(std::vector<double> a, std::string delim)
328 std::stringstream out;
331 for (i = 0; i < a.size(); i++)
342 //Recorre el vector de manera ciclica
343 double vectores::vectorInfinite(std::vector<double> in, int index)
345 //std::cout << "index = " << index;
347 int tamanio = in.size();
348 bool inverso = false;
356 value = tamanio - index%tamanio;
357 if (value == tamanio)
360 value = index%tamanio;
362 //std::cout << ", value=" << value << std::endl;
370 Ordenamiento busbuja, array[0] es el maximo
371 http://mathbits.com/mathbits/compsci/Arrays/Bubble.htm
373 void sorts::bubble(std::vector<double> &array)
375 double i, j, flag = 1; // set flag to 1 to begin initial pass
376 double temp; // holding variable
377 double arrayLength = array.size();
378 for(i = 1; (i <= arrayLength) && flag; i++)
381 for (j=0; j < (arrayLength -1); j++)
383 if (array[(int)(j+1)] > array[(int)j]) // ascending order simply changes to <
385 temp = array[(int)j]; // swap elements
386 array[(int)j] = array[(int)(j+1)];
387 array[(int)(j+1)] = temp;
388 flag = 1; // indicates that a swap occurred.