--- smsdaemon/util.cpp 2008/12/07 12:06:01 140 +++ smsdaemon/util.cpp 2008/12/07 13:28:52 141 @@ -19,10 +19,71 @@ 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)) << " "; @@ -36,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); @@ -45,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; @@ -87,7 +148,7 @@ vector str_split(string input, string delimiter) { - vector retval; + vector retval; while ( 1 ) { @@ -130,7 +191,7 @@ return ::tolower(ch); } - string str_toupper(string str) + string str_toupper(string str) { for (unsigned i=0; i0) + if (res>0) { throw std::runtime_error("Command time out or document not found"); } @@ -239,24 +292,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) { @@ -264,23 +317,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; @@ -290,7 +343,7 @@ //cout << " (" << output[i] << ")" << endl; returnstr += output[i]; } - return returnstr; + return returnstr; } std::string convertToUnicode(std::string str) @@ -311,7 +364,7 @@ int mTimeDiff(const timeval& then, const timeval& now) { return ( (now.tv_sec - then.tv_sec)*1000) + - ((now.tv_usec-then.tv_usec)/1000); + ((now.tv_usec-then.tv_usec)/1000); } @@ -332,7 +385,7 @@ { std::string output; char buf[256]; - while(!feof(p)) + while (!feof(p)) { int len = fread(buf,1,255, p); buf[len] = 0; @@ -347,26 +400,25 @@ string readfile(string filename) { string str; - ifstream in(filename.c_str()); - if (in) - { - char buffer[2048]; - in.read(buffer, 2047); + ifstream in(filename.c_str()); + if (in) + { + char buffer[2048]; + in.read(buffer, 2047); buffer[ in.gcount() ] = 0; - str = string(buffer); - in.close(); - } - else - { - string message = "Could no open "; - message += filename; - throw std::runtime_error(message); - } - return str; + str = string(buffer); + in.close(); + } + else + { + string message = "Could no open "; + message += filename; + throw std::runtime_error(message); + } + return str; } } -