From 35c5cc6c13327c36e3e0bf0f5d6431a9c19c897a Mon Sep 17 00:00:00 2001 From: Gluzskiy Alexandr Date: Tue, 24 Aug 2010 05:07:08 +0300 Subject: parse gpg output in utf8 format, possible locales bug fixed (need checks) --- main.cpp | 67 ++++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 25 deletions(-) (limited to 'main.cpp') diff --git a/main.cpp b/main.cpp index 3d9eba6..e14ff6c 100644 --- a/main.cpp +++ b/main.cpp @@ -78,7 +78,7 @@ static BOOL CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPARAM {//parse gpg output string out; DWORD code; - string::size_type p = 0, p2 = 0, stop = 0; + wstring::size_type p = 0, p2 = 0, stop = 0; { wstring cmd = _T("--batch --list-secret-keys"); if(pxExecute(&cmd, "", &out, &code) == pxNotFound) @@ -87,7 +87,6 @@ static BOOL CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPARAM break; } } - cp866_to_cp1251(&out); while(p != string::npos) { if((p = out.find("sec ", p)) == string::npos) @@ -97,31 +96,31 @@ static BOOL CALLBACK DlgProcFirstRun(HWND hwndDlg,UINT msg,WPARAM wParam,LPARAM break; stop = p; p2 = out.find("/", p) - 1; - TCHAR *tmp = mir_a2t(out.substr(p,p2-p).c_str()); + TCHAR *tmp = mir_utf8decodeW(out.substr(p,p2-p).c_str()); item.pszText = tmp; iRow = ListView_InsertItem(hwndList, &item); ListView_SetItemText(hwndList, iRow, 4, tmp); mir_free(tmp); p2+=2; p = out.find(" ", p2); - tmp = mir_a2t(out.substr(p2,p-p2).c_str()); + tmp = mir_utf8decodeW(out.substr(p2,p-p2).c_str()); ListView_SetItemText(hwndList, iRow, 0, tmp); mir_free(tmp); p = out.find("uid ", p); p2 = out.find_first_not_of(" ", p+5); p = out.find("<", p2); - tmp = mir_a2t(out.substr(p2,p-p2).c_str()); + tmp = mir_utf8decodeW(out.substr(p2,p-p2).c_str()); ListView_SetItemText(hwndList, iRow, 2, tmp); mir_free(tmp); p++; p2 = out.find(">", p); - tmp = mir_a2t(out.substr(p,p2-p).c_str()); + tmp = mir_utf8decodeW(out.substr(p,p2-p).c_str()); ListView_SetItemText(hwndList, iRow, 1, tmp); mir_free(tmp); p = out.find("ssb ", p2) + 6; p = out.find(" ", p) + 1; p2 = out.find("\n", p); - tmp = mir_a2t(out.substr(p,p2-p-1).c_str()); + tmp = mir_utf8decodeW(out.substr(p,p2-p-1).c_str()); ListView_SetItemText(hwndList, iRow, 3, tmp); mir_free(tmp); ListView_SetColumnWidth(hwndList, 0, LVSCW_AUTOSIZE);// not sure about this @@ -558,7 +557,6 @@ static BOOL CALLBACK DlgProcKeyGenDialog(HWND hwndDlg, UINT msg, WPARAM wParam, break; } } - cp866_to_cp1251(&out); ListView_DeleteAllItems(hwndList_g); while(p != string::npos) { @@ -569,31 +567,31 @@ static BOOL CALLBACK DlgProcKeyGenDialog(HWND hwndDlg, UINT msg, WPARAM wParam, break; stop = p; p2 = out.find("/", p) - 1; - TCHAR *tmp = mir_a2t(out.substr(p,p2-p).c_str()); + TCHAR *tmp = mir_utf8decodeW(out.substr(p,p2-p).c_str()); item.pszText = tmp; iRow = ListView_InsertItem(hwndList_g, &item); ListView_SetItemText(hwndList_g, iRow, 4, tmp); mir_free(tmp); p2+=2; p = out.find(" ", p2); - tmp = mir_a2t(out.substr(p2,p-p2).c_str()); + tmp = mir_utf8decodeW(out.substr(p2,p-p2).c_str()); ListView_SetItemText(hwndList_g, iRow, 0, tmp); mir_free(tmp); p = out.find("uid ", p); p2 = out.find_first_not_of(" ", p+5); p = out.find("<", p2); - tmp = mir_a2t(out.substr(p2,p-p2).c_str()); + tmp = mir_utf8decodeW(out.substr(p2,p-p2).c_str()); ListView_SetItemText(hwndList_g, iRow, 2, tmp); mir_free(tmp); p++; p2 = out.find(">", p); - tmp = mir_a2t(out.substr(p,p2-p).c_str()); + tmp = mir_utf8decodeW(out.substr(p,p2-p).c_str()); ListView_SetItemText(hwndList_g, iRow, 1, tmp); mir_free(tmp); p = out.find("ssb ", p2) + 6; p = out.find(" ", p) + 1; p2 = out.find("\n", p); - tmp = mir_a2t(out.substr(p,p2-p-1).c_str()); + tmp = mir_utf8decodeW(out.substr(p,p2-p-1).c_str()); ListView_SetItemText(hwndList_g, iRow, 3, tmp); mir_free(tmp); ListView_SetColumnWidth(hwndList_g, 0, LVSCW_AUTOSIZE);// not sure about this @@ -696,7 +694,6 @@ static BOOL CALLBACK DlgProcLoadExistingKey(HWND hwndDlg,UINT msg,WPARAM wParam, break; } } - cp866_to_cp1251(&out); while(p != string::npos) { if((p = out.find("pub ", p)) == string::npos) @@ -706,31 +703,31 @@ static BOOL CALLBACK DlgProcLoadExistingKey(HWND hwndDlg,UINT msg,WPARAM wParam, break; stop = p; p2 = out.find("/", p) - 1; - TCHAR *tmp = mir_a2t(out.substr(p,p2-p).c_str()); + TCHAR *tmp = mir_utf8decodeW(out.substr(p,p2-p).c_str()); item.pszText = tmp; iRow = ListView_InsertItem(hwndList, &item); ListView_SetItemText(hwndList, iRow, 4, tmp); mir_free(tmp); p2+=2; p = out.find(" ", p2); - tmp = mir_a2t(out.substr(p2,p-p2).c_str()); + tmp = mir_utf8decodeW(out.substr(p2,p-p2).c_str()); ListView_SetItemText(hwndList, iRow, 0, tmp); mir_free(tmp); p = out.find("uid ", p); p2 = out.find_first_not_of(" ", p+5); p = out.find("<", p2); - tmp = mir_a2t(out.substr(p2,p-p2).c_str()); + tmp = mir_utf8decodeW(out.substr(p2,p-p2).c_str()); ListView_SetItemText(hwndList, iRow, 2, tmp); mir_free(tmp); p++; p2 = out.find(">", p); - tmp = mir_a2t(out.substr(p,p2-p).c_str()); + tmp = mir_utf8decodeW(out.substr(p,p2-p).c_str()); ListView_SetItemText(hwndList, iRow, 1, tmp); mir_free(tmp); p = out.find("sub ", p2) + 6; p = out.find(" ", p) + 1; p2 = out.find("\n", p); - tmp = mir_a2t(out.substr(p,p2-p-1).c_str()); + tmp = mir_utf8decodeW(out.substr(p,p2-p-1).c_str()); ListView_SetItemText(hwndList, iRow, 3, tmp); mir_free(tmp); ListView_SetColumnWidth(hwndList, 0, LVSCW_AUTOSIZE);// not sure about this @@ -910,8 +907,8 @@ void ImportKey() return; } DBDeleteContactSetting(new_key_hcnt, szGPGModuleName, "bAlwatsTrust"); - cp866_to_cp1251(&output); { + char *tmp = NULL; string::size_type s = output.find("gpg: key ") + strlen("gpg: key "); string::size_type s2 = output.find(":", s); DBWriteContactSettingString(new_key_hcnt, szGPGModuleName, "KeyID", output.substr(s,s2-s).c_str()); @@ -922,21 +919,41 @@ void ImportKey() s+=2; if((s2 = output.find("(", s)) == string::npos) s2 = output.find("<", s); - DBWriteContactSettingString(new_key_hcnt, szGPGModuleName, "KeyMainName", output.substr(s,s2-s-1).c_str()); + tmp = new char [output.substr(s,s2-s-1).length()+1]; + strcpy(tmp, output.substr(s,s2-s-1).c_str()); + mir_utf8decode(tmp, 0); + DBWriteContactSettingString(new_key_hcnt, szGPGModuleName, "KeyMainName", tmp); + mir_free(tmp); if((s = output.find(")", s2)) == string::npos) s = output.find(">", s2); s2++; if(output[s] == ')') { - DBWriteContactSettingString(new_key_hcnt, szGPGModuleName, "KeyComment", output.substr(s2,s-s2).c_str()); + tmp = new char [output.substr(s2,s-s2).length()+1]; + strcpy(tmp, output.substr(s2,s-s2).c_str()); + mir_utf8decode(tmp, 0); + DBWriteContactSettingString(new_key_hcnt, szGPGModuleName, "KeyComment", tmp); + mir_free(tmp); s+=3; s2 = output.find(">", s); - DBWriteContactSettingString(new_key_hcnt, szGPGModuleName, "KeyMainEmail", output.substr(s,s2-s).c_str()); + tmp = new char [output.substr(s,s2-s).length()+1]; + strcpy(tmp, output.substr(s,s2-s).c_str()); + mir_utf8decode(tmp, 0); + DBWriteContactSettingString(new_key_hcnt, szGPGModuleName, "KeyMainEmail", tmp); + mir_free(tmp); } else + { + tmp = new char [output.substr(s2,s-s2).length()+1]; + strcpy(tmp, output.substr(s2,s-s2).c_str()); + mir_utf8decode(tmp, 0); DBWriteContactSettingString(new_key_hcnt, szGPGModuleName, "KeyMainEmail", output.substr(s2,s-s2).c_str()); - } - MessageBoxA(0, output.c_str(), "", MB_OK); + mir_free(tmp); + } + } + ptmp = mir_utf8decodeW(output.c_str()); + MessageBox(0, ptmp, _T(""), MB_OK); + mir_free(ptmp); DeleteFile(tmp2); } } \ No newline at end of file -- cgit v1.2.3