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

IBrowser Source Code Documentation
generated on 18.09.2008 by doxygen

Homepage