@@ -51,8 +51,6 @@ extern "C" {
5151#include <stdio.h>
5252#include <stdlib.h>
5353#include <string.h>
54- #include <sys/stat.h>
55- #include <sys/types.h>
5654#include <time.h>
5755#include <math.h>
5856
@@ -262,26 +260,6 @@ extern int SystemImpl__setLDFlags(const char *str)
262260}
263261
264262#if defined(__MINGW32__ ) || defined(_MSC_VER )
265- extern int SystemImpl__stringToUnicodeSize (const char * str )
266- {
267- return MultiByteToWideChar (CP_UTF8 , 0 , str , -1 , NULL , 0 );
268- }
269-
270- extern int SystemImpl__stringToUnicode (const char * str , wchar_t * unicode , int size )
271- {
272- return MultiByteToWideChar (CP_UTF8 , 0 , str , -1 , unicode , size );
273- }
274-
275- int SystemImpl__unicodeToStringSize (const wchar_t * unicode )
276- {
277- return WideCharToMultiByte (CP_UTF8 , 0 , unicode , -1 , NULL , 0 , NULL , NULL );
278- }
279-
280- int SystemImpl__unicodeToString (const wchar_t * unicode , char * str , int size )
281- {
282- return WideCharToMultiByte (CP_UTF8 , 0 , unicode , -1 , str , size , NULL , NULL );
283- }
284-
285263/* Make sure windows paths use frontslash and not backslash */
286264void SystemImpl__toWindowsSeperators (char * buffer , int bufferLength )
287265{
@@ -295,14 +273,15 @@ void SystemImpl__toWindowsSeperators(char* buffer, int bufferLength)
295273int SystemImpl__chdir (const char * path )
296274{
297275#if defined(__MINGW32__ ) || defined(_MSC_VER )
298- int unicodePathLength = SystemImpl__stringToUnicodeSize (path );
299- wchar_t unicodePath [unicodePathLength ];
300- SystemImpl__stringToUnicode (path , unicodePath , unicodePathLength );
276+ MULTIBYTE_TO_WIDECHAR_LENGTH (path , unicodePathLength );
277+ MULTIBYTE_TO_WIDECHAR_VAR (path , unicodePath , unicodePathLength );
301278
302279 if (!SetCurrentDirectoryW (unicodePath )) {
280+ MULTIBYTE_OR_WIDECHAR_VAR_FREE (unicodePath );
303281 c_add_message (NULL ,-1 ,ErrorType_scripting ,ErrorLevel_error ,gettext ("SetCurrentDirectoryW failed." ),NULL ,0 );
304282 return -1 ;
305283 }
284+ MULTIBYTE_OR_WIDECHAR_VAR_FREE (unicodePath );
306285 return 0 ;
307286#else
308287 if (chdir (path ) != 0 ) {
@@ -328,11 +307,12 @@ extern char* SystemImpl__pwd(void)
328307 c_add_message (NULL ,-1 ,ErrorType_scripting ,ErrorLevel_error ,gettext ("GetCurrentDirectoryW failed." ),NULL ,0 );
329308 return NULL ;
330309 }
331- int bufferLength = SystemImpl__unicodeToStringSize (unicodePath );
332- char buffer [bufferLength ];
333- SystemImpl__unicodeToString (unicodePath , buffer , bufferLength );
310+ WIDECHAR_TO_MULTIBYTE_LENGTH (unicodePath , bufferLength );
311+ WIDECHAR_TO_MULTIBYTE_VAR (unicodePath , buffer , bufferLength );
334312 SystemImpl__toWindowsSeperators (buffer , bufferLength );
335- return omc_alloc_interface .malloc_strdup (buffer );
313+ char * res = omc_alloc_interface .malloc_strdup (buffer );
314+ MULTIBYTE_OR_WIDECHAR_VAR_FREE (buffer );
315+ return res ;
336316#else
337317 char buf [MAXPATHLEN ];
338318 if (NULL == getcwd (buf ,MAXPATHLEN )) {
@@ -349,12 +329,13 @@ extern int SystemImpl__regularFileExists(const char* str)
349329 WIN32_FIND_DATAW FileData ;
350330 HANDLE sh ;
351331
352- int unicodeFilenameLength = SystemImpl__stringToUnicodeSize (str );
353- wchar_t unicodeFilename [unicodeFilenameLength ];
354- SystemImpl__stringToUnicode (str , unicodeFilename , unicodeFilenameLength );
332+ MULTIBYTE_TO_WIDECHAR_LENGTH (str , unicodeFilenameLength );
333+ MULTIBYTE_TO_WIDECHAR_VAR (str , unicodeFilename , unicodeFilenameLength );
355334
356335 sh = FindFirstFileW (unicodeFilename , & FileData );
357336
337+ MULTIBYTE_OR_WIDECHAR_VAR_FREE (unicodeFilename );
338+
358339 if (sh == INVALID_HANDLE_VALUE ) {
359340 if (strlen (str ) >= MAXPATHLEN )
360341 {
@@ -383,15 +364,7 @@ extern int SystemImpl__regularFileWritable(const char* str)
383364 FILE * f ;
384365 if (!SystemImpl__regularFileExists (str ))
385366 return 0 ;
386- #if defined(__MINGW32__ ) || defined(_MSC_VER )
387- int unicodeFilenameLength = SystemImpl__stringToUnicodeSize (str );
388- wchar_t unicodeFilename [unicodeFilenameLength ];
389- SystemImpl__stringToUnicode (str , unicodeFilename , unicodeFilenameLength );
390-
391- f = _wfopen (unicodeFilename , L"a" );
392- #else
393- f = fopen (str , "a" );
394- #endif
367+ f = omc_fopen (str , "a" );
395368 if (f == NULL )
396369 return 0 ;
397370 fclose (f );
@@ -404,16 +377,11 @@ static char* SystemImpl__readFile(const char* filename)
404377 int res ;
405378 FILE * file = NULL ;
406379#if defined(__MINGW32__ ) || defined(_MSC_VER )
407- int unicodeFilenameLength = SystemImpl__stringToUnicodeSize (filename );
408- wchar_t unicodeFilename [unicodeFilenameLength ];
409- SystemImpl__stringToUnicode (filename , unicodeFilename , unicodeFilenameLength );
410-
411380 struct _stat statstr ;
412- res = _wstat (unicodeFilename , & statstr );
413381#else
414382 struct stat statstr ;
415- res = stat (filename , & statstr );
416383#endif
384+ res = omc_stat (filename , & statstr );
417385
418386 if (res != 0 ) {
419387 const char * c_tokens [2 ]= {strerror (errno ),filename };
@@ -440,11 +408,7 @@ static char* SystemImpl__readFile(const char* filename)
440408 }
441409#endif
442410
443- #if defined(__MINGW32__ ) || defined(_MSC_VER )
444- file = _wfopen (unicodeFilename , L"rb" );
445- #else
446- file = fopen (filename ,"rb" );
447- #endif
411+ file = omc_fopen (filename ,"rb" );
448412 if (file == NULL ) {
449413 const char * c_tokens [2 ]= {strerror (errno ),filename };
450414 c_add_message (NULL , 85 , /* ERROR_OPENING_FILE */
@@ -476,30 +440,14 @@ static char* SystemImpl__readFile(const char* filename)
476440/* returns 0 on success */
477441int SystemImpl__removeFile (const char * filename )
478442{
479- #if defined(__MINGW32__ ) || defined(_MSC_VER )
480- int unicodeFilenameLength = SystemImpl__stringToUnicodeSize (filename );
481- wchar_t unicodeFilename [unicodeFilenameLength ];
482- SystemImpl__stringToUnicode (filename , unicodeFilename , unicodeFilenameLength );
483-
484- return _wunlink (unicodeFilename );
485- #else /* unix */
486- return unlink (filename );
487- #endif
443+ return omc_unlink (filename );
488444}
489445
490446/* returns 0 on success */
491447int SystemImpl__writeFile (const char * filename , const char * data )
492448{
493449#if defined(__MINGW32__ ) || defined(_MSC_VER )
494450 const char * fileOpenMode = "wt" ; /* on Windows do translation so that \n becomes \r\n */
495-
496- int unicodeFilenameLength = SystemImpl__stringToUnicodeSize (filename );
497- wchar_t unicodeFilename [unicodeFilenameLength ];
498- SystemImpl__stringToUnicode (filename , unicodeFilename , unicodeFilenameLength );
499-
500- int unicodeFileOpenModeLength = SystemImpl__stringToUnicodeSize (fileOpenMode );
501- wchar_t unicodeFileOpenMode [unicodeFileOpenModeLength ];
502- SystemImpl__stringToUnicode (fileOpenMode , unicodeFileOpenMode , unicodeFileOpenModeLength );
503451#else
504452 const char * fileOpenMode = "wb" ; /* on Unixes don't bother, do it binary mode */
505453#endif
@@ -509,11 +457,7 @@ int SystemImpl__writeFile(const char* filename, const char* data)
509457 SystemImpl__removeFile (filename );
510458#endif
511459 /* adrpo: 2010-09-22 open the file in BINARY mode as otherwise \r\n becomes \r\r\n! */
512- #if defined(__MINGW32__ ) || defined(_MSC_VER )
513- file = _wfopen (unicodeFilename , unicodeFileOpenMode );
514- #else
515- file = fopen (filename ,fileOpenMode );
516- #endif
460+ file = omc_fopen (filename ,fileOpenMode );
517461 if (file == NULL ) {
518462 const char * c_tokens [1 ]= {filename };
519463 c_add_message (NULL ,21 , /* WRITING_FILE_ERROR */
@@ -658,9 +602,8 @@ int runProcess(const char* cmd)
658602
659603 /* fprintf(stderr, "%s\n", command); fflush(NULL); */
660604
661- int unicodeCommandLength = SystemImpl__stringToUnicodeSize (command );
662- wchar_t unicodeCommand [unicodeCommandLength ];
663- SystemImpl__stringToUnicode (command , unicodeCommand , unicodeCommandLength );
605+ MULTIBYTE_TO_WIDECHAR_LENGTH (command , unicodeCommandLength );
606+ MULTIBYTE_TO_WIDECHAR_VAR (command , unicodeCommand , unicodeCommandLength );
664607
665608 if (CreateProcessW (NULL , unicodeCommand , NULL , NULL , FALSE, CREATE_NO_WINDOW , NULL , NULL , & si , & pi ))
666609 {
@@ -670,6 +613,7 @@ int runProcess(const char* cmd)
670613 CloseHandle (pi .hProcess );
671614 CloseHandle (pi .hThread );
672615 }
616+ MULTIBYTE_OR_WIDECHAR_VAR_FREE (unicodeCommand );
673617 GC_free (command );
674618 return (int )exitCode ;
675619}
@@ -1278,16 +1222,11 @@ extern const char* SystemImpl__readFileNoNumeric(const char* filename)
12781222 int res ,numCount ;
12791223 FILE * file = NULL ;
12801224#if defined(__MINGW32__ ) || defined(_MSC_VER )
1281- int unicodeFilenameLength = SystemImpl__stringToUnicodeSize (filename );
1282- wchar_t unicodeFilename [unicodeFilenameLength ];
1283- SystemImpl__stringToUnicode (filename , unicodeFilename , unicodeFilenameLength );
1284-
12851225 struct _stat statstr ;
1286- res = _wstat (unicodeFilename , & statstr );
12871226#else
12881227 struct stat statstr ;
1289- res = stat (filename , & statstr );
12901228#endif
1229+ res = omc_stat (filename , & statstr );
12911230
12921231 if (res != 0 ) {
12931232 const char * c_tokens [1 ]= {filename };
@@ -1300,11 +1239,7 @@ extern const char* SystemImpl__readFileNoNumeric(const char* filename)
13001239 return "No such file" ;
13011240 }
13021241
1303- #if defined(__MINGW32__ ) || defined(_MSC_VER )
1304- file = _wfopen (unicodeFilename , L"rb" );
1305- #else
1306- file = fopen (filename ,"rb" );
1307- #endif
1242+ file = omc_fopen (filename ,"rb" );
13081243 buf = (char * ) omc_alloc_interface .malloc_atomic (statstr .st_size + 1 );
13091244 bufRes = (char * ) omc_alloc_interface .malloc_atomic ((statstr .st_size + 70 )* sizeof (char ));
13101245 if ( (res = fread (buf , sizeof (char ), statstr .st_size , file )) != statstr .st_size ) {
0 commit comments