]> Creatis software - gdcm.git/blob - src/gdcmUtil.cxx
* src/*.cxx *.h Reference to License.htm fixed to License.html.
[gdcm.git] / src / gdcmUtil.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmUtil.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/09/27 08:39:08 $
7   Version:   $Revision: 1.50 $
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 #include <stdio.h>
22 #include <ctype.h>   // For isspace
23 #include <string.h>  // CLEANME: could this be only string ? Related to Win32 ?
24 #include <iostream>
25
26 /**
27  * \ingroup Globals
28  * \brief   Because is not yet available in g++2.96
29  */
30 std::istream& eatwhite(std::istream& is) {
31    char c;
32    while (is.get(c)) {
33       if (!isspace(c)) {
34          is.putback(c);
35          break;
36       }
37    }
38    return is;
39 }
40
41 /**
42  * \ingroup Globals
43  * \brief Because not available in C++ (?)
44  */
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);
54    }
55 }
56
57 /**
58  * \ingroup Globals
59  * \brief Because not available in C++ (?)
60  *        Counts the number of occurences of a substring within a string
61  */
62  
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
67
68    do
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
73         }
74     }
75    while (x != std::string::npos);  // Carry on until not present
76
77    return count;
78 }
79
80 /**
81  * \ingroup Globals
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
85  */
86 std::string CreateCleanString(std::string s) {
87    std::string str=s;
88
89    for(unsigned int i=0;i<str.size();i++)
90    {
91       if(!isprint(str[i]))
92          str[i]='.';
93    }
94
95    if(str.size()>0)
96       if(!isprint(s[str.size()-1]))
97          if(s[str.size()-1]==0)
98             str[str.size()-1]=' ';
99
100    return str;
101 }
102
103 /**
104  * \ingroup Globals
105  * \brief   Add a SEPARATOR to the end of the name is necessary
106  * @param name file/directory name to normalize 
107  */
108 void NormalizePath(std::string &name)
109 {
110    const char SEPARATOR_X      = '/';
111    const char SEPARATOR_WIN    = '\\';
112    const std::string SEPARATOR = "/";
113    int size=name.size();
114
115    if((name[size-1]!=SEPARATOR_X)&&(name[size-1]!=SEPARATOR_WIN))
116    {
117       name+=SEPARATOR;
118    }
119 }
120
121 /**
122  * \ingroup Globals
123  * \brief   Get the (directory) path from a full path file name
124  * @param   fullName file/directory name to extract Path from
125  */
126 std::string GetPath(std::string &fullName)
127 {
128    int pos1 = fullName.rfind("/");
129    int pos2 = fullName.rfind("\\");
130    if( pos1 > pos2)
131    {
132       fullName.resize(pos1);
133    }
134    else
135    {
136       fullName.resize(pos2);
137    }
138    return fullName;
139 }
140
141 /**
142  * \ingroup Globals
143  * \brief   Get the (last) name of a full path file name
144  * @param   fullName file/directory name to extract end name from
145  */
146 std::string GetName(std::string &fullName)
147 {   
148   int fin=fullName.length()-1;
149   char a =fullName.c_str()[fin];
150   if (a == '/' || a == '\\') {
151      fin--;
152   }
153   int deb = 0;
154   for (int i=fin;i!=0;i--) {
155      if (fullName.c_str()[i] == '/' || fullName.c_str()[i] == '\\')  
156         break;
157       deb = i;
158   }    
159
160   std::string lastName;
161   for (int j=deb;j<fin+1;j++)
162     lastName=lastName+fullName.c_str()[j];
163
164   return lastName;
165