-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathwinapi.h
More file actions
5515 lines (5292 loc) · 197 KB
/
winapi.h
File metadata and controls
5515 lines (5292 loc) · 197 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**********************************************************************************************
*
* This file is part of the WinAPI Wrapper library.
*
* Copyright (c) 2001-2003 Chris Dragan. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom
* the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Current Version: 1.05.01
* Version Date: 5.12.2003
*
**********************************************************************************************/
// Configuration (must be defined before including this header):
// SKIP_STL - don't use STL (functions using STL are not available)
// PURE_WRAPPER - don't use any extra methods for windows and dialogs
// Include this header only once
#ifndef MINGW
#pragma once
#endif
#ifndef _WIN32_IE
#define _WIN32_IE 0x0400
#endif
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0400
#endif
// Wrapper version
#define __WINAPI_WRAPPER_VERSION 0x0105
// Include Windows headers
#if defined(_MSC_VER) && (_MSC_VER >= 1300)
#include <winsock2.h>
#endif
#include <windows.h>
#include <commctrl.h>
#include <commdlg.h>
#include <shlobj.h>
// Undefine Windows macros colliding with STL names
#undef min
#undef max
// Include STL
#include <cassert>
#ifndef SKIP_STL
#if defined(_MSC_VER) && (_MSC_VER < 1300)
#pragma warning( disable: 4786 )
#endif
#include <string>
#include <vector>
#endif
// Defined module handle - for regular executables this always equals 0x00400000
#ifndef __MODULE_HANDLE
#define __MODULE_HANDLE reinterpret_cast<HINSTANCE>(0x00400000)
#endif
//#define __MODULEHANDLE GetModuleHandle(0)
namespace WinAPI {
#ifndef SKIP_STL
using std::string;
using std::vector;
#endif
/**********************************************************************************************
* windows_cast - for avoiding annoying warnings during compilation (64-bit compatibility)
**********************************************************************************************/
template< class Out, class In >
inline Out windows_cast( In x )
{
union {
In x;
Out y;
} convert;
convert.y = 0;
convert.x = x;
return convert.y;
}
/**********************************************************************************************
* NonCopyAble - prevents derived objects from being copied
**********************************************************************************************/
class NonCopyAble {
protected:
NonCopyAble() { }
~NonCopyAble() { }
private:
NonCopyAble( const NonCopyAble& );
NonCopyAble& operator=( const NonCopyAble& );
};
/**********************************************************************************************
* File
**********************************************************************************************/
class File: public NonCopyAble {
HANDLE hFile;
public:
File(): hFile( INVALID_HANDLE_VALUE ) { }
File( LPCTSTR lpszFileName,
DWORD dwCreationDisposition = OPEN_EXISTING,
DWORD dwDesiredAccess = GENERIC_READ | GENERIC_WRITE,
DWORD dwFlagsAndAttributes = FILE_ATTRIBUTE_ARCHIVE | FILE_FLAG_RANDOM_ACCESS,
DWORD dwShareMode = FILE_SHARE_READ,
HANDLE hTemplateFile = 0,
LPSECURITY_ATTRIBUTES lpSecurityAttributes = 0 );
~File() {
Close();
}
operator HANDLE() const {
return hFile;
}
bool Open( LPCTSTR lpszFileName,
DWORD dwCreationDisposition = OPEN_EXISTING,
DWORD dwDesiredAccess = GENERIC_READ | GENERIC_WRITE,
DWORD dwFlagsAndAttributes = FILE_ATTRIBUTE_ARCHIVE | FILE_FLAG_RANDOM_ACCESS,
DWORD dwShareMode = FILE_SHARE_READ,
HANDLE hTemplateFile = 0,
LPSECURITY_ATTRIBUTES lpSecurityAttributes = 0 );
bool SetTo( LPCTSTR lpszFileName,
DWORD dwCreationDisposition = OPEN_EXISTING,
DWORD dwDesiredAccess = GENERIC_READ | GENERIC_WRITE,
DWORD dwFlagsAndAttributes = FILE_ATTRIBUTE_ARCHIVE | FILE_FLAG_RANDOM_ACCESS,
DWORD dwShareMode = FILE_SHARE_READ,
HANDLE hTemplateFile = 0,
LPSECURITY_ATTRIBUTES lpSecurityAttributes = 0 ) {
return Open( lpszFileName, dwCreationDisposition, dwDesiredAccess, dwFlagsAndAttributes, dwShareMode, hTemplateFile, lpSecurityAttributes );
}
bool IsOpen() const {
return hFile != INVALID_HANDLE_VALUE;
}
void Close();
DWORD Read( LPVOID lpBuffer, DWORD nNumberOfBytesToRead, LPOVERLAPPED lpOverlapped = 0 ) const;
DWORD Write( LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite, LPOVERLAPPED lpOverlapped = 0 );
template< typename T >
bool ReadVar( T* p_variable, LPOVERLAPPED lpOverlapped = 0 ) const {
return sizeof(T) == Read( p_variable, sizeof(T), lpOverlapped );
}
template< typename T >
bool WriteVar( const T& variable, LPOVERLAPPED lpOverlapped = 0 ) {
return sizeof(T) == Write( &variable, sizeof(T), lpOverlapped );
}
#ifndef SKIP_STL
template< typename T >
bool ReadVec( vector<T>* p_vec, LPOVERLAPPED lpOverlapped = 0 ) const {
const size_t len = sizeof(T) * p_vec->size();
return len == Read( &(*p_vec)[0], len, lpOverlapped );
}
template< typename T >
bool WriteVec( const vector<T>& vec, LPOVERLAPPED lpOverlapped = 0 ) {
const size_t len = sizeof(T) * vec->size();
return len == Read( &vec[0], len, lpOverlapped );
}
#endif
int GetSize() const {
return GetFileSize( hFile, 0 );
}
int Seek( int nDistanceToMove, DWORD dwMoveMethod = FILE_BEGIN ) const {
return static_cast<int>( SetFilePointer( hFile, static_cast<LONG>( nDistanceToMove ), 0, dwMoveMethod ) );
}
int GetPosition() const {
return Seek( 0, FILE_CURRENT );
}
bool Truncate() {
return SetEndOfFile( hFile ) != 0;
}
bool Flush() const {
return FlushFileBuffers( hFile ) != 0;
}
};
inline File::File( LPCTSTR lpszFileName, DWORD dwCreationDisposition, DWORD dwDesiredAccess, DWORD dwFlagsAndAttributes, DWORD dwShareMode, HANDLE hTemplateFile, LPSECURITY_ATTRIBUTES lpSecurityAttributes )
: hFile( INVALID_HANDLE_VALUE )
{
Open( lpszFileName, dwCreationDisposition, dwDesiredAccess, dwFlagsAndAttributes,
dwShareMode, hTemplateFile, lpSecurityAttributes );
}
inline bool File::Open( LPCTSTR lpszFileName, DWORD dwCreationDisposition, DWORD dwDesiredAccess, DWORD dwFlagsAndAttributes, DWORD dwShareMode, HANDLE hTemplateFile, LPSECURITY_ATTRIBUTES lpSecurityAttributes )
{
Close();
hFile = CreateFile( lpszFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes,
dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile );
return hFile != INVALID_HANDLE_VALUE;
}
inline void File::Close()
{
if ( IsOpen() ) {
CloseHandle( hFile );
hFile = INVALID_HANDLE_VALUE;
}
}
inline DWORD File::Read( LPVOID lpBuffer, DWORD nNumberOfBytesToRead, LPOVERLAPPED lpOverlapped ) const
{
DWORD dwNumRead;
if ( ! ReadFile( hFile, lpBuffer, nNumberOfBytesToRead, &dwNumRead, lpOverlapped ) )
return DWORD(-1);
return dwNumRead;
}
inline DWORD File::Write( LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite, LPOVERLAPPED lpOverlapped )
{
DWORD dwNumWrit;
if ( ! WriteFile( hFile, lpBuffer, nNumberOfBytesToWrite, &dwNumWrit, lpOverlapped ) )
return DWORD(-1);
return dwNumWrit;
}
/**********************************************************************************************
* FindFile
**********************************************************************************************/
class FindFile: public NonCopyAble {
HANDLE hFindFile;
mutable WIN32_FIND_DATA data;
public:
FindFile( LPCTSTR lpFileName = "*.*" ): hFindFile( INVALID_HANDLE_VALUE ) {
hFindFile = FindFirstFile( lpFileName, &data );
}
~FindFile() {
Close();
}
operator HANDLE() const {
return hFindFile;
}
bool IsOpen() const {
return hFindFile != INVALID_HANDLE_VALUE;
}
bool FindNext() const {
return FindNextFile( hFindFile, &data ) != 0;
}
const WIN32_FIND_DATA& GetFindData() const {
return data;
}
bool SetTo( LPCTSTR lpFileName = "*.*" ) {
Close();
hFindFile = FindFirstFile( lpFileName, &data );
return IsOpen();
}
void Close() {
if ( hFindFile != INVALID_HANDLE_VALUE )
FindClose( hFindFile );
}
class iterator: public WIN32_FIND_DATA {
HANDLE hFindFile;
public:
iterator( const WIN32_FIND_DATA& fd, HANDLE hFindFile ): WIN32_FIND_DATA( fd ), hFindFile( hFindFile ) { }
iterator(): hFindFile( INVALID_HANDLE_VALUE ) { cFileName[0] = 0; }
void operator++() {
if ( FindNextFile( hFindFile, this ) != TRUE )
cFileName[0] = 0;
}
void operator--() {
if ( FindNextFile( hFindFile, this ) != TRUE )
cFileName[0] = 0;
}
bool operator==( const iterator& fd ) const {
return cFileName[0] == fd.cFileName[0];
}
bool operator!=( const iterator& fd ) const {
return cFileName[0] != fd.cFileName[0];
}
};
iterator begin() const { return iterator( data, hFindFile ); }
iterator end() const { return iterator(); }
enum FINDTYPE { ALL, FILES, DIRECTORIES };
template< class T >
void ForEach( T& handler, FINDTYPE ft = ALL ) {
if ( IsOpen() ) do {
const bool b_directory = (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
if ( ft == ALL )
handler( data );
else if ( ft == FILES ) {
if ( ! b_directory ) handler( data );
} else
if ( b_directory ) handler( data );
} while ( FindNext() );
}
template< class T >
static void ForEach( LPCTSTR lpFileName, T& handler, FINDTYPE ft = ALL ) {
FindFile finder( lpFileName );
finder.ForEach( handler, ft );
}
#ifndef SKIP_STL
class StringHandler {
vector<string>& filenames;
public:
StringHandler( vector<string>& filenames ): filenames( filenames ) { }
void operator()( const WIN32_FIND_DATA& fd ) { filenames.push_back( string( fd.cFileName ) ); }
};
class FindDataHandler {
vector<WIN32_FIND_DATA>& files;
public:
FindDataHandler( vector<WIN32_FIND_DATA>& files ): files( files ) { }
void operator()( const WIN32_FIND_DATA& fd ) { files.push_back( fd ); }
};
void GetAll( vector<string>* p_files, FINDTYPE ft = ALL ) {
assert( p_files != 0 );
p_files->clear();
StringHandler handler( *p_files );
ForEach( handler, ft );
}
void GetAll( vector<WIN32_FIND_DATA>* p_files, FINDTYPE ft = ALL ) {
assert( p_files != 0 );
p_files->clear();
FindDataHandler handler( *p_files );
ForEach( handler, ft );
}
static void GetAll( LPCTSTR lpFileName, vector<string>* p_files, FINDTYPE ft = ALL ) {
FindFile finder( lpFileName );
finder.GetAll( p_files, ft );
}
static void GetAll( LPCTSTR lpFileName, vector<WIN32_FIND_DATA>* p_files, FINDTYPE ft = ALL ) {
FindFile finder( lpFileName );
finder.GetAll( p_files, ft );
}
#endif
};
/**********************************************************************************************
* FileMapping
**********************************************************************************************/
class FileMapping: public NonCopyAble {
HANDLE hFile;
HANDLE hMapping;
public:
FileMapping(): hFile( INVALID_HANDLE_VALUE ), hMapping( NULL ) { }
FileMapping( File& file, DWORD flProtect = PAGE_READWRITE, DWORD dwMaximumSize = 0, LPCSTR lpName = 0, LPSECURITY_ATTRIBUTES lpAttributes = 0 ): hFile( INVALID_HANDLE_VALUE ), hMapping( NULL ) {
SetTo( file, flProtect, dwMaximumSize, lpName, lpAttributes );
}
~FileMapping() {
Close();
}
bool IsOpen() const {
return hMapping != NULL;
}
bool Open( File& file, DWORD flProtect = PAGE_READWRITE, DWORD dwMaximumSize = 0, LPCSTR lpName = 0, LPSECURITY_ATTRIBUTES lpAttributes = 0 );
bool SetTo( File& file, DWORD flProtect = PAGE_READWRITE, DWORD dwMaximumSize = 0, LPCSTR lpName = 0, LPSECURITY_ATTRIBUTES lpAttributes = 0 ) {
return Open( file, flProtect, dwMaximumSize, lpName, lpAttributes );
}
void Close();
operator HANDLE() const {
return hMapping;
}
class Map: public NonCopyAble {
HANDLE hMapping;
void* address;
SIZE_T size;
public:
Map( FileMapping& mapping, DWORD dwDesiredAccess = FILE_MAP_ALL_ACCESS, DWORD dwFileOffset = 0, SIZE_T dwNumBytesToMap = 0 ): hMapping( mapping ), size( dwNumBytesToMap ), address( 0 ) {
address = MapViewOfFile( hMapping, dwDesiredAccess, 0, dwFileOffset, dwNumBytesToMap );
}
~Map() {
if ( address != 0 ) UnmapViewOfFile( address );
}
operator void*() const { return address; }
operator char*() const { return static_cast<char*>( address ); }
char& operator[]( size_t offset ) const { return static_cast<char*>( address )[ offset ]; }
int GetSize() const { return static_cast<int>( size ); }
int Size() const { return static_cast<int>( size ); }
};
};
inline void FileMapping::Close()
{
if ( IsOpen() ) {
CloseHandle( hMapping );
hMapping = NULL;
}
}
inline bool FileMapping::Open( File& file, DWORD flProtect, DWORD dwMaximumSize, LPCSTR lpName, LPSECURITY_ATTRIBUTES lpAttributes )
{
Close();
if ( ! file.IsOpen() )
return false;
hFile = file;
hMapping = CreateFileMapping( hFile, lpAttributes, flProtect, 0, dwMaximumSize, lpName );
return hMapping != NULL;
}
/**********************************************************************************************
* WPoint
**********************************************************************************************/
class WPoint: public POINT {
public:
WPoint() { x = y = 0; }
WPoint( int _x, int _y ) {
x = _x, y = _y;
}
WPoint( const POINT& pt ) {
x = pt.x, y = pt.y;
}
WPoint operator+( const POINT& p ) const {
return WPoint( x+p.x, y+p.y );
}
WPoint operator-( const POINT& p ) const {
return WPoint( x-p.x, y-p.y );
}
WPoint operator-() const {
return WPoint( -x, -y );
}
WPoint& operator+=( const POINT& p ) {
x += p.x, y += p.y; return *this;
}
WPoint& operator-=( const POINT& p ) {
x -= p.x, y -= p.y; return *this;
}
bool operator==( const POINT& p ) const {
return x == p.x && y == p.y;
}
bool operator!=( const POINT& p ) const {
return x != p.x || y != p.y;
}
};
/**********************************************************************************************
* WSize
**********************************************************************************************/
class WSize: public SIZE {
public:
WSize() { cx = cy = 0; }
WSize( int width, int height ) {
cx = width, cy = height;
}
WSize( const SIZE& size ) {
cx = size.cx, cy = size.cy;
}
WSize operator-() const {
return WSize( -cx, -cy );
}
WSize operator-( const SIZE& s ) const {
return WSize( cx - s.cx, cy - s.cy );
}
WSize operator+( const SIZE& s ) const {
return WSize( cx + s.cx, cy + s.cy );
}
WSize& operator-=( const SIZE& s ) {
cx -= s.cx, cy -= s.cy; return *this;
}
WSize& operator+=( const SIZE& s ) {
cx += s.cx, cy += s.cy; return *this;
}
bool operator==( const SIZE& s ) const {
return cx == s.cx && cy == s.cy;
}
bool operator!=( const SIZE& s ) const {
return cx != s.cx || cy != s.cy;
}
};
/**********************************************************************************************
* WRect
**********************************************************************************************/
class WRect: public RECT {
public:
WRect() { }
WRect( const RECT& rc ) {
left = rc.left, top = rc.top;
right = rc.right, bottom = rc.bottom;
}
WRect( const POINT& p1, const POINT& p2 ) {
left = p1.x, top = p1.y;
right = p2.x, bottom = p2.y;
}
WRect( const POINT& p1, int width, int height ) {
left = p1.x, top = p1.y;
right = p1.x+width, bottom = p1.y+height;
}
WRect( const POINT& p1, const SIZE& size ) {
left = p1.x, top = p1.y;
right = p1.x+size.cx, bottom = p1.y+size.cy;
}
WRect( int _left, int _top, int _right, int _bottom ) {
left = _left, top = _top;
right = _right, bottom = _bottom;
}
bool operator==( const RECT& r ) const {
return left == r.left && top == r.top && right == r.right && bottom == r.bottom;
}
bool operator!=( const RECT& r ) const {
return left != r.left || top != r.top || right != r.right || bottom != r.bottom;
}
WRect operator&( const RECT& r ) const {
return WRect( left < r.left ? r.left : left, top < r.top ? r.top : top, right > r.right ? r.right : right, bottom > r.bottom ? r.bottom : bottom );
}
WRect operator|( const RECT& r ) const {
return WRect( left < r.left ? left : r.left, top < r.top ? top : r.top, right > r.right ? right : r.right, bottom > r.bottom ? bottom : r.bottom );
}
int Width() const {
return right - left;
}
int Height() const {
return bottom - top;
}
WSize Size() const {
return WSize( Width(), Height() );
}
WSize GetSize() const {
return WSize( Width(), Height() );
}
void SetSize( int cx, int cy ) {
right = left + cx, bottom = top + cy;
}
void SetSize( const SIZE& size ) {
right = left + size.cx, bottom = top + size.cy;
}
bool Contains( const POINT& p ) const {
return (p.x >= left) && (p.x < right) && (p.y >= top) && (p.y < bottom);
}
bool Contains( const RECT& r ) const {
return left <= r.left && top <= r.top && right >= r.right && bottom >= r.bottom;
}
bool IsValid() const {
return left < right && top < bottom;
}
bool Intersects( const RECT& r ) const {
return left < r.right && right > r.left && top < r.bottom && bottom > r.top;
}
WPoint LeftTop() {
return WPoint( left, top );
}
WPoint LeftBottom() {
return WPoint( left, bottom );
}
WPoint RightTop() {
return WPoint( right, top );
}
WPoint RightBottom() {
return WPoint( right, bottom );
}
void InsetBy( int x, int y ) {
left += x, top += y, right -= x, bottom -= y;
}
void OffsetBy( int x, int y ) {
left += x, top += y, right += x, bottom += y;
}
void OffsetTo( int x, int y ) {
right += x - left, bottom += y - top; left = x, top = y;
}
void InsetBy( const POINT& p ) {
InsetBy( p.x, p.y );
}
void OffsetBy( const POINT& p ) {
OffsetBy( p.x, p.y );
}
void OffsetTo( const POINT& p ) {
OffsetTo( p.x, p.y );
}
WRect& InsetBySelf( int x, int y ) {
InsetBy( x, y ); return *this;
}
WRect& OffsetBySelf( int x, int y ) {
OffsetBy( x, y ); return *this;
}
WRect& OffsetToSelf( int x, int y ) {
OffsetTo( x, y ); return *this;
}
WRect& InsetBySelf( const POINT& p ) {
InsetBy( p ); return *this;
}
WRect& OffsetBySelf( const POINT& p ) {
OffsetBy( p ); return *this;
}
WRect& OffsetToSelf( const POINT& p ) {
OffsetTo( p ); return *this;
}
WRect InsetByCopy( int x, int y ) const {
WRect r = *this; return r.InsetBySelf( x, y );
}
WRect OffsetByCopy( int x, int y ) const {
WRect r = *this; return r.OffsetBySelf( x, y );
}
WRect OffsetToCopy( int x, int y ) const {
WRect r = *this; return r.OffsetToSelf( x, y );
}
WRect InsetByCopy( const POINT& p ) const {
WRect r = *this; return r.InsetBySelf( p );
}
WRect OffsetByCopy( const POINT& p ) const {
WRect r = *this; return r.OffsetBySelf( p );
}
WRect OffsetToCopy( const POINT& p ) const {
WRect r = *this; return r.OffsetToSelf( p );
}
};
/**********************************************************************************************
* FileTime
**********************************************************************************************/
class FileTime: public FILETIME {
public:
FileTime() { }
FileTime( FILETIME ft ) { *static_cast<FILETIME*>(this) = ft; }
FileTime( const SYSTEMTIME& st ) {
SystemTimeToFileTime( &st, this );
}
operator SYSTEMTIME() const {
SYSTEMTIME st;
FileTimeToSystemTime( this, &st );
return st;
}
FileTime& operator=( const SYSTEMTIME& st ) {
SystemTimeToFileTime( &st, this );
return *this;
}
void ConvertToLocal() {
FILETIME ft;
FileTimeToLocalFileTime( this, &ft );
*static_cast<FILETIME*>(this) = ft;
}
void ConvertToUTC() {
FILETIME ft;
LocalFileTimeToFileTime( this, &ft );
*static_cast<FILETIME*>(this) = ft;
}
void ObtainSystemTime() {
GetSystemTimeAsFileTime( this );
}
void ObtainLocalTime() {
SYSTEMTIME st;
::GetLocalTime( &st );
*this = st;
}
#ifdef SKIP_STL
int FormatTime( char* lpTimeStr, int cchTimeStrLen, DWORD dwFlags = 0, const char* lpFormat = 0, LCID locale = LOCALE_USER_DEFAULT ) const {
const SYSTEMTIME st = *this;
return GetTimeFormat( locale, dwFlags, &st, lpFormat, lpTimeStr, cchTimeStrLen );
}
static int FormatTime( const SYSTEMTIME& st, char* lpTimeStr, int cchTimeStrLen, DWORD dwFlags = 0, const char* lpFormat = 0, LCID locale = LOCALE_USER_DEFAULT ) {
return GetTimeFormat( locale, dwFlags, &st, lpFormat, lpTimeStr, cchTimeStrLen );
}
int FormatDate( char* lpDateStr, int cchDateStrLen, DWORD dwFlags = 0, const char* lpFormat = 0, LCID locale = LOCALE_USER_DEFAULT ) const {
const SYSTEMTIME st = *this;
return GetDateFormat( locale, dwFlags, &st, lpFormat, lpDateStr, cchDateStrLen );
}
static int FormatDate( const SYSTEMTIME& st, char* lpDateStr, int cchDateStrLen, DWORD dwFlags = 0, const char* lpFormat = 0, LCID locale = LOCALE_USER_DEFAULT ) {
return GetDateFormat( locale, dwFlags, &st, lpFormat, lpDateStr, cchDateStrLen );
}
#else
string FormatTime( DWORD dwFlags = 0, const char* lpFormat = 0, LCID locale = LOCALE_USER_DEFAULT ) const {
char s[64];
const SYSTEMTIME st = *this;
const int count = GetTimeFormat( locale, dwFlags, &st, lpFormat, s, 64 ) - 1;
if ( count <= 0 ) return "";
return string( s, count );
}
static string FormatTime( const SYSTEMTIME& st, DWORD dwFlags = 0, const char* lpFormat = 0, LCID locale = LOCALE_USER_DEFAULT ) {
char s[64];
const int count = GetTimeFormat( locale, dwFlags, &st, lpFormat, s, 64 ) - 1;
if ( count <= 0 ) return "";
return string( s, count );
}
string FormatDate( DWORD dwFlags = 0, const char* lpFormat = 0, LCID locale = LOCALE_USER_DEFAULT ) const {
char s[64];
const SYSTEMTIME st = *this;
const int count = GetDateFormat( locale, dwFlags, &st, lpFormat, s, 64 ) - 1;
if ( count <= 0 ) return "";
return string( s, count );
}
static string FormatDate( const SYSTEMTIME& st, DWORD dwFlags = 0, const char* lpFormat = 0, LCID locale = LOCALE_USER_DEFAULT ) {
char s[64];
const int count = GetDateFormat( locale, dwFlags, &st, lpFormat, s, 64 ) - 1;
if ( count <= 0 ) return "";
return string( s, count );
}
#endif
bool operator==( FILETIME ft ) const {
return CompareFileTime( this, &ft ) == 0;
}
bool operator!=( FILETIME ft ) const {
return CompareFileTime( this, &ft ) != 0;
}
bool operator<( FILETIME ft ) const {
return CompareFileTime( this, &ft ) < 0;
}
bool operator>( FILETIME ft ) const {
return CompareFileTime( this, &ft ) > 0;
}
bool operator<=( FILETIME ft ) const {
return CompareFileTime( this, &ft ) <= 0;
}
bool operator>=( FILETIME ft ) const {
return CompareFileTime( this, &ft ) >= 0;
}
};
/**********************************************************************************************
* Address
**********************************************************************************************/
class Address {
in_addr address;
int port;
public:
enum {
any = 0,
loopback = 0x0100007F, // 127.0.0.1
broadcast = 0xFFFFFFFF // 255.255.255.255
};
Address( int port, in_addr address ): address( address ), port ( port ) { }
Address( int port = 0, int addr = any );
Address( int port, const char* name );
Address( const sockaddr& sa );
bool IsValid() const { return port > 0 && port < 65536; }
bool operator==( Address addr ) const { return address.S_un.S_addr == addr.address.S_un.S_addr && port == addr.port; }
bool operator!=( Address addr ) const { return address.S_un.S_addr != addr.address.S_un.S_addr || port != addr.port; }
void SetPort( int p ) { port = p; }
in_addr GetAddress() const { return address; }
int GetPort() const { return port; }
sockaddr GetSockAddr() const;
#ifndef SKIP_STL
string GetName() const;
string GetIP() const { return inet_ntoa( address ); }
#else
const char* GetName() const;
const char* GetIP() const { return inet_ntoa( address ); }
#endif
};
inline Address::Address( int port, int addr )
: port( port )
{
address.S_un.S_addr = addr;
}
inline Address::Address( int port, const char* name )
: port( port )
{
address.S_un.S_addr = any;
const hostent* hostname = gethostbyname( name );
if ( hostname )
address = *reinterpret_cast<in_addr*>( hostname->h_addr );
}
inline Address::Address( const sockaddr& sa )
: port( 0 )
{
address.S_un.S_addr = any;
const sockaddr_in& sa_in = reinterpret_cast<const sockaddr_in&>( sa );
address = sa_in.sin_addr;
port = ntohs( sa_in.sin_port );
}
inline sockaddr Address::GetSockAddr() const
{
sockaddr_in sa;
sa.sin_family = AF_INET;
sa.sin_port = htons( port );
sa.sin_addr = address;
for ( int i=0; i<sizeof( sa.sin_zero ); i++ )
reinterpret_cast<char*>( &sa.sin_zero )[i] = 0;
return *reinterpret_cast<sockaddr*>( &sa );
}
#ifndef SKIP_STL
inline string Address::GetName() const
{
const hostent* hostname = gethostbyaddr( reinterpret_cast<const char*>( &address ), sizeof(address), AF_INET );
return hostname->h_name;
}
#else
inline const char* Address::GetName() const
{
const hostent* hostname = gethostbyaddr( reinterpret_cast<const char*>( &address ), sizeof(address), AF_INET );
return hostname->h_name;
}
#endif
/**********************************************************************************************
* Socket
**********************************************************************************************/
class Socket: public NonCopyAble {
int socket;
public:
enum { empty = -1 };
// Construct AF_INET, SOCK_STREAM or SOCK_DGRAM
Socket( bool b_udp = false ); // b_udp ? SOCK_DGRAM : SOCK_STREAM
// Construct any type of AF_INET socket
Socket( int type, int protocol );
// Keep already existing socket
Socket( int socket ): socket( socket ) { }
// Copy constructor
Socket( Socket& s ): socket( empty ) { *this = s; }
// Assignment operator
Socket& operator=( Socket& s );
// Close socket on destruction
~Socket() { Close(); }
// Close socket
void Close();
// Check if the socket exists
bool IsValid() const { return socket >= 0; }
// Get last error
static int GetError() { return errno; }
// Bind local address to the socket
bool Bind( Address addr ) const;
bool Bind( int port ) const { return Bind( Address( port ) ); }
// Connect to a remote address
bool Connect( Address addr ) const;
// Returns address of the peer to which the socket is connected
Address GetPeerName() const;
// Returns local address of the socket
Address GetSockName() const;
// Puts the socket in a listen state in order to accept incoming connections
bool Listen( int acceptance_count = SOMAXCONN ) const;
// Accepts an incoming connection and returns a socket with the connection
Socket Accept( Address* paddr = 0 ) const;
// Waits until anything's ready to be *read* (used before Accept() and Receive*() on blocking sockets)
bool Select( int timeout_ms = 0 ) const;
// Send a buffer of data (on a connected socket)
int Send( const void* buf, int size ) const;
// Receives a buffer of data (on a connected socket)
int Receive( void* buf, int size ) const;
// Send a buffer of data (connection-less)
int SendTo( const void* buf, int size, Address addr ) const;
// Receives a buffer of data (connection-less)
int ReceiveFrom( void* buf, int size, Address* p_addr = 0 ) const;
// Puts the socket in a blocking or non-blocking state
bool SetBlocking( bool block = true ) const;
// Sets an option on the socket
bool SetOpt( int optname, const char* optval, int optlen, int level = SOL_SOCKET );
template< class T >
bool SetOpt( int optname, const T& optval, int level = SOL_SOCKET ) {
return SetOpt( optname, reinterpret_cast<const char*>( &optval ), sizeof(optval), level );
}
// Controls I/O mode of the socket
bool IOCtl( int command, unsigned long* arg );
};
inline Socket& Socket::operator=( Socket& s )
{
Close();
socket = s.socket;
s.socket = empty;
return *this;
}
inline void Socket::Close()
{
if ( IsValid() ) {
shutdown( socket, 2 ); // 2==SD_BOTH
closesocket( socket );
socket = empty;
}
}
inline bool Socket::Bind( Address addr ) const
{
const sockaddr sa = addr.GetSockAddr();
return bind( socket, &sa, sizeof( sa ) ) == 0;
}
inline bool Socket::Connect( Address addr ) const
{
const sockaddr sa = addr.GetSockAddr();
return connect( socket, &sa, sizeof( sa ) ) == 0;
}
inline Address Socket::GetPeerName() const
{
sockaddr sa;
int sa_size = sizeof( sa );
getpeername( socket, &sa, &sa_size );
return Address( sa );
}
inline Address Socket::GetSockName() const
{
sockaddr sa;
int sa_size = sizeof( sa );
::getsockname( socket, &sa, &sa_size );
return Address( sa );
}
inline bool Socket::Listen( int acceptance_count ) const
{
return listen( socket, acceptance_count ) == 0;
}
inline Socket Socket::Accept( Address* paddr ) const
{
Socket newsocket( empty );
sockaddr sa;
int sa_size = sizeof( sa );
newsocket.socket = static_cast<int>( ::accept( socket, &sa, &sa_size ) );
if ( paddr ) *paddr = Address( sa );
return newsocket;
}
inline bool Socket::Select( int timeout_ms ) const
{
timeval tv;
tv.tv_sec = timeout_ms / 1000;
tv.tv_usec = ( timeout_ms % 1000 ) * 1000;
fd_set fds;
FD_ZERO( &fds );
FD_SET( (unsigned)socket, &fds );
return select( 32, &fds, 0, 0, timeout_ms ? &tv : 0 ) > 0;
}
inline int Socket::Send( const void* buf, int size ) const
{
return send( socket, reinterpret_cast<const char*>( buf ), size, 0 );
}
inline int Socket::Receive( void* buf, int size ) const
{
return recv( socket, reinterpret_cast<char*>( buf ), size, 0 );
}
inline int Socket::SendTo( const void* buf, int size, Address addr ) const
{
sockaddr sa = addr.GetSockAddr();
return sendto( socket, reinterpret_cast<const char*>( buf ), size, 0, &sa, sizeof( sa ) );
}
inline int Socket::ReceiveFrom( void* buf, int size, Address* p_addr ) const
{
sockaddr sa;
int sa_size = sizeof( sa );
int ret = recvfrom( socket, reinterpret_cast<char*>( buf ), size, 0, &sa, &sa_size );
if ( p_addr ) *p_addr = Address( sa );
return ret;
}
inline bool Socket::SetBlocking( bool block ) const
{
unsigned long blk;
return ioctlsocket( socket, FIONBIO, &blk ) == 0;
}
inline bool Socket::SetOpt( int optname, const char* optval, int optlen, int level )
{
return setsockopt( socket, level, optname, optval, optlen ) == 0;
}
inline bool Socket::IOCtl( int command, unsigned long* arg )
{
return ioctlsocket( socket, command, arg ) == 0;
}
/**********************************************************************************************
* Menu
**********************************************************************************************/
class Menu {
HMENU hMenu;
public:
Menu(): hMenu( 0 ) { }
Menu( HMENU hMenu ): hMenu( hMenu ) { }
~Menu() { hMenu = 0; }
operator HMENU() const {
return hMenu;
}
bool Load( int identifier, HINSTANCE hInstance = __MODULE_HANDLE ) {
return (hMenu = LoadMenu( hInstance, MAKEINTRESOURCE(identifier) )) != NULL;
}