~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/corelib/kernel/qfunctions_wince.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2009-11-02 18:30:08 UTC
  • mfrom: (1.2.2 upstream)
  • mto: (15.2.5 experimental)
  • mto: This revision was merged to the branch mainline in revision 88.
  • Revision ID: james.westby@ubuntu.com-20091102183008-b6a4gcs128mvfb3m
Tags: upstream-4.6.0~beta1
ImportĀ upstreamĀ versionĀ 4.6.0~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
 
4
** All rights reserved.
 
5
** Contact: Nokia Corporation (qt-info@nokia.com)
 
6
**
 
7
** This file is part of the QtCore module of the Qt Toolkit.
 
8
**
 
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
 
14
** this package.
 
15
**
 
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.
 
23
**
 
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.
 
27
**
 
28
** If you have questions regarding the use of this file, please contact
 
29
** Nokia at qt-info@nokia.com.
 
30
**
 
31
**
 
32
**
 
33
**
 
34
**
 
35
**
 
36
**
 
37
**
 
38
** $QT_END_LICENSE$
 
39
**
 
40
****************************************************************************/
 
41
#ifdef _WIN32_WCE //Q_OS_WINCE
 
42
 
 
43
#include <windows.h>
 
44
#include <winbase.h>
 
45
#include <kfuncs.h>
 
46
#include <stdio.h>
 
47
#include <altcecrt.h>
 
48
 
 
49
#include "qplatformdefs.h"
 
50
#include "qfunctions_wince.h"
 
51
#include "qstring.h"
 
52
#include "qbytearray.h"
 
53
#include "qhash.h"
 
54
 
 
55
QT_USE_NAMESPACE
 
56
 
 
57
#ifdef __cplusplus
 
58
extern "C" {
 
59
#endif
 
60
 
 
61
wchar_t* CEPrivConvCharToWide(const char* string)
 
62
{
 
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];
 
67
    return wString;
 
68
}
 
69
 
 
70
// Time -------------------------------------------------------------
 
71
time_t qt_wince_ftToTime_t( const FILETIME ft )
 
72
{
 
73
    ULARGE_INTEGER li;
 
74
    li.LowPart  = ft.dwLowDateTime;
 
75
    li.HighPart = ft.dwHighDateTime;
 
76
 
 
77
    // 100-nanosec to seconds
 
78
    li.QuadPart /= 10000000;
 
79
 
 
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)
 
83
    //
 
84
    // ((369y*365d) + 89d) *24h *60min *60sec
 
85
    // = 11644473600 seconds
 
86
    li.QuadPart -= 11644473600;
 
87
    return li.LowPart;
 
88
}
 
89
 
 
90
FILETIME qt_wince_time_tToFt( time_t tt )
 
91
{
 
92
    ULARGE_INTEGER li;
 
93
    li.QuadPart  = tt;
 
94
    li.QuadPart += 11644473600;
 
95
    li.QuadPart *= 10000000;
 
96
 
 
97
    FILETIME ft;
 
98
    ft.dwLowDateTime = li.LowPart;
 
99
    ft.dwHighDateTime = li.HighPart;
 
100
    return ft;
 
101
}
 
102
 
 
103
// File I/O ---------------------------------------------------------
 
104
int errno = 0;
 
105
 
 
106
int qt_wince__getdrive( void )
 
107
{
 
108
    return 1;
 
109
}
 
110
 
 
111
int qt_wince__waccess( const wchar_t *path, int pmode )
 
112
{
 
113
    DWORD res = GetFileAttributes( path );
 
114
    if ( 0xFFFFFFFF == res )
 
115
        return -1;
 
116
 
 
117
    if ( (pmode & W_OK) && (res & FILE_ATTRIBUTE_READONLY) )
 
118
        return -1;
 
119
 
 
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"))) )
 
124
        return -1;
 
125
    }
 
126
 
 
127
    return 0;
 
128
}
 
129
 
 
130
int qt_wince_open( const char *filename, int oflag, int pmode )
 
