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

« back to all changes in this revision

Viewing changes to cuneiform_src/Kern/windummy.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 "winfuncs.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
 
 
95
 
#include "compat_defs.h"
96
 
 
97
 
WINDUMMY_FUNC(int) HFILE_ERROR;
98
 
 
99
 
int LoadString(HINSTANCE hInstance, UINT uID, LPTSTR lpBuffer, int nBufferMax) {
100
 
    return 0;
101
 
}
102
 
 
103
 
int CreateDirectory(const char *dir, void *dummy) {
104
 
       if(!mkdir(dir,0755)) return TRUE;
105
 
       else return FALSE;
106
 
}
107
 
 
108
 
DWORD GetTempPath(DWORD nBufferLength, LPTSTR lpBuffer) {
109
 
    strcpy(lpBuffer, "/tmp");
110
 
    return strlen(lpBuffer);
111
 
}
112
 
 
113
 
int RemoveDirectory(const char *d) {
114
 
  return rmdir(d);
115
 
}
116
 
 
117
 
void* GlobalAlloc(UINT uFlags, int dwBytes) {
118
 
  if(uFlags & GMEM_ZEROINIT)
119
 
    return calloc(dwBytes, 1);
120
 
  return malloc(dwBytes);
121
 
}
122
 
 
123
 
HGLOBAL GlobalFree(void *f) {
124
 
  free(f);
125
 
  return NULL;
126
 
}
127
 
 
128
 
void* GlobalReAlloc(void* hMem, int dwBytes, UINT uFlags) {
129
 
  return realloc(hMem, dwBytes); // Should init to zero on uFlags & GMEM_ZEROINIT.
130
 
}
131
 
 
132
 
int GetTempFileName(LPCTSTR lpPathName, LPCTSTR lpPrefixString,
133
 
                    UINT uUnique, LPTSTR lpTempFileName) {
134
 
    return -1;
135
 
}
136
 
 
137
 
int GetLastError() {
138
 
    return errno;
139
 
}
140
 
 
141
 
 
142
 
