Skip to content

Commit 1107d09

Browse files
committed
Test application:
- using USE_DLL instead of _WIN32, for easier code understanding - unified way of making zlib function wrappers - a little bit ugly, but more compact and much more easier to use
1 parent 7b1e89c commit 1107d09

File tree

1 file changed

+44
-96
lines changed

1 file changed

+44
-96
lines changed

Test/test.cpp

+44-96
Original file line numberDiff line numberDiff line change
@@ -24,107 +24,58 @@
2424

2525
#include "zlib.h"
2626

27-
#define STR2(s) #s
28-
#define STR(s) STR2(s)
29-
27+
// Defines controlling size of compressed data
3028
#define BUFFER_SIZE (256<<20)
3129
#define MAX_ITERATIONS 1 // number of passes to fully fill buffer, i.e. total processed data size will be up to (BUFFER_SIZE * MAX_ITERATIONS)
32-
#define COMPRESS_LEVEL 9
3330

3431
#if _WIN32
32+
#define USE_DLL 1
33+
#endif
34+
35+
#define STR2(s) #s
36+
#define STR(s) STR2(s)
37+
38+
#if USE_DLL
3539

36-
#include <windows.h>
40+
#include <windows.h> // for DLL stuff
3741

3842
static HMODULE zlibDll = NULL;
3943
static bool bWinapiCalls = false;
4044

