From b3008a50cdda200d0f53344edc4f8fa1ffa25bec Mon Sep 17 00:00:00 2001 From: pescuma Date: Fri, 18 Dec 2009 03:28:38 +0000 Subject: utils: sync with berliOS git-svn-id: http://pescuma.googlecode.com/svn/trunk/Miranda@186 c086bb3d-8645-0410-b8da-73a8550f86e7 --- Plugins/utils/mir_icons.cpp | 2 +- Plugins/utils/mir_log.cpp | 73 +++++++++++++++++++++++ Plugins/utils/mir_log.h | 39 ++++--------- Plugins/utils/mir_options.cpp | 80 +++++++++++++++++++++++++- Plugins/utils/mir_options.h | 3 + Plugins/utils/scope.h | 131 ++++++++++++++++++++++++++++++++---------- Plugins/utils/utf8_helpers.h | 104 ++++++++++++++++++++++++++++++--- 7 files changed, 364 insertions(+), 68 deletions(-) diff --git a/Plugins/utils/mir_icons.cpp b/Plugins/utils/mir_icons.cpp index a0d4939..f55d1c2 100644 --- a/Plugins/utils/mir_icons.cpp +++ b/Plugins/utils/mir_icons.cpp @@ -1,5 +1,5 @@ /* -Copyright (C) 2007 Ricardo Pescuma Domenecci +Copyright (C) 2007-2009 Ricardo Pescuma Domenecci This is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public diff --git a/Plugins/utils/mir_log.cpp b/Plugins/utils/mir_log.cpp index c469c20..1a133e9 100644 --- a/Plugins/utils/mir_log.cpp +++ b/Plugins/utils/mir_log.cpp @@ -32,7 +32,80 @@ Boston, MA 02111-1307, USA. int MLog::deep = 0; +MLog::MLog(const char *aModule, const char *aFunction) + : module(aModule) +{ + memset(&total, 0, sizeof(total)); + + function = ""; + for(int i = 0; i < deep; i++) + function += " "; + function += aFunction; + + deep ++; + + mlog(module.c_str(), function.c_str(), "BEGIN"); + + StartTimer(); +} + +MLog::~MLog() +{ + StopTimer(); + + mlog(module.c_str(), function.c_str(), "END in %2.1lf ms", GetTotalTimeMS()); + deep --; +} + +int MLog::log(const char *fmt, ...) +{ + StopTimer(); + + double elapsed = GetElapsedTimeMS(); + + va_list va; + va_start(va, fmt); + + char text[1024]; + mir_snprintf(text, sizeof(text) - 10, "%s [in %2.1lf ms | total %2.1lf ms]", + fmt, GetElapsedTimeMS(), GetTotalTimeMS()); + + int ret = mlog(module.c_str(), function.c_str(), text, va); + + va_end(va); + StartTimer(); + + return ret; +} + +void MLog::StartTimer() +{ + QueryPerformanceCounter(&start); +} + +void MLog::StopTimer() +{ + QueryPerformanceCounter(&end); + + total.QuadPart += end.QuadPart - start.QuadPart; +} + +double MLog::GetElapsedTimeMS() +{ + LARGE_INTEGER frequency; + QueryPerformanceFrequency(&frequency); + + return (end.QuadPart - start.QuadPart) * 1000. / frequency.QuadPart; +} + +double MLog::GetTotalTimeMS() +{ + LARGE_INTEGER frequency; + QueryPerformanceFrequency(&frequency); + + return total.QuadPart * 1000. / frequency.QuadPart; +} int mlog(const char *module, const char *function, const char *fmt, va_list va) diff --git a/Plugins/utils/mir_log.h b/Plugins/utils/mir_log.h index 3ceca42..2efaed2 100644 --- a/Plugins/utils/mir_log.h +++ b/Plugins/utils/mir_log.h @@ -37,39 +37,22 @@ class MLog private: std::string module; std::string function; + LARGE_INTEGER start; + LARGE_INTEGER end; + LARGE_INTEGER total; static int deep; -public: - MLog(const char *aModule, const char *aFunction) : module(aModule) - { - function = ""; - for(int i = 0; i < deep; i++) - function += " "; - function += aFunction; - - deep ++; - - mlog(module.c_str(), function.c_str(), "BEGIN"); - } - - ~MLog() - { - mlog(module.c_str(), function.c_str(), "END"); - deep --; - } + void StartTimer(); + void StopTimer(); + double GetElapsedTimeMS(); + double GetTotalTimeMS(); - int log(const char *fmt, ...) - { - va_list va; - va_start(va, fmt); - - int ret = mlog(module.c_str(), function.c_str(), fmt, va); - - va_end(va); +public: + MLog(const char *aModule, const char *aFunction); + ~MLog(); - return ret; - } + int log(const char *fmt, ...); }; diff --git a/Plugins/utils/mir_options.cpp b/Plugins/utils/mir_options.cpp index b4dfe05..7f1f267 100644 --- a/Plugins/utils/mir_options.cpp +++ b/Plugins/utils/mir_options.cpp @@ -187,6 +187,33 @@ void LoadOpts(OptPageControl *controls, int controlsSize, char *module) MyDBGetContactSettingTString(NULL, module, ctrl->setting, ((TCHAR *) ctrl->var), min(ctrl->max <= 0 ? 1024 : ctrl->max, 1024), ctrl->tszDefValue == NULL ? NULL : TranslateTS(ctrl->tszDefValue)); break; } + case CONTROL_PASSWORD: + { + char tmp[1024]; + tmp[0]=0; + + DBVARIANT dbv = {0}; + if (!DBGetContactSettingString(NULL, module, ctrl->setting, &dbv)) + { + lstrcpynA(tmp, dbv.pszVal, MAX_REGS(tmp)); + DBFreeVariant(&dbv); + } + + if (tmp[0] != 0) + CallService(MS_DB_CRYPT_DECODESTRING, MAX_REGS(tmp), (LPARAM) tmp); + else if (ctrl->szDefValue != NULL) + lstrcpynA(tmp, ctrl->szDefValue, MAX_REGS(tmp)); + + char *var = (char *) ctrl->var; + int size = min(ctrl->max <= 0 ? 1024 : ctrl->max, 1024); + lstrcpynA(var, tmp, size); + break; + } + case CONTROL_INT: + { + *((int *) ctrl->var) = (int) DBGetContactSettingDword(NULL, module, ctrl->setting, ctrl->dwDefValue); + break; + } case CONTROL_FILE: { TCHAR tmp[1024]; @@ -327,6 +354,34 @@ BOOL CALLBACK SaveOptsDlgProc(OptPageControl *controls, int controlsSize, char * SendDlgItemMessage(hwndDlg, ctrl->nID, EM_LIMITTEXT, min(ctrl->max <= 0 ? 1024 : ctrl->max, 1024), 0); break; } + case CONTROL_PASSWORD: + { + char tmp[1024]; + tmp[0]=0; + + DBVARIANT dbv = {0}; + if (!DBGetContactSettingString(NULL, module, ctrl->setting, &dbv)) + { + lstrcpynA(tmp, dbv.pszVal, MAX_REGS(tmp)); + DBFreeVariant(&dbv); + } + + if (tmp[0] != 0) + CallService(MS_DB_CRYPT_DECODESTRING, MAX_REGS(tmp), (LPARAM) tmp); + else if (ctrl->szDefValue != NULL) + lstrcpynA(tmp, ctrl->szDefValue, MAX_REGS(tmp)); + + SetDlgItemTextA(hwndDlg, ctrl->nID, tmp); + SendDlgItemMessage(hwndDlg, ctrl->nID, EM_LIMITTEXT, min(ctrl->max <= 0 ? 1024 : ctrl->max, 1024), 0); + break; + } + case CONTROL_INT: + { + DWORD var = DBGetContactSettingDword(NULL, module, ctrl->setting, ctrl->dwDefValue); + SetDlgItemInt(hwndDlg, ctrl->nID, var, ctrl->min <= 0); + SendDlgItemMessage(hwndDlg, ctrl->nID, EM_LIMITTEXT, 9, 0); + break; + } case CONTROL_FILE: { TCHAR tmp[1024]; @@ -377,6 +432,8 @@ BOOL CALLBACK SaveOptsDlgProc(OptPageControl *controls, int controlsSize, char * { case CONTROL_TEXT: case CONTROL_SPIN: + case CONTROL_INT: + case CONTROL_PASSWORD: { // Don't make apply enabled during buddy set if (HIWORD(wParam) != EN_CHANGE || (HWND)lParam != GetFocus()) @@ -463,10 +520,31 @@ BOOL CALLBACK SaveOptsDlgProc(OptPageControl *controls, int controlsSize, char * case CONTROL_TEXT: { TCHAR tmp[1024]; - GetDlgItemText(hwndDlg, ctrl->nID, tmp, 1024); + GetDlgItemText(hwndDlg, ctrl->nID, tmp, MAX_REGS(tmp)); DBWriteContactSettingTString(NULL, module, ctrl->setting, tmp); break; } + case CONTROL_PASSWORD: + { + char tmp[1024]; + GetDlgItemTextA(hwndDlg, ctrl->nID, tmp, MAX_REGS(tmp)); + CallService(MS_DB_CRYPT_ENCODESTRING, MAX_REGS(tmp), (LPARAM) tmp); + DBWriteContactSettingString(NULL, module, ctrl->setting, tmp); + break; + } + case CONTROL_INT: + { + BOOL trans; + int val = GetDlgItemInt(hwndDlg, ctrl->nID, &trans, ctrl->min <= 0); + if (!trans) + val = ctrl->dwDefValue; + if (ctrl->max != 0) + val = min(val, ctrl->max); + if (ctrl->min != 0) + val = max(val, ctrl->min); + DBWriteContactSettingDword(NULL, module, ctrl->setting, val); + break; + } case CONTROL_FILE: { TCHAR tmp[1024]; diff --git a/Plugins/utils/mir_options.h b/Plugins/utils/mir_options.h index c392373..31b757b 100644 --- a/Plugins/utils/mir_options.h +++ b/Plugins/utils/mir_options.h @@ -34,6 +34,8 @@ Boston, MA 02111-1307, USA. #define CONTROL_COMBO_TEXT 7 // Stored as TCHARs, max len 1024 #define CONTROL_COMBO_ITEMDATA 8 // Stored as TCHARs, max len 1024 #define CONTROL_FILE 9 // Stored as TCHARs, max len 1024 +#define CONTROL_INT 10 // Stored as DWORD +#define CONTROL_PASSWORD 11 // Stored as chars, max len 1024 typedef BOOL (* FPAllowProtocol) (const char *proto); @@ -46,6 +48,7 @@ typedef struct { union { DWORD dwDefValue; TCHAR *tszDefValue; + char *szDefValue; }; union { int nIDSpin; diff --git a/Plugins/utils/scope.h b/Plugins/utils/scope.h index 2fad797..5556e5f 100644 --- a/Plugins/utils/scope.h +++ b/Plugins/utils/scope.h @@ -1,37 +1,106 @@ -#ifndef __PTR_H__ -# define __PTR_H__ - - -template -class scope -{ -public: - scope() : p(NULL) {} - scope(T t) : p(t) {} - ~scope() { release(); } - - void release() - { - if (p != NULL) - delete p; - p = NULL; - } +/* +Copyright (C) 2009 Ricardo Pescuma Domenecci - T operator=(T t) { release(); p = t; return t; } - T operator->() const { return p; } - operator T() const { return p; } +This is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public +License as published by the Free Software Foundation; either +version 2 of the License, or (at your option) any later version. + +This is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with this file; see the file license.txt. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. +*/ - T detach() - { - T ret = p; - p = NULL; - return ret; - } -private: - T p; -}; +#ifndef __SCOPE_H__ +# define __SCOPE_H__ + + +#define DEFINE_SCOPED_TYPE(_NAME_, _DELETE_) \ + template \ + class _NAME_ \ + { \ + _NAME_(_NAME_ &); \ + _NAME_ & operator=(_NAME_ &); \ + \ + public: \ + _NAME_(T *t = NULL) : p(t) {} \ + \ + ~_NAME_() \ + { \ + release(); \ + } \ + \ + T * operator=(T *t) \ + { \ + release(); \ + p = t; \ + return t; \ + } \ + \ + operator T*() const \ + { \ + return p; \ + } \ + \ + bool operator==(T *t) const \ + { \ + return p == t; \ + } \ + \ + bool operator!=(T *t) const \ + { \ + return p != t; \ + } \ + \ + T *get() const \ + { \ + return p; \ + } \ + \ + void release() \ + { \ + if (p != NULL) \ + dealoc(p); \ + p = NULL; \ + } \ + \ + T* detach() \ + { \ + T *ret = p; \ + p = NULL; \ + return ret; \ + } \ + \ + protected: \ + T *p; \ + \ + void dealoc(T *ptr) \ + { \ + _DELETE_; \ + } \ + }; \ + \ + template \ + inline bool operator==(T* p, const _NAME_ &b) { \ + return p == b.get(); \ + } \ + \ + template \ + inline bool operator!=(T* p, const _NAME_ &b) { \ + return p != b.get(); \ + } +DEFINE_SCOPED_TYPE(scoped_ptr, delete ptr) +DEFINE_SCOPED_TYPE(scoped_array, delete[] ptr) +DEFINE_SCOPED_TYPE(scoped_free, free(ptr)) +DEFINE_SCOPED_TYPE(scoped_mir_free, mir_free(ptr)) -#endif // __PTR_H__ +#endif // __SCOPE_H__ diff --git a/Plugins/utils/utf8_helpers.h b/Plugins/utils/utf8_helpers.h index 1b7785d..c48eb67 100644 --- a/Plugins/utils/utf8_helpers.h +++ b/Plugins/utils/utf8_helpers.h @@ -1,3 +1,23 @@ +/* +Copyright (C) 2009 Ricardo Pescuma Domenecci + +This is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public +License as published by the Free Software Foundation; either +version 2 of the License, or (at your option) any later version. + +This is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with this file; see the file license.txt. If +not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. +*/ + + #ifndef __UTF8_HELPERS_H__ # define __UTF8_HELPERS_H__ @@ -39,11 +59,21 @@ public: mir_free(utf8); } - operator char *() + char * get() const + { + return utf8; + } + + operator char *() const { return utf8; } + char operator[](int pos) const + { + return utf8[pos]; + } + private: char *utf8; @@ -121,11 +151,21 @@ public: return ret; } - operator TCHAR *() + TCHAR * get() const + { + return tchar; + } + + operator TCHAR *() const { return tchar; } + TCHAR operator[](int pos) const + { + return tchar[pos]; + } + private: TCHAR *tchar; }; @@ -171,11 +211,21 @@ public: return ret; } - operator const TCHAR *() + const TCHAR * get() const + { + return tchar; + } + + operator const TCHAR *() const { return tchar; } + TCHAR operator[](int pos) const + { + return tchar[pos]; + } + private: #ifdef UNICODE TCHAR *tchar; @@ -225,11 +275,21 @@ public: return ret; } - operator const TCHAR *() + const TCHAR * get() const { return tchar; } + operator const TCHAR *() const + { + return tchar; + } + + TCHAR operator[](int pos) const + { + return tchar[pos]; + } + private: #ifdef UNICODE const TCHAR *tchar; @@ -266,11 +326,21 @@ public: return ret; } - operator const WCHAR *() + const WCHAR * get() const { return wchar; } + operator const WCHAR *() const + { + return wchar; + } + + WCHAR operator[](int pos) const + { + return wchar[pos]; + } + private: WCHAR *wchar; }; @@ -317,11 +387,21 @@ public: return ret; } - operator const char *() + const char * get() const { return val; } + operator const char *() const + { + return val; + } + + char operator[](int pos) const + { + return val[pos]; + } + private: #ifdef UNICODE char *val; @@ -371,11 +451,21 @@ public: return ret; } - operator const WCHAR *() + const WCHAR * get() const { return val; } + operator const WCHAR *() const + { + return val; + } + + WCHAR operator[](int pos) const + { + return val[pos]; + } + private: #ifdef UNICODE const WCHAR *val; -- cgit v1.2.3