CARDS 2.4.140
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 - 2021 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
41void *Malloc(size_t s);
42
43struct keyValue
44{
45 std::string parameter;
46 std::string value;
47};
48
52typedef struct
53{
54 char **items;
55 unsigned int count;
56} itemList;
57
61itemList *initItemList(void);
62
66void addItemToItemList(itemList *list, const char *item);
67
72void freeItemList(itemList *list);
73
77keyValue splitKeyValue
78(std::string s, char delimiter);
79
80std::set<std::string> getKeysList
81(std::string file, std::string delimiter);
82
83std::string getValueOfKey
84(std::string file, std::string delimiter,std::string parameter);
85
86std::string getValue(const std::string& s, char delimiter);
87std::string getValueBefore( const std::string& s, char del );
88std::string getValueBeforeLast( const std::string& s, char del );
89
90std::string itos(unsigned int value);
91std::string ultos(unsigned long int value);
92
93std::string mtos(mode_t mode);
94std::string trimFileName(const std::string& filename);
95std::string sizeHumanRead(int value);
96
97/*param s the string to be searched, param delimiter the delimiter char
98return the value after the first occurance of a delimiter */
99std::string getFirstValueOfKeyAfterDelim(const std::string& s, char delimiter);
100
101/* strip whitespace in the beginning and end of string, return a stripped string */
102std::string stripWhiteSpace(const std::string& s);
103
104/* populate a vector of string with delimited characters */
105std::vector<std::string> parseDelimitedVectorList
106(const std::string& s, const char *delimiter);
107
108/* populate a set of string with delimited characters */
109std::set<std::string> parseDelimitedSetList
110(const std::string& s, const char *delimiter);
111
112/* make sure s1 starts with s2 */
113bool startsWith(const std::string& s, const std::string& with);
114
119bool startsWithNoCase(const std::string& s1, const std::string& s2);
120
121std::string convertToLowerCase(const std::string& s);
122std::string convertToUpperCase(const std::string& s);
123
124std::string replaceAll
125( std::string& in, const std::string& oldString, const std::string& newString );
126
136template <class T>
137void split( const std::string& s, char del,
138 T& target,
139 int startPos, bool useEmpty )
140{
141 std::string line = s;
142 std::string ss;
143 std::string::size_type pos;
144 int offset = startPos;
145 while ( ( pos = line.find( del, offset ) ) != std::string::npos ) {
146 offset = 0;
147 if ( line[pos-1] == '\\' ) {
148 line.erase(pos-1,1);
149 ss = ss + line.substr(0,pos);
150 line.erase(0,pos);
151 continue;
152 }
153 std::string val = line.substr( 0, pos );
154 if ( ( useEmpty || !stripWhiteSpace( val ).empty() ) ||
155 ( ss.length() > 0 ) ) {
156 target.push_back( ss + val );
157 }
158 line.erase( 0, pos+1 );
159 }
160
161 if ( ( line.length() > 0 ) || ( ss.length() > 0 ) ) {
162 target.push_back( ss + line );
163 }
164}
165
166#endif /* STRING_UTILS_H */
167// vim:set ts=2 :
Definition: libcards.h:484
Definition: libcards.h:475