libatomprobe
Library for Atom Probe Tomography (APT) computation
stringFuncs.h
Go to the documentation of this file.
1 /*
2  * helper/stringFuncs.h - String manipulation header
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 
19 #ifndef ATOMPROBE_STRINGFUNCS_H
20 #define ATOMPROBE_STRINGFUNCS_H
21 
22 #include <string>
23 #include <sstream>
24 #include <vector>
25 
26 namespace AtomProbe
27 {
28 
29 
31 //IO operator
32 template<class T1, class T2> bool stream_cast(T1 &result, const T2 &obj)
33 {
34  std::stringstream ss;
35  ss << obj;
36  ss >> result;
37  return ss.fail();
38 }
39 
40 template<class T>
41 void strAppend(std::string s, const T &a)
42 {
43  std::string tmp;
44  stream_cast(tmp,a);
45  s+=tmp;
46 }
47 
48 //Convert an RGBA 8-bit/channel quadruplet into its hexadecimal colour string
49 
50 void genColString(unsigned char r, unsigned char g,
51  unsigned char b, unsigned char a, std::string &s);
52 //Convert an RGB 8-bit/channel triplet into its hexadecimal colour string
53 void genColString(unsigned char r, unsigned char g,
54  unsigned char b, std::string &s);
55 
57 bool parseColString(const std::string &str,
58  unsigned char &r, unsigned char &g, unsigned char &b, unsigned char &a);
59 
61 //leading digit will generate the string 001
62 std::string digitString(unsigned int thisDigit, unsigned int maxDigit);
63 
64 //Strip given whitespace (\f,\n,\r,\t,\ )from a string
65 std::string stripWhite(const std::string &str);
66 //Strip specified chars from a string
67 std::string stripChars(const std::string &Str, const char *chars);
69 std::string lowercase(std::string s);
71 std::string uppercase(std::string s);
72 
73 //Drop empty entries from a string of vector
74 void stripZeroEntries(std::vector<std::string> &s);
75 
77 std::string stripWhite(const std::string &str);
78 
80 void splitStrsRef(const char *cpStr, const char delim,std::vector<std::string> &v );
81 
83 void splitStrsRef(const char *cpStr, const char *delim,std::vector<std::string> &v );
84 
86 std::string onlyFilename( const std::string& path );
88 // - do not use with UNC windows paths
89 std::string onlyDir( const std::string& path );
90 
92 // - do not use with UNC windows paths
93 std::string convertFileStringToNative(const std::string &s);
94 
96 std::string convertFileStringToCanonical(const std::string &s);
97 
98 //Print N tabs to a string
99 inline std::string tabs(unsigned int nTabs)
100 {
101  std::string s;
102  s.resize(nTabs);
103  std::fill(s.begin(),s.end(),'\t');
104  return s;
105 }
106 
107 //Brute force convert a wide STL str to a normal stl str
108 inline std::string stlWStrToStlStr(const std::wstring& s)
109 {
110  std::string temp(s.length(),' ');
111  std::copy(s.begin(), s.end(), temp.begin());
112  return temp;
113 }
114 //Brute force convert an stlStr to an stl wide str
115 inline std::wstring stlStrToStlWStr(const std::string& s)
116 {
117  std::wstring temp(s.length(),L' ');
118  std::copy(s.begin(), s.end(), temp.begin());
119  return temp;
120 }
121 
122 //Set a given token to null, to trim a string
123 void nullifyMarker(char *buffer, char marker);
124 }
125 #endif
std::string convertFileStringToCanonical(const std::string &s)
Convert a path format into a unix path from native format.
std::string uppercase(std::string s)
Return a uppercase version for a given string.
std::string onlyDir(const std::string &path)
Return only the directory name component of the full path.
Definition: stringFuncs.cpp:44
std::string tabs(unsigned int nTabs)
Definition: stringFuncs.h:99
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...
std::string convertFileStringToNative(const std::string &s)
Convert a path format into a native path from unix format.
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 stlWStrToStlStr(const std::wstring &s)
Definition: stringFuncs.h:108
std::wstring stlStrToStlWStr(const std::string &s)
Definition: stringFuncs.h:115
std::string lowercase(std::string s)
Return a lowercase version for a given string.
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 strAppend(std::string s, const T &a)
Definition: stringFuncs.h:41
std::string stripChars(const std::string &Str, const char *chars)