131
{
 
132
    QString fn( QString::fromLatin1(filename) );
 
133
    return _wopen( (wchar_t*)fn.utf16(), oflag, pmode );
 
134
}
 
135
 
 
136
int qt_wince__wopen( const wchar_t *filename, int oflag, int /*pmode*/ )
 
137
{
 
138
    wchar_t *flag;
 
139
 
 
140
    if ( oflag & _O_APPEND ) {
 
141
        if ( oflag & _O_WRONLY ) {
 
142
            flag = L"a";
 
143
        } else if ( oflag & _O_RDWR ) {
 
144
            flag = L"a+";
 
145
        }
 
146
    } else if (oflag & _O_BINARY) {
 
147
        if ( oflag & _O_WRONLY ) {
 
148
            flag = L"wb";
 
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 ) {
 
152
            flag = L"rb";
 
153
        } else {
 
154
            flag = L"b";
 
155
        }
 
156
    } else {
 
157
        if ( oflag & _O_WRONLY ) {
 
158
            flag = L"wt";
 
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 ) {
 
162
            flag = L"rt";
 
163
        } else {
 
164
            flag = L"t";
 
165
        }
 
166
    }
 
167
 
 
168
    int retval = (int)_wfopen( filename, flag );
 
169
    return (retval == NULL) ? -1 : retval;
 
170
}
 
171
 
 
172
long qt_wince__lseek( int handle, long offset, int origin )
 
173
{
 
174
    return fseek( (FILE*)handle, offset, origin );
 
175
}
 
176
 
 
177
int qt_wince__read( int handle, void *buffer, unsigned int count )
 
178
{
 
179
    return fread( buffer, 1, count, (FILE*)handle );
 
180
}
 
181
 
 
182
int qt_wince__write( int handle, const void *buffer, unsigned int count )
 
183
{
 
184
    return fwrite( buffer, 1, count, (FILE*)handle );
 
185
}
 
186
 
 
187
int qt_wince__close( int handle )
 
188
{
 
189
    if (!handle)
 
190
        return 0;
 
191
    return fclose( (FILE*)handle );
 
192
}
 
193
 
 
194
FILE *qt_wince__fdopen(int handle, const char* /*mode*/)
 
195
{
 
196
    return (FILE*)handle;
 
197
}
 
198
 
 
199
FILE *qt_wince_fdopen( int handle, const char* /*mode*/ )
 
200
{
 
201
    return (FILE*)handle;
 
202
}
 
203
 
 
204
void qt_wince_rewind( FILE *stream )
 
205
{
 
206
    fseek( stream, 0L, SEEK_SET );
 
207
}
 
208
 
 
209
int qt_wince___fileno(FILE *f)
 
210
{
 
211
    return (int) _fileno(f);
 
212
}
 
213
 
 
214
FILE *qt_wince_tmpfile( void )
 
215
{
 
216
    static long i = 0;
 
217
    char name[16];
 
218
    sprintf( name, "tmp%i", i++ );
 
219
    return fopen( name, "r+" );
 
220
}
 
221
 
 
222
int qt_wince__mkdir(const char *dirname)
 
223
{
 
224
    return CreateDirectory(reinterpret_cast<const wchar_t *> (QString(QString::fromLatin1(dirname)).utf16()), 0) ? 0 : -1;
 
225
}
 
226
 
 
227
int qt_wince__rmdir(const char *dirname)
 
228
{
 
229
    return RemoveDirectory(reinterpret_cast<const wchar_t *> (QString::fromLatin1(dirname).utf16())) ? 0 : -1;
 
230
}
 
231
 
 
232
int qt_wince__access( const char *path, int pmode )
 
233
{
 
234
    return _waccess(reinterpret_cast<const wchar_t *> (QString::fromLatin1(path).utf16()),pmode);
 
235
}
 
236
 
 
237
int qt_wince__rename( const char *oldname, const char *newname )
 
