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