summaryrefslogtreecommitdiff
path: root/wince/wince.c
blob: c1a950c1c163a5fd4bc1cfd832808aa706425080 (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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/***************************************************************
  wince.c

  author : uema2
  date   : Nov 30, 2002

  You can freely use, copy, modify, and redistribute
  the whole contents.
***************************************************************/

#include <windows.h>
#include <tchar.h>
#include "wince.h"

/* global for GetCommandLineA */
char *_commandLine;

extern char _currentdir[];

/* make up Win32API except wce_* functions.  */

DWORD GetModuleFileNameA( 
	HMODULE hModule, LPSTR lpFileName,
	DWORD size )
{
	LPWSTR lpFileNameW;
	LPSTR  mb;
	size_t ret;

	if( size==0 ) return 0;

	lpFileNameW = (LPWSTR)malloc( size*sizeof(wchar_t) );
	ret = GetModuleFileNameW( hModule, lpFileNameW, size );
	mb = wce_wctomb(lpFileNameW);
	strcpy(lpFileName, mb);
	free(mb);
	free(lpFileNameW);

	return ret;
}

#if _WIN32_WCE < 300
FARPROC GetProcAddressA(HMODULE hModule, LPCSTR lpProcName)
{
	FARPROC p;
	LPWSTR  lpwProcName;

	lpwProcName = wce_mbtowc( lpProcName );
	p = GetProcAddressW( hModule, lpwProcName );
	free( lpwProcName );
	return p;
}
#endif 

char * GetCommandLineA(void)
{
	return _commandLine;
}

/* this is not Win32API. GetCommandLineA helper. */
void wce_SetCommandLine(LPCWSTR wcmd)
{
	char* acmd;

	acmd = wce_wctomb( wcmd );
	_commandLine = (char*)malloc( strlen(acmd)+5 );
	sprintf( _commandLine, "ruby %s", acmd );
	free(acmd);
}

/* this is not Win32API. GetCommandLineA helper. */
void wce_FreeCommandLine(void)
{
	free(_commandLine);
	_commandLine = NULL;
}

/* I have no idea how to replace this. */
BOOL GetProcessTimes(HANDLE hprocess,
	LPFILETIME lpCreationTime, LPFILETIME lpExitTime,
	LPFILETIME lpKernelTime, LPFILETIME lpUserTime) 
{
	return 0;
}

/* --------------  file attributes functions. ------------------- */
DWORD GetFileAttributesA(LPCSTR lpFileName)
{
	LPWSTR lpwFileName;
	DWORD dw;

	lpwFileName = wce_mbtowc(lpFileName);
	dw = GetFileAttributesW(lpwFileName);
	free(lpwFileName);
	return dw;
}

BOOL SetFileAttributesA(
	LPCSTR lpFileName, DWORD attributes) 
{
	LPWSTR lpwFileName;
	BOOL b;

	lpwFileName = wce_mbtowc(lpFileName);
	b = SetFileAttributesW(lpwFileName, attributes);
	free(lpwFileName);
	return b;
}

/* ---------------  move and remove functions. ------------------- */
BOOL MoveFileA(LPCSTR fn1, LPCSTR fn2)
{
	LPWSTR wfn1, wfn2;
	BOOL b;

	wfn1 = wce_mbtowc(fn1);
	wfn2 = wce_mbtowc(fn2);
	b = MoveFileW(wfn1, wfn2);
	free(wfn1);
	free(wfn2);
	return 0;
}

BOOL MoveFileEx(LPCSTR oldname, LPCSTR newname, DWORD dwFlags)
{
	LPWSTR woldname, wnewname;
	BOOL b;

	woldname = wce_mbtowc(oldname);
	wnewname = wce_mbtowc(newname);

	if( (dwFlags&MOVEFILE_REPLACE_EXISTING)!=0 )
		DeleteFileW( wnewname );

	b = MoveFileW( woldname, wnewname );

	free(woldname);
	free(wnewname);

	return b;
}

BOOL DeleteFileA(LPCSTR path)
{ 
	LPWSTR wpath;
	BOOL b;

	wpath = wce_mbtowc(path);
	b = DeleteFileW(wpath);
	free(wpath);
	return 0;
}

/* --------------- EnvironmentVariable functions. ----------------- */
DWORD GetEnvironmentVariable(
	LPCSTR name, LPSTR value, DWORD size)
{
	/* use registry instead of "environment valuable". */
	HKEY	hk;
	LONG	lret;
	LPBYTE	lpData;
	DWORD	dwType=REG_SZ, cbData;
	TCHAR   buf[MAX_PATH]={0};
	LPWSTR  wname;
	LPSTR   avalue;

	lret = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
				_T("Software\\ruby_mswince"),
				0, KEY_QUERY_VALUE, &hk );

	if ( lret != ERROR_SUCCESS )
	{
		strcpy( value, "" );
		return 0;
	}

	lpData = (LPBYTE)buf;
	cbData = MAX_PATH*sizeof(*buf);
	wname  = wce_mbtowc( name );

	lret = RegQueryValueEx( hk, wname,
		NULL, &dwType, lpData, &cbData );
	RegCloseKey( hk );

	if ( lret != ERROR_SUCCESS )
	{
		strcpy( value, "" );
		free( wname );
		return 0;
	}

	avalue = wce_wctomb( (LPCTSTR)lpData );
	strcpy( value, avalue );
	free( avalue );
	free( wname );

	return strlen(value);
}

