1
/****************************************************************************
3
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4
** All rights reserved.
5
** Contact: Nokia Corporation (qt-info@nokia.com)
7
** This file is part of the QtCore module of the Qt Toolkit.
9
** $QT_BEGIN_LICENSE:LGPL$
10
** No Commercial Usage
11
** This file contains pre-release code and may not be distributed.
12
** You may use this file in accordance with the terms and conditions
13
** contained in the Technology Preview License Agreement accompanying
16
** GNU Lesser General Public License Usage
17
** Alternatively, this file may be used under the terms of the GNU Lesser
18
** General Public License version 2.1 as published by the Free Software
19
** Foundation and appearing in the file LICENSE.LGPL included in the
20
** packaging of this file. Please review the following information to
21
** ensure the GNU Lesser General Public License version 2.1 requirements
22
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
** In addition, as a special exception, Nokia gives you certain additional
25
** rights. These rights are described in the Nokia Qt LGPL Exception
26
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
40
****************************************************************************/
41
#ifdef _WIN32_WCE //Q_OS_WINCE
49
#include "qplatformdefs.h"
50
#include "qfunctions_wince.h"
52
#include "qbytearray.h"
61
wchar_t* CEPrivConvCharToWide(const char* string)
63
size_t length = strlen(string);
64
wchar_t* wString = new wchar_t[length +1];
65
for (unsigned int i = 0; i < (length +1); i++)
66
wString[i] = string[i];
70
// Time -------------------------------------------------------------
71
time_t qt_wince_ftToTime_t( const FILETIME ft )
74
li.LowPart = ft.dwLowDateTime;
75
li.HighPart = ft.dwHighDateTime;
77
// 100-nanosec to seconds
78
li.QuadPart /= 10000000;
80
// FILETIME is from 1601-01-01 T 00:00:00
81
// time_t is from 1970-01-01 T 00:00:00
82
// 1970 - 1601 = 369 year (89 leap years)
84
// ((369y*365d) + 89d) *24h *60min *60sec
85
// = 11644473600 seconds
86
li.QuadPart -= 11644473600;
90
FILETIME qt_wince_time_tToFt( time_t tt )
94
li.QuadPart += 11644473600;
95
li.QuadPart *= 10000000;
98
ft.dwLowDateTime = li.LowPart;
99
ft.dwHighDateTime = li.HighPart;
103
// File I/O ---------------------------------------------------------
106
int qt_wince__getdrive( void )
111
int qt_wince__waccess( const wchar_t *path, int pmode )
113
DWORD res = GetFileAttributes( path );
114
if ( 0xFFFFFFFF == res )
117
if ( (pmode & W_OK) && (res & FILE_ATTRIBUTE_READONLY) )
120
if ( (pmode & X_OK) && !(res & FILE_ATTRIBUTE_DIRECTORY) ) {
121
QString file = QString::fromWCharArray(path);
122
if ( !(file.endsWith(QString::fromLatin1(".exe")) ||
123
file.endsWith(QString::fromLatin1(".com"))) )
130
int qt_wince_open( const char *filename, int oflag, int pmode )
132
QString fn( QString::fromLatin1(filename) );
133
return _wopen( (wchar_t*)fn.utf16(), oflag, pmode );
136
int qt_wince__wopen( const wchar_t *filename, int oflag, int /*pmode*/ )
140
if ( oflag & _O_APPEND ) {
141
if ( oflag & _O_WRONLY ) {
143
} else if ( oflag & _O_RDWR ) {
146
} else if (oflag & _O_BINARY) {
147
if ( oflag & _O_WRONLY ) {
149
} else if ( oflag & _O_RDWR ) {
150
flag = L"w+b"; // slightly different from "r+" where the file must exist
151
} else if ( oflag & _O_RDONLY ) {
157
if ( oflag & _O_WRONLY ) {
159
} else if ( oflag & _O_RDWR ) {
160
flag = L"w+t"; // slightly different from "r+" where the file must exist
161
} else if ( oflag & _O_RDONLY ) {
168
int retval = (int)_wfopen( filename, flag );
169
return (retval == NULL) ? -1 : retval;
172
long qt_wince__lseek( int handle, long offset, int origin )
174
return fseek( (FILE*)handle, offset, origin );
177
int qt_wince__read( int handle, void *buffer, unsigned int count )
179
return fread( buffer, 1, count, (FILE*)handle );
182
int qt_wince__write( int handle, const void *buffer, unsigned int count )
184
return fwrite( buffer, 1, count, (FILE*)handle );
187
int qt_wince__close( int handle )
191
return fclose( (FILE*)handle );
194
FILE *qt_wince__fdopen(int handle, const char* /*mode*/)
196
return (FILE*)handle;
199
FILE *qt_wince_fdopen( int handle, const char* /*mode*/ )
201
return (FILE*)handle;
204
void qt_wince_rewind( FILE *stream )
206
fseek( stream, 0L, SEEK_SET );
209
int qt_wince___fileno(FILE *f)
211
return (int) _fileno(f);
214
FILE *qt_wince_tmpfile( void )
218
sprintf( name, "tmp%i", i++ );
219
return fopen( name, "r+" );
222
int qt_wince__mkdir(const char *dirname)
224
return CreateDirectory(reinterpret_cast<const wchar_t *> (QString(QString::fromLatin1(dirname)).utf16()), 0) ? 0 : -1;
227
int qt_wince__rmdir(const char *dirname)
229
return RemoveDirectory(reinterpret_cast<const wchar_t *> (QString::fromLatin1(dirname).utf16())) ? 0 : -1;
232
int qt_wince__access( const char *path, int pmode )
234
return _waccess(reinterpret_cast<const wchar_t *> (QString::fromLatin1(path).utf16()),pmode);
237
int qt_wince__rename( const char *oldname, const char *newname )
239
return !MoveFile(reinterpret_cast<const wchar_t *> (QString::fromLatin1(oldname).utf16()), reinterpret_cast<const wchar_t *> (QString::fromLatin1(newname).utf16()));
242
int qt_wince__remove( const char *name )
244
return !DeleteFile(reinterpret_cast<const wchar_t *> (QString::fromLatin1(name).utf16()));
247
int qt_wince_stat( const char *path, struct stat *buffer )
249
WIN32_FIND_DATA finfo;
250
HANDLE ff = FindFirstFile( reinterpret_cast<const wchar_t *> (QString::fromLatin1(path).utf16()), &finfo );
252
if ( ff == INVALID_HANDLE_VALUE )
255
buffer->st_ctime = qt_wince_ftToTime_t( finfo.ftCreationTime );
256
buffer->st_atime = qt_wince_ftToTime_t( finfo.ftLastAccessTime );
257
buffer->st_mtime = qt_wince_ftToTime_t( finfo.ftLastWriteTime );
258
buffer->st_nlink = 0;
259
buffer->st_size = finfo.nFileSizeLow; // ### missing high!
260
buffer->st_mode = (finfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? _S_IFDIR : _S_IFREG;
261
buffer->st_mode |= (finfo.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? _O_RDONLY : _O_RDWR;
262
return (FindClose(ff) == 0);
265
int qt_wince__fstat( int handle, struct stat *buffer)
267
BY_HANDLE_FILE_INFORMATION fInfo;
268
BOOL res = GetFileInformationByHandle((HANDLE)handle, &fInfo);
270
buffer->st_ctime = qt_wince_ftToTime_t( fInfo.ftCreationTime );
271
buffer->st_atime = qt_wince_ftToTime_t( fInfo.ftLastAccessTime );
272
buffer->st_mtime = qt_wince_ftToTime_t( fInfo.ftLastWriteTime );
273
buffer->st_nlink = 0;
274
buffer->st_size = fInfo.nFileSizeLow; // ### missing high!
275
buffer->st_mode = (fInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? _S_IFDIR : _S_IFREG;
276
buffer->st_mode |= (fInfo.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? _O_RDONLY : _O_RDWR;
280
int qt_wince_SetErrorMode(int newValue)
283
int result = oldValue;
288
bool qt_wince__chmod(const char *file, int mode)
290
return _wchmod( reinterpret_cast<const wchar_t *> (QString::fromLatin1(file).utf16()), mode);
293
bool qt_wince__wchmod(const wchar_t *file, int mode)
295
BOOL success = FALSE;
296
// ### Does not work properly, what about just adding one property?
298
success = SetFileAttributes(file, FILE_ATTRIBUTE_NORMAL);
299
} else if((mode&_S_IREAD) && !(mode&_S_IWRITE)) {
300
success = SetFileAttributes(file, FILE_ATTRIBUTE_READONLY);
302
return success ? 0 : -1;
305
HANDLE qt_wince_CreateFileA(LPCSTR filename, DWORD access, DWORD share, LPSECURITY_ATTRIBUTES attr, DWORD dispo, DWORD flags, HANDLE tempFile)
307
return CreateFileW( reinterpret_cast<const wchar_t *>(QString::fromLatin1(filename).utf16()), access, share, attr, dispo, flags, tempFile);
310
// Graphics ---------------------------------------------------------
311
BOOL qt_wince_SetWindowOrgEx( HDC /*hdc*/, int /*X*/, int /*Y*/, LPPOINT /*lpPoint*/) {
315
// Threading --------------------------------------------------------
316
HANDLE qt_wince__beginthread(void( *start_address )( void * ), unsigned stack_size, void *arglist)
318
unsigned initflag = 0;
320
initflag |= STACK_SIZE_PARAM_IS_A_RESERVATION;
321
return CreateThread(NULL, stack_size, (LPTHREAD_START_ROUTINE)start_address, arglist, initflag, NULL);
324
unsigned long qt_wince__beginthreadex( void *security,
326
unsigned (__stdcall *start_address)(void *),
332
initflag |= STACK_SIZE_PARAM_IS_A_RESERVATION;
333
return (unsigned long)
334
CreateThread( (LPSECURITY_ATTRIBUTES)security,
336
(LPTHREAD_START_ROUTINE)start_address,
338
(DWORD)initflag | CREATE_SUSPENDED,
342
void qt_wince__endthreadex(unsigned nExitCode) {
343
ExitThread((DWORD)nExitCode);
346
void *qt_wince_bsearch(const void *key,
350
int (__cdecl *compare)(const void *, const void *))
353
size_t high = num - 1;
354
while (low <= high) {
355
unsigned int mid = ((unsigned) (low + high)) >> 1;
356
int c = compare(key, (char*)base + mid * size);
362
return (char*) base + mid * size;
367
void *lfind(const void* key, const void* base, size_t* elements, size_t size,
368
int (__cdecl *compare)(const void*, const void*))
370
const char* current = (char*) base;
371
const char* const end = (char*) (current + (*elements) * size);
372
while (current != end) {
373
if (compare(current, key) == 0)
374
return (void*)current;
380
DWORD qt_wince_GetThreadLocale(void)
382
return GetUserDefaultLCID();
385
void *qt_wince_calloc( size_t num, size_t size )
387
void *ptr = malloc( num * size );
389
memset( ptr, 0, num * size );
393
// _getpid is currently only used for creating a temporary filename
394
int qt_wince__getpid()
396
return qAbs((int)GetCurrentProcessId());
402
// Environment ------------------------------------------------------
403
inline QHash<QByteArray, QByteArray>& qt_app_environment()
405
static QHash<QByteArray, QByteArray> internalEnvironment;
406
return internalEnvironment;
409
errno_t qt_wince_getenv_s(size_t* sizeNeeded, char* buffer, size_t bufferSize, const char* varName)
414
if (!qt_app_environment().contains(varName)) {
420
QByteArray value = qt_app_environment().value(varName);
421
if (!value.endsWith('\0')) // win32 guarantees terminated string
424
if (bufferSize < (size_t)value.size()) {
425
*sizeNeeded = value.size();
429
strcpy(buffer, value.constData());
433
errno_t qt_wince__putenv_s(const char* varName, const char* value)
435
QByteArray input = value;
436
if (input.isEmpty()) {
437
if (qt_app_environment().contains(varName))
438
qt_app_environment().remove(varName);
440
// win32 guarantees terminated string
441
if (!input.endsWith('\0'))
443
qt_app_environment()[varName] = input;