Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenutility.h
Go to the documentation of this file.
00001 /* 00002 * Copyright 2003, 2004, 2005 Martin Fuchs 00003 * 00004 * This library is free software; you can redistribute it and/or 00005 * modify it under the terms of the GNU Lesser General Public 00006 * License as published by the Free Software Foundation; either 00007 * version 2.1 of the License, or (at your option) any later version. 00008 * 00009 * This library is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 * Lesser General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU Lesser General Public 00015 * License along with this library; if not, write to the Free Software 00016 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00017 */ 00018 00019 00020 // 00021 // Explorer clone 00022 // 00023 // utility.h 00024 // 00025 // Martin Fuchs, 23.07.2003 00026 // 00027 00028 // standard windows headers 00029 #define WIN32_LEAN_AND_MEAN 00030 #define WIN32_EXTRA_LEAN 00031 #include <windows.h> 00032 #include <undocuser.h> 00033 00034 // Unicode support 00035 #if defined(UNICODE) && !defined(_UNICODE) 00036 #define _UNICODE 00037 #endif 00038 #include <tchar.h> 00039 00040 #include <windowsx.h> // for SelectBrush(), ListBox_SetSel(), SubclassWindow(), ... 00041 #include <commctrl.h> 00042 00043 #include <objbase.h> 00044 #include <oleauto.h> // for VARIANT 00045 00046 #include <malloc.h> // for alloca() 00047 #include <assert.h> 00048 #include <stdlib.h> // for _MAX_DIR, ... 00049 #include <stdio.h> // for sprintf() 00050 #include <time.h> 00051 00052 #ifdef __cplusplus 00053 00054 #ifdef _MSC_VER 00055 #pragma warning(disable: 4786) // disable warnings about too long debug information symbols 00056 #endif 00057 00058 // STL headers for strings and streams 00059 #include <string> 00060 #include <iostream> 00061 using namespace std; 00062 00063 #endif /* __cplusplus */ 00064 00065 #ifndef _MAX_PATH 00066 #define _MAX_DRIVE 3 00067 #define _MAX_FNAME 256 00068 #define _MAX_DIR _MAX_FNAME 00069 #define _MAX_EXT _MAX_FNAME 00070 #define _MAX_PATH 260 00071 #endif 00072 00073 #define W_VER_NT 0 // constant for HIWORD(GetVersion())>>14 00074 00075 00076 #ifdef __cplusplus 00077 extern "C" { 00078 #endif 00079 00080 00081 #define for if (0) {} else for 00082 00083 #ifdef _countof 00084 #define COUNTOF _countof 00085 #else 00086 #define COUNTOF(x) (sizeof(x)/sizeof(x[0])) 00087 #endif 00088 00089 00090 #define BUFFER_LEN 2048 00091 00092 00093 extern void _log_(LPCTSTR txt); 00094 00095 #define LOG(txt) _log_(txt) 00096 00097 00098 #ifdef _MSC_VER 00099 #define LONGLONGARG TEXT("I64") 00100 #else 00101 #define LONGLONGARG TEXT("L") 00102 #endif 00103 00104 00105 #ifndef _tcsrchr 00106 #ifdef UNICODE 00107 #define _tcsrchr wcsrchr 00108 #else 00109 #define _tcsrchr strrchr 00110 #endif 00111 #endif 00112 00113 #ifndef _stprintf 00114 #ifdef UNICODE 00115 #define _stprintf wcsprintf 00116 #else 00117 #define _stprintf sprintf 00118 #endif 00119 #endif 00120 00121 #define U2A(s, d, l) WideCharToMultiByte(CP_ACP, 0, s, -1, d, l, NULL, NULL) 00122 #define U2nA(s, d, l) WideCharToMultiByte(CP_ACP, 0, s, l, d, l, NULL, NULL) 00123 #define A2U(s, d, l) MultiByteToWideChar(CP_ACP, 0, s, -1, d, l) 00124 #define A2nU(s, d, l) MultiByteToWideChar(CP_ACP, 0, s, l, d, l) 00125 00126 00127 #ifdef __WINE__ 00128 #ifdef UNICODE 00129 extern void _wsplitpath(const WCHAR* path, WCHAR* drv, WCHAR* dir, WCHAR* name, WCHAR* ext); 00130 #else 00131 extern void _splitpath(const CHAR* path, CHAR* drv, CHAR* dir, CHAR* name, CHAR* ext); 00132 #endif 00133 #define _tcsnicmp strncasecmp 00134 #define _tcsicoll strcasecmp 00135 #endif 00136 00137 #ifndef FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 00138 #define FILE_ATTRIBUTE_ENCRYPTED 0x00000040 00139 #define FILE_ATTRIBUTE_SPARSE_FILE 0x00000200 00140 #define FILE_ATTRIBUTE_REPARSE_POINT 0x00000400 00141 #define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x00002000 00142 #endif 00143 00144 00145 #define SetDlgCtrlID(hwnd, id) SetWindowLongPtr(hwnd, GWL_ID, id) 00146 #define SetWindowStyle(hwnd, val) (DWORD)SetWindowLongPtr(hwnd, GWL_STYLE, val) 00147 #define SetWindowExStyle(h, val) (DWORD)SetWindowLongPtr(hwnd, GWL_EXSTYLE, val) 00148 #define Window_SetIcon(hwnd, type, hicon) (HICON)SendMessage(hwnd, WM_SETICON, type, (LPARAM)(hicon)) 00149 00150 00151 // center window in respect to its parent window 00152 extern void CenterWindow(HWND hwnd); 00153 00154 // move window into visibility 00155 extern void MoveVisible(HWND hwnd); 00156 00157 // display error message 00158 extern void display_error(HWND hwnd, DWORD error); 00159 00160 // convert time_t to WIN32 FILETIME 00161 extern BOOL time_to_filetime(const time_t* t, FILETIME* ftime); 00162 00163 // search for windows of a specific classname 00164 extern int find_window_class(LPCTSTR classname); 00165 00166 // create a directory with all missing parent directories 00167 BOOL RecursiveCreateDirectory(LPCTSTR path_in); 00168 00169 // read DWORD value from registry 00170 DWORD RegGetDWORDValue(HKEY root, LPCTSTR path, LPCTSTR valueName, DWORD def); 00171 00172 // write DWORD value to registry 00173 BOOL RegSetDWORDValue(HKEY root, LPCTSTR path, LPCTSTR valueName, DWORD value); 00174 00175 // test for existing directory 00176 BOOL exists_path(LPCTSTR path); 00177 00178 00179 #ifdef __cplusplus 00180 } // extern "C" 00181 #endif 00182 00183 00184 // secure CRT functions 00185 //@@ _MS_VER: temporarily needed for the ReactOS build environment 00186 #if defined(__STDC_WANT_SECURE_LIB__) && defined(_MS_VER) // for VS 2005: _MSC_VER>=1400 00187 00188 #define _stprintf_s1 _stprintf_s 00189 #define _stprintf_s2 _stprintf_s 00190 00191 #else // __STDC_WANT_SECURE_LIB__ 00192 00193 #define strcpy_s(d, l, s) strcpy(d, s) 00194 #define wcscpy_s(d, l, s) wcscpy(d, s) 00195 #define wcsncpy_s(d, l, s, n) wcsncpy(d, s, n) 00196 00197 #if defined(_tcscpy) && !defined(_tcscpy_s) 00198 #define _tcscpy_s(d, l, s) _tcscpy(d, s) 00199 #endif 00200 00201 #if defined(_tsplitpath) && !defined(_tsplitpath_s) 00202 #define _tsplitpath_s(f, d,dl, p,pl, n,nl, e,el) _tsplitpath(f, d, p, n, e) 00203 #else 00204 #define _wsplitpath_s(f, d,dl, p,pl, n,nl, e,el) _wsplitpath(f, d, p, n, e) 00205 #define _splitpath_s(f, d,dl, p,pl, n,nl, e,el) _splitpath(f, d, p, n, e) 00206 #endif 00207 00208 #define _stprintf_s1(b, l, f, p1) _stprintf(b, f, p1) 00209 #define _stprintf_s2(b, l, f, p1,p2) _stprintf(b, f, p1,p2) 00210 00211 #endif // __STDC_WANT_SECURE_LIB__ 00212 00213 00214 #ifdef __cplusplus 00215 00216 // containers 00217 #include <map> 00218 #include <set> 00219 #include <list> 00220 #include <stack> 00221 #include <vector> 00222 00223 00224 /* not necessary with correct include file order for comdef.h ("<MS PSDK>\include" path first) */ 00225 #if _MSC_VER>=1300 // VS.Net 00226 #define _NO_COMUTIL 00227 #endif 00228 00229 00230 #if defined(_MSC_VER) && !defined(_NO_COMUTIL) 00231 00232 // COM utility headers 00233 #include <comdef.h> 00234 using namespace _com_util; 00235 00236 #endif // _MSC_VER && !_NO_COMUTIL 00237 00238 00239 // launch a program or document file 00240 extern BOOL launch_file(HWND hwnd, LPCTSTR cmd, UINT nCmdShow=SW_SHOWNORMAL, LPCTSTR parameters=NULL); 00241 #ifdef UNICODE 00242 extern BOOL launch_fileA(HWND hwnd, LPSTR cmd, UINT nCmdShow=SW_SHOWNORMAL, LPCSTR parameters=NULL); 00243 #else 00244 #define launch_fileA launch_file 00245 #endif 00246 00247 // call an DLL export like rundll32 00248 extern BOOL RunDLL(HWND hwnd, LPCTSTR dllname, LPCSTR procname, LPCTSTR cmdline, UINT nCmdShow); 00249 00250 // launch control panel applet 00251 extern BOOL launch_cpanel(HWND hwnd, LPCTSTR applet); 00252 00253 00255 struct CommonControlInit 00256 { 00257 CommonControlInit(DWORD flags=ICC_LISTVIEW_CLASSES|ICC_TREEVIEW_CLASSES|ICC_BAR_CLASSES|ICC_PROGRESS_CLASS|ICC_COOL_CLASSES) 00258 { 00259 INITCOMMONCONTROLSEX icc = {sizeof(INITCOMMONCONTROLSEX), flags}; 00260 00261 InitCommonControlsEx(&icc); 00262 } 00263 }; 00264 00265 00267 00268 struct WaitCursor 00269 { 00270 WaitCursor() 00271 { 00272 _old_cursor = SetCursor(LoadCursor(0, IDC_WAIT)); 00273 } 00274 00275 ~WaitCursor() 00276 { 00277 SetCursor(_old_cursor); 00278 } 00279 00280 protected: 00281 HCURSOR _old_cursor; 00282 }; 00283 00284 00286 struct WindowHandle 00287 { 00288 WindowHandle(HWND hwnd=0) 00289 : _hwnd(hwnd) {} 00290 00291 operator HWND() const {return _hwnd;} 00292 HWND* operator&() {return &_hwnd;} 00293 00294 protected: 00295 HWND _hwnd; 00296 }; 00297 00298 00300 struct HiddenWindow : public WindowHandle 00301 { 00302 HiddenWindow(HWND hwnd) 00303 : WindowHandle(IsWindowVisible(hwnd)? hwnd: 0) 00304 { 00305 if (_hwnd) 00306 SetWindowPos(_hwnd, 0, 0, 0, 0, 0, SWP_HIDEWINDOW|SWP_NOREDRAW|SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER); 00307 } 00308 00309 ~HiddenWindow() 00310 { 00311 if (_hwnd) 00312 SetWindowPos(_hwnd, 0, 0, 0, 0, 0, SWP_SHOWWINDOW|SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER); 00313 } 00314 }; 00315 00316 00318 00319 struct CritSect : public CRITICAL_SECTION 00320 { 00321 CritSect() 00322 { 00323 InitializeCriticalSection(this); 00324 } 00325 00326 ~CritSect() 00327 { 00328 DeleteCriticalSection(this); 00329 } 00330 }; 00331 00332 00334 00335 struct Lock 00336 { 00337 Lock(CritSect& crit_sect) 00338 : _crit_sect(crit_sect) 00339 { 00340 EnterCriticalSection(&crit_sect); 00341 } 00342 00343 ~Lock() 00344 { 00345 LeaveCriticalSection(&_crit_sect); 00346 } 00347 00348 protected: 00349 CritSect& _crit_sect; 00350 }; 00351 00352 00354 00355 struct Thread 00356 { 00357 Thread() 00358 : _alive(false), 00359 _destroy(false) 00360 { 00361 _hThread = INVALID_HANDLE_VALUE; 00362 _evtFinish = CreateEvent(NULL, TRUE, FALSE, NULL); 00363 } 00364 00365 virtual ~Thread() 00366 { 00367 Stop(); 00368 00369 CloseHandle(_evtFinish); 00370 CloseHandle(_hThread); 00371 00372 if (_destroy) 00373 delete this; 00374 } 00375 00376 void Start() 00377 { 00378 if (!_alive) { 00379 _alive = true; 00380 _hThread = CreateThread(NULL, 0, ThreadProc, this, 0, NULL); 00381 } 00382 } 00383 00384 void Stop() 00385 { 00386 SetEvent(_evtFinish); 00387 00388 if (_alive) { 00389 { 00390 Lock lock(_crit_sect); 00391 _alive = false; 00392 } 00393 00394 // wait for finishing 00395 WaitForSingleObject(_hThread, INFINITE); 00396 } 00397 } 00398 00399 virtual int Run() = 0; 00400 00401 bool is_alive() const {return _alive;} 00402 00403 CritSect _crit_sect; 00404 00405 protected: 00406 static DWORD WINAPI ThreadProc(void* para); 00407 00408 HANDLE _hThread; 00409 HANDLE _evtFinish; 00410 bool _alive; 00411 bool _destroy; 00412 }; 00413 00414 00415 // window utilities 00416 00418 struct ClientRect : public RECT 00419 { 00420 ClientRect(HWND hwnd) 00421 { 00422 GetClientRect(hwnd, this); 00423 } 00424 00425 operator LPRECT() {return this;} 00426 00427 POINT& pos() {return *(LPPOINT)this;} 00428 }; 00429 00431 struct WindowRect : public RECT 00432 { 00433 WindowRect(HWND hwnd) 00434 { 00435 GetWindowRect(hwnd, this); 00436 } 00437 00438 operator LPRECT() {return this;} 00439 00440 POINT& pos() {return *(LPPOINT)this;} 00441 }; 00442 00444 struct Point : public POINT 00445 { 00446 Point(LONG x_, LONG y_) 00447 { 00448 x = x_; 00449 y = y_; 00450 } 00451 00452 // constructor for being used in processing WM_MOUSEMOVE, WM_LBUTTONDOWN, ... messages 00453 Point(LPARAM lparam) 00454 { 00455 x = GET_X_LPARAM(lparam); 00456 y = GET_Y_LPARAM(lparam); 00457 } 00458 00459 operator LPPOINT() {return this;} 00460 }; 00461 00462 00464 inline void ClientToScreen(HWND hwnd, RECT* prect) 00465 {::ClientToScreen(hwnd,(LPPOINT)&prect->left); ::ClientToScreen(hwnd,(LPPOINT)&prect->right);} 00466 00468 inline void ScreenToClient(HWND hwnd, RECT* prect) 00469 {::ScreenToClient(hwnd,(LPPOINT)&prect->left); ::ScreenToClient(hwnd,(LPPOINT)&prect->right);} 00470 00471 00473 struct FullScreenParameters 00474 { 00475 FullScreenParameters() 00476 : _mode(FALSE) 00477 { 00478 } 00479 00480 BOOL _mode; 00481 RECT _orgPos; 00482 BOOL _wasZoomed; 00483 }; 00484 00485 00486 // drawing utilities 00487 00489 struct PaintCanvas : public PAINTSTRUCT 00490 { 00491 PaintCanvas(HWND hwnd) 00492 : _hwnd(hwnd) 00493 { 00494 BeginPaint(hwnd, this); 00495 } 00496 00497 ~PaintCanvas() 00498 { 00499 EndPaint(_hwnd, this); 00500 } 00501 00502 operator HDC() const {return hdc;} 00503 00504 protected: 00505 HWND _hwnd; 00506 }; 00507 00509 struct Canvas 00510 { 00511 Canvas(HDC hdc) : _hdc(hdc) {} 00512 00513 operator HDC() {return _hdc;} 00514 00515 protected: 00516 HDC _hdc; 00517 }; 00518 00520 struct WindowCanvas : public Canvas 00521 { 00522 WindowCanvas(HWND hwnd) 00523 : Canvas(GetDC(hwnd)), _hwnd(hwnd) {} 00524 00525 ~WindowCanvas() {ReleaseDC(_hwnd, _hdc);} 00526 00527 protected: 00528 HWND _hwnd; 00529 }; 00530 00531 00532 // double buffering classes 00533 00535 struct MemCanvas : public Canvas 00536 { 00537 MemCanvas(HDC hdc=0) 00538 : Canvas(CreateCompatibleDC(hdc)) {assert(_hdc);} 00539 00540 ~MemCanvas() {DeleteDC(_hdc);} 00541 }; 00542 00544 struct SelectedBitmap 00545 { 00546 SelectedBitmap(HDC hdc, HBITMAP hbmp) 00547 : _hdc(hdc), _old_hbmp(SelectBitmap(hdc, hbmp)) {} 00548 00549 ~SelectedBitmap() {DeleteObject(SelectBitmap(_hdc, _old_hbmp));} 00550 00551 protected: 00552 HDC _hdc; 00553 HBITMAP _old_hbmp; 00554 }; 00555 00557 struct BufferCanvas : public MemCanvas 00558 { 00559 BufferCanvas(HDC hdc, int x, int y, int w, int h) 00560 : MemCanvas(hdc), _hdctarg(hdc), 00561 _x(x), _y(y), _w(w), _h(h), 00562 _bmp(_hdc, CreateCompatibleBitmap(hdc, w, h)) {} 00563 00564 BufferCanvas(HDC hdc, const RECT& rect) 00565 : MemCanvas(hdc), _hdctarg(hdc), 00566 _x(rect.left), _y(rect.top), _w(rect.right-rect.left), _h(rect.bottom-rect.top), 00567 _bmp(_hdc, CreateCompatibleBitmap(hdc, _w, _h)) {} 00568 00569 protected: 00570 HDC _hdctarg; 00571 int _x, _y, _w, _h; 00572 SelectedBitmap _bmp; 00573 }; 00574 00576 struct BufferedCanvas : public BufferCanvas 00577 { 00578 BufferedCanvas(HDC hdc, int x, int y, int w, int h, DWORD mode=SRCCOPY) 00579 : BufferCanvas(hdc, x, y, w, h), _mode(mode) {} 00580 00581 BufferedCanvas(HDC hdc, const RECT& rect, DWORD mode=SRCCOPY) 00582 : BufferCanvas(hdc, rect), _mode(mode) {} 00583 00584 ~BufferedCanvas() {BitBlt(_hdctarg, _x, _y, _w, _h, _hdc, 0, 0, _mode);} 00585 00586 DWORD _mode; 00587 }; 00588 00590 struct BufferedPaintCanvas : public PaintCanvas, public BufferedCanvas 00591 { 00592 BufferedPaintCanvas(HWND hwnd) 00593 : PaintCanvas(hwnd), 00594 BufferedCanvas(PAINTSTRUCT::hdc, 0, 0, rcPaint.right, rcPaint.bottom) 00595 { 00596 } 00597 00598 operator HDC() {return BufferedCanvas::_hdc;} 00599 }; 00600 00601 00603 struct TextColor 00604 { 00605 TextColor(HDC hdc, COLORREF color) 00606 : _hdc(hdc), _old_color(SetTextColor(hdc, color)) {} 00607 00608 ~TextColor() {SetTextColor(_hdc, _old_color);} 00609 00610 protected: 00611 HDC _hdc; 00612 COLORREF _old_color; 00613 }; 00614 00616 struct BkMode 00617 { 00618 BkMode(HDC hdc, int bkmode) 00619 : _hdc(hdc), _old_bkmode(SetBkMode(hdc, bkmode)) {} 00620 00621 ~BkMode() {SetBkMode(_hdc, _old_bkmode);} 00622 00623 protected: 00624 HDC _hdc; 00625 COLORREF _old_bkmode; 00626 }; 00627 00629 struct FontSelection 00630 { 00631 FontSelection(HDC hdc, HFONT hFont) 00632 : _hdc(hdc), _old_hFont(SelectFont(hdc, hFont)) {} 00633 00634 ~FontSelection() {SelectFont(_hdc, _old_hFont);} 00635 00636 protected: 00637 HDC _hdc; 00638 HFONT _old_hFont; 00639 }; 00640 00642 struct BitmapSelection 00643 { 00644 BitmapSelection(HDC hdc, HBITMAP hBmp) 00645 : _hdc(hdc), _old_hBmp(SelectBitmap(hdc, hBmp)) {} 00646 00647 ~BitmapSelection() {SelectBitmap(_hdc, _old_hBmp);} 00648 00649 protected: 00650 HDC _hdc; 00651 HBITMAP _old_hBmp; 00652 }; 00653 00655 struct BrushSelection 00656 { 00657 BrushSelection(HDC hdc, HBRUSH hBrush) 00658 : _hdc(hdc), _old_hBrush(SelectBrush(hdc, hBrush)) {} 00659 00660 ~BrushSelection() {SelectBrush(_hdc, _old_hBrush);} 00661 00662 protected: 00663 HDC _hdc; 00664 HBRUSH _old_hBrush; 00665 }; 00666 00667 00669 struct PopupMenu 00670 { 00671 PopupMenu() 00672 : _hmenu(CreatePopupMenu()) 00673 { 00674 } 00675 00676 ~PopupMenu() 00677 { 00678 DestroyMenu(_hmenu); 00679 } 00680 00681 PopupMenu(UINT nid); 00682 00683 operator HMENU() {return _hmenu;} 00684 00685 void Append(UINT id, LPCTSTR str, UINT flags=MF_STRING) 00686 { 00687 AppendMenu(_hmenu, flags, id, str); 00688 } 00689 00690 int TrackPopupMenu(HWND hwnd, const POINT& pt, UINT flags=TPM_LEFTBUTTON|TPM_RIGHTBUTTON, LPTPMPARAMS tpm=NULL) { 00691 return TrackPopupMenuEx(_hmenu, flags, pt.x, pt.y, hwnd, tpm); 00692 } 00693 00694 int PopupContextMenu(HWND hwnd, POINTS pos, UINT flags=TPM_LEFTBUTTON|TPM_RIGHTBUTTON) { 00695 POINT pt; 00696 pt.x = pos.x; 00697 pt.y = pos.y; 00698 return TrackPopupMenuEx(_hmenu, flags, pt.x, pt.y, hwnd, NULL); 00699 } 00700 00701 int TrackPopupMenu(HWND hwnd, POINTS pos, UINT flags=TPM_LEFTBUTTON|TPM_RIGHTBUTTON) { 00702 POINT pt; 00703 pt.x = pos.x; 00704 pt.y = pos.y; 00705 ClientToScreen(hwnd, &pt); 00706 return TrackPopupMenuEx(_hmenu, flags, pt.x, pt.y, hwnd, NULL); 00707 } 00708 00709 int TrackPopupMenuAtCursor(HWND hwnd, UINT flags=TPM_LEFTBUTTON) { 00710 POINT pt; GetCursorPos(&pt); 00711 return TrackPopupMenuEx(_hmenu, flags, pt.x, pt.y, hwnd, NULL); 00712 } 00713 00714 int TrackPopupMenuAtPos(HWND hwnd, DWORD pos, UINT flags=TPM_LEFTBUTTON) { 00715 return TrackPopupMenuEx(_hmenu, flags, GET_X_LPARAM(pos), GET_Y_LPARAM(pos), hwnd, NULL); 00716 } 00717 00718 protected: 00719 HMENU _hmenu; 00720 }; 00721 00722 00723 struct Variant : public VARIANT 00724 { 00725 Variant() {VariantInit(this);} 00726 Variant(const VARIANT& var); 00727 Variant(const VARIANT* var); 00728 ~Variant(); 00729 00730 operator long() const; 00731 operator bool() const; 00732 operator VARIANT_BOOL() const; 00733 operator IDispatch*() const; 00734 }; 00735 00736 00737 struct BStr 00738 { 00739 BStr() 00740 { 00741 _p = NULL; 00742 } 00743 00744 BStr(const BSTR s) 00745 { 00746 _p = SysAllocString(s); 00747 } 00748 00749 BStr(LPCSTR s) 00750 { 00751 WCHAR b[BUFFER_LEN]; 00752 00753 if (s) 00754 _p = SysAllocStringLen(b, MultiByteToWideChar(CP_ACP, 0, s, -1, b, BUFFER_LEN)-1); 00755 else 00756 _p = NULL; 00757 } 00758 00759 BStr(LPCWSTR s) 00760 { 00761 _p = SysAllocString(s); 00762 } 00763 00764 BStr(const VARIANT& var) 00765 : _p(NULL) 00766 { 00767 assign(var); 00768 } 00769 00770 ~BStr() 00771 { 00772 SysFreeString(_p); 00773 } 00774 00775 void assign(BSTR s); 00776 void assign(const VARIANT& var); 00777 00778 operator BSTR() const 00779 { 00780 return _p? _p: (BSTR)L""; 00781 } 00782 00783 int length() const 00784 { 00785 return _p? wcslen(_p): 0; 00786 } 00787 00788 protected: 00789 BSTR _p; 00790 }; 00791 00792 00794 struct String 00795 #ifdef UNICODE 00796 : public wstring 00797 #else 00798 : public string 00799 #endif 00800 { 00801 #ifdef UNICODE 00802 typedef wstring super; 00803 #else 00804 typedef string super; 00805 #endif 00806 00807 String() {} 00808 00809 String(LPCTSTR s) {if (s) super::assign(s);} 00810 String(LPCTSTR s, int l) : super(s, l) {} 00811 00812 String(const super& other) : super(other) {} 00813 String(const String& other) : super(other) {} 00814 00815 #ifdef UNICODE 00816 String(LPCSTR s) {assign(s);} 00817 String(LPCSTR s, int l) {assign(s, l);} 00818 String(const string& other) {assign(other.c_str());} 00819 String& operator=(LPCSTR s) {assign(s); return *this;} 00820 void assign(LPCSTR s) {if (s) {TCHAR b[BUFFER_LEN]; super::assign(b, MultiByteToWideChar(CP_ACP, 0, s, -1, b, BUFFER_LEN)-1);} else erase();} 00821 void assign(LPCSTR s, int l) {if (s) {TCHAR b[BUFFER_LEN]; super::assign(b, MultiByteToWideChar(CP_ACP, 0, s, l, b, BUFFER_LEN));} else erase();} 00822 void assign(const BStr& s) {int l = s.length(); super::assign(s, l);} 00823 #else 00824 String(LPCWSTR s) {assign(s);} 00825 String(LPCWSTR s, int l) {assign(s, l);} 00826 String(const wstring& other) {assign(other.c_str());} 00827 String& operator=(LPCWSTR s) {assign(s); return *this;} 00828 void assign(LPCWSTR s) {if (s) {char b[BUFFER_LEN]; super::assign(b, WideCharToMultiByte(CP_ACP, 0, s, -1, b, BUFFER_LEN, 0, 0)-1);} else erase();} 00829 void assign(LPCWSTR s, int l) {if (s) {char b[BUFFER_LEN]; super::assign(b, WideCharToMultiByte(CP_ACP, 0, s, l, b, BUFFER_LEN, 0, 0));} else erase();} 00830 void assign(const BStr& s) {int l = s.length(); if (l) {char b[BUFFER_LEN]; super::assign(b, WideCharToMultiByte(CP_ACP, 0, s, l, b, BUFFER_LEN, 0, 0));} else erase();} 00831 #endif 00832 String(const BStr& s) {assign(s);} 00833 String& operator=(const BStr& s) {assign(s); return *this;} 00834 00835 String& operator=(LPCTSTR s) {if (s) super::assign(s); else erase(); return *this;} 00836 String& operator=(const super& s) {super::assign(s); return *this;} 00837 void assign(LPCTSTR s) {super::assign(s);} 00838 void assign(LPCTSTR s, int l) {super::assign(s, l);} 00839 00840 operator LPCTSTR() const {return c_str();} 00841 00842 #ifdef UNICODE 00843 operator string() const {char b[BUFFER_LEN]; return string(b, WideCharToMultiByte(CP_ACP, 0, c_str(), -1, b, BUFFER_LEN, 0, 0)-1);} 00844 #else 00845 operator wstring() const {WCHAR b[BUFFER_LEN]; return wstring(b, MultiByteToWideChar(CP_ACP, 0, c_str(), -1, b, BUFFER_LEN)-1);} 00846 #endif 00847 00848 LPTSTR str() {return (LPTSTR)data();} 00849 00850 String& printf(LPCTSTR fmt, ...) 00851 { 00852 va_list l; 00853 TCHAR b[BUFFER_LEN]; 00854 00855 va_start(l, fmt); 00856 super::assign(b, _vsntprintf(b, COUNTOF(b), fmt, l)); 00857 va_end(l); 00858 00859 return *this; 00860 } 00861 00862 String& vprintf(LPCTSTR fmt, va_list l) 00863 { 00864 TCHAR b[BUFFER_LEN]; 00865 00866 super::assign(b, _vsntprintf(b, COUNTOF(b), fmt, l)); 00867 00868 return *this; 00869 } 00870 00871 String& appendf(LPCTSTR fmt, ...) 00872 { 00873 va_list l; 00874 TCHAR b[BUFFER_LEN]; 00875 00876 va_start(l, fmt); 00877 super::append(b, _vsntprintf(b, COUNTOF(b), fmt, l)); 00878 va_end(l); 00879 00880 return *this; 00881 } 00882 00883 String& vappendf(LPCTSTR fmt, va_list l) 00884 { 00885 TCHAR b[BUFFER_LEN]; 00886 00887 super::append(b, _vsntprintf(b, COUNTOF(b), fmt, l)); 00888 00889 return *this; 00890 } 00891 00892 void toLower() 00893 { 00894 if (!empty()) 00895 CharLower(str()); 00896 } 00897 }; 00898 00899 #define _STRING_DEFINED 00900 00901 00902 struct FmtString : public String 00903 { 00904 FmtString(LPCTSTR fmt, ...) 00905 { 00906 va_list l; 00907 00908 va_start(l, fmt); 00909 vprintf(fmt, l); 00910 va_end(l); 00911 } 00912 }; 00913 00914 00915 #ifdef UNICODE 00916 00917 struct ANS 00918 { 00919 ANS(LPCWSTR s) 00920 { 00921 int l = wcslen(s) + 1; 00922 _str = (LPSTR) malloc(2*l); 00923 00924 if (WideCharToMultiByte(CP_ACP, 0, s, -1, _str, 2*l, 0, 0) <= 0) 00925 *_str = '\0'; 00926 } 00927 00928 ~ANS() 00929 { 00930 free(_str); 00931 } 00932 00933 operator LPCSTR() {return _str;} 00934 00935 protected: 00936 LPSTR _str; 00937 }; 00938 00939 #define UNC(x) ((LPCWSTR)(x)) 00940 00941 #else 00942 00943 #define ANS(x) ((LPCSTR)(x)) 00944 00945 struct UNC 00946 { 00947 UNC(LPCSTR s) 00948 { 00949 int l = strlen(s) + 1; 00950 _str = (LPWSTR) malloc(2*l); 00951 00952 if (_str && MultiByteToWideChar(CP_ACP, 0, s, -1, _str, l) <= 0) 00953 *_str = '\0'; 00954 } 00955 00956 ~UNC() 00957 { 00958 free(_str); 00959 } 00960 00961 operator LPCWSTR() {return _str;} 00962 00963 protected: 00964 LPWSTR _str; 00965 }; 00966 00967 #endif 00968 00969 00970 // determine windows version string 00971 String get_windows_version_str(); 00972 00973 00975 template<typename FCT> struct DynamicFct 00976 { 00977 DynamicFct(LPCTSTR moduleName, UINT ordinal) 00978 { 00979 HMODULE hModule = GetModuleHandle(moduleName); 00980 00981 _fct = (FCT) GetProcAddress(hModule, (LPCSTR)ordinal); 00982 } 00983 00984 DynamicFct(LPCTSTR moduleName, LPCSTR name) 00985 { 00986 HMODULE hModule = GetModuleHandle(moduleName); 00987 00988 _fct = (FCT) GetProcAddress(hModule, name); 00989 } 00990 00991 FCT operator*() const {return _fct;} 00992 operator bool() const {return _fct? true: false;} 00993 00994 protected: 00995 FCT _fct; 00996 }; 00997 00998 01000 template<typename FCT> struct DynamicLoadLibFct 01001 { 01002 DynamicLoadLibFct(LPCTSTR moduleName, UINT ordinal) 01003 { 01004 _hModule = LoadLibrary(moduleName); 01005 01006 _fct = (FCT) GetProcAddress(_hModule, (LPCSTR)ordinal); 01007 } 01008 01009 DynamicLoadLibFct(LPCTSTR moduleName, LPCSTR name) 01010 { 01011 _hModule = LoadLibrary(moduleName); 01012 01013 _fct = (FCT) GetProcAddress(_hModule, name); 01014 } 01015 01016 ~DynamicLoadLibFct() 01017 { 01018 FreeLibrary(_hModule); 01019 } 01020 01021 FCT operator*() const {return _fct;} 01022 operator bool() const {return _fct? true: false;} 01023 01024 protected: 01025 HMODULE _hModule; 01026 FCT _fct; 01027 }; 01028 01029 01030 struct Context 01031 { 01032 Context(const char* ctx) 01033 : _ctx(ctx) 01034 { 01035 _last = s_current; 01036 s_current = this; 01037 } 01038 01039 Context(const char* ctx, LPCSTR obj) 01040 : _ctx(ctx), 01041 _obj(obj) 01042 { 01043 _last = s_current; 01044 s_current = this; 01045 } 01046 01047 Context(const char* ctx, LPCWSTR obj) 01048 : _ctx(ctx), 01049 _obj(obj) 01050 { 01051 _last = s_current; 01052 s_current = this; 01053 } 01054 01055 Context(const Context& other) 01056 : _ctx(other._ctx), 01057 _obj(other._obj) 01058 { 01059 _last = NULL; 01060 } 01061 01062 ~Context() 01063 { 01064 if (_last) { 01065 s_current = _last; 01066 _last = NULL; 01067 } 01068 } 01069 01070 String toString() const; 01071 String getStackTrace() const; 01072 01073 const char* _ctx; 01074 String _obj; 01075 01076 static Context& current() {return *s_current;} 01077 01078 protected: 01079 Context* _last; 01080 01081 static Context* s_current; 01082 static Context s_main; 01083 }; 01084 01085 #define CONTEXT_OBJ __ctx__._obj 01086 #define CONTEXT(c) Context __ctx__(c) 01087 #define CURRENT_CONTEXT Context::current() 01088 #define OBJ_CONTEXT(c, o) Context __ctx__(c, o) 01089 01090 01091 extern bool SplitFileSysURL(LPCTSTR url, String& dir_out, String& fname_out); 01092 01093 01094 #endif // __cplusplus Generated on Sun May 27 2012 04:18:42 for ReactOS by
1.7.6.1
|