41-
static gzFile gzopen_imp(const char* filename, const char* params)
42-
{
43-
if (zlibDll)
44-
{
45-
typedef gzFile ( *gzopen_f )(const char* filename, const char* params);
46-
typedef gzFile (WINAPI *gzopen_fw)(const char* filename, const char* params);
47-
static gzopen_f gzopen_ptr = NULL;
48-
if (gzopen_ptr == NULL)
49-
{
50-
gzopen_ptr = (gzopen_f)GetProcAddress(zlibDll, "gzopen");
51-
// assert(gzopen_ptr);
52-
}
53-
return bWinapiCalls ? ((gzopen_fw)gzopen_ptr)(filename, params) : gzopen_ptr(filename, params);
54-
}
55-
else
56-
{
57-
return gzopen(filename, params);
58-
}
45+
// Make a wrappers for zlib functions allowing to call statically-linked function, or cdecl or winapi dll function.
46+
// This macro receives argument list twice - with and without type specifiers.
47+
#define DECLARE_WRAPPER(type, name, prototype, args)\
48+
static type name##_imp prototype \
49+
{ \
50+
if (zlibDll) \
51+
{ \
52+
typedef type (*name##_f) prototype; \
53+
typedef type (WINAPI *name##_fw) prototype; \
54+
static name##_f func_ptr = NULL; \
55+
if (func_ptr == NULL) \
56+
{ \
57+
func_ptr = (name##_f)GetProcAddress(zlibDll, STR(name)); \
58+
} \
59+
return bWinapiCalls ? ((name##_fw)func_ptr) args : func_ptr args; \
60+
} \
61+
else \
62+
{ \
63+
return name args; \
64+
} \
5965
}
6066

61-
static int gzwrite_imp(gzFile file, voidpc buf, unsigned len)
62-
{
63-
if (zlibDll)
64-
{
65-
typedef int ( *gzwrite_f )(gzFile file, voidpc buf, unsigned len);
66-
typedef int (WINAPI *gzwrite_fw)(gzFile file, voidpc buf, unsigned len);
67-
static gzwrite_f gzwrite_ptr = NULL;
68-
if (gzwrite_ptr == NULL)
69-
{
70-
gzwrite_ptr = (gzwrite_f)GetProcAddress(zlibDll, "gzwrite");
71-
// assert(gzwrite_ptr);
72-
}
73-
return bWinapiCalls ? ((gzwrite_fw)gzwrite_ptr)(file, buf, len) : gzwrite_ptr(file, buf, len);
74-
}
75-
else
76-
{
77-
return gzwrite(file, buf, len);
78-
}
79-
}
80-
81-
static int gzread_imp(gzFile file, voidp buf, unsigned len)
82-
{
83-
if (zlibDll)
84-
{
85-
typedef int ( *gzread_f )(gzFile file, voidp buf, unsigned len);
86-
typedef int (WINAPI *gzread_fw)(gzFile file, voidp buf, unsigned len);
87-
static gzread_f gzread_ptr = NULL;
88-
if (gzread_ptr == NULL)
89-
{
90-
gzread_ptr = (gzread_f)GetProcAddress(zlibDll, "gzread");
91-
// assert(gzread_ptr);
92-
}
93-
return bWinapiCalls ? ((gzread_fw)gzread_ptr)(file, buf, len) : gzread_ptr(file, buf, len);
94-
}
95-
else
96-
{
97-
return gzread(file, buf, len);
98-
}
99-
}
100-
101-
static int gzclose_imp(gzFile file)
102-
{
103-
if (zlibDll)
104-
{
105-
typedef int ( *gzclose_f )(gzFile file);
106-
typedef int (WINAPI *gzclose_fw)(gzFile file);
107-
static gzclose_f gzclose_ptr = NULL;
108-
if (gzclose_ptr == NULL)
109-
{
110-
gzclose_ptr = (gzclose_f)GetProcAddress(zlibDll, "gzclose");
111-
// assert(gzclose_ptr);
112-
}
113-
return bWinapiCalls ? ((gzclose_fw)gzclose_ptr)(file) : gzclose_ptr(file);
114-
}
115-
else
116-
{
117-
return gzclose(file);
118-
}
119-
}
67+
DECLARE_WRAPPER(gzFile, gzopen, (const char* filename, const char* params), (filename, params))
68+
DECLARE_WRAPPER(int, gzwrite, (gzFile file, voidpc buf, unsigned len), (file, buf, len))
69+
DECLARE_WRAPPER(int, gzread, (gzFile file, voidp buf, unsigned len), (file, buf, len))
70+
DECLARE_WRAPPER(int, gzclose, (gzFile file), (file))
12071

12172
// Hook gzip functions
12273
#define gzopen gzopen_imp
12374
#define gzwrite gzwrite_imp
12475
#define gzread gzread_imp
12576
#define gzclose gzclose_imp
12677

127-
#endif // _WIN32
78+
#endif // USE_DLL
12879

12980
std::vector<std::string> fileList;
13081
std::vector<std::string> fileExclude;
@@ -147,7 +98,7 @@ static bool ScanDirectory(const char *dir, bool recurse = true, int baseDirLen =
14798
if (baseDirLen < 0)
14899
baseDirLen = strlen(dir) + 1;
149100

150-
#if _WIN32
101+
#if USE_DLL
151102
sprintf(Path, "%s/*.*", dir);
152103
_finddatai64_t found;
153104
intptr_t hFind = _findfirsti64(Path, &found);
@@ -240,7 +191,7 @@ int main(int argc, const char **argv)
240191
"Options:\n"
241192
" --level=[0-9] set compression level, default 9\n"
242193
" --exclude=<dir> exclude specified directory from tests\n"
243-
#if _WIN32
194+
#if USE_DLL
244195
" --dll=<file> use external WINAPI zlib dll\n"
245196
#endif
246197
" --compact use compact output\n"
@@ -257,7 +208,7 @@ int main(int argc, const char **argv)
257208
bool unpackFile = false;
258209
bool eraseCompressedFile = false;
259210

260-
#if _WIN32
211+
#if USE_DLL
261212
const char* dllName = NULL;
262213
#endif
263214

@@ -289,7 +240,7 @@ int main(int argc, const char **argv)
289240
{
290241
eraseCompressedFile = true;
291242
}
292-
#if _WIN32
243+
#if USE_DLL
293244
else if (!strnicmp(arg, "dll=", 4))
294245
{
295246
if (zlibDll != NULL) goto usage;
@@ -301,7 +252,7 @@ int main(int argc, const char **argv)
301252
exit(1);
302253
}
303254
}
304-
#endif // _WIN32
255+
#endif // USE_DLL
305256
else
306257
{
307258
goto usage;
@@ -329,15 +280,15 @@ int main(int argc, const char **argv)
329280
}
330281
// printf("%d files\n", fileList.size());
331282

332-
#if _WIN32
283+
#if USE_DLL
333284
if (zlibDll)
334285
{
335286
typedef unsigned (*zlibCompileFlags_f)();
336287
zlibCompileFlags_f zlibCompileFlags_ptr = (zlibCompileFlags_f)GetProcAddress(zlibDll, "zlibCompileFlags");
337288
unsigned zlibFlags = zlibCompileFlags_ptr();
338289
bWinapiCalls = (zlibFlags & 0x400) != 0;
339290
}
340-
#endif // _WIN32
291+
#endif // USE_DLL
341292

342293
clock_t clocks = 0;
343294

@@ -368,7 +319,7 @@ int main(int argc, const char **argv)
368319

369320
// print results
370321
const char* method = STR(VERSION);
371-
#if _WIN32
322+
#if USE_DLL
372323
if (zlibDll) method = "DLL";
373324
#endif
374325
float time = clocks / (float)CLOCKS_PER_SEC;
@@ -410,11 +361,8 @@ int main(int argc, const char **argv)
410361
printf(" Unpack: %5.2f Mb/s", totalDataSize / double(1<<20) / time);
411362
}
412363

413-
#if _WIN32
414-
if (zlibDll)
415-
{
416-
printf(" (%s)", dllName);
417-
}
364+
#if USE_DLL
365+
if (zlibDll) printf(" (%s)", dllName);
418366
#endif
419367

420368
printf("\n");

0 commit comments

Comments
 (0)