BOOL SetEnvironmentVariable(LPCSTR name, LPCSTR value)
{
	/* use registry instead of "environment valuable". */
	HKEY	hk;
	LONG	lret;
	LPBYTE	lpData;
	DWORD	ret, dwType=REG_SZ, cbData;
	LPWSTR  wname, wvalue;

	lret = RegCreateKeyEx( HKEY_LOCAL_MACHINE,
			_T("Software\\ruby_mswince"),
			0, _T(""), 0,
			0, NULL, &hk, &ret );
	if( lret != ERROR_SUCCESS )
		return FALSE;

	wname  = wce_mbtowc(name);
	wvalue = wce_mbtowc(value);

	lpData = (LPBYTE)wvalue;
	cbData = (wcslen(wvalue) + 1) * sizeof(*wvalue);
	lret = RegSetValueEx( hk, wname,
		0, dwType, lpData, cbData );
	RegCloseKey( hk );
	free(wname);
	free(wvalue);
	return lret == ERROR_SUCCESS;
}

LPVOID GetEnvironmentStrings(VOID)
{
	return NULL;
}

BOOL FreeEnvironmentStrings(LPSTR lpszEnvironmentBlock)
{
	return FALSE;
}

/* DuplicateHandle, LockFile, etc... */
/* I have no idea...  */
BOOL GenerateConsoleCtrlEvent(DWORD dwCtrlEvent,
		DWORD dwProcessGroupID)
{
	return 0;
}

BOOL DuplicateHandle(
	HANDLE source_process, HANDLE source,
	HANDLE dest_process, HANDLE *dest,
	DWORD access, BOOL inherit, DWORD options)
{
	return 0;
}

BOOL LockFile(HANDLE hFile,
	DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
	DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh)
{
	return FALSE;
}

BOOL LockFileEx(HANDLE hFile,
	DWORD dwFlags, DWORD dwReserved,
	DWORD nNumberOfBytesToLockLow, DWORD nNumberOfBytesToLockHigh,
	LPOVERLAPPED lpOverlapped)
{
	return FALSE;
}

BOOL UnlockFile( HFILE hFile,
	DWORD dwFileOffsetLow, DWORD dwFileOffsetHigh,
	DWORD nNumberOfBytesToUnlockLow, DWORD nNumberOfBytesToUnlockHigh)
{
	return FALSE;
}

BOOL UnlockFileEx(HANDLE hFile,
	DWORD dwReserved, DWORD nNumberOfBytesToUnlockLow,
	DWORD nNumberOfBytesToUnlockHigh, LPOVERLAPPED lpOverlapped)
{
	return FALSE;
}

/* --------------------- etc, etc, etc... ----------------------- */
BOOL GetVersionExA(OSVERSIONINFOA *v)
{
	OSVERSIONINFOW wv;
	BOOL b;
	LPSTR mb;

	b = GetVersionExW(&wv);
	mb = wce_wctomb(wv.szCSDVersion);

	strcpy( v->szCSDVersion, mb );
	free(mb);
	return b;
}

