1 /*=========================================================================
4 Module: $RCSfile: gdcmUtil.cxx,v $
6 Date: $Date: 2004/09/27 08:39:08 $
7 Version: $Revision: 1.50 $
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"
22 #include <ctype.h> // For isspace
23 #include <string.h> // CLEANME: could this be only string ? Related to Win32 ?
28 * \brief Because is not yet available in g++2.96
30 std::istream& eatwhite(std::istream& is) {
43 * \brief Because not available in C++ (?)
45 void Tokenize (const std::string& str,
46 std::vector<std::string>& tokens,
47 const std::string& delimiters) {
48 std::string::size_type lastPos = str.find_first_not_of(delimiters,0);
49 std::string::size_type pos = str.find_first_of (delimiters,lastPos);
50 while (std::string::npos != pos || std::string::npos != lastPos) {
51 tokens.push_back(str.substr(lastPos, pos - lastPos));
52 lastPos = str.find_first_not_of(delimiters, pos);
53 pos = str.find_first_of (delimiters, lastPos);
59 * \brief Because not available in C++ (?)
60 * Counts the number of occurences of a substring within a string
63 int CountSubstring (const std::string& str,
64 const std::string& subStr) {
65 int count = 0; // counts how many times it appears
66 unsigned int x = 0; // The index position in the string
69 { x = str.find(subStr,x); // Find the substring
70 if (x != std::string::npos) // If present
71 { count++; // increase the count
72 x += subStr.length(); // Skip this word
75 while (x != std::string::npos); // Carry on until not present
82 * \brief Weed out a string from the non-printable characters (in order
83 * to avoid corrupting the terminal of invocation when printing)
84 * @param s string to remove non printable characters from
86 std::string CreateCleanString(std::string s) {
89 for(unsigned int i=0;i<str.size();i++)
96 if(!isprint(s[str.size()-1]))
97 if(s[str.size()-1]==0)
98 str[str.size()-1]=' ';
105 * \brief Add a SEPARATOR to the end of the name is necessary
106 * @param name file/directory name to normalize
108 void NormalizePath(std::string &name)
110 const char SEPARATOR_X = '/';
111 const char SEPARATOR_WIN = '\\';
112 const std::string SEPARATOR = "/";
113 int size=name.size();
115 if((name[size-1]!=SEPARATOR_X)&&(name[size-1]!=SEPARATOR_WIN))
123 * \brief Get the (directory) path from a full path file name
124 * @param fullName file/directory name to extract Path from
126 std::string GetPath(std::string &fullName)
128 int pos1 = fullName.rfind("/");
129 int pos2 = fullName.rfind("\\");
132 fullName.resize(pos1);
136 fullName.resize(pos2);
143 * \brief Get the (last) name of a full path file name
144 * @param fullName file/directory name to extract end name from
146 std::string GetName(std::string &fullName)
148 int fin=fullName.length()-1;
149 char a =fullName.c_str()[fin];
150 if (a == '/' || a == '\\') {
154 for (int i=fin;i!=0;i--) {
155 if (fullName.c_str()[i] == '/' || fullName.c_str()[i] == '\\')
160 std::string lastName;
161 for (int j=deb;j<fin+1;j++)
162 lastName=lastName+fullName.c_str()[j];