aboutsummaryrefslogtreecommitdiff
path: root/src/StringUtil.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/StringUtil.cc')
-rw-r--r--src/StringUtil.cc141
1 files changed, 0 insertions, 141 deletions
diff --git a/src/StringUtil.cc b/src/StringUtil.cc
deleted file mode 100644
index 221d68d..0000000
--- a/src/StringUtil.cc
+++ /dev/null
@@ -1,141 +0,0 @@
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.14 2002/12/01 13:41:59 rathnor Exp $
23
24#include "StringUtil.hh"
25
26#include <string>
27#include <cstdio>
28#include <cstdlib>
29#include <cctype>
30#include <cassert>
31#include <memory>
32
33using namespace std;
34
35namespace StringUtil
36{
37
38//------- strdup ------------------------
39// Takes a pointer to string *s as an argument,
40// creates a new string n, copies s to n and
41// returns a pointer to n.
42//----------------------------------------
43char *strdup(const char *s) {
44 int l = strlen(s) + 1;
45 char *n = new char[l];
46 strncpy(n, s, l);
47 return n;
48}
49
50//------- strcasestr --------------
51// Tries to find a string in another and
52// ignoring the case of the characters
53// Returns 0 on success else pointer to str.
54//---------------------------------
55const char *strcasestr(const char *str, const char *ptn) {
56 const char *s2, *p2;
57 for( ; *str; str++) {
58 for(s2=str, p2=ptn; ; s2++,p2++) {
59 if (!*p2) return str; // check if we reached the end of ptn, if so, return str
60 if (toupper(*s2) != toupper(*p2)) break; // check if the chars match(ignoring case)
61 }
62 }
63 return 0;
64}
65
66//------------- expandFilename ----------------------
67// if ~ then expand it to home of user
68// returns expanded filename
69//---------------------------------------------------
70string expandFilename(const std::string &filename) {
71
72 string retval;
73 size_t pos = filename.find_first_not_of(" \t");
74 if (pos != std::string::npos && filename[pos] == '~') {
75 retval = getenv("HOME");
76 if (pos != filename.size()) {
77 // copy from the character after '~'
78 retval += static_cast<const char *>(filename.c_str() + pos + 1);
79 }
80 } else
81 return filename; //return unmodified value
82
83 return retval;
84}
85
86//------------- getStringBetween -----------
87// Parses a string between "first" and "last" characters
88// and ignoring ok_chars as whitespaces. The value is
89// returned in "out".
90// Returns negative value on error and this value is the position
91// in the in-string where the error occured.
92// Returns positive value on success and this value is
93// for the position + 1 in the in-string where the "last"-char value
94// was found.
95//------------------------------------------
96int getStringBetween(std::string& out, const char *instr, const char first, const char last,
97 const char *ok_chars) {
98 assert(first);
99 assert(last);
100 assert(instr);
101
102 std::string::size_type i = 0,
103 total_add=0; //used to add extra if there is a \last to skip
104 std::string in(instr);
105
106 // eat leading whitespace
107 i = in.find_first_not_of(ok_chars);
108 if (i == std::string::npos)
109 return -in.size(); // nothing left but whitespace
110
111 if (in[i]!=first)
112 return -i; //return position to error
113
114 // find the end of the token
115 std::string::size_type j = i;
116 while (1) {
117 j = in.find_first_of(last, j+1);
118 if (j==std::string::npos)
119 return -in.size(); //send negative size
120
121 //we found the last char, check so it doesn't have a '\' before
122 if (j>1 && in[j-1] != '\\')
123 break;
124 else if (j>1) {
125 in.erase(j-1, 1); //remove the '\'
126 j--;
127 total_add++; //save numchars removed so we can calculate totalpos
128 }
129 }
130
131 out = in.substr(i+1, j-i-1); //copy the string between first and last
132 //return value to last character
133 return (j+1+total_add);
134}
135
136void toLower(char * const conv) {
137 for (size_t byte_pos = 0; byte_pos < strlen(conv); ++byte_pos)
138 conv[byte_pos] = tolower(conv[byte_pos]);
139}
140
141}; //end namespace StringUtil