1
// StringUtil.cc for fluxbox
2
// Copyright (c) 2001 - 2002 Henrik Kinnunen (fluxgen@linuxmail.org)
4
// Permission is hereby granted, free of charge, to any person obtaining a
5
// copy of this software and associated documentation files (the "Software"),
6
// to deal in the Software without restriction, including without limitation
7
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
// and/or sell copies of the Software, and to permit persons to whom the
9
// Software is furnished to do so, subject to the following conditions:
11
// The above copyright notice and this permission notice shall be included in
12
// all copies or substantial portions of the Software.
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20
// DEALINGS IN THE SOFTWARE.
22
//�$Id: StringUtil.cc,v 1.7 2002/01/27 12:46:28 fluxgen Exp $
24
#include "StringUtil.hh"
34
//------- strdup ------------------------
36
//----------------------------------------
37
char *StringUtil::strdup(const char *s) {
38
int l = strlen(s) + 1;
39
char *n = new char[l];
44
//------- strcasestr --------------
45
// Tries to find a string in another and
46
// ignoring the case of the characters
47
// Returns 0 on success else pointer to str.
49
//---------------------------------
50
const char * StringUtil::strcasestr(const char *str, const char *ptn) {
53
for(s2=str, p2=ptn; ; s2++,p2++) {
55
if (toupper(*s2) != toupper(*p2)) break;
61
//------------- expandFilename ----------------------
62
// if ~ then expand it to home of user
63
// returns expanded filename
64
// (note: the function creates new memory for the string)
65
//---------------------------------------------------
66
char *StringUtil::expandFilename(const char *filename) {
68
auto_ptr<char> retval( new char[strlen(filename)+strlen(getenv("HOME"))+2]);
69
if (filename[0]=='~') {
70
strcpy(retval.get(), getenv("HOME"));
71
strcat(retval.get(), &filename[1]);
73
return StringUtil::strdup(filename); //return unmodified value
75
return StringUtil::strdup(retval.get()); //return modified value
78
//------------- getStringBetween -----------
79
// Parses a string between "first" and "last" characters
80
// and ignoring ok_chars as whitespaces. The value is
82
// Returns negative value on error and this value is the position
83
// in the in-string where the error occured.
84
// Returns positive value on success and this value is
85
// for the position + 1 in the in-string where the "last"-char value
87
//------------------------------------------
88
int StringUtil::getStringBetween(string& out, const char *instr, const char first, const char last,
89
const char *ok_chars) {
94
string::size_type i = 0,
95
total_add=0; //used to add extra if there is a \last to skip
98
// eat leading whitespace
99
i = in.find_first_not_of(ok_chars);
100
if (i == string::npos)
101
return -in.size(); // nothing left but whitespace
104
return -i; //return position to error
106
// find the end of the token
107
string::size_type j = i;
109
j = in.find_first_of(last, j+1);
111
return -in.size(); //send negative size
113
//we found the last char, check so it doesn't have a '\' before
114
if (j>1 && in[j-1] != '\\')
117
in.erase(j-1, 1); //remove the�'\'
119
total_add++; //save numchars removed so we can calculate totalpos
123
out = in.substr(i+1, j-i-1); //copy the string between first and last
124
//return value to last character
125
return (j+1+total_add);