libatomprobe
Library for Atom Probe Tomography (APT) computation
stringFuncs.cpp
Go to the documentation of this file.
1 /*
2  * stringFuncs.cpp - string manipulation routines
3  * Copyright (C) 2014, D Haley
4 
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14 
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
20 
22 
23 
24 #include <fstream>
25 #include <ctime>
26 
27 namespace AtomProbe
28 {
29 
30 using std::string;
31 using std::vector;
32 
33 std::string onlyFilename( const std::string& path)
34 {
35 #if defined(_WIN32) || defined(_WIN64)
36  //windows uses "\" as path sep, just to be different
37  return path.substr( path.find_last_of( '\\' ) +1 );
38 #else
39  //The entire world, including the interwebs, uses "/" as the path sep.
40  return path.substr( path.find_last_of( '/' ) +1 );
41 #endif
42 }
43 
44 std::string onlyDir( const std::string& path)
45 {
46 #if defined(_WIN32) || defined(_WIN64)
47  //windows uses "\" as path sep, just to be different
48  return path.substr(0, path.find_last_of( '\\' ) +1 );
49 #else
50  //The entire world, including the interwebs, uses "/" as the path sep.
51  return path.substr(0, path.find_last_of( '/' ) +1 );
52 #endif
53 }
54 
55 void nullifyMarker(char *buffer, char marker)
56 {
57  while(*(buffer))
58  {
59  if(*buffer == marker)
60  {
61  *buffer='\0';
62  break;
63  }
64  buffer++;
65  }
66 }
67 
68 void ucharToHexStr(unsigned char c, std::string &s)
69 {
70  s=" ";
71  char h1,h2;
72 
73  h1 = c/16;
74  h2 = c%16;
75 
76  if(h1 < 10)
77  {
78  s[0]=(h1+'0');
79  }
80  else
81  s[0]=((h1-10)+'a');
82 
83  if(h2 < 10)
84  {
85  s[1]=(h2+'0');
86  }
87  else
88  s[1]=((h2-10)+'a');
89 
90 }
91 
92 void hexStrToUChar(const std::string &s, unsigned char &c)
93 {
94  ASSERT(s.size() ==2);
95 
96  ASSERT((s[0] >= '0' && s[0] <= '9') ||
97  (s[0] >= 'a' && s[0] <= 'f'));
98  ASSERT((s[1] >= '0' && s[1] <= '9') ||
99  (s[1] >= 'a' && s[1] <= 'f'));
100 
101  int high,low;
102  if(s[0] <= '9' && s[0] >='0')
103  high = s[0]-(int)'0';
104  else
105  high = s[0] -(int)'a' + 10;
106 
107  if(s[1] <= '9' && s[1] >='0')
108  low = s[1]-(int)'0';
109  else
110  low = s[1] -(int)'a' + 10;
111 
112  c = 16*high + low;
113 }
114 
115 std::string digitString(unsigned int thisDigit, unsigned int maxDigit)
116 {
117  std::string s,thisStr;
118  stream_cast(thisStr,thisDigit);
119 
120  stream_cast(s,maxDigit);
121  for(unsigned int ui=0;ui<s.size();ui++)
122  s[ui]='0';
123 
124 
125  s=s.substr(0,s.size()-thisStr.size());
126 
127  return s+thisStr;
128 }
129 
130 
131 //Strip "whitespace"
132 std::string stripWhite(const std::string &str)
133 {
134  return stripChars(str,"\f\n\r\t ");
135 }
136 
137 std::string stripChars(const std::string &str, const char *chars)
138 {
139  using std::string;
140 
141  size_t start;
142  size_t end;
143  if(!str.size())
144  return str;
145 
146  start = str.find_first_not_of(chars);
147  end = str.find_last_not_of(chars);
148  if(start == string::npos)
149  return string("");
150  else
151  return string(str, start,
152  end-start+1);
153 }
154 
155 void stripZeroEntries(std::vector<std::string> &sVec)
156 {
157  //Create a truncated vector and reserve mem.
158  std::vector<std::string> tVec;
159  tVec.reserve(sVec.size());
160  std::string s;
161  for(unsigned int ui=0;ui<sVec.size(); ui++)
162  {
163  //Only copy entries with a size.
164  if(sVec[ui].size())
165  {
166  //Push dummy string and swap,
167  // to avoid copy
168  tVec.push_back(s);
169  tVec.back().swap(sVec[ui]);
170  }
171  }
172  //Swap out the truncated vector with the source
173  tVec.swap(sVec);
174 }
175 
176 std::string lowercase(std::string s)
177 {
178  for(unsigned int ui=0;ui<s.size();ui++)
179  {
180  if(isalpha(s[ui]) && isupper(s[ui]))
181  s[ui] = tolower(s[ui]);
182  }
183  return s;
184 }
185 
186 //Split strings around a delimiter
187 void splitStrsRef(const char *cpStr, const char delim,std::vector<string> &v )
188 {
189  const char *thisMark, *lastMark;
190  string str;
191  v.clear();
192 
193  //check for null string
194  if(!*cpStr)
195  return;
196  thisMark=cpStr;
197  lastMark=cpStr;
198  while(*thisMark)
199  {
200  if(*thisMark==delim)
201  {
202  str.assign(lastMark,thisMark-lastMark);
203  v.push_back(str);
204 
205  thisMark++;
206  lastMark=thisMark;
207  }
208  else
209  thisMark++;
210  }
211 
212  if(thisMark!=lastMark)
213  {
214  str.assign(lastMark,thisMark-lastMark);
215  v.push_back(str);
216  }
217 
218 }
219 
220 //Split strings around any of a string of delimiters
221 void splitStrsRef(const char *cpStr, const char *delim,std::vector<string> &v )
222 {
223  const char *thisMark, *lastMark;
224  string str;
225  v.clear();
226 
227  //check for null string
228  if(!(*cpStr))
229  return;
230  thisMark=cpStr;
231  lastMark=cpStr;
232  while(*thisMark)
233  {
234  //Loop over possible delimiters to determine if this char is a delimiter
235  bool isDelim;
236  const char *tmp;
237  tmp=delim;
238  isDelim=false;
239  while(*tmp)
240  {
241  if(*thisMark==*tmp)
242  {
243  isDelim=true;
244  break;
245  }
246  tmp++;
247  }
248 
249  if(isDelim)
250  {
251  str.assign(lastMark,thisMark-lastMark);
252  v.push_back(str);
253 
254  thisMark++;
255  lastMark=thisMark;
256  }
257  else
258  thisMark++;
259  }
260 
261  if(thisMark!=lastMark)
262  {
263  str.assign(lastMark,thisMark-lastMark);
264  v.push_back(str);
265  }
266 
267 }
268 
269 bool parseColString(const std::string &str,
270  unsigned char &r, unsigned char &g, unsigned char &b, unsigned char &a)
271 {
272  //Input string is in 2 char hex form, 3 or 4 colour, with # leading. RGB order
273  //lowercase string.
274  if(str.size() != 9 && str.size() != 7)
275  return false;
276 
277  if(str[0] != '#')
278  return false;
279 
280  string rS,gS,bS,aS;
281  rS=str.substr(1,2);
282  gS=str.substr(3,2);
283  bS=str.substr(5,2);
284 
285  if(!isxdigit(rS[0]) || !isxdigit(rS[1]))
286  return false;
287  if(!isxdigit(gS[0]) || !isxdigit(gS[1]))
288  return false;
289  if(!isxdigit(bS[0]) || !isxdigit(bS[1]))
290  return false;
291 
292  hexStrToUChar(str.substr(1,2),r);
293  hexStrToUChar(str.substr(3,2),g);
294  hexStrToUChar(str.substr(5,2),b);
295  //3 colour must have a=255.
296  if(str.size() == 7)
297  a = 255;
298  else
299  {
300  aS=str.substr(7,2);
301  if(!isxdigit(aS[0]) || !isxdigit(aS[1]))
302  return false;
303  hexStrToUChar(str.substr(7,2),a);
304  }
305  return true;
306 }
307 
308 void genColString(unsigned char r, unsigned char g,
309  unsigned char b, std::string &s)
310 {
311  string tmp;
312  s="#";
313  ucharToHexStr(r,tmp);
314  s+=tmp;
315  ucharToHexStr(g,tmp);
316  s+=tmp;
317  ucharToHexStr(b,tmp);
318  s+=tmp;
319 }
320 }
321 
std::string onlyDir(const std::string &path)
Return only the directory name component of the full path.
Definition: stringFuncs.cpp:44
std::string onlyFilename(const std::string &path)
Return only the filename component.
Definition: stringFuncs.cpp:33
std::string stripWhite(const std::string &str)
Strip whitespace, (eg tab,space) from either side of a string.
void nullifyMarker(char *buffer, char marker)
Definition: stringFuncs.cpp:55
std::string digitString(unsigned int thisDigit, unsigned int maxDigit)
Generate a string with leading digits up to maxDigit (eg, if maxDigit is 424, and thisDigit is 1...
void ucharToHexStr(unsigned char c, std::string &s)
Definition: stringFuncs.cpp:68
void genColString(unsigned char r, unsigned char g, unsigned char b, unsigned char a, std::string &s)
void splitStrsRef(const char *cpStr, const char delim, std::vector< std::string > &v)
Split string references using a single delimiter.
std::string lowercase(std::string s)
Return a lowercase version for a given string.
#define ASSERT(f)
bool parseColString(const std::string &str, unsigned char &r, unsigned char &g, unsigned char &b, unsigned char &a)
Parse a colour string containing rgb[a]; hex for with leading #.
bool stream_cast(T1 &result, const T2 &obj)
Template function to cast and object to another by the stringstream.
Definition: stringFuncs.h:32
void stripZeroEntries(std::vector< std::string > &s)
void hexStrToUChar(const std::string &s, unsigned char &c)
Definition: stringFuncs.cpp:92
std::string stripChars(const std::string &Str, const char *chars)