--- smsdaemon/util.cpp 2008/06/11 06:46:14 43 +++ smsdaemon/Util.cpp 2008/12/18 06:53:29 196 @@ -7,19 +7,83 @@ #include #include #include - +#include #include +#include +#include -#include "util.h" +#include +#include "Util.h" using namespace std; namespace Util { + const int GN_CHAR_ALPHABET_SIZE = 128; + unsigned char gsm_default_alphabet[GN_CHAR_ALPHABET_SIZE] = + { + + /* ETSI GSM 03.38, version 6.0.1, section 6.2.1; Default alphabet */ + /* Characters in hex position 10, [12 to 1a] and 24 are not present on + * latin1 charset, so we cannot reproduce on the screen, however they are + * greek symbol not present even on my Nokia */ + + '@', 0xa3, '$', 0xa5, 0xe8, 0xe9, 0xf9, 0xec, + 0xf2, 0xc7, '\n', 0xd8, 0xf8, '\r', 0xc5, 0xe5, + '?', '_', '?', '?', '?', '?', '?', '?', + '?', '?', '?', '?', 0xc6, 0xe6, 0xdf, 0xc9, + ' ', '!', '\"', '#', 0xa4, '%', '&', '\'', + '(', ')', '*', '+', ',', '-', '.', '/', + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', ':', ';', '<', '=', '>', '?', + 0xa1, 'A', 'B', 'C', 'D', 'E', 'F', 'G', + 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', + 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', + 'X', 'Y', 'Z', 0xc4, 0xd6, 0xd1, 0xdc, 0xa7, + 0xbf, 'a', 'b', 'c', 'd', 'e', 'f', 'g', + 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', + 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', + 'x', 'y', 'z', 0xe4, 0xf6, 0xf1, 0xfc, 0xe0 + }; + + static unsigned char gsm_reverse_default_alphabet[256]; + static bool reversed = false; + + static void tbl_setup_reverse() + { + int i; + + if (reversed) return; + memset(gsm_reverse_default_alphabet, 0x3f, 256); + for (i = GN_CHAR_ALPHABET_SIZE - 1; i >= 0; i--) + gsm_reverse_default_alphabet[ gsm_default_alphabet[i] ] = i; + gsm_reverse_default_alphabet['?'] = 0x3f; + reversed = true; + } + + unsigned char char_def_alphabet_encode(unsigned char value) + { + tbl_setup_reverse(); + return gsm_reverse_default_alphabet[value]; + } + + unsigned char char_def_alphabet_decode(unsigned char value) + { + if (value < GN_CHAR_ALPHABET_SIZE) + { + return gsm_default_alphabet[value]; + } + else + { + return '?'; + } + } + + void str_dump(const string& str) { - for (unsigned i=0; i(str.at(i)) << " "; @@ -33,7 +97,7 @@ string str_replace(string str, string search, string replace) { unsigned int pos = 0; - + while ( (pos = str.find(search,pos)) != string::npos) { str.replace(pos, search.size(), replace); @@ -42,7 +106,7 @@ return str; } - std::string str_replace_char(std::string str, char search, char replace) + std::string str_replace_char(std::string str, char search, char replace) { unsigned int pos = 0; @@ -53,13 +117,16 @@ return str; } - + bool my_isspace(char ch) + { + return (isspace(ch) || ch == 0); + } string str_trim(string str) { - while (str.length() > 0 && isspace(str.at(0))) + while (str.length() > 0 && my_isspace(str.at(0))) str.erase(0,1); - while (str.length() > 0 && isspace(str.at(str.length()-1))) + while (str.length() > 0 && my_isspace(str.at(str.length()-1))) str.erase(str.length()-1,1); return str; } @@ -79,9 +146,21 @@ return str; } + + vector str_split(string input) + { + string buf; + stringstream ss(input); + vector tokens; + while (ss >> buf) + tokens.push_back(buf); + return tokens; + } + + vector str_split(string input, string delimiter) { - vector retval; + vector retval; while ( 1 ) { @@ -101,12 +180,36 @@ return retval; } + char danish_map[3][2] = { {198,230}, {216,248}, {197,229} }; // aelig, oslash, aring - string str_toupper(string str) + unsigned char my_toupper(unsigned char ch) + { + if (ch == 230) + return 198; + if (ch == 248) + return 216; + if (ch == 229) + return 197; + + return ::toupper(ch); + } + + unsigned char my_tolower(unsigned char ch) + { + if (ch == 198) + return 230; + if (ch == 216) + return 248; + if (ch == 197) + return 229; + + return ::tolower(ch); + } + string str_toupper(string str) { for (unsigned i=0; i0) + { + throw std::runtime_error("Command time out or document not found"); + } ifstream in( tempfile.c_str() ); @@ -193,24 +315,24 @@ } in.close(); unlink(tempfile.c_str()); - + return document; - } + }*/ + - string iconv_wrapper(string _input, string to_format, string from_format) { char* input,*output,*input_ptr, *output_ptr; input = input_ptr = (char*) malloc(_input.size()+1); strcpy(input, _input.c_str()); - + output = output_ptr = (char*) malloc(_input.size()*2); - + unsigned int realinsize,insize,outsize,realsize; - + iconv_t icv = iconv_open(to_format.c_str(), from_format.c_str()); if (icv == (iconv_t)-1) { @@ -218,23 +340,23 @@ return ""; } - + realsize = outsize = _input.size()*2; realinsize = insize = _input.size(); - + iconv(icv, - &input_ptr, - &insize, - &output_ptr, - &outsize); + &input_ptr, + &insize, + &output_ptr, + &outsize); perror(0); -/* cout << "len=" << len << endl; - cout << "outsize=" << outsize << endl; - cout << "realsize=" << realsize << endl; - cout << "insize=" << insize << endl; - cout << "realinsize=" << realinsize << endl;*/ + /* cout << "len=" << len << endl; + cout << "outsize=" << outsize << endl; + cout << "realsize=" << realsize << endl; + cout << "insize=" << insize << endl; + cout << "realinsize=" << realinsize << endl;*/ iconv_close(icv); string returnstr; @@ -244,7 +366,7 @@ //cout << " (" << output[i] << ")" << endl; returnstr += output[i]; } - return returnstr; + return returnstr; } std::string convertToUnicode(std::string str) @@ -264,9 +386,62 @@ int mTimeDiff(const timeval& then, const timeval& now) { - return uTimeDiff(then,now) / 1000; + return ( (now.tv_sec - then.tv_sec)*1000) + + ((now.tv_usec-then.tv_usec)/1000); } -} + timeval GetTimeOfDay() + { + timeval now; + gettimeofday(&now,0); + return now; + } + + int my_system(const char* cmd, std::string* response) + { + FILE* p; + if ((p = popen(cmd,"r")) == NULL) + return (-1); + + if (response) + { + std::string output; + char buf[256]; + while (!feof(p)) + { + int len = fread(buf,1,255, p); + buf[len] = 0; + output += buf; + } + *response = output; + } + + return (pclose(p)); + } + + string readfile(string filename) + { + string str; + ifstream in(filename.c_str()); + if (in) + { + char buffer[4096]; + in.read(buffer, 4095); + buffer[ in.gcount() ] = 0; + str = string(buffer); + in.close(); + } + else + { + string message = "Could not open "; + message += filename; + throw std::runtime_error(message); + } + return str; + } + + + +}