~ubuntu-branches/ubuntu/lucid/kdeadmin/lucid-proposed

« back to all changes in this revision

Viewing changes to lilo-config/common/String.cc

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2010-02-27 21:36:45 UTC
  • mfrom: (1.2.40 upstream)
  • Revision ID: james.westby@ubuntu.com-20100227213645-ronst6mahkgf6j9w
Tags: 4:4.4.1-0ubuntu1
* New upstream release
  - Bump build-depends
  - Update kcron.install

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* String.cc
2
 
**
3
 
** Copyright (C) 2000,2001 by Bernhard Rosenkraenzer
4
 
**
5
 
** Contributions by A. Seigo and W. Bastian.
6
 
**
7
 
*/
8
 
 
9
 
/*
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.
14
 
**
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.
19
 
**
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.
24
 
*/
25
 
 
26
 
/*
27
 
** Bug reports and questions can be sent to <kde-devel@kde.org>
28
 
*/
29
 
 
30
 
#define _GNU_SOURCE 1
31
 
#include "String.h"
32
 
#include <features.h>
33
 
#include <string.h>
34
 
#include <string>
35
 
#include <stdio.h>
36
 
#include <regex.h>
37
 
#include <stdlib.h>
38
 
 
39
 
using namespace std;
40
 
 
41
 
void String::sprintf(const char *format, ...)
42
 
{
43
 
        va_list arg;
44
 
        va_start(arg, format);
45
 
        char *buf=0;
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;
49
 
                int bufsiz=1024;
50
 
                while(size==-1) {
51
 
                        buf=(char *) malloc(bufsiz);
52
 
                        size=vsnprintf(buf, 0, format, arg);
53
 
                        bufsiz+=1024;
54
 
                        free(buf);
55
 
                }
56
 
        }
57
 
        buf=(char *) malloc(size+1);
58
 
        vsnprintf(buf, size+1, format, arg);
59
 
        string str=buf;
60
 
        *this=buf;
61
 
        va_end(arg);
62
 
        free(buf);
63
 
        return;
64
 
}
65
 
bool String::readfile(String filename)
66
 
{
67
 
        FILE *f=fopen(filename, "r");
68
 
        if(!f)
69
 
                return false;
70
 
 
71
 
        string str="";
72
 
        char *buf=(char *) malloc(1024);
73
 
        while(!feof(f) && !ferror(f)) {
74
 
                if(!fgets(buf, 1024, f))
75
 
                        continue;
76
 
                str += buf;
77
 
        };
78
 
        *this=buf;
79
 
        free(buf);
80
 
        fclose(f);
81
 
        return true;
82
 
}
83
 
char *String::cstr() const
84
 
{
85
 
        char *a=new char[size()+1];
86
 
        a[size()]=0;
87
 
        strncpy(a, data(), size());
88
 
        return a;
89
 
}
90
 
bool String::cmp(char const * const s) const
91
 
{
92
 
        if(size() != strlen(s))
93
 
                return false;
94
 
        return (strncmp(data(), s, size())==0);
95
 
}
96
 
bool String::casecmp(char const * const s) const
97
 
{
98
 
        if(size() != strlen(s))
99
 
                return false;
100
 
        return (strncasecmp(data(), s, size())==0);
101
 
}
102
 
bool String::contains(String const &s, bool cs) const
103
 
{
104
 
        if(cs)
105
 
                if(strstr(cstr(), s.cstr()))
106
 
                        return true;
107
 
                else
108
 
                        return false;
109
 
        else
110
 
                if(strcasestr(cstr(), s.cstr()))
111
 
                        return true;
112
 
                else
113
 
                        return false;
114
 
}
115
 
int String::locate(String const &s, bool cs, unsigned int startat) const
116
 
{
117
 
        if(startat>=size())
118
 
                return -1;
119
 
 
120
 
        char *s0=cstr(), *s1=s.cstr(), *s2;
121
 
        int r;
122
 
        if(cs)
123
 
                s2=strstr(s0+startat, s1);
124
 
        else
125
 
                s2=strcasestr(s0+startat, s1);
126
 
        if(s2==NULL) {
127
 
                delete [] s0;
128
 
                delete [] s1;
129
 
                return -1;
130
 
        }
131
 
        r=(s2-s0);
132
 
        if(startat>0) r++;
133
 
        delete [] s0;
134
 
        delete [] s1;
135
 
        return r;
136
 
}
137
 
String const String::operator +(char const &s) {
138
 
        char a[2];
139
 
        a[0]=s;
140
 
        a[1]=0;
141
 
        String st=cstr();
142
 
        st+=a;
143
 
        return st;
144
 
}
145
 
String const String::operator +(char const * const s) {
146
 
        String st=cstr();
147
 
        st += s;
148
 
        return st;
149
 
}
150
 
bool String::operator ==(char s) {
151
 
        if(size()==1 && cstr()[0]==s)
152
 
                return true;
153
 
        else
154
 
                return false;
155
 
}
156
 
