1 /*=========================================================================
4 Module: $RCSfile: gdcmUtil.cxx,v $
6 Date: $Date: 2004/10/09 02:57:12 $
7 Version: $Revision: 1.52 $
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"
25 * \brief Provide a better 'c++' approach for sprintf
26 * For example c code is:
27 * sprintf(trash, "%04x|%04x", group , element);
30 * std::ostringstream buf;
31 * buf << std::right << std::setw(4) << std::setfill('0') << std::hex
32 * << group << "|" << std::right << std::setw(4) << std::setfill('0')
33 * << std::hex << element;
36 #include <stdarg.h> //only included in implementation file
37 #include <stdio.h> //only included in implementation file
39 std::string Format(const char* format, ...)
43 va_start(args, format);
44 vsprintf(buffer, format, args); //might be a security flaw
51 * \brief Because not available in C++ (?)
53 void Tokenize (const std::string& str,
54 std::vector<std::string>& tokens,
55 const std::string& delimiters)
57 std::string::size_type lastPos = str.find_first_not_of(delimiters,0);
58 std::string::size_type pos = str.find_first_of (delimiters,lastPos);
59 while (std::string::npos != pos || std::string::npos != lastPos)
61 tokens.push_back(str.substr(lastPos, pos - lastPos));
62 lastPos = str.find_first_not_of(delimiters, pos);
63 pos = str.find_first_of (delimiters, lastPos);
69 * \brief Because not available in C++ (?)
70 * Counts the number of occurences of a substring within a string
73 int CountSubstring (const std::string& str,
74 const std::string& subStr)
76 int count = 0; // counts how many times it appears
77 unsigned int x = 0; // The index position in the string
81 x = str.find(subStr,x); // Find the substring
82 if (x != std::string::npos) // If present
84 count++; // increase the count
85 x += subStr.length(); // Skip this word
88 while (x != std::string::npos); // Carry on until not present
95 * \brief Weed out a string from the non-printable characters (in order
96 * to avoid corrupting the terminal of invocation when printing)
97 * @param s string to remove non printable characters from
99 std::string CreateCleanString(std::string s)
103 for(unsigned int i=0;i<str.size();i++)
113 if(!isprint(s[str.size()-1]))
115 if(s[str.size()-1]==0)
117 str[str.size()-1]=' ';
127 * \brief Add a SEPARATOR to the end of the name is necessary
128 * @param name file/directory name to normalize
130 void NormalizePath(std::string &name)
132 const char SEPARATOR_X = '/';
133 const char SEPARATOR_WIN = '\\';
134 const std::string SEPARATOR = "/";
135 int size = name.size();
137 if((name[size-1]!=SEPARATOR_X)&&(name[size-1]!=SEPARATOR_WIN))
145 * \brief Get the (directory) path from a full path file name
146 * @param fullName file/directory name to extract Path from
148 std::string GetPath(std::string &fullName)
150 int pos1 = fullName.rfind("/");
151 int pos2 = fullName.rfind("\\");
154 fullName.resize(pos1);
158 fullName.resize(pos2);
166 * \brief Get the (last) name of a full path file name
167 * @param fullName file/directory name to extract end name from
169 std::string GetName(std::string &fullName)
171 int fin = fullName.length()-1;
172 char a =fullName.c_str()[fin];
173 if (a == '/' || a == '\\')
178 for (int i=fin;i!=0;i--)
180 if (fullName.c_str()[i] == '/' || fullName.c_str()[i] == '\\')
187 std::string lastName;
188 for (int j=deb;j<fin+1;j++)
190 lastName=lastName+fullName.c_str()[j];