#include #include #include #include #include #include #include #include #include #include #include #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)) << " "; } cout.width(1); cout << dec << endl; } 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); pos += replace.size(); } return str; } std::string str_replace_char(std::string str, char search, char replace) { unsigned int pos = 0; while ( (pos = str.find(search,pos)) != string::npos) { str.replace(pos, 1, 1, replace); } return str; } bool my_isspace(char ch) { return (isspace(ch) || ch == 0); } string str_trim(string str) { while (str.length() > 0 && my_isspace(str.at(0))) str.erase(0,1); while (str.length() > 0 && my_isspace(str.at(str.length()-1))) str.erase(str.length()-1,1); return str; } string str_striptags(string str) { unsigned int pos=0; while ( (pos=str.find("<",pos)) != string::npos) { unsigned int endpos = str.find(">",pos); if (endpos == string::npos) break; str.erase(pos, (endpos-pos)+1); } 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; while ( 1 ) { unsigned int endpos = input.find(delimiter); string entry = input.substr(0,endpos); retval.push_back( entry ); if (endpos == string::npos) break; endpos += delimiter.length(); input = input.substr(endpos, input.length() - endpos); } return retval; } char danish_map[3][2] = { {198,230}, {216,248}, {197,229} }; // aelig, oslash, aring 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() ); if (!in) return ""; while (!in.eof() ) { in.getline(buf,128000-1); document += buf; document += "\n"; } 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) { perror(0); return ""; } realsize = outsize = _input.size()*2; realinsize = insize = _input.size(); iconv(icv, &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;*/ iconv_close(icv); string returnstr; for (unsigned int i=0; i