aboutsummaryrefslogtreecommitdiff
path: root/src/StringUtil.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/StringUtil.cc')
-rw-r--r--src/StringUtil.cc76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/StringUtil.cc b/src/StringUtil.cc
new file mode 100644
index 0000000..036a4f4
--- /dev/null
+++ b/src/StringUtil.cc
@@ -0,0 +1,76 @@
1// StringUtil.cc for fluxbox
2// Copyright (c) 2001 - 2002 Henrik Kinnunen (fluxgen@linuxmail.org)
3//
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:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
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.
21
22// $Id: StringUtil.cc,v 1.5 2002/01/09 14:11:20 fluxgen Exp $
23
24#include "StringUtil.hh"
25
26#include <string>
27#include <cstdio>
28#include <cstdlib>
29#include <cctype>
30#include <memory>
31
32using namespace std;
33
34//------- strdup ------------------------
35//TODO: comment this
36//----------------------------------------
37char *StringUtil::strdup(const char *s) {
38 int l = strlen(s) + 1;
39 char *n = new char[l];
40 strncpy(n, s, l);
41 return n;
42}
43
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.
48// TODO: comment this
49//---------------------------------
50const char * StringUtil::strcasestr(const char *str, const char *ptn) {
51 const char *s2, *p2;
52 for( ; *str; str++) {
53 for(s2=str, p2=ptn; ; s2++,p2++) {
54 if (!*p2) return str;
55 if (toupper(*s2) != toupper(*p2)) break;
56 }
57 }
58 return 0;
59}
60
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//---------------------------------------------------
66char *StringUtil::expandFilename(const char *filename) {
67
68 auto_ptr<char> retval( new char[strlen(filename)+strlen(getenv("HOME"))+2]);
69 if (filename[0]=='~') {
70 strcat(retval.get(), getenv("HOME"));
71 strcat(retval.get(), &filename[1]);
72 } else
73 return StringUtil::strdup(filename); //return unmodified value
74
75 return StringUtil::strdup(retval.get()); //return modified value
76}