]> Creatis software - bbtk.git/blob - packages/vtk/src/names.cxx
(some of the) unused variables
[bbtk.git] / packages / vtk / src / names.cxx
1 #include "names.h"
2
3 bool checknum(std::string s)
4 {
5         std::istringstream iss(s);
6         std::ostringstream oss;
7         int x;
8         iss >> x;
9         oss << x;       
10         
11         if( s == oss.str())
12                 return true;
13         
14         return false;
15 }
16
17
18
19 std::string StringClear(std::string in, std::vector<std::string> no)
20 {
21   size_t found;
22   
23 for (int i=0; i<no.size(); i++)
24 {
25   std::string viejo = no.at(i);
26   found=in.find(viejo);
27   while (found!=std::string::npos)
28   {
29     //Mienstras halla
30     int inicio = int(found);
31     in.replace(found,viejo.length(),"");
32     found=in.find(viejo);
33   }
34 }
35   return in;
36 }
37
38 std::string StringReplace(std::string in, std::string viejo, std::string nuevo)
39 {
40   size_t found;
41
42   // different member versions of find in the same order as above:
43   found=in.find(viejo);
44   while (found!=std::string::npos)
45   {
46     //Mienstras halla
47     //int inicio = int(found); // JPR
48     in.replace(found,viejo.length(),nuevo);
49     found=in.find(viejo);
50   }
51 return in;
52 }
53
54 std::vector < std::string > StringSplit(std::string str, std::string delim)
55 {
56         std::vector<std::string> results;
57         int cutAt;
58         while( (cutAt = (int)str.find_first_of(delim)) != str.npos )
59         {
60                 if(cutAt > 0)
61                 {
62                         results.push_back(str.substr(0,cutAt));
63                 }
64                         str = str.substr(cutAt+1);
65                 }
66                 if(str.length() > 0)
67                 {
68                         results.push_back(str);
69                 }
70         return results;
71 }
72
73 std::string generateFileName(std::string input, int number, std::string prefix)
74 {
75         //
76         int ceros = 6;
77         int position = input.find( "\\" ); // find first space 
78         while ( position != std::string::npos ) 
79         {
80                 input.replace( position, 1, "/" );
81                 position = input.find( "\\", position + 1 );
82         }
83         
84     bool comienza_con_slash = false;
85    
86     if (input.at(0) == '/')
87                 comienza_con_slash = true;
88         //
89         std::vector< std::string > partes = StringSplit(input, "/");
90
91         std::stringstream number_str;
92         number_str << number;
93     std::string number_str_texto = number_str.str();
94     
95     
96     int tamanio = number_str_texto.size();
97     int diferencia = ceros-tamanio;
98     for (int i=0; i<diferencia ; i++)
99         number_str_texto="0"+number_str_texto;
100
101         std::string rta = partes[0];
102
103         for (unsigned int i=1; i<partes.size(); i++)
104         {
105                 if (i==partes.size()-1)
106                 {
107                         std::vector<std::string> partes_punto;
108                         partes_punto = StringSplit(partes[i], ".");
109                         /*
110                         La extension es: partes_punto[partes_punto.size()-1]
111                         */
112                         std::string oldname = "";
113                         for (unsigned int h=0; h<partes_punto.size();h++)
114                         {
115                                 oldname += partes_punto.at(h)+"_";
116                         }
117                         if (partes_punto.size() > 1)
118                                 rta+="/"+prefix+"_"+oldname+"_"+number_str_texto+"."+partes_punto[partes_punto.size()-1];
119                         else
120                                 rta+="/"+prefix+"_"+oldname+"_"+number_str_texto;
121                 }
122                 else
123                 {
124                         rta+="/"+partes[i];
125                 }
126         }
127         if (comienza_con_slash)
128                 rta = "/"+rta;
129         return rta;
130 }
131
132 bool existFile(std::string myFileName)
133 {
134         std::ifstream inp;
135 inp.open(myFileName.c_str(), std::ifstream::in);
136 inp.close();
137 if (!inp.fail())
138 {
139    return true;
140 }else{
141    return false;
142 }    
143 }
144
145 std::string guessName(std::string nombre)
146 {
147    int iterador = 0;
148    int max_iter = 10000;
149    
150    while(iterador < max_iter)
151    {
152    std::string nuevo = generateFileName(nombre, iterador, "Data");
153    
154    if (existFile(nuevo))
155    {
156       iterador++;
157    }
158    else
159    {
160            std::cout << nuevo << std::endl;
161        return nuevo;
162    }
163    }
164    std::cout << nombre << std::endl;
165    return nombre;
166 }
167
168 std::string guessExistent(std::string nombre, int* actual, bool* hay)
169 {
170         std::string nada = "";
171         if (actual == NULL || hay == NULL)
172                 return nada;
173    int iterador = *actual;
174    int max_iter = 1000;
175    
176    while(iterador < max_iter)
177    {
178    std::string nuevo = generateFileName(nombre, iterador, "Data");
179    
180    if (existFile(nuevo))
181    {
182            *hay = true;
183            *actual = iterador;
184       return nuevo;
185    }
186    else
187    {
188            iterador++;
189    }
190    }
191    *hay = false;
192    return nada;
193 }