Qt 4.8
qfunctions_wince.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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 
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  size_t mid = (low + high) >> 1;
356  int c = compare(key, (char*)base + mid * size);
357  if (c < 0) {
358  if (!mid)
359  break;
360  high = mid - 1;
361  } else if (c > 0)
362  low = mid + 1;
363  else
364  return (char*) base + mid * size;
365  }
366  return 0;
367 }
368 
369 void *lfind(const void* key, const void* base, size_t* elements, size_t size,
370  int (__cdecl *compare)(const void*, const void*))
371 {
372  const char* current = (char*) base;
373  const char* const end = (char*) (current + (*elements) * size);
374  while (current != end) {
375  if (compare(current, key) == 0)
376  return (void*)current;
377  current += size;
378  }
379  return 0;
380 }
381 
382 DWORD qt_wince_GetThreadLocale(void)
383 {
384  return GetUserDefaultLCID();
385 }
386 
387 void *qt_wince_calloc( size_t num, size_t size )
388 {
389  void *ptr = malloc( num * size );
390  if( ptr )
391  memset( ptr, 0, num * size );
392  return ptr;
393 }
394 
395 // _getpid is currently only used for creating a temporary filename
396 int qt_wince__getpid()
397 {
398  return qAbs((int)GetCurrentProcessId());
399 }
400 
401 #ifdef __cplusplus
402 } // extern "C"
403 #endif
404 // Environment ------------------------------------------------------
405 inline QHash<QByteArray, QByteArray>& qt_app_environment()
406 {
407  static QHash<QByteArray, QByteArray> internalEnvironment;
408  return internalEnvironment;
409 }
410 
411 errno_t qt_wince_getenv_s(size_t* sizeNeeded, char* buffer, size_t bufferSize, const char* varName)
412 {
413  if (!sizeNeeded)
414  return EINVAL;
415 
416  if (!qt_app_environment().contains(varName)) {
417  if (buffer)
418  buffer[0] = '\0';
419  return ENOENT;
420  }
421 
422  QByteArray value = qt_app_environment().value(varName);
423  if (!value.endsWith('\0')) // win32 guarantees terminated string
424  value.append('\0');
425 
426  if (bufferSize < (size_t)value.size()) {
427  *sizeNeeded = value.size();
428  return 0;
429  }
430 
431  strcpy(buffer, value.constData());
432  return 0;
433 }
434 
435 errno_t qt_wince__putenv_s(const char* varName, const char* value)
436 {
437  QByteArray input = value;
438  if (input.isEmpty()) {
439  if (qt_app_environment().contains(varName))
440  qt_app_environment().remove(varName);
441  } else {
442  // win32 guarantees terminated string
443  if (!input.endsWith('\0'))
444  input.append('\0');
445  qt_app_environment()[varName] = input;
446  }
447 
448  return 0;
449 }
450 
451 #endif // Q_OS_WINCE
static QString fromWCharArray(const wchar_t *, int size=-1)
Returns a copy of the string, where the encoding of string depends on the size of wchar...
Definition: qstring.cpp:1019
#define _S_IREAD
#define _waccess(a, b)
long qt_wince__lseek(int handle, long offset, int origin)
void * qt_wince_bsearch(const void *key, const void *base, size_t num, size_t size, int(__cdecl *compare)(const void *, const void *))
DWORD qt_wince_GetThreadLocale(void)
void qt_wince_rewind(FILE *stream)
unsigned char c[8]
Definition: qnumeric_p.h:62
int qt_wince__rmdir(const char *dirname)
time_t st_ctime
QByteArray & append(char c)
Appends the character ch to this byte array.
errno_t qt_wince_getenv_s(size_t *, char *, size_t, const char *)
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
FILETIME qt_wince_time_tToFt(time_t tt)
HANDLE qt_wince__beginthread(void(*start_address)(void *), unsigned stack_size, void *arglist)
bool qt_wince__wchmod(const wchar_t *file, int mode)
int qt_wince___fileno(FILE *)
int qt_wince__remove(const char *name)
The QString class provides a Unicode character string.
Definition: qstring.h:83
int qt_wince__fstat(int handle, struct stat *buffer)
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qdatastream.h:66
Q_DECL_CONSTEXPR T qAbs(const T &t)
Definition: qglobal.h:1201
#define _S_IFREG
int qt_wince__getdrive(void)
static const uint base
Definition: qurl.cpp:268
int qt_wince_SetErrorMode(int)
#define _wchmod(a, b)
#define _S_IFDIR
void qt_wince__endthreadex(unsigned nExitCode)
FILE * qt_wince__fdopen(int handle, const char *mode)
static bool compare(const QVariant::Private *a, const QVariant::Private *b)
Compares a to b.
Definition: qvariant.cpp:383
static FILE * stream
unsigned long time_t
int access(const char *, int)
FILE * qt_wince_tmpfile(void)
const char * name
#define _wopen(a, b, c)
int qt_wince__waccess(const wchar_t *path, int pmode)
int qt_wince__close(int handle)
#define FALSE
Synonym for false.
Definition: qglobal.h:1019
#define _O_RDWR
const T * ptr(const T &t)
#define _O_WRONLY
void * HANDLE
Definition: qnamespace.h:1671
BOOL qt_wince_SetWindowOrgEx(HDC hdc, int X, int Y, LPPOINT lpPoint)
#define TRUE
Synonym for true.
Definition: qglobal.h:1018
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
HANDLE qt_wince_CreateFileA(LPCSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE)
FILE * qt_wince_fdopen(int handle, const char *mode)
int qt_wince__mkdir(const char *dirname)
int qt_wince__access(const char *path, int pmode)
bool qt_wince__chmod(const char *file, int mode)
int qt_wince__wopen(const wchar_t *filename, int oflag, int pmode)
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
int key
#define _O_APPEND
time_t st_atime
int qt_wince_open(const char *filename, int oflag, int pmode=0)
errno_t qt_wince__putenv_s(const char *, const char *)
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
#define QT_USE_NAMESPACE
This macro expands to using QT_NAMESPACE if QT_NAMESPACE is defined and nothing otherwise.
Definition: qglobal.h:88
static const QTextHtmlElement elements[Html_NumElements]
int qt_wince__write(int handle, const void *buffer, unsigned int count)
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
unsigned long qt_wince__beginthreadex(void *security, unsigned stack_size, unsigned(__stdcall *start_address)(void *), void *arglist, unsigned initflag, unsigned *thrdaddr)
int qt_wince_stat(const char *path, struct stat *buffer)
void * qt_wince_calloc(size_t num, size_t size)
#define _O_RDONLY
#define _S_IWRITE
bool endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string ends with s; otherwise returns false.
Definition: qstring.cpp:3796
int st_nlink
static const KeyPair *const end
time_t st_mtime
int qt_wince__getpid(void)
QByteArray & remove(int index, int len)
Removes len bytes from the array, starting at index position pos, and returns a reference to the arra...
int qt_wince__rename(const char *oldname, const char *newname)
int errno
bool endsWith(const QByteArray &a) const
Returns true if this byte array ends with byte array ba; otherwise returns false. ...
int qt_wince__read(int handle, void *buffer, unsigned int count)