service: add sort of servicelist including all required layers
[enigma2.git] / lib / base / estring.cpp
index bf30d5f94dfa8387b1cdc7d3f7b88c70f9507aaf..72ca3654637e9d2880077c044b8bac28633e6d7f 100644 (file)
@@ -1,27 +1,13 @@
-#include <lib/base/estring.h>
+#include <string>
 #include <ctype.h>
 #include <limits.h>
 #include <lib/base/elock.h>
+#include <lib/base/eerror.h>
+#include <lib/base/estring.h>
 
 static pthread_mutex_t lock=PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP;
 
-///////////////////////////////////////// eString sprintf /////////////////////////////////////////////////
-eString& eString::sprintf(char *fmt, ...)
-{
-       singleLock s(lock);
-// Implements the normal sprintf method, to use format strings with eString
-// The max length of the result string is 1024 char.
-       static char buf[1024];
-       va_list ap;
-       va_start(ap, fmt);
-       std::vsnprintf(buf, 1024, fmt, ap);
-       va_end(ap);
-       assign(buf);
-       return *this;
-}
-
-///////////////////////////////////////// eString setNum(uint, uint) ///////////////////////////////////////
-eString& eString::setNum(int val, int sys)
+std::string getNum(int val, int sys)
 {
 //     Returns a string that contain the value val as string
 //     if sys == 16 than hexadezimal if sys == 10 than decimal
@@ -32,94 +18,12 @@ eString& eString::setNum(int val, int sys)
        else if (sys == 16)
                std::snprintf(buf, 12, "%X", val);              
        
-       assign(buf);
-       return *this;
-}
-
-///////////////////////////////////////// eString replaceChars(char, char) /////////////////////////////
-eString& eString::removeChars(char fchar)
-{
-//     Remove all chars that equal to fchar, and returns a reference to itself
-       unsigned int index=0;
-
-       while ( ( index = find(fchar, index) ) != npos )
-               erase(index, 1);
-
-       return *this;
-}
-
-/////////////////////////////////////// eString upper() ////////////////////////////////////////////////
-eString& eString::upper()
-{
-//     convert all lowercase characters to uppercase, and returns a reference to itself
-       for (iterator it = begin(); it != end(); it++)
-               switch(*it)
-               {
-                       case 'a' ... 'z' :
-                               *it -= 32;
-                       break;
-
-                       case 'ä' :
-                               *it = 'Ä';
-                       break;
-                       
-                       case 'ü' :
-                               *it = 'Ü';
-                       break;
-                       
-                       case 'ö' :
-                               *it = 'Ö';
-                       break;
-               }
-
-       return *this;
-}
-
-eString& eString::strReplace(const char* fstr, const eString& rstr)
-{
-//     replace all occurrence of fstr with rstr and, and returns a reference to itself
-       unsigned int index=0;
-       unsigned int fstrlen = strlen(fstr);
-       int rstrlen=rstr.size();
-
-       while ( ( index = find(fstr, index) ) != npos )
-       {
-               replace(index, fstrlen, rstr);
-               index+=rstrlen;
-       }
-       
-       return *this;
-}
-
-int strnicmp(const char *s1, const char *s2, int len)
-{
-//     makes a case insensitive string compare with len Chars
-       while ( *s1 && *s2 && len-- )
-               if ( tolower(*s1) != tolower(*s2) )
-                       return tolower(*s1) < tolower(*s2) ? -1 : 1;
-               else
-                       s1++, s2++;
-
-       return 0;
-}
-
-/////////////////////////////////////// eString icompare(const eString&) ////////////////////////////////////////////////
-int eString::icompare(const eString& s)
-{
-//     makes a case insensitive string compare
-       std::string::const_iterator p = begin(),
-                                                                                                                       p2 = s.begin();
-
-       while ( p != end() && p2 != s.end() )
-               if ( tolower(*p) != tolower(*p2) )
-                       return tolower(*p) < tolower(*p2) ? -1 : 1;
-               else
-                       p++, p2++;
-
-       return length() == s.length() ? 0 : length() < s.length() ? -1 : 1;
+       std::string res;
+       res.assign(buf);
+       return res;
 }
 
-               // 8859-x to dvb coding tables. taken from www.unicode.org/Public/MAPPINGS/ISO8859/
+               // 8859-x to ucs coding tables. taken from www.unicode.org/Public/MAPPINGS/ISO8859/
 static unsigned long c88595[128]={
 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087, 0x0088, 0x0089, 0x008a, 0x008b, 0x008c, 0x008d, 0x008e, 0x008f, 
 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, 0x0098, 0x0099, 0x009a, 0x009b, 0x009c, 0x009d, 0x009e, 0x009f, 
@@ -278,7 +182,12 @@ static inline unsigned int recode(unsigned char d, int cp)
        }
 }
 
-eString convertDVBUTF8(unsigned char *data, int len, int table)
+std::string convertDVBUTF8(const std::string &s, int table)
+{
+       return convertDVBUTF8((const unsigned char*)s.c_str(), s.size(), table);
+}
+
+std::string convertDVBUTF8(const unsigned char *data, int len, int table)
 {
        int i;
        if (!len)
@@ -349,12 +258,12 @@ eString convertDVBUTF8(unsigned char *data, int len, int table)
        }
        if ( t != bytesneeded)
                eFatal("t: %d, bytesneeded: %d", t, bytesneeded);
-       return eString().assign((char*)res, t);
+       return std::string().assign((char*)res, t);
 }
 
-eString convertUTF8DVB(const eString &string)
+std::string convertUTF8DVB(const std::string &string)
 {
-       eString ss=eString();
+       std::string ss=std::string();
        
        int len=string.length();
        for(int i=0;i<len;i++){
@@ -383,7 +292,7 @@ eString convertUTF8DVB(const eString &string)
        return ss;
 }
 
-eString convertLatin1UTF8(const eString &string)
+std::string convertLatin1UTF8(const std::string &string)
 {
        unsigned int bytesneeded=0, t=0, i;
        
@@ -432,10 +341,10 @@ eString convertLatin1UTF8(const eString &string)
        }
        if ( t != bytesneeded)
                eFatal("t: %d, bytesneeded: %d", t, bytesneeded);
-       return eString().assign((char*)res, t);
+       return std::string().assign((char*)res, t);
 }
 
-int isUTF8(const eString &string)
+int isUTF8(const std::string &string)
 {
        unsigned int len=string.size();
        
@@ -466,3 +375,36 @@ int isUTF8(const eString &string)
        return 1; // can be UTF8 (or pure ASCII, at least no non-UTF-8 8bit characters)
 }
 
+std::string removeDVBChars(const std::string &s)
+{
+       std::string res;
+       
+       int len = s.length();
+       
+       for(int i = 0; i < len-1; i++)
+       {
+               unsigned char c1 = s[i];
+               unsigned int c;
+               
+                       /* UTF8? decode (but only simple) */
+               if(c1 > 0x80)
+               {
+                       unsigned char c2 = s[i + 1];
+                       c = ((c1&0x3F)<<6) + (c2&0x3F);
+                       if ((c >= 0x80) && (c <= 0x9F))
+                       {
+                               ++i; /* skip 2nd utf8 char */
+                               continue;
+                       }
+               }
+               
+               res += s[i];
+       }
+       
+       return res;
+}
+
+void makeUpper(std::string &s)
+{
+       std::transform(s.begin(), s.end(), s.begin(), (int(*)(int)) toupper);
+}