238
{
 
239
    return !MoveFile(reinterpret_cast<const wchar_t *> (QString::fromLatin1(oldname).utf16()), reinterpret_cast<const wchar_t *> (QString::fromLatin1(newname).utf16()));
 
240
}
 
241
 
 
242
int qt_wince__remove( const char *name )
 
243
{
 
244
    return !DeleteFile(reinterpret_cast<const wchar_t *> (QString::fromLatin1(name).utf16()));
 
245
}
 
246
 
 
247
int qt_wince_stat( const char *path, struct stat *buffer )
 
248
{
 
249
    WIN32_FIND_DATA finfo;
 
250
    HANDLE ff = FindFirstFile( reinterpret_cast<const wchar_t *> (QString::fromLatin1(path).utf16()), &finfo );
 
251
 
 
252
    if ( ff == INVALID_HANDLE_VALUE )
 
253
        return -1;
 
254
 
 
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);
 
263
}
 
264
 
 
265
int qt_wince__fstat( int handle, struct stat *buffer)
 
266
{
 
267
    BY_HANDLE_FILE_INFORMATION fInfo;
 
268
    BOOL res = GetFileInformationByHandle((HANDLE)handle, &fInfo);
 
269
 
 
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;
 
277
    return (res == 0);
 
278
}
 
279
 
 
280
int qt_wince_SetErrorMode(int newValue)
 
281
{
 
282
    static int oldValue;
 
283
    int result = oldValue;
 
284
    oldValue = newValue;
 
285
    return result;
 
286
}
 
287
 
 
288
bool qt_wince__chmod(const char *file, int mode)
 
289
{
 
290
    return _wchmod( reinterpret_cast<const wchar_t *> (QString::fromLatin1(file).utf16()), mode);
 
291
}
 
292
 
 
293
bool qt_wince__wchmod(const wchar_t *file, int mode)
 
294
{
 
295
    BOOL success = FALSE;
 
296
    // ### Does not work properly, what about just adding one property?
 
297
    if(mode&_S_IWRITE) {
 
298
        success = SetFileAttributes(file, FILE_ATTRIBUTE_NORMAL);
 
299
    } else if((mode&_S_IREAD) && !(mode&_S_IWRITE)) {
 
300
        success = SetFileAttributes(file, FILE_ATTRIBUTE_READONLY);
 
301
    }
 
302
    return success ? 0 : -1;
 
303
}
 
304
 
 
305
HANDLE qt_wince_CreateFileA(LPCSTR filename, DWORD access, DWORD share, LPSECURITY_ATTRIBUTES attr, DWORD dispo, DWORD flags, HANDLE tempFile)
 
306
{
 
307
    return CreateFileW( reinterpret_cast<const wchar_t *>(QString::fromLatin1(filename).utf16()), access, share, attr, dispo, flags, tempFile);
 
308
}
 
309
 
 
310
// Graphics ---------------------------------------------------------
 
311
BOOL qt_wince_SetWindowOrgEx( HDC /*hdc*/, int /*X*/, int /*Y*/, LPPOINT /*lpPoint*/) {
 
312
    return TRUE;
 
313
}
 
314
 
 
315
// Threading --------------------------------------------------------
 
316
HANDLE qt_wince__beginthread(void( *start_address )( void * ), unsigned stack_size, void *arglist)
 
317
{
 
318
    unsigned initflag = 0;
 
319
    if (stack_size > 0)
 
320
        initflag |= STACK_SIZE_PARAM_IS_A_RESERVATION;
 
321
    return CreateThread(NULL, stack_size, (LPTHREAD_START_ROUTINE)start_address, arglist, initflag, NULL);
 
322
}
 
323
 
 
324
unsigned long qt_wince__beginthreadex( void *security,
 
325
                  unsigned stack_size,
 
326
                  unsigned (__stdcall *start_address)(void *),
 
327
                  void *arglist,
 
328
                  unsigned initflag,
 
329
                  unsigned *thrdaddr)
 
