ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/branches/new_design/OOPSE-4/src/utils/StringUtils.cpp
Revision: 1824
Committed: Thu Dec 2 03:12:25 2004 UTC (19 years, 9 months ago) by tim
File size: 3167 byte(s)
Log Message:
replace misuse of using namespace std in header files

File Contents

# User Rev Content
1 tim 1492 #include "utils/StringUtils.hpp"
2 gezelter 1490
3 gezelter 1614 namespace oopse {
4 tim 1824 std::string UpperCase(const std::string& S) {
5     std::string uc = S;
6 gezelter 1490 unsigned int n = uc.size();
7     for (unsigned int j = 0; j < n; j++) {
8     char sj = uc[j];
9     if (sj >= 'a' && sj <= 'z') uc[j] = (char)(sj - ('a' - 'A'));
10     }
11     return uc;
12     }
13    
14 tim 1824 std::string LowerCase(const std::string& S) {
15     std::string lc = S;
16 gezelter 1490 unsigned int n = lc.size();
17     for (unsigned int j = 0; j < n; j++) {
18     char sj = lc[j];
19     if (sj >= 'A' && sj <= 'Z') lc[j] = (char)(sj + ('a' - 'A'));
20     }
21     return lc;
22     }
23 gezelter 1590
24 chrisfen 1598 char* trimSpaces(char *str) {
25     size_t len;
26     char *right, *left;
27    
28     if (strlen(str) == 0) return(str);
29    
30     /* Trim whitespace from left side */
31     for (left = str; isspace(*left); left++);
32    
33     /* Trim whitespace from right side */
34     if ((len = strlen(left)))
35     {
36     right = left + (len - 1);
37    
38     while (isspace(*right))
39     {
40     *right = '\0';
41     right--;
42     }
43     }
44    
45     /* Only do the str copy if there were spaces to the left */
46     if (left != str)
47     strcpy(str, left);
48    
49     return (str);
50     }
51    
52 tim 1824 int findBegin(std::istream &theStream, char* startText ){
53 gezelter 1590 const int MAXLEN = 1024;
54     char readLine[MAXLEN];
55     int foundText = 0;
56     int lineNum;
57     char* the_token;
58     char* eof_test;
59    
60     // rewind the stream
61 tim 1824 theStream.seekg (0, std::ios::beg);
62 gezelter 1590 lineNum = 0;
63    
64     if (!theStream.eof()) {
65     theStream.getline(readLine, MAXLEN);
66     lineNum++;
67     } else {
68     printf( "Error fast forwarding stream: stream is empty.\n");
69     return -1;
70     }
71    
72     while ( !foundText ) {
73    
74     if (theStream.eof()) {
75     printf("Error fast forwarding stream at line %d: "
76     "stream ended unexpectedly.\n", lineNum);
77     return -1;
78     }
79 chrisfen 1598
80 gezelter 1590 the_token = strtok( readLine, " ,;\t" );
81 chrisfen 1598 if ( the_token != NULL)
82 gezelter 1600 if (!strcasecmp("begin", the_token)) {
83 chrisfen 1598 the_token = strtok( NULL, " ,;\t" );
84     if ( the_token != NULL){
85 gezelter 1600 foundText = !strcasecmp( startText, the_token );
86 chrisfen 1598 }
87     }
88    
89 gezelter 1590 if (!foundText) {
90     if (!theStream.eof()) {
91     theStream.getline(readLine, MAXLEN);
92     lineNum++;
93     } else {
94     printf( "Error fast forwarding stream at line %d: "
95     "stream ended unexpectedly.\n", lineNum);
96 chrisfen 1598 return -1;
97 gezelter 1590 }
98     }
99     }
100     return lineNum;
101     }
102    
103     int countTokens(char *line, char *delimiters) {
104     /* PURPOSE: RETURN A COUNT OF THE NUMBER OF TOKENS ON THE LINE. */
105    
106     char *working_line; /* WORKING COPY OF LINE. */
107     int ntokens; /* NUMBER OF TOKENS FOUND IN LINE. */
108     char *strtok_ptr; /* POINTER FOR STRTOK. */
109    
110     strtok_ptr= working_line= strdup(line);
111    
112     ntokens=0;
113     while (strtok(strtok_ptr,delimiters)!=NULL)
114     {
115     ntokens++;
116     strtok_ptr=NULL;
117     }
118    
119     free(working_line);
120     return(ntokens);
121     }
122    
123     int isEndLine(char *line) {
124     char *working_line;
125     char *foo;
126    
127     working_line = strdup(line);
128    
129     foo = strtok(working_line, " ,;\t");
130    
131 gezelter 1600 if (foo != NULL) {
132    
133     if (!strcasecmp(foo, "end")) return 1;
134    
135     }
136 gezelter 1590
137     return 0;
138     }
139 tim 1792
140 tim 1822
141     std::string getPrefix(const std::string& str) {
142     return str.substr(0, str.rfind('.'));
143 gezelter 1614 }
144 tim 1822
145     std::string getSuffix(const std::string& str) {
146     return str.substr(0, str.find('.'));
147     }
148    
149     }