DWORD WaitForMultipleObjectsEx(DWORD count,
	const HANDLE *handles, BOOL wait_all,
	DWORD timeout, BOOL alertable)
{
	return WaitForMultipleObjects(
		count, handles, wait_all,
		timeout );
}

BOOL CreateProcessA(LPCSTR appname, LPCSTR commandline,
	LPSECURITY_ATTRIBUTES att, LPSECURITY_ATTRIBUTES threadatt,
	BOOL bOpt, DWORD dwFlag, LPVOID lpEnv, LPSTR dir,
	LPSTARTUPINFO lpsi, LPPROCESS_INFORMATION lppi)
{
	LPWSTR wappname, wcommandline, wdir;
	BOOL b;

	wappname     = wce_mbtowc(appname);
	wcommandline = wce_mbtowc(commandline);
	wdir         = wce_mbtowc(dir);

	b = CreateProcessW(wappname, wcommandline,
			att, threadatt, bOpt, dwFlag, lpEnv,
			wdir, lpsi, lppi);

	free(wappname);
	free(wcommandline);
	free(wdir);

	return b;
}

HANDLE CreateEventA(SECURITY_ATTRIBUTES *sa, 
	BOOL manual_reset, BOOL initial_state, LPCSTR name)
{
	HANDLE h;
	LPWSTR wname;

	wname = wce_mbtowc(name);
	h = CreateEventW(sa, manual_reset,
		initial_state, wname);
	free(wname);

	return h;
}

DWORD FormatMessageA(DWORD dwFlags, LPCVOID lpSource, 
	DWORD dwMessageId, DWORD dwLanguageId, LPSTR lpBuffer, 
	DWORD nSize, va_list* args)
{
	DWORD dw;
	LPWSTR lpWBuffer;

	lpWBuffer = wce_mbtowc(lpBuffer);
	dw = FormatMessageW( dwFlags, lpSource,
			dwMessageId, dwLanguageId,
			lpWBuffer, nSize, (va_list*)args );
	free(lpWBuffer);
	return dw;
}

/*---------------- FindFirstFile, FindNextFile ------------------ */
HANDLE FindFirstFileA(LPCSTR path,
			WIN32_FIND_DATAA *data)
{
	LPWSTR wpath;
	LPSTR  mb;
	HANDLE h;
	WIN32_FIND_DATAW wdata;

	wpath = wce_mbtowc(path);
	h = FindFirstFileW( wpath, &wdata );
	free(wpath);
	
	mb = wce_wctomb( wdata.cFileName );
	strcpy( data->cFileName, mb );
	free(mb);

	return h;
}

BOOL FindNextFileA(HANDLE handle,
	WIN32_FIND_DATAA *data)
{
	BOOL b;
	WIN32_FIND_DATAW wdata;
	LPSTR mb1;

	b = FindNextFileW(handle, &wdata);

	mb1 = wce_wctomb( wdata.cFileName );
	strcpy( data->cFileName, mb1 );
	free(mb1);

	return b;
}

/* CreateFile doesn't support SECURITY_ATTRIBUTES in WinCE. */
/* it must be NULL. */
HANDLE CreateFileA(LPCSTR filename, DWORD access,
	DWORD sharing, LPSECURITY_ATTRIBUTES sa,
	DWORD creation, DWORD attributes, HANDLE template)
{
	LPWSTR wfilename;
	HANDLE h;

	wfilename = wce_mbtowc(filename);
	h = CreateFileW(wfilename, access, sharing,
			NULL, creation, 0, NULL);
	free(wfilename);

	return 0;
}

/* ---------------- CharNext, CharPrev. ---------------------*/
LPSTR CharNextA(LPCSTR a)
{
	char *p=(char *)a;
	if( TRUE==IsDBCSLeadByteEx(CP_ACP, (BYTE)*a) )
		p+=2;
	else
		p++;

	return p;
}

LPSTR CharPrevA(LPCSTR start, LPCSTR ptr)
{
	if( start==ptr ) return (LPSTR)start;
	else if( start+1==ptr ) return (LPSTR)start;
	else if( TRUE==IsDBCSLeadByteEx(CP_ACP, (BYTE)*(ptr-2)) )
		return (LPSTR)(ptr-2);
	else
		return (LPSTR)(ptr-1);
}

