1 /*=========================================================================
4 Module: $RCSfile: gdcmUtil.cxx,v $
6 Date: $Date: 2004/10/12 04:35:48 $
7 Version: $Revision: 1.54 $
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 <stdarg.h> //only included in implementation file
23 #include <stdio.h> //only included in implementation file
30 * \brief Provide a better 'c++' approach for sprintf
31 * For example c code is:
32 * sprintf(trash, "%04x|%04x", group , element);
35 * std::ostringstream buf;
36 * buf << std::right << std::setw(4) << std::setfill('0') << std::hex
37 * << group << "|" << std::right << std::setw(4) << std::setfill('0')
38 * << std::hex << element;
42 std::string Util::Format(const char* format, ...)
46 va_start(args, format);
47 vsprintf(buffer, format, args); //might be a security flaw
54 * \brief Because not available in C++ (?)
56 void Util::Tokenize (const std::string& str,
57 std::vector<std::string>& tokens,
58 const std::string& delimiters)
60 std::string::size_type lastPos = str.find_first_not_of(delimiters,0);
61 std::string::size_type pos = str.find_first_of (delimiters,lastPos);
62 while (std::string::npos != pos || std::string::npos != lastPos)
64 tokens.push_back(str.substr(lastPos, pos - lastPos));
65 lastPos = str.find_first_not_of(delimiters, pos);
66 pos = str.find_first_of (delimiters, lastPos);
72 * \brief Because not available in C++ (?)
73 * Counts the number of occurences of a substring within a string
76 int Util::CountSubstring (const std::string& str,
77 const std::string& subStr)
79 int count = 0; // counts how many times it appears
80 unsigned int x = 0; // The index position in the string
84 x = str.find(subStr,x); // Find the substring
85 if (x != std::string::npos) // If present
87 count++; // increase the count
88 x += subStr.length(); // Skip this word
91 while (x != std::string::npos); // Carry on until not present
98 * \brief Weed out a string from the non-printable characters (in order
99 * to avoid corrupting the terminal of invocation when printing)
100 * @param s string to remove non printable characters from
102 std::string Util::CreateCleanString(std::string s)
106 for(unsigned int i=0;i<str.size();i++)
116 if(!isprint(s[str.size()-1]))
118 if(s[str.size()-1]==0)
120 str[str.size()-1]=' ';
130 * \brief Add a SEPARATOR to the end of the name is necessary
131 * @param name file/directory name to normalize
133 void Util::NormalizePath(std::string &name)
135 const char SEPARATOR_X = '/';
136 const char SEPARATOR_WIN = '\\';
137 const std::string SEPARATOR = "/";
138 int size = name.size();
140 if((name[size-1]!=SEPARATOR_X)&&(name[size-1]!=SEPARATOR_WIN))
148 * \brief Get the (directory) path from a full path file name
149 * @param fullName file/directory name to extract Path from
151 std::string Util::GetPath(std::string &fullName)
153 int pos1 = fullName.rfind("/");
154 int pos2 = fullName.rfind("\\");
157 fullName.resize(pos1);
161 fullName.resize(pos2);
169 * \brief Get the (last) name of a full path file name
170 * @param fullName file/directory name to extract end name from
172 std::string Util::GetName(std::string &fullName)
174 int fin = fullName.length()-1;
175 char a =fullName.c_str()[fin];
176 if (a == '/' || a == '\\')
181 for (int i=fin;i!=0;i--)
183 if (fullName.c_str()[i] == '/' || fullName.c_str()[i] == '\\')
190 std::string lastName;
191 for (int j=deb;j<fin+1;j++)
193 lastName=lastName+fullName.c_str()[j];
199 } // end namespace gdcm