| 1 |
// String - guess what: it's a string class! Cut down version of Java string |
| 2 |
// class interface |
| 3 |
|
| 4 |
// For all support, instructions and copyright go to: |
| 5 |
// http://e2guardian.org/ |
| 6 |
// Released under the GPL v2, with the OpenSSL exception described in the README file. |
| 7 |
|
| 8 |
#ifndef __HPP_STRING |
| 9 |
#define __HPP_STRING |
| 10 |
|
| 11 |
// INCLUDES |
| 12 |
|
| 13 |
#include <iostream> |
| 14 |
#include <string> |
| 15 |
#include <sys/types.h> |
| 16 |
#include "RegExp.hpp" |
| 17 |
|
| 18 |
// DECLARATIONS |
| 19 |
|
| 20 |
class String : public std::string |
| 21 |
{ |
| 22 |
public: |
| 23 |
String() |
| 24 |
: std::string(){}; |
| 25 |
//~String(); |
| 26 |
|
| 27 |
// constructor from c-string |
| 28 |
String(const char *bs) |
| 29 |
: std::string(bs){}; |
| 30 |
// construct from c++ string |
| 31 |
String(const std::string &s) |
| 32 |
: std::string(s){}; |
| 33 |
// copy constructor |
| 34 |
String(const String &s) |
| 35 |
: std::string(s){}; |
| 36 |
// construct string represenations of numbers |
| 37 |
String(const int num); |
| 38 |
String(const long num); |
| 39 |
String(const long unsigned num); |
| 40 |
String(const unsigned int num); |
| 41 |
String(const bool flag); |
| 42 |
|
| 43 |
#ifndef OFFT_COLLISION |
| 44 |
// If large file support is not enabled (and possibly even if it is), |
| 45 |
// the type of off_t may be a typedef of a type for which we already |
| 46 |
// have a constructor. In that case, don't define one which takes an |
| 47 |
// off_t, or we get compiler errors. |
| 48 |
String(const off_t num); |
| 49 |
#endif |
| 50 |
// substring constructors |
| 51 |
String(const char *bs, int len) |
| 52 |
: std::string(bs, len){}; |
| 53 |
String(const char *bs, int start, int len) |
| 54 |
: std::string(bs + start, len){}; |
| 55 |
|
| 56 |
// operators |
| 57 |
String operator+(const int &i) |
| 58 |
{ |
| 59 |
return (*this) + String(i); |
| 60 |
}; |
| 61 |
|
| 62 |
// return c-string |
| 63 |
const char *toCharArray() const |
| 64 |
{ |
| 65 |
return (this->c_str()); |
| 66 |
}; |
| 67 |
// return substring of length l from start |
| 68 |
String subString(int start, int l) const |
| 69 |
{ |
| 70 |
return this->substr(start, l); |
| 71 |
}; |
| 72 |
|
| 73 |
// convert to integer/long integer |
| 74 |
int toInteger(); |
| 75 |
long int toLong(); |
| 76 |
off_t toOffset(); |
| 77 |
// return integer from hex string |
| 78 |
long int hexToInteger(); |
| 79 |
// case conversions |
| 80 |
void toLower(); |
| 81 |
void toUpper(); |
| 82 |
|
| 83 |
// Swap characters |
| 84 |
void swapChar(char old, char newc); |
| 85 |
|
| 86 |
// decode %xx to characters (checkme: duplicate code?) |
| 87 |
void hexDecode(); |
| 88 |
|
| 89 |
// does the string start/end with this text? |
| 90 |
bool startsWith(const String &s) const; |
| 91 |
bool endsWith(const String &s) const; |
| 92 |
// does this string start with the given text after conversion to lowercase? |
| 93 |
// (pass the search string in in lowercase; only the text being searched |
| 94 |
// is converted) |
| 95 |
bool startsWithLower(const String &s) const; |
| 96 |
// return offset of substring s within the string |
| 97 |
int indexOf(const char *s) const; |
| 98 |
// does it contain this text? |
| 99 |
bool contains(const char *s) const; |
| 100 |
|
| 101 |
// chop to base dir (for file paths) |
| 102 |
void baseDir(); |
| 103 |
// convert relative path filename to fully pathed |
| 104 |
void fullPath(String &base_dir); |
| 105 |
|
| 106 |
// index operator mark 2 |
| 107 |
unsigned char charAt(int index) |
| 108 |
{ |
| 109 |
return (*this)[index]; |
| 110 |
}; |
| 111 |
|
| 112 |
// return string following first occurrence of bs |
| 113 |
String after(const char *bs) const; |
| 114 |
// return string preceding first occurrence of bs |
| 115 |
String before(const char *bs) const; |
| 116 |
// search & replace |
| 117 |
void replaceall(const char *what, const char *with); |
| 118 |
|
| 119 |
// return header value (after ':' and any leading whitespace) - assumes header finishes with '\r' |
| 120 |
bool headerVal(); |
| 121 |
|
| 122 |
// remove character from end/beginning |
| 123 |
void chop(); |
| 124 |
void lop(); |
| 125 |
// remove leading & trailing whitespace |
| 126 |
void removeWhiteSpace(); |
| 127 |
// remove punctuation |
| 128 |
void removePunctuation(); |
| 129 |
// remove protocol prefix (e.g. http://) |
| 130 |
void removePTP(); |
| 131 |
// get hostname from string as url |
| 132 |
String getHostname(); |
| 133 |
|
| 134 |
// truncate to given length |
| 135 |
int limitLength(unsigned int l); |
| 136 |
// remove all occurrences of char from String |
| 137 |
void removeChar(char c); |
| 138 |
// remove repeated occurrences of this character |
| 139 |
void removeMultiChar(unsigned char c); |
| 140 |
// clean up slashes, trailing dots, etc. in file paths |
| 141 |
void realPath(); |
| 142 |
|
| 143 |
// generate MD5 hash of string (using given salt) |
| 144 |
String md5(); |
| 145 |
String md5(const char *salt); |
| 146 |
|
| 147 |
// Sort search words |
| 148 |
String sort_search(); |
| 149 |
|
| 150 |
// get CN name that is oK for cert from url |
| 151 |
String CN(); |
| 152 |
|
| 153 |
// bool isNull(); |
| 154 |
|
| 155 |
// redact string/url |
| 156 |
String anonimise(); |
| 157 |
|
| 158 |
bool isIp(); |
| 159 |
bool is_valid_domain(); |
| 160 |
}; |
| 161 |
|
| 162 |
#endif |