330
{
 
331
    if (stack_size > 0)
 
332
        initflag |= STACK_SIZE_PARAM_IS_A_RESERVATION;
 
333
    return (unsigned long)
 
334
            CreateThread( (LPSECURITY_ATTRIBUTES)security,
 
335
                          (DWORD)stack_size,
 
336
                          (LPTHREAD_START_ROUTINE)start_address,
 
337
                          (LPVOID)arglist,
 
338
                          (DWORD)initflag | CREATE_SUSPENDED,
 
339
                          (LPDWORD)thrdaddr);
 
340
}
 
341
 
 
342
void qt_wince__endthreadex(unsigned nExitCode) {
 
343
    ExitThread((DWORD)nExitCode);
 
344
}
 
345
 
 
346
void *qt_wince_bsearch(const void *key,
 
347
           const void *base,
 
348
           size_t num,
 
349
           size_t size,
 
350
           int (__cdecl *compare)(const void *, const void *))
 
351
{
 
352
    size_t low = 0;
 
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);
 
357
        if (c < 0)
 
358
            high = mid - 1;
 
359
        else if (c > 0)
 
360
            low = mid + 1;
 
361
        else
 
362
            return (char*) base + mid * size;
 
363
    }
 
364
    return (NULL);
 
365
}
 
366
 
 
367
void *lfind(const void* key, const void* base, size_t* elements, size_t size,
 
368
            int (__cdecl *compare)(const void*, const void*))
 
369
{
 
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;
 
375
        current += size;
 
376
    }
 
377
    return 0;
 
378
}
 
379
 
 
380
DWORD qt_wince_GetThreadLocale(void)
 
381
{
 
382
    return GetUserDefaultLCID();
 
383
}
 
384
 
 
385
void *qt_wince_calloc( size_t num, size_t size )
 
386
{
 
387
    void *ptr = malloc( num * size );
 
388
    if( ptr )
 
389
        memset( ptr, 0, num * size );
 
390
    return ptr;
 
391
}
 
392
 
 
393
// _getpid is currently only used for creating a temporary filename
 
394
int qt_wince__getpid()
 
395
{
 
396
    return qAbs((int)GetCurrentProcessId());
 
397
}
 
398
 
 
399
#ifdef __cplusplus
 
400
} // extern "C"
 
401
#endif
 
402
// Environment ------------------------------------------------------
 
403
inline QHash<QByteArray, QByteArray>& qt_app_environment()
 
404
{
 
405
    static QHash<QByteArray, QByteArray> internalEnvironment;
 
406
    return internalEnvironment;
 
407
}
 
408
 
 
409
errno_t qt_wince_getenv_s(size_t* sizeNeeded, char* buffer, size_t bufferSize, const char* varName)
 
410
{
 
411
    if (!sizeNeeded)
 
412
        return EINVAL;
 
413
 
 
414
    if (!qt_app_environment().contains(varName)) {
 
415
        if (buffer)
 
416
            buffer[0] = '\0';
 
417
        return ENOENT;
 
418
    }
 
419
 
 
420
    QByteArray value = qt_app_environment().value(varName);
 
421
    if (!value.endsWith('\0')) // win32 guarantees terminated string
 
422
        value.append('\0');
 
423
 
 
424
    if (bufferSize < (size_t)value.size()) {
 
425
        *sizeNeeded = value.size();
 
426
        return 0;
 
427
    }
 
428
 
 
429
    strcpy(buffer, value.constData());
 
430
    return 0;
 
431
}
 
432
 
 
433
errno_t qt_wince__putenv_s(const char* varName, const char* value)
 
434
{
 
435
    QByteArray input = value;
 
436
    if (input.isEmpty()) {
 
437
        if (qt_app_environment().contains(varName))
 
438
            qt_app_environment().remove(varName);
 
439
    } else {
 
440
        // win32 guarantees terminated string
 
441
        if (!input.endsWith('\0'))
 
442
            input.append('\0');
 
443
        qt_app_environment()[varName] = input;
 
444
    }
 
445
 
 
446
    return 0;
 
447
}
 
448
 
 
449
#endif // Q_OS_WINCE