import of enigma2
[vuplus_dvbapp] / lib / base / estring.h
1 #ifndef __E_STRING__
2 #define __E_STRING__
3
4 #include <string>
5 #include <stdarg.h>
6 #include <stdio.h>
7 #include "eerror.h"
8
9 int strnicmp(const char*, const char*, int);
10
11 class eString : public std::string
12 {
13 public:
14 // constructors
15         inline eString()        {}      
16         inline eString(const char* p);
17         inline eString(const char* p, int cnt);
18         inline eString(const std::string &s);
19 // methods
20         inline eString left(unsigned int len) const;
21         inline eString mid(unsigned int index, unsigned int len=(unsigned)-1) const;
22         inline eString right(unsigned int len) const;
23         bool isNull() const;
24 // operators
25         inline operator bool() const;
26         inline bool operator!() const;
27 // methods with implementation in estring.cpp
28         eString& sprintf(char *fmt, ...);
29         eString& setNum(int val, int sys=10);
30         eString& removeChars(const char fchar);
31         eString& strReplace(const char* fstr, const eString& rstr);
32         eString& upper();
33         int icompare(const eString& s);
34 };
35
36 eString convertDVBUTF8(unsigned char *data, int len, int table=5);
37 eString convertUTF8DVB(const eString &string);  // with default ISO8859-5
38 eString convertLatin1UTF8(const eString &string);
39 int isUTF8(const eString &string);
40
41 /////////////////////////////////////////////// Copy Constructors ////////////////////////////////////////////////
42 inline eString::eString(const std::string &s)
43         :std::string(s)
44 {
45 }
46
47 inline eString::eString(const char* p)
48         :std::string(p?p:"")     // when the char* p is null, than use ""... otherwise crash...
49 {
50 }
51
52 inline eString::eString(const char* p, int cnt)
53         :std::string(p, cnt)
54 {
55 }
56
57 ///////////////////////////////////////// eString operator bool /////////////////////////////////////////////////
58 inline eString::operator bool() const
59 {
60 // Returns a bool that contains true if the string longer than 0 Character otherwise false;
61         return !empty();
62 }
63
64 ///////////////////////////////////////// eString operator! ////////////////////////////////////////////////////
65 inline bool eString::operator!() const
66 {
67 // Returns a bool that contains true if the string ist empty otherwise false;
68         return empty();
69 }
70
71 ///////////////////////////////////////// eString left //////////////////////////////////////////////////////////
72 inline eString eString::left(unsigned int len) const
73 {
74 //      Returns a substring that contains the len leftmost characters of the string.
75 //      The whole string is returned if len exceeds the length of the string.
76         return len >= length() ? *this : substr(0, len);
77 }
78
79 //////////////////////////////////////// eString mid ////////////////////////////////////////////////////////////
80 inline eString eString::mid(unsigned int index, unsigned int len) const
81 {
82 //      Returns a substring that contains the len characters of this string, starting at position index.
83 //      Returns a null string if the string is empty or index is out of range. Returns the whole string from index if index+len exceeds the length of the string.
84         register unsigned int strlen = length();
85
86         if (index >= strlen)
87                 return eString();
88
89         if (len == (unsigned)-1)
90                 return substr(index);
91
92         if (strlen < index + len)
93                 len = strlen-index;
94
95         return substr(index, len);
96 }
97
98 //////////////////////////////////////// eString right ////////////////////////////////////////////////////////////
99 inline eString eString::right(unsigned int len) const
100 {
101 //      Returns a substring that contains the len rightmost characters of the string.
102 //      The whole string is returned if len exceeds the length of the string.
103         register unsigned int strlen = length();
104         return len >= strlen ? *this : substr(strlen-len, len);
105 }
106
107 inline bool eString::isNull() const
108 {
109 //      Returns a bool, that contains true, when the internal char* is null (only when a string ist empty constructed)
110         return !c_str();
111 }
112
113 #endif // __E_STRING__