]> Creatis software - gdcm.git/blob - src/gdcmUtil.cxx
b4907d912c4b4a169b6c60081807241cf9fdfd82
[gdcm.git] / src / gdcmUtil.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmUtil.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/10/10 00:42:55 $
7   Version:   $Revision: 1.53 $
8                                                                                 
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.
12                                                                                 
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.
16                                                                                 
17 =========================================================================*/
18
19 #include "gdcmUtil.h"
20 #include "gdcmDebug.h"
21
22
23 /**
24  * \ingroup Globals
25  * \brief Provide a better 'c++' approach for sprintf
26  * For example c code is:
27  * sprintf(trash, "%04x|%04x", group , element);
28  *
29  * c++ is 
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;
34  * buf.str();
35  */
36 #include <stdarg.h>  //only included in implementation file
37 #include <stdio.h>   //only included in implementation file
38
39 std::string gdcmUtil::Format(const char* format, ...)
40 {
41    char buffer[2048];
42    va_list args;
43    va_start(args, format);
44    vsprintf(buffer, format, args);  //might be a security flaw
45    return buffer;
46 }
47
48
49 /**
50  * \ingroup Globals
51  * \brief Because not available in C++ (?)
52  */
53 void gdcmUtil::Tokenize (const std::string& str,
54                std::vector<std::string>& tokens,
55                const std::string& delimiters)
56 {
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)
60    {
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);
64    }
65 }
66
67 /**
68  * \ingroup Globals
69  * \brief Because not available in C++ (?)
70  *        Counts the number of occurences of a substring within a string
71  */
72  
73 int gdcmUtil::CountSubstring (const std::string& str,
74                     const std::string& subStr)
75 {
76    int count = 0;   // counts how many times it appears
77    unsigned int x = 0;       // The index position in the string
78
79    do
80    {
81       x = str.find(subStr,x);       // Find the substring
82       if (x != std::string::npos)   // If present
83       {
84          count++;                  // increase the count
85          x += subStr.length();     // Skip this word
86       }
87    }
88    while (x != std::string::npos);  // Carry on until not present
89
90    return count;
91 }
92
93 /**
94  * \ingroup Globals
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
98  */
99 std::string gdcmUtil::CreateCleanString(std::string s)
100 {
101    std::string str = s;
102
103    for(unsigned int i=0;i<str.size();i++)
104    {
105       if(!isprint(str[i]))
106       {
107          str[i]='.';
108       }
109    }
110
111    if(str.size()>0)
112    {
113       if(!isprint(s[str.size()-1]))
114       {
115          if(s[str.size()-1]==0)
116          {
117             str[str.size()-1]=' ';
118          }
119       }
120    }
121
122    return str;
123 }
124
125 /**
126  * \ingroup Globals
127  * \brief   Add a SEPARATOR to the end of the name is necessary
128  * @param name file/directory name to normalize 
129  */
130 void gdcmUtil::NormalizePath(std::string &name)
131 {
132    const char SEPARATOR_X      = '/';
133    const char SEPARATOR_WIN    = '\\';
134    const std::string SEPARATOR = "/";
135    int size = name.size();
136
137    if((name[size-1]!=SEPARATOR_X)&&(name[size-1]!=SEPARATOR_WIN))
138    {
139       name+=SEPARATOR;
140    }
141 }
142
143 /**
144  * \ingroup Globals
145  * \brief   Get the (directory) path from a full path file name
146  * @param   fullName file/directory name to extract Path from
147  */
148 std::string gdcmUtil::GetPath(std::string &fullName)
149 {
150    int pos1 = fullName.rfind("/");
151    int pos2 = fullName.rfind("\\");
152    if( pos1 > pos2)
153    {
154       fullName.resize(pos1);
155    }
156    else
157    {
158       fullName.resize(pos2);
159    }
160
161    return fullName;
162 }
163
164 /**
165  * \ingroup Globals
166  * \brief   Get the (last) name of a full path file name
167  * @param   fullName file/directory name to extract end name from
168  */
169 std::string gdcmUtil::GetName(std::string &fullName)
170 {   
171    int fin = fullName.length()-1;
172    char a =fullName.c_str()[fin];
173    if (a == '/' || a == '\\')
174    {
175       fin--;
176    }
177    int deb = 0;
178    for (int i=fin;i!=0;i--)
179    {
180       if (fullName.c_str()[i] == '/' || fullName.c_str()[i] == '\\')
181       {
182          break;
183       }
184       deb = i;
185    }
186
187    std::string lastName;
188    for (int j=deb;j<fin+1;j++)
189    {
190       lastName=lastName+fullName.c_str()[j];
191    }
192
193   return lastName;
194