Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/src/win32.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 121 - (show annotations) (download) (as text)
Tue Jan 8 07:04:36 2013 UTC (11 years, 3 months ago) by z0rac
File MIME type: text/x-chdr
File size: 4942 byte(s)


1 #ifndef H_WIN32 /* -*- mode: c++ -*- */
2 /*
3 * Copyright (C) 2009-2013 TSUBAKIMOTO Hiroya <z0rac@users.sourceforge.jp>
4 *
5 * This software comes with ABSOLUTELY NO WARRANTY; for details of
6 * the license terms, see the LICENSE.txt file included with the program.
7 */
8 #define H_WIN32
9
10 #include <exception>
11 #include <list>
12 #include <string>
13 #include <windows.h>
14
15 using namespace std;
16
17 class win32 {
18 typedef int (WINAPI* _dtfn)(LCID,DWORD,const SYSTEMTIME*,LPCSTR,LPSTR,int);
19 static string _datetime(time_t utc, DWORD flags, _dtfn fn);
20 public:
21 win32(LPCSTR mutex = NULL);
22
23 static string profile(LPCSTR section, LPCSTR key, LPCSTR file);
24 static void profile(LPCSTR section, LPCSTR key, LPCSTR value, LPCSTR file);
25 static string xenv(const string& s);
26 static string date(time_t utc, DWORD flags = 0)
27 { return _datetime(utc, flags, GetDateFormat); }
28 static string time(time_t utc, DWORD flags = 0)
29 { return _datetime(utc, flags, GetTimeFormat); }
30 static HANDLE shell(const string& cmd, unsigned flags = 0);
31 #ifdef _MT
32 static HANDLE thread(void (*func)(void*), void* param);
33 #endif
34 #ifdef _DEBUG
35 static size_t cheapsize();
36 static size_t heapsize();
37 #endif
38 public:
39 // module - module(EXE/DLL) handler
40 class module {
41 HMODULE _h;
42 public:
43 module(HMODULE h = NULL) : _h(h) {}
44 operator HMODULE() const { return _h; }
45 FARPROC operator()(LPCSTR name) const;
46 FARPROC operator()(LPCSTR name, FARPROC def) const;
47 public:
48 string text(UINT id) const;
49 string textf(UINT id, ...) const;
50 list<string> texts(UINT id) const;
51 void* resource(LPCSTR type, LPCSTR name) const;
52 };
53 static const module exe;
54
55 // dll - DLL loader
56 class dll : public module {
57 dll(const dll&); void operator=(const dll&); // disable to copy
58 public:
59 dll(LPCSTR file, DWORD flags = 0)
60 : module(LoadLibraryEx(file, NULL, flags)) {}
61 ~dll() { *this && FreeLibrary(*this); }
62 };
63
64 // mex - exclusive control
65 class mex {
66 CRITICAL_SECTION _cs;
67 mex(const mex&); void operator=(const mex&); // disable to copy
68 public:
69 mex() { InitializeCriticalSectionAndSpinCount(&_cs, 4000); }
70 ~mex() { DeleteCriticalSection(&_cs); }
71 public:
72 class lock {
73 mex& _m;
74 lock(const lock&); void operator=(const lock&);
75 public:
76 lock(mex& m) : _m(m) { EnterCriticalSection(&m._cs); }
77 ~lock() { LeaveCriticalSection(&_m._cs); }
78 };
79 friend class lock;
80 public:
81 class trylock {
82 mex* _mp;
83 trylock(const trylock&); void operator=(const trylock&);
84 public:
85 trylock(mex& m) : _mp(TryEnterCriticalSection(&m._cs) ? &m : NULL) {}
86 ~trylock() { if (_mp) LeaveCriticalSection(&_mp->_cs); }
87 operator bool() const { return _mp != NULL; }
88 };
89 friend class trylock;
90 };
91
92 // textbuf - text buffer template
93 template<typename _Ty>
94 struct textbuf {
95 _Ty* data;
96 textbuf() : data(NULL) {}
97 textbuf(size_t n) : data(new _Ty[n]) {}
98 ~textbuf() { delete [] data; }
99 _Ty* operator()(size_t n)
100 { delete [] data, data = NULL; return data = new _Ty[n]; }
101 };
102
103 // wstr - wide character string
104 class wstr {
105 LPWSTR _data;
106 static LPWSTR _new(LPCSTR s, UINT cp);
107 public:
108 wstr() : _data(NULL) {}
109 wstr(LPCWSTR ws) : _data(NULL) { *this = ws; }
110 wstr(const wstr& ws) : _data(NULL) { *this = ws; }
111 wstr(LPCSTR s, UINT cp = CP_ACP) : _data(_new(s, cp)) {}
112 wstr(const string& s, UINT cp = CP_ACP) : _data(_new(s.c_str(), cp)) {}
113 ~wstr() { delete [] _data; }
114 wstr& operator=(LPCWSTR ws);
115 wstr& operator=(const wstr& ws) { return operator=(LPCWSTR(ws)); }
116 wstr& operator+=(LPCWSTR ws);
117 operator LPCWSTR() const { return _data; }
118 size_t size() const { return _data ? lstrlenW(_data) : 0; }
119 string mbstr(UINT cp = CP_ACP) const { return mbstr(_data, cp); }
120 static string mbstr(LPCWSTR ws, UINT cp = CP_ACP);
121 };
122
123 // find - find files with the path
124 class find : public WIN32_FIND_DATA {
125 HANDLE _h;
126 WIN32_FIND_DATA* _fd() { return this; }
127 void _close() { FindClose(_h), _h = INVALID_HANDLE_VALUE; }
128 public:
129 find(LPCSTR path) : _h(FindFirstFile(path, _fd())) {}
130 find(const string& path) : _h(FindFirstFile(path.c_str(), _fd())) {}
131 ~find() { if (_h != INVALID_HANDLE_VALUE) _close(); }
132 operator bool() const { return _h != INVALID_HANDLE_VALUE; }
133 void next() { if (!FindNextFile(_h, this)) _close(); }
134 };
135
136 // error - exception type
137 class error : public exception {
138 string _msg;
139 public:
140 error() : _msg(emsg()) {}
141 error(const string& msg) : _msg(msg) {}
142 ~error() throw() {}
143 const char* what() const throw() { return _msg.c_str(); }
144 static string emsg();
145 };
146 template<typename _Th>
147 static _Th valid(_Th h) { if (h) return h; throw error(); }
148 };
149
150 #endif

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26