/* WinCE doesn't have "drives". */
DWORD GetLogicalDrives(VOID)
{
	return 0;
}

/* WinCE doesn't have "user name". */
BOOL GetUserName(LPSTR lpBuffer, LPDWORD nSize)
{
	return 0;
}

/*------------------- LoadLibrary -----------------------*/
HINSTANCE LoadLibraryA(LPCSTR libname)
{
	HINSTANCE h;
	LPWSTR wlibname;

	// if starts ".\", replace current directory.
//	wlibname = wce_replaceRelativeDir(libname);

	wlibname = wce_mbtowc(libname);
	h = LoadLibraryW(wlibname);
	free(wlibname);
	return h;
}

HINSTANCE LoadLibraryExA(LPCSTR lpLibFileName, HANDLE hFile,
			DWORD dwFlags)
{
	HINSTANCE h;
	LPWSTR wlibname;

	wlibname = wce_mbtowc(lpLibFileName);
//	wlibname = wce_replaceRelativeDir(lpLibFileName);

#if _WIN32_WCE < 300
	h = LoadLibraryW(wlibname);
#else
	h = LoadLibraryExW(wlibname, hFile, dwFlags);
#endif
	free(wlibname);
	return h;
}

/* WinCE doesn't have "CreatePipe". */
BOOL CreatePipe(PHANDLE hReadPipe, PHANDLE hWritePipe,
	LPSECURITY_ATTRIBUTES lpPipeAttributes, DWORD nSize)
{
	return FALSE;
}

/* WinCE doesn't have "Standard Devices". */
HANDLE GetStdHandle(DWORD nStdHandle)
{
	return NULL;
}

BOOL SetStdHandle(DWORD nStdHandle, HANDLE h)
{
	return FALSE;
}

#if _WIN32_WCE < 300
VOID ZeroMemory(PVOID p, DWORD length)
{
	memset(p,0,length);
}
#endif


/* need in ruby/io.c. */
int ReadDataPending()
{
	return 0;
}

/*---------------- helper functions. ---------------------------- */
FILE *wce_fopen( const char *fname, const char *mode )
{
	TCHAR* tfname = wce_replaceRelativeDir(fname);
	TCHAR* tmode = wce_mbtowc(mode);
	FILE* fp = _tfopen(tfname, tmode);
	free(tfname); free(tmode);
	return fp;
}

void wce_SetCurrentDir()
{
	WCHAR tbuf[MAX_PATH+1]={0};
	WCHAR *tp;
	char *buf;

	GetModuleFileNameW( NULL, tbuf, MAX_PATH );
	tp = _tcsrchr( tbuf, '\\' );
	if( tp!=NULL ) *tp=_T('\0');
	buf = wce_wctomb(tbuf);
	strcpy( _currentdir, buf );
	free(buf);
}

TCHAR *wce_replaceRelativeDir(const char* str)
{
	TCHAR *tbuf;

	if( 2<=strlen(str) && str[0]=='.' &&
		(str[1]=='/' || str[1]=='\\') )
	{
		char *buf;
		int len = strlen(str) + strlen(_currentdir);
		buf = malloc( len+1 );
		sprintf(buf, "%s%s", _currentdir, &str[1]);
		tbuf = wce_mbtowc(buf);
		free(buf);
	}
	else
		tbuf = wce_mbtowc(str);
	return tbuf;
}

/* char -> wchar_t */
wchar_t* wce_mbtowc(const char* a)
{
	int length;
	wchar_t *wbuf;

	length = MultiByteToWideChar(CP_ACP, 0, 
		a, -1, NULL, 0);
	wbuf = (wchar_t*)malloc( (length+1)*sizeof(wchar_t) );
	MultiByteToWideChar(CP_ACP, 0,
		a, -1, wbuf, length);

	return wbuf;
}

/* wchar_t -> char */
char* wce_wctomb(const wchar_t* w)
{
	DWORD charlength;
	char* pChar;

	charlength = WideCharToMultiByte(CP_ACP, 0, w,
					-1, NULL, 0, NULL, NULL);
	pChar = (char*)malloc(charlength+1);
	WideCharToMultiByte(CP_ACP, 0, w,
		-1, pChar, charlength, NULL, NULL);

	return pChar;
}