bool String::operator !=(char s) {
157
 
        if(size()!=1 || cstr()[0]!=s)
158
 
                return true;
159
 
        else
160
 
                return false;
161
 
}
162
 
String String::simplifyWhiteSpace() const {
163
 
        char *s=cstr();
164
 
        for(unsigned int i=0; i<size(); i++)
165
 
                if(isspace(s[i]))
166
 
                        s[i]=' ';
167
 
        while(*s==' ')
168
 
                strcpy(s, s+1);
169
 
        int l = strlen(s);
170
 
        while(l && (s[l-1]==' '))
171
 
                s[--l]=0;
172
 
                
173
 
        while(strstr(s, "  "))
174
 
                strcpy(strstr(s, "  "), strstr(s, "  ")+1);
175
 
        return s;
176
 
}
177
 
String String::left(unsigned int num) const
178
 
{
179
 
        if(num==0) return "";
180
 
        char *s=cstr();
181
 
        if(size()<=num)
182
 
                return s;
183
 
        s[num]=0;
184
 
        return s;
185
 
}
186
 
String String::right(unsigned int num) const
187
 
{
188
 
        if(num==0) return "";
189
 
        char *s=cstr();
190
 
        if(size()<=num)
191
 
                return s;
192
 
        strcpy(s, s+strlen(s)-num);
193
 
        return s;
194
 
}
195
 
String String::mid(unsigned int start, unsigned int num) const
196
 
{
197
 
        if(start>=size())
198
 
                return "";
199
 
        char *s=cstr();
200
 
        start--;
201
 
        if(start>0)
202
 
                strcpy(s, s+start);
203
 
        if(num>0 && num<=strlen(s))
204
 
                s[num]=0;
205
 
        return s;
206
 
}
207
 
String &String::regex(String const &expr, bool cs) const
208
 
{
209
 
        regex_t regexp;
210
 
        int err;
211
 
        regmatch_t reg[1];
212
 
        String *ret=new String("");
213
 
        if((err=regcomp(&regexp, expr, cs?REG_EXTENDED:REG_EXTENDED|REG_ICASE))) {
214
 
                regfree(&regexp);
215
 
                return *ret;
216
 
        }
217
 
        err=regexec(&regexp, cstr(), 1, reg, 0);
218
 
        regfree(&regexp);
219
 
        if(err)
220
 
                return *ret;
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;
224
 
                delete ret;
225
 
                ret=new String(s);
226
 
                free(s);
227
 
        }
228
 
        return *ret;
229
 
}
230
 
String &String::replace(String const &what, String const &with, bool all) const
231
 
{
232
 
        String *result;
233
 
        if(!contains(what)) {
234
 
                result=new String(*this);
235
 
                return *result;
236
 
        }
237
 
        result=new String;
238
 
        *result=left(locate(what));
239
 
        *result+=with;
240
 
        if(!all) {
241
 
                *result+=right(size()-locate(what)-what.size());
242
 
        } else {
243
 
                unsigned int start=locate(what)+what.size()+1;
244
 
                int loc;
245
 
                while((loc=locate(what, true, start+1))!=-1) {
246
 
                        *result+=mid(start, loc-start);
247
 
                        *result+=with;
248
 
                        start=locate(what, true, start)+what.size();
249
 
                }
250
 
                if(size()>start)
251
 
                        *result+=right(size()-start+1);
252
 
        }
253
 
        return *result;
254
 
}
255
 
String String::escapeForRegExp(String const &s)
256
 
{
257
 
        static const char meta[] = "$()*+.?[\\]^{|}";
258
 
        String quoted = s;
259
 
        int i = 0;
260
 
 
261
 
        while ( i < (int) quoted.length() ) {
262
 
                if ( strchr(meta, quoted.at(i)) != 0 )
263
 
                        quoted.insert( i++, "\\" );
264
 
                i++;
265
 
        }
266
 
        return quoted;
267
 
}
268
 
StringList::StringList(String const &s)
269
 
{
270
 
        clear();
271
 
        char *st=strdup((char const * const)s);
272
 
        char *tok;
273
 
        char *line=strtok_r(st, "\n", &tok);
274
 
        while(line) {
275
 
                if(line[strlen(line)-1]=='\r') // Handle sucking OSes
276
 
                        line[strlen(line)-1]=0;
277
 
                insert(end(), line);
278
 
                line=strtok_r(NULL, "\n", &tok);
279
 
        }
280
 
        free(st);
281
 
}
282
 
 
283
 
StringList::StringList(char **strs, int num)
284
 
{
285
 
        clear();
286
 
        if(num>=0) {
287
 
                for(int i=0; i<num; i++)
288
 
                        insert(end(), strs[i]);
289
 
        } else {
290
 
                for(int i=0; strs[i]!=NULL; i++)
291
 
                        insert(end(), strs[i]);
292
 
        }
293
 
}
294
 
 
295
 
bool StringList::readfile(String const &filename)
296
 