DWORD GetModuleFileName(HMODULE hModule, LPTSTR lpFilename, DWORD 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(LPCTSTR lpFileName, DWORD dwDesiredAccess,
153
 
DWORD dwShareMode, void* lpSecurityAttributes,
154
 
DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile) {
155
 
    return 0;
156
 
}
157
 
 
158
 
HWND FindWindow(LPCTSTR lpClassName, LPCTSTR lpWindowName) {
159
 
    return 0;
160
 
}
161
 
 
162
 
UINT RegisterWindowMessage(LPCTSTR lpString) {
163
 
    return 0;
164
 
}
165
 
 
166
 
int _findclose(long handle);
167
 
long _findfirst(const char *filespec, struct _finddata_t *fileinfo);
168
 
int _findnext(long handle, struct _finddata_t *fileinfo);
169
 
long _tell(int handle) {
170
 
    return lseek(handle, 0, SEEK_CUR);
171
 
}
172
 
 
173
 
BOOL GetComputerName(LPTSTR lpBuffer, long unsigned int *lpnSize) {
174
 
    strncpy(lpBuffer, "CompName", *lpnSize);
175
 
    *lpnSize = strlen(lpBuffer);
176
 
    return TRUE;
177
 
}
178
 
 
179
 
LONG RegOpenKeyEx(HKEY hKey, LPCTSTR lpSubKey, DWORD ulOptions,
180
 
  REGSAM samDesired, PHKEY phkResult) {
181
 
    return 0;
182
 
}
183
 
 
184
 
LONG RegQueryValueEx(HKEY hKey, LPCTSTR lpValueName,
185
 
LPDWORD lpReserved, LPDWORD lpType, LPBYTE lpData,
186
 
LPDWORD lpcbData) {
187
 
    return 0;
188
 
}
189
 
 
190
 
BOOL GetClientRect(HWND hWnd, LPRECT lpRect) {
191
 
    return 0;
192
 
}
193
 
 
194
 
BOOL WritePrivateProfileString(LPCTSTR lpAppName,
195
 
        LPCTSTR lpKeyName, LPCTSTR lpString, LPCTSTR lpFileName) {
196
 
    return 0;
197
 
}
198
 
 
199
 
DWORD GetPrivateProfileString(LPCTSTR lpAppName, LPCTSTR lpKeyName,
200
 
LPCTSTR lpDefault, LPTSTR lpReturnedString, DWORD nSize, LPCTSTR lpFileName) {
201
 
    return 0;
202
 
}
203
 
 
204
 
UINT GetPrivateProfileInt(LPCTSTR lpAppName,
205
 
  LPCTSTR lpKeyName, INT nDefault, LPCTSTR lpFileName) {
206
 
    return 0;
207
 
}
208
 
 
209
 
int WideCharToMultiByte(UINT CodePage, DWORD dwFlags, const wchar_t *lpWideCharStr,
210
 
  int cchWideChar, LPSTR lpMultiByteStr, int cbMultiByte,
211
 
  LPCSTR lpDefaultChar, LPBOOL lpUsedDefaultChar) {
212
 
    return 0;
213
 
}
214
 
BOOL ShowWindow(HWND hWnd, int nCmdShow) {
215
 
    return 0;
216
 
}
217
 
 
218
 
long _filelength(int fd) {
219
 
    struct stat foo;
220
 
    if(fstat(fd, &foo) != 0) {
221
 
        return 0;
222
 
    }
223
 
    return foo.st_size;
224
 
}
225
 
 
226
 
long filelength(int fd) {
227
 
        return _filelength(fd);
228
 
}
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,LPCTSTR 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
 
void strlwr(char *foo) {
273
 
    // FIXME: this is probably actually used somewhere.
274
 
}
275
 
 
276
 
HWND CreateWindow(
277
 
    LPCTSTR lpClassName,
278
 
    LPCTSTR lpWindowName,
279
 
    DWORD dwStyle,
280
 
    int x,
281
 
    int y,
282
 
    int nWidth,
283
 
    int nHeight,
284
 
    HWND hWndParent,
285
 
    HMENU hMenu,
286
 
    HINSTANCE hInstance,
287
 
    LPVOID lpParam) {
288
 
    return (HWND) 55;
289
 
}
290
 
 
291
 
HGDIOBJ SelectObject(HDC hdc, HGDIOBJ hgdiobj) {
292
 
    return 0;
293
 
}
294
 
 
295
 
LPTSTR lstrcat(LPTSTR lpString1, LPTSTR lpString2) {
296
 
    return strcat(lpString1, lpString2);
297
 
}
298
 
 
299
 
int lstrlen(LPCTSTR lpString) {
300
 
    return strlen(lpString);
301
 
}
302
 
 
303
 
int lstrcmp(LPCTSTR lpString1, LPCTSTR lpString2) {
304
 
    return strcmp(lpString1, lpString2);
305
 
}
306
 
 
307
 
LPTSTR lstrcpy(LPTSTR lpString1, LPCTSTR lpString2) {
308
 
    return strcpy(lpString1, lpString2);
309
 
}
310
 
 
311
 
int wsprintf(LPTSTR lpOut, LPCTSTR lpFmt, ...) {
312
 
    char buffer[256];
313
 
    int ret;
314
 
    va_list args;
315
 
    va_start (args, lpFmt);
316
 
    ret = vsprintf(lpOut,lpFmt, args);
317
 
    if(ret < 0)
318
 
        perror(buffer);
319
 
    va_end(args);
320
 
    return ret;
321
 
}
322
 
 
323
 
int lstrcmpi(LPCTSTR lpString1, LPCTSTR lpString2) {
324
 
    return strcasecmp(lpString1, lpString2);
325
 
}
326
 
 
327
 
BOOL DeleteObject(HGDIOBJ hObject) {
328
 
    return 0;
329
 
}
330
 
 
331
 
HWND GetFocus() {
332
 
    return NULL;
333
 
}
334
 
 
335
 
int MessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType) {
336
 
    fprintf(stderr, "MessageBox %s: %s\n", lpCaption, lpText);
337
 
    return 0;
338
 
}
339
 
 
340
 
int WINAPI GlobalSize(HGLOBAL hMem) {
341
 
    return 0;
342
 
}
343
 
 
344
 
LPVOID GlobalLock(HGLOBAL hMem) {
345
 
    return NULL;
346
 
}
347
 
 
348
 
