#include #include #include #include #include #include #include #include #include #include #include #include #include "util.h" using namespace std; namespace Util { 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 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 my_toupper(char ch) { if (ch == 'æ') return 'Æ'; if (ch == 'ø') return 'Ø'; if (ch == 'å') return 'Å'; return ::toupper(ch); } char my_tolower(char ch) { if (ch == 'Æ') return 'æ'; if (ch == 'Ø') return 'ø'; if (ch == 'Å') return 'å'; 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