CARDS 2.4.87
Package manager for the NuTyX GNU/Linux distribution
string_utils.h
1 //
2 // string_utils.h
3 //
4 // Copyright (c) 2002 by Johannes Winkelmann
5 // Copyright (c) 2013-2017 by NuTyX team (http://nutyx.org)
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 // USA.
21 //
22 
23 #ifndef STRING_UTILS_H
24 #define STRING_UTILS_H
25 
26 #include "error_treat.h"
27 
28 #include <fstream>
29 #include <map>
30 #include <set>
31 #include <vector>
32 
33 #include <sys/stat.h>
34 
35 #define GIGA 1e9
36 #define MEGA 1e6
37 #define KILO 1e3
38 #define PACKAGE_LOCALE_DIR "/usr/share/locale"
39 #define GETTEXT_PACKAGE "cards"
40 
41 void *Malloc(size_t s);
42 
43 struct keyValue
44 {
45  std::string parameter;
46  std::string value;
47 };
48 
52 typedef struct
53 {
54  char **items;
55  unsigned int count;
56 } itemList;
57 
61 itemList *initItemList(void);
62 
66 void addItemToItemList(itemList *list, const char *item);
67 
72 void freeItemList(itemList *list);
73 
77 keyValue splitKeyValue
78 (std::string s, char delimiter);
79 
80 std::set<std::string> getKeysList
81 (std::string file, std::string delimiter);
82 
83 std::string getValueOfKey
84 (std::string file, std::string delimiter,std::string parameter);
85 
86 std::string getValue(const std::string& s, char delimiter);
87 std::string getValueBefore( const std::string& s, char del );
88 std::string itos(unsigned int value);
89 std::string ultos(unsigned long int value);
90 
91 std::string mtos(mode_t mode);
92 std::string trimFileName(const std::string& filename);
93 std::string sizeHumanRead(int value);
94 
95 /*param s the string to be searched, param delimiter the delimiter char
96 return the value after the first occurance of a delimiter */
97 std::string getFirstValueOfKeyAfterDelim(const std::string& s, char delimiter);
98 
99 /*param s the string to be searched param delimiter the delimiter char
100 return the value before the first occurance of a delimiter */
101 std::string getFirstValueOfKeyBeforeDelim(const std::string& s, char delimiter);
102 
103 /* strip whitespace in the beginning and end of string, return a stripped string */
104 std::string stripWhiteSpace(const std::string& s);
105 
106 /* populate a vector of string with delimited character */
107 std::vector<std::string> parseDelimitedList
108 (const std::string& s, const char delimiter);
109 
110 /* populate a set of string with delimited character */
111 std::set<std::string> parseDelimitedSetList
112 (const std::string& s, const char delimiter);
113 
114 /* make sure s1 starts with s2 */
115 bool startsWith(const std::string& s, const std::string& with);
116 
121 bool startsWithNoCase(const std::string& s1, const std::string& s2);
122 
123 std::string convertToLowerCase(const std::string& s);
124 std::string convertToUpperCase(const std::string& s);
125 
126 std::string replaceAll
127 ( std::string& in, const std::string& oldString, const std::string& newString );
128 
138 template <class T>
139 void split( const std::string& s, char del,
140  T& target,
141  int startPos, bool useEmpty )
142 {
143  std::string line = s;
144  std::string ss;
145  std::string::size_type pos;
146  int offset = startPos;
147  while ( ( pos = line.find( del, offset ) ) != std::string::npos ) {
148  offset = 0;
149  if ( line[pos-1] == '\\' ) {
150  line.erase(pos-1,1);
151  ss = ss + line.substr(0,pos);
152  line.erase(0,pos);
153  continue;
154  }
155  std::string val = line.substr( 0, pos );
156  if ( ( useEmpty || !stripWhiteSpace( val ).empty() ) ||
157  ( ss.length() > 0 ) ) {
158  target.push_back( ss + val );
159  }
160  line.erase( 0, pos+1 );
161  }
162 
163  if ( ( line.length() > 0 ) || ( ss.length() > 0 ) ) {
164  target.push_back( ss + line );
165  }
166 }
167 
168 #endif /* STRING_UTILS_H */
169 // vim:set ts=2 :
Definition: libcards.h:481
Definition: libcards.h:472