{
297
 
        clear();
298
 
        FILE *f=fopen(filename, "r");
299
 
        if(!f)
300
 
                return false;
301
 
        char *buf=(char *) malloc(1024);
302
 
        while(!feof(f) && !ferror(f)) {
303
 
                if(!fgets(buf, 1024, f))
304
 
                        continue;
305
 
                while(strlen(buf) && (buf[strlen(buf)-1]=='\n' || buf[strlen(buf)-1]=='\r'))
306
 
                        buf[strlen(buf)-1]=0;
307
 
                insert(end(), buf);
308
 
        };
309
 
        free(buf);
310
 
        fclose(f);
311
 
        return true;
312
 
}
313
 
bool StringList::writefile(String const &filename) const
314
 
{
315
 
        FILE *f=fopen(filename, "w");
316
 
        if(!f)
317
 
                return false;
318
 
        for(const_iterator it=begin(); it!=end(); it++) {
319
 
                fputs(*it, f);
320
 
                fputs("\n", f);
321
 
        }
322
 
        fclose(f);
323
 
        return true;
324
 
}
325
 
void StringList::operator +=(StringList const &s)
326
 
{
327
 
        for(const_iterator it=s.begin(); it!=s.end(); it++)
328
 
                insert(end(), *it);
329
 
}
330
 
void StringList::operator +=(StringList const * const s)
331
 
{
332
 
        for(const_iterator it=s->begin(); it!=s->end(); it++)
333
 
                insert(end(), *it);
334
 
}
335
 
bool StringList::contains(String const &s) const
336
 
{
337
 
        for(const_iterator it=begin(); it!=end(); it++)
338
 
                if(*it == s)
339
 
                        return true;
340
 
        return false;
341
 
}
342
 
void StringList::remove(String const &s)
343
 
{
344
 
        bool done=false;
345
 
        for(iterator it=begin(); !done && it!=end(); it++)
346
 
                if(*it==s) {
347
 
                        erase(it);
348
 
                        done=true;
349
 
                }
350
 
}
351
 
String const &StringList::grep(String const &s) const
352
 
{
353
 
        for(const_iterator it=begin(); it!=end(); it++)
354
 
                if(!(*it).regex(s).empty())
355
 
                        return *it;
356
 
        String *r=new String;
357
 
        return *r;
358
 
}
359
 
int __stringlist_compare(const void *a, const void *b)
360
 
{
361
 
        if(a==0 && b==0)
362
 
                return 0;
363
 
        else if(a==0)
364
 
                return 1;
365
 
        else if(b==0)
366
 
                return -1;
367
 
        else
368
 
                return strcmp((const char *)a,(const char *)b);
369
 
}
370
 
int __stringlist_compare_noncs(const void *a, const void *b)
371
 
{
372
 
        if(a==0 && b==0)
373
 
                return 0;
374
 
        else if(a==0)
375
 
                return 1;
376
 
        else if(b==0)
377
 
                return -1;
378
 
        else
379
 
                return strcasecmp((const char *)a,(const char *)b);
380
 
}
381
 
void StringList::sort(bool cs)
382
 
{
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();
387
 
        if(cs)
388
 
                qsort(strings, s, sizeof(char*),  __stringlist_compare);
389
 
        else
390
 
                qsort(strings, s, sizeof(char*), __stringlist_compare_noncs);
391
 
        clear();
392
 
        for(i=0; i<s; i++) {
393
 
                insert(end(), strings[i]);
394
 
                delete [] strings[i];
395
 
        }
396
 
        delete [] strings;
397
 
}
398
 
StringList::operator String() const
399
 
{
400
 
        String s="";
401
 
        for(const_iterator it=begin(); it!=end(); it++) {
402
 
                s+=(*it);
403
 
                if(s.right()!=String("\n") && s.right()!=String("\r"))  //krazy:exclude=duoblequote_chars
404
 
                        s+="\n";        //krazy:exclude=duoblequote_chars
405
 
        }
406
 
        return s;
407
 
}
408
 
 
409
 
ostream &operator <<(ostream &os, String const &s)
410
 
{
411
 
        if(!s.empty())
412
 
                os << (char const * const) s;
413
 
        return os;
414
 
}
415
 
ostream &operator <<(ostream &os, String const *s)
416
 
{
417
 
        if(!s->empty())
418
 
                os << (char const * const) *s;
419
 
        return os;
420
 
}
421
 
ostream &operator <<(ostream &os, StringList const &s)
422
 
{
423
 
        for(StringList::const_iterator it=s.begin(); it!=s.end(); it++) {
424
 
                os << *it;
425
 
                if((*it).right()!=String("\n") && (*it).right()!=String("\r"))
426
 
                        os << endl;
427
 
        }
428
 
        return os;
429
 
}
430
 
ostream &operator <<(ostream &os, StringList const *s)
431
 
{
432
 
        for(StringList::const_iterator it=s->begin(); it!=s->end(); it++) {
433
 
                os << *it;
434
 
                if((*it).right()!=String("\n") && (*it).right()!=String("\r"))
435
 
                        os << endl;
436
 
        }
437
 
        return os;
438
 
}