ACE_TString RebuildString(const ACE_TString& str) { ACE_TString resultstr = str, tmpstr; ACE_TCHAR search[3][3] = { ACE_TEXT("\\n"), ACE_TEXT("\\r"), ACE_TEXT("\\\"") }; ACE_TCHAR replace[3][2] = { ACE_TEXT("\n"), ACE_TEXT("\r"), ACE_TEXT("\"") }; for(size_t j = 0; j < 3; j++) { size_t pos1 = 0, oldpos = 0; tmpstr = resultstr; resultstr.clear(); while( (pos1 = tmpstr.find(search[j], pos1)) != ACE_TString::npos) { int i = (int)pos1; int slashes = 0; while(i >= 0 && tmpstr[i--] == '\\')slashes++; if(slashes % 2 != 0) //odd number of \ means the character should be escaped { resultstr += tmpstr.substr(oldpos, pos1 - oldpos) + replace[j]; oldpos = pos1 + 2; } pos1 += 2; } resultstr += tmpstr.substr(oldpos, tmpstr.length() - oldpos); } replace_all(resultstr, ACE_TEXT("\\\\"), ACE_TEXT("\\")); return resultstr; }