BOOL GlobalUnlock(HGLOBAL hMem) {
349
 
    return 0;
350
 
}
351
 
 
352
 
BOOL IsBadWritePtr(LPVOID lp, int ucb) {
353
 
    return 0;
354
 
}
355
 
 
356
 
void OutputDebugString(LPCTSTR lpOutputString) {
357
 
 
358
 
}
359
 
 
360
 
BOOL SetRect(LPRECT lprc, int xLeft, int yTop,
361
 
int xRight, int yBottom) {
362
 
    lprc->left = xLeft;
363
 
    lprc->right = xRight;
364
 
    lprc->top = yTop;
365
 
    lprc->bottom = yBottom;
366
 
    return TRUE;
367
 
}
368
 
 
369
 
BOOL PtInRect(const RECT *lprc, POINT pt) {
370
 
    if(pt.x >= lprc->left && pt.x < lprc->right
371
 
            && pt.y >= lprc->top && pt.y < lprc->bottom)
372
 
        return TRUE;
373
 
    return FALSE;
374
 
}
375
 
 
376
 
/* This is only called from creatertf.cpp. It does not use lprcDst at all so
377
 
 * we do not need to calculate it.
378
 
 */
379
 
 
380
 
BOOL IntersectRect(LPRECT lprcDst, const RECT *lprcSrc1, const RECT *lprcSrc2) {
381
 
    if(lprcSrc1->left > lprcSrc2->right
382
 
       || lprcSrc1->right < lprcSrc2->left
383
 
       || lprcSrc1->top > lprcSrc2->bottom
384
 
       || lprcSrc1->bottom < lprcSrc2->top)
385
 
        return FALSE;
386
 
    return TRUE;
387
 
}
388
 
 
389
 
BOOL UnionRect(LPRECT lprcDst, const RECT *lprcSrc1, const RECT *lprcSrc2) {
390
 
    if(lprcSrc1->left - lprcSrc1->right == 0 || lprcSrc1->top - lprcSrc1->bottom == 0) {
391
 
        lprcDst->left = lprcSrc2->left;
392
 
        lprcDst->right = lprcSrc2->right;
393
 
        lprcDst->top = lprcSrc2->top;
394
 
        lprcDst->bottom = lprcSrc2->bottom;
395
 
        return TRUE;
396
 
    }
397
 
    if(lprcSrc2->left - lprcSrc2->right == 0 || lprcSrc2->top - lprcSrc2->bottom == 0) {
398
 
        lprcDst->left = lprcSrc1->left;
399
 
        lprcDst->right = lprcSrc1->right;
400
 
        lprcDst->top = lprcSrc1->top;
401
 
        lprcDst->bottom = lprcSrc1->bottom;
402
 
        return TRUE;
403
 
    }
404
 
 
405
 
    lprcDst->left = lprcSrc1->left < lprcSrc2->left ? lprcSrc1->left : lprcSrc2->left;
406
 
    lprcDst->right = lprcSrc1->right > lprcSrc2->right ? lprcSrc1->right : lprcSrc2->right;
407
 
    lprcDst->top = lprcSrc1->top < lprcSrc2->top ? lprcSrc1->top : lprcSrc2->top;
408
 
    lprcDst->bottom = lprcSrc1->bottom > lprcSrc2->bottom ? lprcSrc1->bottom : lprcSrc2->bottom;
409
 
 
410
 
    return 0;
411
 
}
412
 
 
413
 
HWND GetActiveWindow() {
414
 
    return NULL;
415
 
}
416
 
 
417
 
HFONT CreateFont(int nHeight, int nWidth, int nEscapement,
418
 
int nOrientation, int fnWeight, DWORD fdwItalic, DWORD fdwUnderline,
419
 
DWORD fdwStrikeOut, DWORD fdwCharSet, DWORD fdwOutputPrecision,
420
 
DWORD fdwClipPrecision, DWORD fdwQuality, DWORD fdwPitchAndFamily,
421
 
LPCTSTR lpszFace) {
422
 
    return 0;
423
 
}
424
 
 
425
 
BOOL GetTextExtentPoint32(HDC hdc, LPCTSTR lpString, int c, LPSIZE lpSize) {
426
 
    return 0;
427
 
}
428
 
 
429
 
