summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/StringUtil.cc25
-rw-r--r--src/StringUtil.hh85
2 files changed, 59 insertions, 51 deletions
diff --git a/src/StringUtil.cc b/src/StringUtil.cc
index a85735a..54ef357 100644
--- a/src/StringUtil.cc
+++ b/src/StringUtil.cc
@@ -19,7 +19,7 @@
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE. 20// DEALINGS IN THE SOFTWARE.
21 21
22// $Id: StringUtil.cc,v 1.7 2002/01/27 12:46:28 fluxgen Exp $ 22// $Id: StringUtil.cc,v 1.8 2002/03/20 11:32:03 fluxgen Exp $
23 23
24#include "StringUtil.hh" 24#include "StringUtil.hh"
25 25
@@ -31,10 +31,13 @@
31 31
32using namespace std; 32using namespace std;
33 33
34namespace StringUtil
35{
36
34//------- strdup ------------------------ 37//------- strdup ------------------------
35//TODO: comment this 38//TODO: comment this
36//---------------------------------------- 39//----------------------------------------
37char *StringUtil::strdup(const char *s) { 40char *strdup(const char *s) {
38 int l = strlen(s) + 1; 41 int l = strlen(s) + 1;
39 char *n = new char[l]; 42 char *n = new char[l];
40 strncpy(n, s, l); 43 strncpy(n, s, l);
@@ -47,7 +50,7 @@ char *StringUtil::strdup(const char *s) {
47// Returns 0 on success else pointer to str. 50// Returns 0 on success else pointer to str.
48// TODO: comment this 51// TODO: comment this
49//--------------------------------- 52//---------------------------------
50const char * StringUtil::strcasestr(const char *str, const char *ptn) { 53const char *strcasestr(const char *str, const char *ptn) {
51 const char *s2, *p2; 54 const char *s2, *p2;
52 for( ; *str; str++) { 55 for( ; *str; str++) {
53 for(s2=str, p2=ptn; ; s2++,p2++) { 56 for(s2=str, p2=ptn; ; s2++,p2++) {
@@ -63,7 +66,7 @@ const char * StringUtil::strcasestr(const char *str, const char *ptn) {
63// returns expanded filename 66// returns expanded filename
64// (note: the function creates new memory for the string) 67// (note: the function creates new memory for the string)
65//--------------------------------------------------- 68//---------------------------------------------------
66char *StringUtil::expandFilename(const char *filename) { 69char *expandFilename(const char *filename) {
67 70
68 auto_ptr<char> retval( new char[strlen(filename)+strlen(getenv("HOME"))+2]); 71 auto_ptr<char> retval( new char[strlen(filename)+strlen(getenv("HOME"))+2]);
69 if (filename[0]=='~') { 72 if (filename[0]=='~') {
@@ -85,29 +88,29 @@ char *StringUtil::expandFilename(const char *filename) {
85// for the position + 1 in the in-string where the "last"-char value 88// for the position + 1 in the in-string where the "last"-char value
86// was found. 89// was found.
87//------------------------------------------ 90//------------------------------------------
88int StringUtil::getStringBetween(string& out, const char *instr, const char first, const char last, 91int getStringBetween(std::string& out, const char *instr, const char first, const char last,
89 const char *ok_chars) { 92 const char *ok_chars) {
90 assert(first); 93 assert(first);
91 assert(last); 94 assert(last);
92 assert(instr); 95 assert(instr);
93 96
94 string::size_type i = 0, 97 std::string::size_type i = 0,
95 total_add=0; //used to add extra if there is a \last to skip 98 total_add=0; //used to add extra if there is a \last to skip
96 string in(instr); 99 std::string in(instr);
97 100
98 // eat leading whitespace 101 // eat leading whitespace
99 i = in.find_first_not_of(ok_chars); 102 i = in.find_first_not_of(ok_chars);
100 if (i == string::npos) 103 if (i == std::string::npos)
101 return -in.size(); // nothing left but whitespace 104 return -in.size(); // nothing left but whitespace
102 105
103 if (in[i]!=first) 106 if (in[i]!=first)
104 return -i; //return position to error 107 return -i; //return position to error
105 108
106 // find the end of the token 109 // find the end of the token
107 string::size_type j = i; 110 std::string::size_type j = i;
108 while (1) { 111 while (1) {
109 j = in.find_first_of(last, j+1); 112 j = in.find_first_of(last, j+1);
110 if (j==string::npos) 113 if (j==std::string::npos)
111 return -in.size(); //send negative size 114 return -in.size(); //send negative size
112 115
113 //we found the last char, check so it doesn't have a '\' before 116 //we found the last char, check so it doesn't have a '\' before
@@ -124,3 +127,5 @@ int StringUtil::getStringBetween(string& out, const char *instr, const char firs
124 //return value to last character 127 //return value to last character
125 return (j+1+total_add); 128 return (j+1+total_add);
126} 129}
130
131}; //end namespace StringUtil
diff --git a/src/StringUtil.hh b/src/StringUtil.hh
index a931fa2..91732ab 100644
--- a/src/StringUtil.hh
+++ b/src/StringUtil.hh
@@ -19,60 +19,63 @@
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE. 20// DEALINGS IN THE SOFTWARE.
21 21
22//$Id: StringUtil.hh,v 1.6 2002/02/17 18:52:20 fluxgen Exp $ 22//$Id: StringUtil.hh,v 1.7 2002/03/20 11:32:03 fluxgen Exp $
23 23
24#ifndef STRINGUTIL_HH 24#ifndef STRINGUTIL_HH
25#define STRINGUTIL_HH 25#define STRINGUTIL_HH
26 26
27#include <string> 27#include <string>
28 28
29struct StringUtil 29namespace StringUtil
30{ 30{
31 static char *strdup(const char *); 31
32char *strdup(const char *);
32 33
33 //Similar to `strstr' but this function ignores the case of both strings 34//Similar to `strstr' but this function ignores the case of both strings
34 static const char *strcasestr(const char *str, const char *ptn); 35const char *strcasestr(const char *str, const char *ptn);
35 36
36 static char *expandFilename(const char *filename); 37char *expandFilename(const char *filename);
37 static int getStringBetween(std::string& out, const char *instr, const char first, const char last, 38int getStringBetween(std::string& out, const char *instr, const char first, const char last,
38 const char *ok_chars=" \t\n"); 39 const char *ok_chars=" \t\n");
39 //--------- stringtok ---------------------------------- 40
40 // Breaks a string into tokens 41//--------- stringtok ----------------------------------
41 // Usage check: 42// Breaks a string into tokens
42 // http://gcc.gnu.org/onlinedocs/libstdc++/21_strings/howto.html#3 43// Usage check:
43 // Taken from an example at: 44// http://gcc.gnu.org/onlinedocs/libstdc++/21_strings/howto.html#3
44 // http://gcc.gnu.org/onlinedocs/libstdc++/21_strings/stringtok_std_h.txt 45// Taken from an example at:
45 //-------------------------------------------------- 46// http://gcc.gnu.org/onlinedocs/libstdc++/21_strings/stringtok_std_h.txt
46 template <typename Container> 47//--------------------------------------------------
47 static void 48template <typename Container>
48 stringtok (Container &container, std::string const &in, 49static void
49 const char * const delimiters = " \t\n") 50stringtok (Container &container, std::string const &in,
50 { 51 const char * const delimiters = " \t\n")
51 const std::string::size_type len = in.length(); 52{
52 std::string::size_type i = 0; 53 const std::string::size_type len = in.length();
54 std::string::size_type i = 0;
55
56 while ( i < len ) {
57 // eat leading whitespace
58 i = in.find_first_not_of(delimiters, i);
59 if (i == std::string::npos)
60 return; // nothing left but white space
53 61
54 while ( i < len ) { 62 // find the end of the token
55 // eat leading whitespace 63 std::string::size_type j = in.find_first_of(delimiters, i);
56 i = in.find_first_not_of(delimiters, i);
57 if (i == std::string::npos)
58 return; // nothing left but white space
59 64
60 // find the end of the token 65 // push token
61 std::string::size_type j = in.find_first_of(delimiters, i); 66 if (j == std::string::npos) {
67 container.push_back(in.substr(i));
68 return;
69 } else
70 container.push_back(in.substr(i, j-i));
62 71
63 // push token 72 // set up for next loop
64 if (j == std::string::npos) { 73 i = j + 1;
65 container.push_back(in.substr(i)); 74 }
66 return; 75}
67 } else
68 container.push_back(in.substr(i, j-i));
69 76
70 // set up for next loop 77};//end namespace StringUtil
71 i = j + 1;
72 }
73 }
74};
75 78
76 79
77 80
78#endif // _STRINGUTIL_HH_ 81#endif // STRINGUTIL_HH