~ubuntu-branches/ubuntu/trusty/cuneiform/trusty

« back to all changes in this revision

Viewing changes to cuneiform_src/Kern/cfcompat/cfcompat.c

  • Committer: Bazaar Package Importer
  • Author(s): Jakub Wilk, c-assert.diff, slovenian-slv.diff
  • Date: 2011-01-26 21:53:07 UTC
  • mfrom: (5.1.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20110126215307-62x61mcesr607qb7
Tags: 1.0.0+dfsg-2
* Upload to unstable.
* Explicitly build-depend on pkg-config. Thanks to Stefano Rivera for the
  bug report.
* Add Vcs-* fields.
* Use the standard C assert() macro, rather than custom Cuneiform one.
  [c-assert.diff]
* Pass CFLAGS, CXXFLAGS and LDFLAGS (get from dpkg-buildflags) to cmake
  (closes: #608345). Thanks to Sami Liedes for the bug report.
  + Build depend on dpkg-dev (>= 1.15.7).
* Pass --parallel to dh.
  + Bump debhelper minimum version to 7.4.10.
* Update debian/copyright to the latest DEP-5 version.
* Bump year in debian/copyright.
* Explicitly link to GraphicsMagick (rather than via the ImageMagick
  compatibility layer).
* Don't ship /usr/lib/cuneiform/*.so symlinks. These libraries are
  considered private, at least until #598616 is fixed.
* Rename some private variables in debian/rules to make them lowercase.
* Update patch headers.
* Provide proper ‘build-arch’ and ‘build-indep’ targets in debian/rules.
* Document input format in the manual page (closes: #572061). Thanks to
  Janusz S. Bień for the bug report.
* Use ‘slv’ (rather than ‘slo’) as language code for Slovenian.
  [slovenian-slv.diff]
* Fix package description: Slovenian is supported, Slovak is not.
* Improve documentation of the language (-l) option (closes: #602512).
  Thanks to Jari Aalto for the bug report.
* Install reasons file for vrms.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 Copyright (c) 2008, 2009 Jussi Pakkanen
 
3
 
 
4
 Разрешается повторное распространение и использование как в виде исходного кода,
 
5
 так и в двоичной форме, с изменениями или без, при соблюдении следующих условий:
 
6
 
 
7
 * При повторном распространении исходного кода должны оставаться указанное
 
8
 выше уведомление об авторском праве, этот список условий и последующий
 
9
 отказ от гарантий.
 
10
 * При повторном распространении двоичного кода в документации и/или в
 
11
 других материалах, поставляемых при распространении, должны сохраняться
 
12
 указанная выше информация об авторском праве, этот список условий и
 
13
 последующий отказ от гарантий.
 
14
 * Ни название Cognitive Technologies, ни имена ее сотрудников не могут
 
15
 быть использованы в качестве средства поддержки и/или продвижения
 
16
 продуктов, основанных на этом ПО, без предварительного письменного
 
17
 разрешения.
 
18
 
 
19
 ЭТА ПРОГРАММА ПРЕДОСТАВЛЕНА ВЛАДЕЛЬЦАМИ АВТОРСКИХ ПРАВ И/ИЛИ ДРУГИМИ ЛИЦАМИ "КАК
 
20
 ОНА ЕСТЬ" БЕЗ КАКОГО-ЛИБО ВИДА ГАРАНТИЙ, ВЫРАЖЕННЫХ ЯВНО ИЛИ ПОДРАЗУМЕВАЕМЫХ,
 
21
 ВКЛЮЧАЯ ГАРАНТИИ КОММЕРЧЕСКОЙ ЦЕННОСТИ И ПРИГОДНОСТИ ДЛЯ КОНКРЕТНОЙ ЦЕЛИ, НО НЕ
 
22
 ОГРАНИЧИВАЯСЬ ИМИ. НИ ВЛАДЕЛЕЦ АВТОРСКИХ ПРАВ И НИ ОДНО ДРУГОЕ ЛИЦО, КОТОРОЕ
 
23
 МОЖЕТ ИЗМЕНЯТЬ И/ИЛИ ПОВТОРНО РАСПРОСТРАНЯТЬ ПРОГРАММУ, НИ В КОЕМ СЛУЧАЕ НЕ
 
24
 НЕСЁТ ОТВЕТСТВЕННОСТИ, ВКЛЮЧАЯ ЛЮБЫЕ ОБЩИЕ, СЛУЧАЙНЫЕ, СПЕЦИАЛЬНЫЕ ИЛИ
 
25
 ПОСЛЕДОВАВШИЕ УБЫТКИ, СВЯЗАННЫЕ С ИСПОЛЬЗОВАНИЕМ ИЛИ ПОНЕСЕННЫЕ ВСЛЕДСТВИЕ
 
26
 НЕВОЗМОЖНОСТИ ИСПОЛЬЗОВАНИЯ ПРОГРАММЫ (ВКЛЮЧАЯ ПОТЕРИ ДАННЫХ, ИЛИ ДАННЫЕ,
 
27
 СТАВШИЕ НЕГОДНЫМИ, ИЛИ УБЫТКИ И/ИЛИ ПОТЕРИ ДОХОДОВ, ПОНЕСЕННЫЕ ИЗ-ЗА ДЕЙСТВИЙ
 
28
 ТРЕТЬИХ ЛИЦ И/ИЛИ ОТКАЗА ПРОГРАММЫ РАБОТАТЬ СОВМЕСТНО С ДРУГИМИ ПРОГРАММАМИ,
 
29
 НО НЕ ОГРАНИЧИВАЯСЬ ЭТИМИ СЛУЧАЯМИ), НО НЕ ОГРАНИЧИВАЯСЬ ИМИ, ДАЖЕ ЕСЛИ ТАКОЙ
 
30
 ВЛАДЕЛЕЦ ИЛИ ДРУГОЕ ЛИЦО БЫЛИ ИЗВЕЩЕНЫ О ВОЗМОЖНОСТИ ТАКИХ УБЫТКОВ И ПОТЕРЬ.
 
31
 
 
32
 Redistribution and use in source and binary forms, with or without modification,
 
33
 are permitted provided that the following conditions are met:
 
34
 
 
35
 * Redistributions of source code must retain the above copyright notice,
 
36
 this list of conditions and the following disclaimer.
 
37
 * Redistributions in binary form must reproduce the above copyright notice,
 
38
 this list of conditions and the following disclaimer in the documentation
 
39
 and/or other materials provided with the distribution.
 
40
 * Neither the name of the Cognitive Technologies nor the names of its
 
41
 contributors may be used to endorse or promote products derived from this
 
42
 software without specific prior written permission.
 
43
 
 
44
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 
45
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 
46
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
47
 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
 
48
 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
49
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 
50
 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 
51
 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 
52
 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
53
 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
54
 */
 
55
 
 
56
#include <stdio.h>
 
57
#include <sys/types.h>
 
58
#include <sys/stat.h>
 
59
#include <fcntl.h>
 
60
#include "config.h"
 
61
 
 
62
#include "cfcompat.h"
 
63
 
 
64
#ifndef WIN32
 
65
 
 
66
/* Minimal implementations of win32-functionality.
 
67
 * Eventually these should be rewritten in standard POSIX.
 
68
 *
 
69
 * At the end of the file are some helper functions, which should be used
 
70
 * on Windows too.
 
71
 */
 
72
 
 
73
#include <errno.h>
 
74
#include <stdlib.h>
 
75
#include <sys/types.h>
 
76
#include <sys/stat.h>
 
77
#include <unistd.h>
 
78
#include <assert.h>
 
79
#include <string.h>
 
80
#include <dlfcn.h>
 
81
#include <stdarg.h>
 
82
 
 
83
#if defined(__APPLE__) && defined(__MACH__)
 
84
#include <malloc/malloc.h>
 
85
#define malloc_usable_size(a) malloc_size(a)
 
86
 
 
87
#elif defined(__FreeBSD__)
 
88
#include <malloc_np.h>
 
89
 
 
90
#else
 
91
#include <malloc.h>
 
92
#endif
 
93
 
 
94
#include "compat_defs.h"
 
95
 
 
96
CFCOMPAT_FUNC(int) HFILE_ERROR;
 
97
 
 
98
int LoadString(HINSTANCE hInstance, uint uID, char* lpBuffer, int nBufferMax) {
 
99
        return 0;
 
100
}
 
101
 
 
102
int CreateDirectory(const char *dir, void *dummy) {
 
103
        if (!mkdir(dir, 0755))
 
104
                return TRUE;
 
105
        else
 
106
                return FALSE;
 
107
}
 
108
 
 
109
uint32_t GetTempPath(uint32_t nBufferLength, char* lpBuffer) {
 
110
        strcpy(lpBuffer, "/tmp");
 
111
        return strlen(lpBuffer);
 
112
}
 
113
 
 
114
int RemoveDirectory(const char *d) {
 
115
        return rmdir(d);
 
116
}
 
117
 
 
118
void* GlobalAlloc(uint uFlags, int dwBytes) {
 
119
        if (uFlags & GMEM_ZEROINIT)
 
120
                return calloc(dwBytes, 1);
 
121
        return malloc(dwBytes);
 
122
}
 
123
 
 
124
HGLOBAL GlobalFree(void *f) {
 
125
        free(f);
 
126
        return NULL;
 
127
}
 
128
 
 
129
void* GlobalReAlloc(void* hMem, int dwBytes, uint uFlags) {
 
130
        return realloc(hMem, dwBytes); // Should init to zero on uFlags & GMEM_ZEROINIT.
 
131
}
 
132
 
 
133
int GetTempFileName(const char * lpPathName, const char * lpPrefixString, uint uUnique,
 
134
                char* lpTempFileName) {
 
135
        return -1;
 
136
}
 
137
 
 
138
int GetLastError() {
 
139
        return errno;
 
140
}
 
141
 
 
142
uint32_t GetModuleFileName(HMODULE hModule, char* lpFilename, uint32_t nSize) {
 
143
        lpFilename[0] = '.'; /* Currently all modules must be in the directory pumatest was run in. */
 
144
        lpFilename[1] = '\0';
 
145
        return 1;
 
146
}
 
147
 
 
148
Bool CloseHandle(HANDLE hObject) {
 
149
        return FALSE;
 
150
}
 
151
 
 
152
HANDLE CreateFile(const char * lpFileName, uint32_t dwDesiredAccess,
 
153
                uint32_t dwShareMode, void* lpSecurityAttributes,
 
154
                uint32_t dwCreationDisposition, uint32_t dwFlagsAndAttributes,
 
155
                HANDLE hTemplateFile) {
 
156
        return 0;
 
157
}
 
158
 
 
159
HWND FindWindow(const char * lpClassName, const char * lpWindowName) {
 
160
        return 0;
 
161
}
 
162
 
 
163
uint RegisterWindowMessage(const char * lpString) {
 
164
        return 0;
 
165
}
 
166
 
 
167
int _findclose(long handle);
 
168
long _findfirst(const char *filespec, struct _finddata_t *fileinfo);
 
169
int _findnext(long handle, struct _finddata_t *fileinfo);
 
170
long _tell(int handle) {
 
171
        return lseek(handle, 0, SEEK_CUR);
 
172
}
 
173
 
 
174
Bool GetComputerName(char* lpBuffer, long unsigned int *lpnSize) {
 
175
        strncpy(lpBuffer, "CompName", *lpnSize);
 
176
        *lpnSize = strlen(lpBuffer);
 
177
        return TRUE;
 
178
}
 
179
 
 
180
int32_t RegOpenKeyEx(HKEY hKey, const char * lpSubKey, uint32_t ulOptions,
 
181
                REGSAM samDesired, PHKEY phkResult) {
 
182
        return 0;
 
183
}
 
184
 
 
185
int32_t RegQueryValueEx(HKEY hKey, const char * lpValueName, LPDWORD lpReserved,
 
186
                LPDWORD lpType, LPBYTE lpData, LPDWORD lpcbData) {
 
187
        return 0;
 
188
}
 
189
 
 
190
Bool GetClientRect(HWND hWnd, LPRECT lpRect) {
 
191
        return 0;
 
192
}
 
193
 
 
194
Bool WritePrivateProfileString(const char * lpAppName, const char * lpKeyName,
 
195
                const char * lpString, const char * lpFileName) {
 
196
        return 0;
 
197
}
 
198
 
 
199
uint32_t GetPrivateProfileString(const char * lpAppName, const char * lpKeyName,
 
200
                const char * lpDefault, char* lpReturnedString, uint32_t nSize,
 
201
                const char * lpFileName) {
 
202
        return 0;
 
203
}
 
204
 
 
205
uint GetPrivateProfileInt(const char * lpAppName, const char * lpKeyName, int16_t nDefault,
 
206
                const char * lpFileName) {
 
207
        return 0;
 
208
}
 
209
 
 
210
int WideCharToMultiByte(uint CodePage, uint32_t dwFlags,
 
211
                const wchar_t *lpWideCharStr, int cchWideChar, char* lpMultiByteStr,
 
212
                int cbMultiByte, const char * lpDefaultChar, pBool lpUsedDefaultChar) {
 
213
        return 0;
 
214
}
 
215
Bool ShowWindow(HWND hWnd, int nCmdShow) {
 
216
        return 0;
 
217
}
 
218
 
 
219
long _filelength(int fd) {
 
220
        struct stat foo;
 
221
        if (fstat(fd, &foo) != 0) {
 
222
                return 0;
 
223
        }
 
224
        return foo.st_size;
 
225
}
 
226
 
 
227
long filelength(int fd) {
 
228
        return _filelength(fd);
 
229
}
 
230
 
 
231
long _msize(void *memblock) {
 
232
        return malloc_usable_size(memblock);
 
233
}
 
234
 
 
235
/* All uses in Cuneiform just check if the file exists. Ignoring all other
 
236
 * cases.
 
237
 */
 
238
int _access(const char *filename, int mode) {
 
239
        struct stat foo;
 
240
        assert(mode == 0);
 
241
        return stat(filename, &foo);
 
242
}
 
243
 
 
244
Bool SetWindowText(HWND hWnd, const char * lpString) {
 
245
        return 0;
 
246
}
 
247
 
 
248
int ReleaseDC(HWND hWnd, HDC hDC) {
 
249
        return 0;
 
250
}
 
251
 
 
252
Bool IsIconic(HWND hWnd) {
 
253
        return 0;
 
254
}
 
255
 
 
256
HDC GetDC(HWND hWnd) {
 
257
        return 0;
 
258
}
 
259
 
 
260
Bool EndPaint(HWND hWnd, ...) {
 
261
        return 1;
 
262
}
 
263
 
 
264
HDC BeginPaint(HWND hwnd, ...) {
 
265
        return 0;
 
266
}
 
267
 
 
268
LRESULT SendMessage(HWND hWnd, uint Msg, WPARAM wParam, LPARAM lParam) {
 
269
        return 0;
 
270
}
 
271
 
 
272
#ifndef __CYGWIN__
 
273
void strlwr(char *foo) {
 
274
        // FIXME: this is probably actually used somewhere.
 
275
}
 
276
#endif
 
277
 
 
278
HWND CreateWindow(const char * lpClassName, const char * lpWindowName, uint32_t dwStyle,
 
279
                int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu,
 
280
                HINSTANCE hInstance, pvoid lpParam) {
 
281
        return (HWND) 55;
 
282
}
 
283
 
 
284
HGDIOBJ SelectObject(HDC hdc, HGDIOBJ hgdiobj) {
 
285
        return 0;
 
286
}
 
287
 
 
288
char* lstrcat(char* lpString1, char* lpString2) {
 
289
        return strcat(lpString1, lpString2);
 
290
}
 
291
 
 
292
int lstrlen(const char * lpString) {
 
293
        return strlen(lpString);
 
294
}
 
295
 
 
296
int lstrcmp(const char * lpString1, const char * lpString2) {
 
297
        return strcmp(lpString1, lpString2);
 
298
}
 
299
 
 
300
char* lstrcpy(char* lpString1, const char * lpString2) {
 
301
        return strcpy(lpString1, lpString2);
 
302
}
 
303
 
 
304
int wsprintf(char* lpOut, const char * lpFmt, ...) {
 
305
        char buffer[256];
 
306
        int ret;
 
307
        va_list args;
 
308
        va_start (args, lpFmt);
 
309
        ret = vsprintf(lpOut, lpFmt, args);
 
310
        if (ret < 0)
 
311
                perror(buffer);
 
312
        va_end(args);
 
313
        return ret;
 
314
}
 
315
 
 
316
int lstrcmpi(const char * lpString1, const char * lpString2) {
 
317
        return strcasecmp(lpString1, lpString2);
 
318
}
 
319
 
 
320
Bool DeleteObject(HGDIOBJ hObject) {
 
321
        return 0;
 
322
}
 
323
 
 
324
HWND GetFocus() {
 
325
        return NULL;
 
326
}
 
327
 
 
328
int MessageBox(HWND hWnd, const char * lpText, const char * lpCaption, uint uType) {
 
329
        fprintf(stderr, "MessageBox %s: %s\n", lpCaption, lpText);
 
330
        return 0;
 
331
}
 
332
 
 
333
int WINAPI GlobalSize(HGLOBAL hMem) {
 
334
        return 0;
 
335
}
 
336
 
 
337
pvoid GlobalLock(HGLOBAL hMem) {
 
338
        return NULL;
 
339
}
 
340
 
 
341
Bool GlobalUnlock(HGLOBAL hMem) {
 
342
        return 0;
 
343
}
 
344
 
 
345
Bool IsBadWritePtr(pvoid lp, int ucb) {
 
346
        return 0;
 
347
}
 
348
 
 
349
void OutputDebugString(const char * lpOutputString) {
 
350
 
 
351
}
 
352
 
 
353
Bool SetRect(LPRECT lprc, int xLeft, int yTop, int xRight, int yBottom) {
 
354
        lprc->left = xLeft;
 
355
        lprc->right = xRight;
 
356
        lprc->top = yTop;
 
357
        lprc->bottom = yBottom;
 
358
        return TRUE;
 
359
}
 
360
 
 
361
Bool PtInRect(const RECT *lprc, POINT pt) {
 
362
        if (pt.x >= lprc->left && pt.x < lprc->right && pt.y >= lprc->top && pt.y
 
363
                        < lprc->bottom)
 
364
                return TRUE;
 
365
        return FALSE;
 
366
}
 
367
 
 
368
/* This is only called from creatertf.cpp. It does not use lprcDst at all so
 
369
 * we do not need to calculate it.
 
370
 */
 
371
 
 
372
Bool IntersectRect(LPRECT lprcDst, const RECT *lprcSrc1, const RECT *lprcSrc2) {
 
373
        if (lprcSrc1->left > lprcSrc2->right || lprcSrc1->right < lprcSrc2->left
 
374
                        || lprcSrc1->top > lprcSrc2->bottom || lprcSrc1->bottom
 
375
                        < lprcSrc2->top)
 
376
                return FALSE;
 
377
        return TRUE;
 
378
}
 
379
 
 
380
Bool UnionRect(LPRECT lprcDst, const RECT *lprcSrc1, const RECT *lprcSrc2) {
 
381
        if (lprcSrc1->left - lprcSrc1->right == 0 || lprcSrc1->top
 
382
                        - lprcSrc1->bottom == 0) {
 
383
                lprcDst->left = lprcSrc2->left;
 
384
                lprcDst->right = lprcSrc2->right;
 
385
                lprcDst->top = lprcSrc2->top;
 
386
                lprcDst->bottom = lprcSrc2->bottom;
 
387
                return TRUE;
 
388
        }
 
389
        if (lprcSrc2->left - lprcSrc2->right == 0 || lprcSrc2->top
 
390
                        - lprcSrc2->bottom == 0) {
 
391
                lprcDst->left = lprcSrc1->left;
 
392
                lprcDst->right = lprcSrc1->right;
 
393
                lprcDst->top = lprcSrc1->top;
 
394
                lprcDst->bottom = lprcSrc1->bottom;
 
395
                return TRUE;
 
396
        }
 
397
 
 
398
        lprcDst->left = lprcSrc1->left < lprcSrc2->left ? lprcSrc1->left
 
399
                        : lprcSrc2->left;
 
400
        lprcDst->right = lprcSrc1->right > lprcSrc2->right ? lprcSrc1->right
 
401
                        : lprcSrc2->right;
 
402
        lprcDst->top = lprcSrc1->top < lprcSrc2->top ? lprcSrc1->top
 
403
                        : lprcSrc2->top;
 
404
        lprcDst->bottom = lprcSrc1->bottom > lprcSrc2->bottom ? lprcSrc1->bottom
 
405
                        : lprcSrc2->bottom;
 
406
 
 
407
        return 0;
 
408
}
 
409
 
 
410
HWND GetActiveWindow() {
 
411
        return NULL;
 
412
}
 
413
 
 
414
HFONT CreateFont(int nHeight, int nWidth, int nEscapement, int nOrientation,
 
415
                int fnWeight, uint32_t fdwItalic, uint32_t fdwUnderline,
 
416
                uint32_t fdwStrikeOut, uint32_t fdwCharSet,
 
417
                uint32_t fdwOutputPrecision, uint32_t fdwClipPrecision,
 
418
                uint32_t fdwQuality, uint32_t fdwPitchAndFamily, const char * lpszFace) {
 
419
        return 0;
 
420
}
 
421
 
 
422
Bool GetTextExtentPoint32(HDC hdc, const char * lpString, int c, LPSIZE lpSize) {
 
423
        return 0;
 
424
}
 
425
 
 
426
Bool EnumWindows(WNDENUMPROC lpEnumFunc, LPARAM lParam) {
 
427
        return 0;
 
428
}
 
429
 
 
430
int GetWindowText(HWND hWnd, char* lpString, int nMaxCount) {
 
431
        return 0;
 
432
}
 
433
 
 
434
HMODULE LoadLibrary(const char * lpFileName) {
 
435
        return dlopen(lpFileName, RTLD_LAZY);
 
436
}
 
437
 
 
438
Bool FreeLibrary(HMODULE hModule) {
 
439
        return dlclose(hModule);
 
440
}
 
441
 
 
442
void* GetProcAddress(HMODULE hModule, const char * lpProcName) {
 
443
        return dlsym(hModule, lpProcName);
 
444
}
 
445
 
 
446
HGDIOBJ GetStockObject(int fnObject) {
 
447
        return -1;
 
448
}
 
449
 
 
450
Bool IsWindowVisible(HWND hWnd) {
 
451
        return 0;
 
452
}
 
453
 
 
454
LRESULT DefWindowProc(HWND hWnd, uint Msg, WPARAM wParam, LPARAM lParam) {
 
455
        return 0;
 
456
}
 
457
 
 
458
int32_t GetWindowLong(HWND hWnd, int nIndex) {
 
459
        return 0;
 
460
}
 
461
 
 
462
Bool RegisterClass(const WNDCLASS *lpWndClass) {
 
463
        return 0;
 
464
}
 
465
 
 
466
HMODULE GetModuleHandle(const char * lpModuleName) {
 
467
        return NULL;
 
468
}
 
469
 
 
470
HICON LoadIcon(HINSTANCE hInstance, const char * lpIconName) {
 
471
        return NULL;
 
472
}
 
473
 
 
474
int LoadCursor(HINSTANCE hInstance, const char * lpCursorName) {
 
475
        return 0;
 
476
}
 
477
 
 
478
Bool Rectangle(HDC hdc, int nLeftRect, int nTopRect, int nRightRect,
 
479
                int nBottomRect) {
 
480
        return 0;
 
481
}
 
482
 
 
483
/* String to uppercase. */
 
484
 
 
485
char* _strupr(char*s) {
 
486
        return s;
 
487
}
 
488
 
 
489
#else /* WIN32 */
 
490
 
 
491
#include<io.h>
 
492
#include<direct.h>
 
493
#include"cfcompat.h"
 
494
#include "cttypes.h"
 
495
#include <windows.h>
 
496
#include <malloc.h>
 
497
 
 
498
static HMODULE thismod;
 
499
 
 
500
CFCOMPAT_FUNC(Bool)
 
501
WINAPI DllMain(HINSTANCE hinstDLL, uint32_t fdwReason, pvoid lpvReserved) {
 
502
        thismod = (HMODULE) hinstDLL;
 
503
        return TRUE;
 
504
}
 
505
 
 
506
CFCOMPAT_FUNC(char*)
 
507
mkdtemp(char *tmpl) {
 
508
        static const char charset[] =
 
509
        "=#abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
 
510
        static const unsigned int charset_len = sizeof(charset) - 1;
 
511
 
 
512
        const int len = strlen (tmpl);
 
513
        char* x_tail = tmpl + len - 6;
 
514
        LARGE_INTEGER rand_seed;
 
515
        uint64_t value = rand_seed.QuadPart ^ GetCurrentThreadId();
 
516
        unsigned int cnt = 0;
 
517
 
 
518
        if (len < 6)
 
519
        return NULL;
 
520
        if(memcmp(x_tail, "XXXXXX", 6))
 
521
        return NULL;
 
522
 
 
523
        QueryPerformanceCounter(&rand_seed);
 
524
 
 
525
        do {
 
526
                uint64_t val = value;
 
527
                char* x_char = x_tail;
 
528
                while(*x_char) {
 
529
                        *x_char++ = charset[val % charset_len];
 
530
                        val /= charset_len;
 
531
                }
 
532
                if (CreateDirectory (tmpl, NULL))
 
533
                return tmpl;
 
534
                if (ERROR_ALREADY_EXISTS != GetLastError())
 
535
                return NULL;
 
536
                value += 65537;
 
537
                ++cnt;
 
538
        }while (cnt < TMP_MAX);
 
539
 
 
540
        return NULL;
 
541
}
 
542
 
 
543
#endif /* WIN32 */
 
544
 
 
545
/* General helper functions. */
 
546
 
 
547
static const char *separator = "/"; /* Yes, on Windows too. */
 
548
 
 
549
#ifdef WIN32
 
550
 
 
551
static void get_install_path(char *path) {
 
552
        const int psize = 128;
 
553
        char modulepath[128]; /* MSVC fails when psize is put in these brackets. */
 
554
        char fname[50];
 
555
        char suffix[10];
 
556
 
 
557
        GetModuleFileName(thismod, modulepath, psize);
 
558
        winpath_to_internal(modulepath);
 
559
        split_path(modulepath, path, fname, suffix);
 
560
}
 
561
 
 
562
#else
 
563
 
 
564
static void get_install_path(char *path) {
 
565
        strcat(path, INSTALL_DATADIR);
 
566
}
 
567
 
 
568
#endif
 
569
 
 
570
/* We try to locate data files in two locations:
 
571
 *
 
572
 * 1. At the directory pointed to by environment variable CF_DATADIR
 
573
 * 2. At a platform-dependent install location
 
574
 *
 
575
 * Caller's responsibility is to ensure *e1 and *e2 are large enough.
 
576
 *
 
577
 */
 
578
 
 
579
static void build_name_estimates(const char *base_name, char *env_name,
 
580
                char *prefix_name) {
 
581
        const char *env_prefix;
 
582
        const char *varname = "CF_DATADIR";
 
583
        int len = 0;
 
584
 
 
585
        env_name[0] = '\0';
 
586
        prefix_name[0] = '\0';
 
587
 
 
588
        env_prefix = getenv(varname);
 
589
        if (env_prefix)
 
590
                len = strlen(env_prefix);
 
591
        if (len > 0) {
 
592
                strcat(env_name, env_prefix);
 
593
                if (strcmp(env_prefix + len - 1, separator) != 0) {
 
594
                        strcat(env_name, separator);
 
595
                }
 
596
                strcat(env_name, base_name);
 
597
        }
 
598
 
 
599
        get_install_path(prefix_name);
 
600
        if (strlen(prefix_name) > 0) {
 
601
                strcat(prefix_name, separator);
 
602
                strcat(prefix_name, base_name);
 
603
        }
 
604
}
 
605
 
 
606
CFCOMPAT_FUNC(int)
 
607
open_data_file(const char *basename, int mode) {
 
608
        char ename[1024];
 
609
        char pname[1024];
 
610
        int i;
 
611
 
 
612
        build_name_estimates(basename, ename, pname);
 
613
        i = open(ename, mode);
 
614
        if(i != -1)
 
615
        return i;
 
616
        return open(pname, mode);
 
617
}
 
618
 
 
619
CFCOMPAT_FUNC(int)
 
620
data_file_exists(const char *basename) {
 
621
        char ename[1024];
 
622
        char pname[1024];
 
623
 
 
624
        build_name_estimates(basename, ename, pname);
 
625
        if(_access(ename, 0) == 0)
 
626
        return 0;
 
627
        return _access(pname, 0);
 
628
}
 
629
 
 
630
/* Split a file name in three: path, base file name, and extension.
 
631
 * All internal file names use / as path separator, even on Windows.
 
632
 */
 
633
CFCOMPAT_FUNC(void)
 
634
split_path(const char *fname, char *file_path, char *basename, char *ext) {
 
635
        int last_path = -1;
 
636
        int suff = -1;
 
637
        size_t l = strlen(fname);
 
638
        int path_end, base_start, base_end, ext_start;
 
639
        size_t i;
 
640
 
 
641
        file_path[0] = '\0';
 
642
        basename[0] = '\0';
 
643
 
 
644
        for (i = 0; i < l; i++) {
 
645
                if (fname[i] == '.')
 
646
                suff = i;
 
647
                if (fname[i] == '/')
 
648
                last_path = i;
 
649
        }
 
650
 
 
651
        path_end = 0;
 
652
        base_start = 0;
 
653
        if (last_path == 0) { // File in root.
 
654
                path_end = 1;
 
655
                base_start = 1;
 
656
        }
 
657
        if (last_path > 0) {
 
658
                path_end = last_path;
 
659
                base_start = last_path + 1;
 
660
        }
 
661
 
 
662
        if (suff > last_path) {
 
663
                ext_start = suff + 1;
 
664
                base_end = suff;
 
665
        } else {
 
666
                base_end = ext_start = l;
 
667
        }
 
668
 
 
669
        memcpy(file_path, fname, path_end);
 
670
        file_path[path_end] = '\0';
 
671
        memcpy(basename, fname + base_start, base_end - base_start);
 
672
        basename[base_end - base_start] = '\0';
 
673
        memcpy(ext, fname + ext_start, l - ext_start);
 
674
        ext[l - ext_start] = '\0';
 
675
}
 
676
 
 
677
CFCOMPAT_FUNC(void)
 
678
make_path(char *opath, const char *dir, const char *basename, const char *ext) {
 
679
        const char dirsep = '/';
 
680
        const char *dirseps = "/";
 
681
        opath[0] = '\0';
 
682
 
 
683
        if(dir) {
 
684
                strcat(opath, dir);
 
685
                if(opath[strlen(opath) - 1] != dirsep) {
 
686
                        strcat(opath, dirseps);
 
687
                }
 
688
        }
 
689
 
 
690
        if(basename)
 
691
        strcat(opath, basename);
 
692
 
 
693
        if(ext) {
 
694
                if(ext[0] != '.')
 
695
                strcat(opath, ".");
 
696
                strcat(opath, ext);
 
697
        }
 
698
}
 
699
 
 
700
/**
 
701
 * Convert backslashes to slashes. No-op on UNIX.
 
702
 */
 
703
CFCOMPAT_FUNC(void)
 
704
winpath_to_internal(char *p) {
 
705
#if WIN32
 
706
        int i;
 
707
        for(i=0; p[i] != '\0'; i++) {
 
708
                if(p[i] == '\\')
 
709
                p[i] = '/';
 
710
        }
 
711
#endif
 
712
}
 
713
 
 
714
/* Get current working directory. */
 
715
 
 
716
CFCOMPAT_FUNC(unsigned int) curr_dir(unsigned int bsize, char* buf) {
 
717
#ifdef _MSC_VER
 
718
        _getcwd(buf, bsize);
 
719
#else
 
720
        getcwd(buf, bsize);
 
721
#endif
 
722
        winpath_to_internal(buf);
 
723
        return strlen(buf);
 
724
}
 
725
 
 
726
#if WIN32
 
727
 
 
728
#define BUFSIZE 100
 
729
 
 
730
CFCOMPAT_FUNC (FILE*)
 
731
create_temp_file(void) {
 
732
        char temppath[BUFSIZE];
 
733
        char tempfname[BUFSIZE];
 
734
        uint32_t retval;
 
735
 
 
736
        retval = GetTempPath(BUFSIZE, temppath);
 
737
        if(retval >= BUFSIZE || retval == 0)
 
738
        return NULL;
 
739
 
 
740
        if(GetTempFileName(temppath, "CF", 0, tempfname) == 0)
 
741
        return NULL;
 
742
 
 
743
        return fopen(tempfname, "w+bD");
 
744
}
 
745
 
 
746
#else
 
747
 
 
748
CFCOMPAT_FUNC (FILE*)
 
749
create_temp_file(void) {
 
750
        FILE *tmp_file;
 
751
        int tmp_fd;
 
752
        char* pattrn = malloc(100);
 
753
        strcpy(pattrn, "/tmp/CF.XXXXXX");
 
754
 
 
755
        tmp_fd = mkstemp(pattrn);
 
756
 
 
757
        /* unlink file immediatly, it gets unlinked when file descriptor is closed */
 
758
        unlink(pattrn);
 
759
        free(pattrn);
 
760
 
 
761
        if (tmp_fd == -1)
 
762
        return NULL;
 
763
 
 
764
        if (!(tmp_file = fdopen(tmp_fd, "w+b"))) {
 
765
                return NULL;
 
766
        }
 
767
 
 
768
        return tmp_file;
 
769
}
 
770
#endif