1 /*=========================================================================
4 Module: $RCSfile: gdcmUtil.cxx,v $
6 Date: $Date: 2004/10/08 04:43:38 $
7 Version: $Revision: 1.51 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See Doc/License.txt or
11 http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
20 #include "gdcmDebug.h"
24 * \brief Because not available in C++ (?)
26 void Tokenize (const std::string& str,
27 std::vector<std::string>& tokens,
28 const std::string& delimiters)
30 std::string::size_type lastPos = str.find_first_not_of(delimiters,0);
31 std::string::size_type pos = str.find_first_of (delimiters,lastPos);
32 while (std::string::npos != pos || std::string::npos != lastPos)
34 tokens.push_back(str.substr(lastPos, pos - lastPos));
35 lastPos = str.find_first_not_of(delimiters, pos);
36 pos = str.find_first_of (delimiters, lastPos);
42 * \brief Because not available in C++ (?)
43 * Counts the number of occurences of a substring within a string
46 int CountSubstring (const std::string& str,
47 const std::string& subStr)
49 int count = 0; // counts how many times it appears
50 unsigned int x = 0; // The index position in the string
54 x = str.find(subStr,x); // Find the substring
55 if (x != std::string::npos) // If present
57 count++; // increase the count
58 x += subStr.length(); // Skip this word
61 while (x != std::string::npos); // Carry on until not present
68 * \brief Weed out a string from the non-printable characters (in order
69 * to avoid corrupting the terminal of invocation when printing)
70 * @param s string to remove non printable characters from
72 std::string CreateCleanString(std::string s)
76 for(unsigned int i=0;i<str.size();i++)
86 if(!isprint(s[str.size()-1]))
88 if(s[str.size()-1]==0)
90 str[str.size()-1]=' ';
100 * \brief Add a SEPARATOR to the end of the name is necessary
101 * @param name file/directory name to normalize
103 void NormalizePath(std::string &name)
105 const char SEPARATOR_X = '/';
106 const char SEPARATOR_WIN = '\\';
107 const std::string SEPARATOR = "/";
108 int size = name.size();
110 if((name[size-1]!=SEPARATOR_X)&&(name[size-1]!=SEPARATOR_WIN))
118 * \brief Get the (directory) path from a full path file name
119 * @param fullName file/directory name to extract Path from
121 std::string GetPath(std::string &fullName)
123 int pos1 = fullName.rfind("/");
124 int pos2 = fullName.rfind("\\");
127 fullName.resize(pos1);
131 fullName.resize(pos2);
139 * \brief Get the (last) name of a full path file name
140 * @param fullName file/directory name to extract end name from
142 std::string GetName(std::string &fullName)
144 int fin = fullName.length()-1;
145 char a =fullName.c_str()[fin];
146 if (a == '/' || a == '\\')
151 for (int i=fin;i!=0;i--)
153 if (fullName.c_str()[i] == '/' || fullName.c_str()[i] == '\\')
160 std::string lastName;
161 for (int j=deb;j<fin+1;j++)
163 lastName=lastName+fullName.c_str()[j];