Various changes to preferences object, file loading, and error logging.
[jben.git] / string_utils.h
blob840004f3494c5922a37d3ada5d37e8e1e46f9c1a
1 /*
2 Project: J-Ben
3 Author: Paul Goins
4 Website: http://www.vultaire.net/software/jben/
5 License: GNU General Public License (GPL) version 2
6 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt)
8 File: string_utils.h
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>
24 #ifndef string_utils_h
25 #define string_utils_h
27 #include <string>
28 #include <list>
29 using namespace std;
31 bool GetIndexAfterParens(const string& s, size_t indexParen, size_t& indexNext,
32 char cOpenParen='(', char cCloseParen=')');
33 bool GetIndexBeforeParens(const string& s, size_t indexParen, size_t& indexNext,
34 char cOpenParen='(', char cCloseParen=')');
35 string ToUpper(string src);
36 wstring ToUpper(wstring src);
37 string ToLower(string src);
38 wstring ToLower(wstring src);
39 string Trim(string);
40 wstring Trim(wstring);
42 /* Template functions */
43 template <class t>
44 list< basic_string<t> > StrTokenize
45 (const basic_string<t>& src,
46 const t *delimiters,
47 bool emptyTokens = false,
48 size_t maxTokens=0) {
49 list< basic_string<t> > l;
50 size_t start = 0;
51 size_t end = src.find_first_of(delimiters, start);
53 while(end!=basic_string<t>::npos
54 && (maxTokens==0 || l.size()<maxTokens-1)) {
55 if(emptyTokens || (start!=end)) {
56 l.push_back(
57 src.substr(start, end-start));
59 start = end + 1;
60 end = src.find(delimiters, start);
63 if(start!=basic_string<t>::npos) {
64 basic_string<t> lastStr = src.substr(start);
65 if(lastStr.length()>0) l.push_back(lastStr);
67 return l;
70 /* Substring replace function.
71 "from" string MUST have length > 0. */
72 template <class t>
73 basic_string<t> TextReplace(const basic_string<t>& src,
74 const basic_string<t>& from,
75 const basic_string<t>& to) {
76 basic_string<t> result;
77 size_t start=0;
78 size_t end;
79 if(from.size()<1) return src;
81 end = src.find(from, start);
82 while(end!=basic_string<t>::npos) {
83 /* Append string particle, if present */
84 if(start!=end) {
85 result.append(src.substr(start, end-start));
87 /* Append "to" string */
88 result.append(to);
89 /* Skip over the "from" string */
90 start = end + from.length();
92 end = src.find(from, start);
95 if(start!=basic_string<t>::npos) {
96 basic_string<t> lastStr = src.substr(start);
97 if(lastStr.length()>0) result.append(lastStr);
100 return result;
103 /* t must be a class based upon std::basic_string, like string or wstring. */
104 template <class t>
105 t ListToString(const list<t>& l, const t& separator) {
106 t target;
108 /* Append the first entry */
109 typename list<t>::const_iterator li = l.begin();
110 if(li!=l.end()) target.append(*li);
112 /* Append remaining entries */
113 for(li++; li!=l.end(); li++) {
114 target.append(separator);
115 target.append(*li);
118 return target;
121 #endif