3
** Copyright (C) 2000,2001 by Bernhard Rosenkraenzer
5
** Contributions by A. Seigo and W. Bastian.
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 in a file called COPYING; if not, write to
22
** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23
** MA 02110-1301, USA.
27
** Bug reports and questions can be sent to <kde-devel@kde.org>
41
void String::sprintf(const char *format, ...)
44
va_start(arg, format);
46
int size=vsnprintf(buf, 0, format, arg);
47
if(size==-1) { /* ARGH!!! */
48
cerr << "WARNING: Your C library (libc) does not conform to the ISO C99 standard!" << endl << "Consider upgrading to glibc 2.1 or higher!" << endl;
51
buf=(char *) malloc(bufsiz);
52
size=vsnprintf(buf, 0, format, arg);
57
buf=(char *) malloc(size+1);
58
vsnprintf(buf, size+1, format, arg);
65
bool String::readfile(String filename)
67
FILE *f=fopen(filename, "r");
72
char *buf=(char *) malloc(1024);
73
while(!feof(f) && !ferror(f)) {
74
if(!fgets(buf, 1024, f))
83
char *String::cstr() const
85
char *a=new char[size()+1];
87
strncpy(a, data(), size());
90
bool String::cmp(char const * const s) const
92
if(size() != strlen(s))
94
return (strncmp(data(), s, size())==0);
96
bool String::casecmp(char const * const s) const
98
if(size() != strlen(s))
100
return (strncasecmp(data(), s, size())==0);
102
bool String::contains(String const &s, bool cs) const
105
if(strstr(cstr(), s.cstr()))
110
if(strcasestr(cstr(), s.cstr()))
115
int String::locate(String const &s, bool cs, unsigned int startat) const
120
char *s0=cstr(), *s1=s.cstr(), *s2;
123
s2=strstr(s0+startat, s1);
125
s2=strcasestr(s0+startat, s1);
137
String const String::operator +(char const &s) {
145
String const String::operator +(char const * const s) {
150
bool String::operator ==(char s) {
151
if(size()==1 && cstr()[0]==s)
156
bool String::operator !=(char s) {
157
if(size()!=1 || cstr()[0]!=s)
162
String String::simplifyWhiteSpace() const {
164
for(unsigned int i=0; i<size(); i++)
170
while(l && (s[l-1]==' '))
173
while(strstr(s, " "))
174
strcpy(strstr(s, " "), strstr(s, " ")+1);
177
String String::left(unsigned int num) const
179
if(num==0) return "";
186
String String::right(unsigned int num) const
188
if(num==0) return "";
192
strcpy(s, s+strlen(s)-num);
195
String String::mid(unsigned int start, unsigned int num) const
203
if(num>0 && num<=strlen(s))
207
String &String::regex(String const &expr, bool cs) const
212
String *ret=new String("");
213
if((err=regcomp(®exp, expr, cs?REG_EXTENDED:REG_EXTENDED|REG_ICASE))) {
217
err=regexec(®exp, cstr(), 1, reg, 0);
221
if(reg[0].rm_so!=-1) {
222
char *s=strdup(cstr()+reg[0].rm_so);
223
s[reg[0].rm_eo-reg[0].rm_so]=0;
230
String &String::replace(String const &what, String const &with, bool all) const
233
if(!contains(what)) {
234
result=new String(*this);
238
*result=left(locate(what));
241
*result+=right(size()-locate(what)-what.size());
243
unsigned int start=locate(what)+what.size()+1;
245
while((loc=locate(what, true, start+1))!=-1) {
246
*result+=mid(start, loc-start);
248
start=locate(what, true, start)+what.size();
251
*result+=right(size()-start+1);
255
String String::escapeForRegExp(String const &s)
257
static const char meta[] = "$()*+.?[\\]^{|}";
261
while ( i < (int) quoted.length() ) {
262
if ( strchr(meta, quoted.at(i)) != 0 )
263
quoted.insert( i++, "\\" );
268
StringList::StringList(String const &s)
271
char *st=strdup((char const * const)s);
273
char *line=strtok_r(st, "\n", &tok);
275
if(line[strlen(line)-1]=='\r') // Handle sucking OSes
276
line[strlen(line)-1]=0;
278
line=strtok_r(NULL, "\n", &tok);
283
StringList::StringList(char **strs, int num)
287
for(int i=0; i<num; i++)
288
insert(end(), strs[i]);
290
for(int i=0; strs[i]!=NULL; i++)
291
insert(end(), strs[i]);
295
bool StringList::readfile(String const &filename)
298
FILE *f=fopen(filename, "r");
301
char *buf=(char *) malloc(1024);
302
while(!feof(f) && !ferror(f)) {
303
if(!fgets(buf, 1024, f))
305
while(strlen(buf) && (buf[strlen(buf)-1]=='\n' || buf[strlen(buf)-1]=='\r'))
306
buf[strlen(buf)-1]=0;
313
bool StringList::writefile(String const &filename) const
315
FILE *f=fopen(filename, "w");
318
for(const_iterator it=begin(); it!=end(); it++) {
325
void StringList::operator +=(StringList const &s)
327
for(const_iterator it=s.begin(); it!=s.end(); it++)
330
void StringList::operator +=(StringList const * const s)
332
for(const_iterator it=s->begin(); it!=s->end(); it++)
335
bool StringList::contains(String const &s) const
337
for(const_iterator it=begin(); it!=end(); it++)
342
void StringList::remove(String const &s)
345
for(iterator it=begin(); !done && it!=end(); it++)
351
String const &StringList::grep(String const &s) const
353
for(const_iterator it=begin(); it!=end(); it++)
354
if(!(*it).regex(s).empty())
356
String *r=new String;
359
int __stringlist_compare(const void *a, const void *b)
368
return strcmp((const char *)a,(const char *)b);
370
int __stringlist_compare_noncs(const void *a, const void *b)
379
return strcasecmp((const char *)a,(const char *)b);
381
void StringList::sort(bool cs)
383
unsigned int i=0, s=size();
384
char **strings=new char*[s];
385
for(const_iterator it=begin(); it!=end(); it++)
386
strings[i++]=(*it).cstr();
388
qsort(strings, s, sizeof(char*), __stringlist_compare);
390
qsort(strings, s, sizeof(char*), __stringlist_compare_noncs);
393
insert(end(), strings[i]);
394
delete [] strings[i];
398
StringList::operator String() const
401
for(const_iterator it=begin(); it!=end(); it++) {
403
if(s.right()!=String("\n") && s.right()!=String("\r")) //krazy:exclude=duoblequote_chars
404
s+="\n"; //krazy:exclude=duoblequote_chars
409
ostream &operator <<(ostream &os, String const &s)
412
os << (char const * const) s;
415
ostream &operator <<(ostream &os, String const *s)
418
os << (char const * const) *s;
421
ostream &operator <<(ostream &os, StringList const &s)
423
for(StringList::const_iterator it=s.begin(); it!=s.end(); it++) {
425
if((*it).right()!=String("\n") && (*it).right()!=String("\r"))
430
ostream &operator <<(ostream &os, StringList const *s)
432
for(StringList::const_iterator it=s->begin(); it!=s->end(); it++) {
434
if((*it).right()!=String("\n") && (*it).right()!=String("\r"))