Various changes to preferences object, file loading, and error logging.
[jben.git] / string_utils.cpp
blob6d5e9fc0521c2aac3e314e32438bc59086178bff
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.cpp
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 #include "string_utils.h"
25 #include <cctype>
26 #include <cwctype>
27 using namespace std;
29 bool GetIndexAfterParens(const string& s, size_t indexParen, size_t& indexNext,
30 char cOpenParen, char cCloseParen) {
31 int parenCount = 1;
32 size_t index = indexParen;
33 string sParenChars(" ");
34 sParenChars[0]=cOpenParen;
35 sParenChars[1]=cCloseParen;
37 while(parenCount>0) {
38 /* Skip "index" ahead to after the closing parenthesis, or to the
39 end if not found. */
40 index = s.find_first_of(sParenChars, index+1);
41 if(index==string::npos) break;
42 if(s.at(index)==cOpenParen)
43 parenCount++;
44 else {
45 parenCount--;
46 if(parenCount==0) break;
50 /* If the end of the string is found before parenthesis parsing is
51 finished, then set the break position to the end of the string. */
52 if(index==string::npos) return false;
54 indexNext = index+1;
55 return true;
58 bool GetIndexBeforeParens(const string& s, size_t indexParen, size_t& indexNext,
59 char cOpenParen, char cCloseParen) {
60 int parenCount = 1;
61 size_t index = indexParen-1;
62 string sParenChars(" ");
63 sParenChars[0]=cOpenParen;
64 sParenChars[1]=cCloseParen;
66 while(parenCount>0) {
67 /* Skip "index" ahead to after the closing parenthesis, or to the
68 end if not found. */
69 index = s.find_last_of(sParenChars, index);
70 if(index==string::npos) break;
71 if(s.at(index)==cCloseParen)
72 parenCount++;
73 else {
74 parenCount--;
75 if(parenCount==0) break;
79 /* If the end of the string is found before parenthesis parsing is
80 finished, then set the break position to the end of the string. */
81 if(index==string::npos) return false;
83 indexNext = index-1;
84 return true;
87 string ToUpper(string src) {
88 for(string::iterator i = src.begin(); i != src.end(); i++) {
89 *i = toupper(*i);
91 return src;
94 wstring ToUpper(wstring src) {
95 for(wstring::iterator i = src.begin(); i != src.end(); i++) {
96 *i = towupper(*i);
98 return src;
101 string ToLower(string src) {
102 for(string::iterator i = src.begin(); i != src.end(); i++) {
103 *i = tolower(*i);
105 return src;
108 wstring ToLower(wstring src) {
109 for(wstring::iterator i = src.begin(); i != src.end(); i++) {
110 *i = towlower(*i);
112 return src;
115 string Trim(string src) {
116 int len = src.length();
117 int begin, end;
119 for(begin=0; begin<len; begin++) {
120 if(!isspace(src[begin])) break;
122 if(begin==len) return "";
124 for(end=len-1; end>=0; end--) {
125 if(!isspace(src[end])) break;
128 return src.substr(begin, end+1 - begin);
131 wstring Trim(wstring src) {
132 int len = src.length();
133 int begin, end;
135 for(begin=0; begin<len; begin++) {
136 if(!iswspace(src[begin])) break;
138 if(begin==len) return L"";
140 for(end=len-1; end>=0; end--) {
141 if(!iswspace(src[end])) break;
144 return src.substr(begin, end+1 - begin);