BOOL EnumWindows(WNDENUMPROC lpEnumFunc, LPARAM lParam) {
430
 
    return 0;
431
 
}
432
 
 
433
 
int GetWindowText(HWND hWnd, LPTSTR lpString, int nMaxCount) {
434
 
    return 0;
435
 
}
436
 
 
437
 
HMODULE LoadLibrary(LPCTSTR lpFileName) {
438
 
        return dlopen(lpFileName, RTLD_LAZY);
439
 
}
440
 
 
441
 
BOOL FreeLibrary(HMODULE hModule) {
442
 
        return dlclose(hModule);
443
 
}
444
 
 
445
 
void* GetProcAddress(HMODULE hModule, LPCSTR lpProcName) {
446
 
        return dlsym(hModule, lpProcName);
447
 
}
448
 
 
449
 
HGDIOBJ GetStockObject(int fnObject) {
450
 
        return -1;
451
 
}
452
 
 
453
 
BOOL IsWindowVisible(HWND hWnd) {
454
 
        return 0;
455
 
}
456
 
 
457
 
LRESULT DefWindowProc(HWND hWnd, UINT Msg, WPARAM wParam,
458
 
    LPARAM lParam) {
459
 
        return 0;
460
 
}
461
 
 
462
 
LONG GetWindowLong(HWND hWnd, int nIndex) {
463
 
        return 0;
464
 
}
465
 
 
466
 
BOOL RegisterClass(const WNDCLASS *lpWndClass) {
467
 
        return 0;
468
 
}
469
 
 
470
 
HMODULE GetModuleHandle(LPCTSTR lpModuleName) {
471
 
        return NULL;
472
 
}
473
 
 
474
 
HICON LoadIcon(HINSTANCE hInstance, LPCTSTR lpIconName) {
475
 
        return NULL;
476
 
}
477
 
 
478
 
int LoadCursor(HINSTANCE hInstance, LPCTSTR lpCursorName) {
479
 
        return 0;
480
 
}
481
 
 
482
 
BOOL Rectangle(HDC hdc,
483
 
  int nLeftRect, int nTopRect, int nRightRect, int nBottomRect) {
484
 
        return 0;
485
 
}
486
 
 
487
 
/* String to uppercase. */
488
 
 
489
 
char* _strupr(char*s) {
490
 
  return s;
491
 
}
492
 
 
493
 
#else /* WIN32 */
494
 
 
495
 
#include<io.h>
496
 
#include<direct.h>
497
 
#include"winfuncs.h"
498
 
#include "cttypes.h"
499
 
#include <windows.h>
500
 
#include <malloc.h>
501
 
 
502
 
static HMODULE thismod;
503
 
 
504
 
WINDUMMY_FUNC(BOOL)
505
 
WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
506
 
        thismod = (HMODULE) hinstDLL;
507
 
        return TRUE;
508
 
}
509
 
 
510
 
WINDUMMY_FUNC(char*)
511
 
mkdtemp(char *tmpl) {
512
 
    static const char charset[] =
513
 
        "=#abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
514
 
    static const unsigned int charset_len = sizeof(charset) - 1;
515
 
 
516
 
    const int len = strlen (tmpl);
517
 
    char* x_tail = tmpl + len - 6;
518
 
    LARGE_INTEGER rand_seed;
519
 
    uint64_t value = rand_seed.QuadPart ^ GetCurrentThreadId();
520
 
    unsigned int cnt = 0;
521
 
 
522
 
        if (len < 6)
523
 
        return NULL;
524
 
    if(memcmp(x_tail, "XXXXXX", 6))
525
 
        return NULL;
526
 
 
527
 
    QueryPerformanceCounter(&rand_seed);
528
 
 
529
 
    do {
530
 
        uint64_t val = value;
531
 
        char* x_char = x_tail;
532
 
        while(*x_char) {
533
 
            *x_char++ = charset[val % charset_len];
534
 
            val /= charset_len;
535
 
        }
536
 
        if (CreateDirectory (tmpl, NULL))
537
 
            return tmpl;
538
 
        if (ERROR_ALREADY_EXISTS != GetLastError())
539
 
            return NULL;
540
 
        value += 65537;
541
 
        ++cnt;
542
 
    } while (cnt < TMP_MAX);
543
 
 
544
 
    return NULL;
545
 
}
546
 
 
547
 
