summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel/qfunctions_wince.cpp
blob: 2de34c3bdc7708d26c087d86bd3356fce9a105fd (plain)
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
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifdef _WIN32_WCE //Q_OS_WINCE

#include <windows.h>
#include <winbase.h>
#include <kfuncs.h>
#include <stdio.h>
#include <altcecrt.h>

#include "qplatformdefs.h"
#include "qfunctions_wince.h"
#include "qstring.h"
#include "qbytearray.h"
#include "qhash.h"

QT_USE_NAMESPACE

#ifdef __cplusplus
extern "C" {
#endif

wchar_t* CEPrivConvCharToWide(const char* string)
{
    size_t length = strlen(string);
    wchar_t* wString = new wchar_t[length +1];
    for (unsigned int i = 0; i < (length +1); i++)
        wString[i] = string[i];
    return wString;
}

// Time -------------------------------------------------------------
time_t qt_wince_ftToTime_t( const FILETIME ft )
{
    ULARGE_INTEGER li;
    li.LowPart  = ft.dwLowDateTime;
    li.HighPart = ft.dwHighDateTime;

    // 100-nanosec to seconds
    li.QuadPart /= 10000000;

    // FILETIME is from 1601-01-01 T 00:00:00
    // time_t   is from 1970-01-01 T 00:00:00
    // 1970 - 1601 = 369 year (89 leap years)
    //
    // ((369y*365d) + 89d) *24h *60min *60sec
    // = 11644473600 seconds
    li.QuadPart -= 11644473600;
    return li.LowPart;
}

FILETIME qt_wince_time_tToFt( time_t tt )
{
    ULARGE_INTEGER li;
    li.QuadPart  = tt;
    li.QuadPart += 11644473600;
    li.QuadPart *= 10000000;

    FILETIME ft;
    ft.dwLowDateTime = li.LowPart;
    ft.dwHighDateTime = li.HighPart;
    return ft;
}

// File I/O ---------------------------------------------------------
int errno = 0;

int qt_wince__getdrive( void )
{
    return 1;
}

int qt_wince__waccess( const wchar_t *path, int pmode )
{
    DWORD res = GetFileAttributes( path );
    if ( 0xFFFFFFFF == res )
        return -1;

    if ( (pmode & W_OK) && (res & FILE_ATTRIBUTE_READONLY) )
        return -1;

    if ( (pmode & X_OK) && !(res & FILE_ATTRIBUTE_DIRECTORY) ) {
        QString file = QString::fromWCharArray(path);
        if ( !(file.endsWith(QString::fromLatin1(".exe")) ||
           file.endsWith(QString::fromLatin1(".com"))) )
        return -1;
    }

    return 0;
}

int qt_wince_open( const char *filename, int oflag, int pmode )
{
    QString fn( QString::fromLatin1(filename) );
    return _wopen( (wchar_t*)fn.utf16(), oflag, pmode );
}

int qt_wince__wopen( const wchar_t *filename, int oflag, int /*pmode*/ )
{
    wchar_t *flag;

    if ( oflag & _O_APPEND ) {
        if ( oflag & _O_WRONLY ) {
            flag = L"a";
        } else if ( oflag & _O_RDWR ) {
            flag = L"a+";
        }
    } else if (oflag & _O_BINARY) {
        if ( oflag & _O_WRONLY ) {
            flag = L"wb";
        } else if ( oflag & _O_RDWR ) {
            flag = L"w+b"; // slightly different from "r+" where the file must exist
        } else if ( oflag & _O_RDONLY ) {
            flag = L"rb";
        } else {
            flag = L"b";
        }
    } else {
        if ( oflag & _O_WRONLY ) {
            flag = L"wt";
        } else if ( oflag & _O_RDWR ) {
            flag = L"w+t"; // slightly different from "r+" where the file must exist
        } else if ( oflag & _O_RDONLY ) {
            flag = L"rt";
        } else {
            flag = L"t";
        }
    }

    int retval = (int)_wfopen( filename, flag );
    return (retval == NULL) ? -1 : retval;
}

long qt_wince__lseek( int handle, long offset, int origin )
{
    return fseek( (FILE*)handle, offset, origin );
}

int qt_wince__read( int handle, void *buffer, unsigned int count )
{
    return fread( buffer, 1, count, (FILE*)handle );
}

int qt_wince__write( int handle, const void *buffer, unsigned int count )
{
    return fwrite( buffer, 1, count, (FILE*)handle );
}

int qt_wince__close( int handle )
{
    if (!handle)
        return 0;
    return fclose( (FILE*)handle );
}

FILE *qt_wince__fdopen(int handle, const char* /*mode*/)
{
    return (FILE*)handle;
}

FILE *qt_wince_fdopen( int handle, const char* /*mode*/ )
{
    return (FILE*)handle;
}

void qt_wince_rewind( FILE *stream )
{
    fseek( stream, 0L, SEEK_SET );
}

int qt_wince___fileno(FILE *f)
{
    return (int) _fileno(f);
}

FILE *qt_wince_tmpfile( void )
{
    static long i = 0;
    char name[16];
    sprintf( name, "tmp%i", i++ );
    return fopen( name, "r+" );
}

int qt_wince__mkdir(const char *dirname)
{
    return CreateDirectory(reinterpret_cast<const wchar_t *> (QString(QString::fromLatin1(dirname)).utf16()), 0) ? 0 : -1;
}

int qt_wince__rmdir(const char *dirname)
{
    return RemoveDirectory(reinterpret_cast<const wchar_t *> (QString::fromLatin1(dirname).utf16())) ? 0 : -1;
}

int qt_wince__access( const char *path, int pmode )
{
    return _waccess(reinterpret_cast<const wchar_t *> (QString::fromLatin1(path).utf16()),pmode);
}

int qt_wince__rename( const char *oldname, const char *newname )
{
    return !MoveFile(reinterpret_cast<const wchar_t *> (QString::fromLatin1(oldname).utf16()), reinterpret_cast<const wchar_t *> (QString::fromLatin1(newname).utf16()));
}

int qt_wince__remove( const char *name )
{
    return !DeleteFile(reinterpret_cast<const wchar_t *> (QString::fromLatin1(name).utf16()));
}

int qt_wince_stat( const char *path, struct stat *buffer )
{
    WIN32_FIND_DATA finfo;
    HANDLE ff = FindFirstFile( reinterpret_cast<const wchar_t *> (QString::fromLatin1(path).utf16()), &finfo );

    if ( ff == INVALID_HANDLE_VALUE )
        return -1;

    buffer->st_ctime = qt_wince_ftToTime_t( finfo.ftCreationTime );
    buffer->st_atime = qt_wince_ftToTime_t( finfo.ftLastAccessTime );
    buffer->st_mtime = qt_wince_ftToTime_t( finfo.ftLastWriteTime );
    buffer->st_nlink = 0;
    buffer->st_size  = finfo.nFileSizeLow; // ### missing high!
    buffer->st_mode  = (finfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? _S_IFDIR : _S_IFREG;
    buffer->st_mode |= (finfo.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? _O_RDONLY : _O_RDWR;
    return (FindClose(ff) == 0);
}

int qt_wince__fstat( int handle, struct stat *buffer)
{
    BY_HANDLE_FILE_INFORMATION fInfo;
    BOOL res = GetFileInformationByHandle((HANDLE)handle, &fInfo);

    buffer->st_ctime = qt_wince_ftToTime_t( fInfo.ftCreationTime );
    buffer->st_atime = qt_wince_ftToTime_t( fInfo.ftLastAccessTime );
    buffer->st_mtime = qt_wince_ftToTime_t( fInfo.ftLastWriteTime );
    buffer->st_nlink = 0;
    buffer->st_size  = fInfo.nFileSizeLow; // ### missing high!
    buffer->st_mode  = (fInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? _S_IFDIR : _S_IFREG;
    buffer->st_mode |= (fInfo.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? _O_RDONLY : _O_RDWR;
    return (res == 0);
}

int qt_wince_SetErrorMode(int newValue)
{
    static int oldValue;
    int result = oldValue;
    oldValue = newValue;
    return result;
}

bool qt_wince__chmod(const char *file, int mode)
{
    return _wchmod( reinterpret_cast<const wchar_t *> (QString::fromLatin1(file).utf16()), mode);
}

bool qt_wince__wchmod(const wchar_t *file, int mode)
{
    BOOL success = FALSE;
    // ### Does not work properly, what about just adding one property?
    if(mode&_S_IWRITE) {
        success = SetFileAttributes(file, FILE_ATTRIBUTE_NORMAL);
    } else if((mode&_S_IREAD) && !(mode&_S_IWRITE)) {
        success = SetFileAttributes(file, FILE_ATTRIBUTE_READONLY);
    }
    return success ? 0 : -1;
}

HANDLE qt_wince_CreateFileA(LPCSTR filename, DWORD access, DWORD share, LPSECURITY_ATTRIBUTES attr, DWORD dispo, DWORD flags, HANDLE tempFile)
{
    return CreateFileW( reinterpret_cast<const wchar_t *>(QString::fromLatin1(filename).utf16()), access, share, attr, dispo, flags, tempFile);
}

// Graphics ---------------------------------------------------------
BOOL qt_wince_SetWindowOrgEx( HDC /*hdc*/, int /*X*/, int /*Y*/, LPPOINT /*lpPoint*/) {
    return TRUE;
}

// Threading --------------------------------------------------------
HANDLE qt_wince__beginthread(void( *start_address )( void * ), unsigned stack_size, void *arglist)
{
    unsigned initflag = 0;
    if (stack_size > 0)
        initflag |= STACK_SIZE_PARAM_IS_A_RESERVATION;
    return CreateThread(NULL, stack_size, (LPTHREAD_START_ROUTINE)start_address, arglist, initflag, NULL);
}

unsigned long qt_wince__beginthreadex( void *security,
                  unsigned stack_size,
                  unsigned (__stdcall *start_address)(void *),
                  void *arglist,
                  unsigned initflag,
                  unsigned *thrdaddr)
{
    if (stack_size > 0)
        initflag |= STACK_SIZE_PARAM_IS_A_RESERVATION;
    return (unsigned long)
            CreateThread( (LPSECURITY_ATTRIBUTES)security,
                          (DWORD)stack_size,
                          (LPTHREAD_START_ROUTINE)start_address,
                          (LPVOID)arglist,
                          (DWORD)initflag | CREATE_SUSPENDED,
                          (LPDWORD)thrdaddr);
}

void qt_wince__endthreadex(unsigned nExitCode) {
    ExitThread((DWORD)nExitCode);
}

void *qt_wince_bsearch(const void *key,
           const void *base,
           size_t num,
           size_t size,
           int (__cdecl *compare)(const void *, const void *))
{
    size_t low = 0;
    size_t high = num - 1;
    while (low <= high) {
        size_t mid = (low + high) >> 1;
        int c = compare(key, (char*)base + mid * size);
        if (c < 0) {
            if (!mid)
                break;
            high = mid - 1;
        } else if (c > 0)
            low = mid + 1;
        else
            return (char*) base + mid * size;
    }
    return 0;
}

void *lfind(const void* key, const void* base, size_t* elements, size_t size,
            int (__cdecl *compare)(const void*, const void*))
{
    const char* current = (char*) base;
    const char* const end = (char*) (current + (*elements) * size);
    while (current != end) {
        if (compare(current, key) == 0)
            return (void*)current;
        current += size;
    }
    return 0;
}

DWORD qt_wince_GetThreadLocale(void)
{
    return GetUserDefaultLCID();
}

void *qt_wince_calloc( size_t num, size_t size )
{
    void *ptr = malloc( num * size );
    if( ptr )
        memset( ptr, 0, num * size );
    return ptr;
}

// _getpid is currently only used for creating a temporary filename
int qt_wince__getpid()
{
    return qAbs((int)GetCurrentProcessId());
}

#ifdef __cplusplus
} // extern "C"
#endif
// Environment ------------------------------------------------------
inline QHash<QByteArray, QByteArray>& qt_app_environment()
{
    static QHash<QByteArray, QByteArray> internalEnvironment;
    return internalEnvironment;
}

errno_t qt_wince_getenv_s(size_t* sizeNeeded, char* buffer, size_t bufferSize, const char* varName)
{
    if (!sizeNeeded)
        return EINVAL;

    if (!qt_app_environment().contains(varName)) {
        if (buffer)
            buffer[0] = '\0';
        return ENOENT;
    }

    QByteArray value = qt_app_environment().value(varName);
    if (!value.endsWith('\0')) // win32 guarantees terminated string
        value.append('\0');

    if (bufferSize < (size_t)value.size()) {
        *sizeNeeded = value.size();
        return 0;
    }

    strcpy(buffer, value.constData());
    return 0;
}

errno_t qt_wince__putenv_s(const char* varName, const char* value)
{
    QByteArray input = value;
    if (input.isEmpty()) {
        if (qt_app_environment().contains(varName))
            qt_app_environment().remove(varName);
    } else {
        // win32 guarantees terminated string
        if (!input.endsWith('\0'))
            input.append('\0');
        qt_app_environment()[varName] = input;
    }

    return 0;
}

#endif // Q_OS_WINCE