]> Creatis software - gdcm.git/blob - src/gdcmUtil.cxx
STYLE: Cleanup outside API, there is still too much offered to user
[gdcm.git] / src / gdcmUtil.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmUtil.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/06/23 03:36:24 $
7   Version:   $Revision: 1.45 $
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.htm 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  Weed out a string from the non-printable characters (in order
60  *         to avoid corrupting the terminal of invocation when printing)
61  * @param s string to remove non printable characters from
62  */
63 std::string CreateCleanString(std::string s) {
64    std::string str=s;
65
66    for(unsigned int i=0;i<str.size();i++)
67    {
68       if(!isprint(str[i]))
69          str[i]='.';
70    }
71
72    if(str.size()>0)
73       if(!isprint(s[str.size()-1]))
74          if(s[str.size()-1]==0)
75             str[str.size()-1]=' ';
76
77    return str;
78 }
79
80 /**
81  * \ingroup Globals
82  * \brief   Add a SEPARATOR to the end of the name is necessary
83  * @param name file/directory name to normalize 
84  */
85 void NormalizePath(std::string &name)
86 {
87    const char SEPARATOR_X      = '/';
88    const char SEPARATOR_WIN    = '\\';
89    const std::string SEPARATOR = "/";
90    int size=name.size();
91
92    if((name[size-1]!=SEPARATOR_X)&&(name[size-1]!=SEPARATOR_WIN))
93    {
94       name+=SEPARATOR;
95    }
96 }
97
98 /**
99  * \ingroup Globals
100  * \brief   Get the (directory) path from a full path file name
101  * @param   fullName file/directory name to extract Path from
102  */
103 std::string GetPath(std::string &fullName)
104 {
105    int pos1=fullName.rfind("/");
106    int pos2=fullName.rfind("\\");
107    if(pos1>pos2)
108       fullName.resize(pos1);
109    else
110       fullName.resize(pos2);
111    return fullName;
112 }
113
114 /**
115  * \ingroup Globals
116  * \brief   Get the (last) name of a full path file name
117  * @param   fullName file/directory name to extract end name from
118  */
119 std::string GetName(std::string &fullName)
120 {   
121   int fin=fullName.length()-1;
122   char a =fullName.c_str()[fin];
123   if (a == '/' || a == '\\') {
124      fin--;
125   }
126   int deb;
127   for (int i=fin;i!=0;i--) {
128      if (fullName.c_str()[i] == '/' || fullName.c_str()[i] == '\\')  
129         break;
130       deb = i;
131   }    
132
133   std::string lastName;
134   for (int j=deb;j<fin+1;j++)
135     lastName=lastName+fullName.c_str()[j];
136
137   return lastName;
138