#endif /* WIN32 */
548
 
 
549
 
/* General helper functions. */
550
 
 
551
 
static const char *separator = "/"; /* Yes, on Windows too. */
552
 
 
553
 
#ifdef WIN32
554
 
 
555
 
static void get_install_path(char *path) {
556
 
        const int psize = 128;
557
 
        char modulepath[128]; /* MSVC fails when psize is put in these brackets. */
558
 
        char fname[50];
559
 
        char suffix[10];
560
 
 
561
 
        GetModuleFileName(thismod, modulepath, psize);
562
 
        winpath_to_internal(modulepath);
563
 
        split_path(modulepath, path, fname, suffix);
564
 
}
565
 
 
566
 
#else
567
 
 
568
 
static void get_install_path(char *path) {
569
 
        strcat(path, INSTALL_DATADIR);
570
 
}
571
 
 
572
 
#endif
573
 
 
574
 
/* We try to locate data files in two locations:
575
 
 *
576
 
 * 1. At the directory pointed to by environment variable CF_DATADIR
577
 
 * 2. At a platform-dependent install location
578
 
 *
579
 
 * Caller's responsibility is to ensure *e1 and *e2 are large enough.
580
 
 *
581
 
 */
582
 
 
583
 
static void build_name_estimates(const char *base_name, char *env_name, char *prefix_name) {
584
 
    const char *env_prefix;
585
 
    const char *varname = "CF_DATADIR";
586
 
    int len = 0;
587
 
 
588
 
    env_name[0] = '\0';
589
 
    prefix_name[0] = '\0';
590
 
 
591
 
    env_prefix = getenv(varname);
592
 
    if(env_prefix)
593
 
        len = strlen(env_prefix);
594
 
    if(len > 0) {
595
 
        strcat(env_name, env_prefix);
596
 
        if(strcmp(env_prefix + len-1, separator) != 0) {
597
 
            strcat(env_name, separator);
598
 
        }
599
 
        strcat(env_name, base_name);
600
 
    }
601
 
 
602
 
    get_install_path(prefix_name);
603
 
    if(strlen(prefix_name) > 0) {
604
 
        strcat(prefix_name, separator);
605
 
        strcat(prefix_name, base_name);
606
 
    }
607
 
}
608
 
 
609
 
 
610
 
WINDUMMY_FUNC(int)
611
 
open_data_file(const char *basename, int mode) {
612
 
    char ename[1024];
613
 
    char pname[1024];
614
 
    int i;
615
 
 
616
 
    build_name_estimates(basename, ename, pname);
617
 
    i = open(ename, mode);
618
 
    if(i != -1)
619
 
        return i;
620
 
    return open(pname, mode);
621
 
}
622
 
 
623
 
 
624
 
WINDUMMY_FUNC(int)
625
 
data_file_exists(const char *basename) {
626
 
    char ename[1024];
627
 
    char pname[1024];
628
 
 
629
 
    build_name_estimates(basename, ename, pname);
630
 
    if(_access(ename, 0) == 0)
631
 
        return 0;
632
 
    return _access(pname, 0);
633
 
}
634
 
 
635
 
/* Split a file name in three: path, base file name, and extension.
636
 
 * All internal file names use / as path separator, even on Windows.
637
 
 */
638
 
WINDUMMY_FUNC(void)
639
 
split_path(const char *fname, char *file_path, char *basename, char *ext) {
640
 
    int last_path = -1;
641
 
    int suff = -1;
642
 
    size_t l = strlen(fname);
643
 
    int path_end, base_start, base_end, ext_start;
644
 
    size_t i;
645
 
 
646
 
    file_path[0] = '\0';
647
 
    basename[0] = '\0';
648
 
 
649
 
    for (i = 0; i < l; i++) {
650
 
        if (fname[i] == '.')
651
 
            suff = i;
652
 
        if (fname[i] == '/')
653
 
            last_path = i;
654
 
    }
655
 
 
656
 
    path_end = 0;
657
 
    base_start = 0;
658
 
    if (last_path == 0) { // File in root.
659
 
        path_end = 1;
660
 
        base_start = 1;
661
 
    }
662
 
    if (last_path > 0) {
663
 
        path_end = last_path;
664
 
        base_start = last_path + 1;
665
 
    }
666
 
 
667
 
    if (suff > last_path) {
668
 
        ext_start = suff + 1;
669
 
        base_end = suff;
670
 
    } else {
671
 
        base_end = ext_start = l;
672
 
    }
673
 
 
674
 
    memcpy(file_path, fname, path_end);
675
 
    file_path[path_end] = '\0';
676
 
    memcpy(basename, fname + base_start, base_end - base_start);
677
 
    basename[base_end - base_start] = '\0';
678
 
    memcpy(ext, fname + ext_start, l - ext_start);
679
 
    ext[l - ext_start] = '\0';
680
 
}
681
 
 
682
 
 
683
 
WINDUMMY_FUNC(void)
684
 
make_path(char *opath, const char *dir, const char *basename, const char *ext) {
685
 
    const char dirsep = '/';
686
 
    const char *dirseps = "/";
687
 
    opath[0] = '\0';
688
 
 
689
 
    if(dir) {
690
 
        strcat(opath, dir);
691
 
        if(opath[strlen(opath) - 1] != dirsep) {
692
 
            strcat(opath, dirseps);
693
 
        }
694
 
    }
695
 
 
696
 
    if(basename)
697
 
        strcat(opath, basename);
698
 
 
699
 
    if(ext) {
700
 
        if(ext[0] != '.')
701
 
            strcat(opath, ".");
702
 
        strcat(opath, ext);
703
 
    }
704
 
}
705
 
 
706
 
/**
707
 
 * Convert backslashes to slashes. No-op on UNIX.
708
 
 */
709
 
WINDUMMY_FUNC(void)
710
 
winpath_to_internal(char *p) {
711
 
#if WIN32
712
 
    int i;
713
 
    for(i=0; p[i] != '\0'; i++) {
714
 
        if(p[i] == '\\')
715
 
            p[i] = '/';
716
 
    }
717
 
#endif
718
 
}
719
 
 
720
 
/* Get current working directory. */
721
 
 
722
 
WINDUMMY_FUNC(unsigned int) curr_dir(unsigned int bsize, char* buf) {
723
 
#ifdef _MSC_VER
724
 
        _getcwd(buf, bsize);
725
 
#else
726
 
    getcwd(buf, bsize);
727
 
#endif
728
 
    winpath_to_internal(buf);
729
 
    return strlen(buf);
730
 
}
731
 
 
732
 
#if WIN32
733
 
 
734
 
#define BUFSIZE 100
735
 
 
736
 
WINDUMMY_FUNC (FILE*)
737
 
create_temp_file(void) {
738
 
    char temppath[BUFSIZE];
739
 
    char tempfname[BUFSIZE];
740
 
    DWORD retval;
741
 
 
742
 
    retval = GetTempPath(BUFSIZE, temppath);
743
 
    if(retval >= BUFSIZE || retval == 0)
744
 
        return NULL;
745
 
 
746
 
    if(GetTempFileName(temppath, "CF", 0, tempfname) == 0)
747
 
        return NULL;
748
 
 
749
 
    return fopen(tempfname, "w+bD");
750
 
}
751
 
 
752
 
#else
753
 
 
754
 
WINDUMMY_FUNC (FILE*)
755
 
create_temp_file(void) {
756
 
        FILE *tmp_file;
757
 
        int tmp_fd;
758
 
        char* pattrn = malloc(100);
759
 
        strcpy(pattrn, "/tmp/CF.XXXXXX");
760
 
 
761
 
        tmp_fd = mkstemp(pattrn);
762
 
 
763
 
        /* unlink file immediatly, it gets unlinked when file descriptor is closed */
764
 
        unlink(pattrn);
765
 
    free(pattrn);
766
 
 
767
 
        if (tmp_fd == -1)
768
 
                return NULL;
769
 
 
770
 
        if (!(tmp_file = fdopen(tmp_fd, "w+b"))) {
771
 
                return NULL;
772
 
        }
773
 
 
774
 
        return tmp_file;
775
 
}
776
 
#endif