Skip to content

Commit 33fb7b9

Browse files
committed
Use the macros for unicode to utf-8 and back conversions
Added a `omc_file.h/c` which contains the file system operations like fopen, stat etc. Use the functions declared in this file instead of directly calling them.
1 parent 93a3ce5 commit 33fb7b9

32 files changed

+314
-232
lines changed

OMCompiler/Compiler/runtime/SimulationResults.c

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "read_csv.h"
99
#include <math.h>
1010
#include <gc.h>
11+
#include "omc_file.h"
1112
#include "omc_msvc.h" /* For INFINITY and NAN */
1213
#include <time.h>
1314
#include <sys/types.h>
@@ -64,22 +65,14 @@ static PlotFormat SimulationResultsImpl__openFile(const char *filename, Simulati
6465
int len = strlen(filename);
6566
const char *msg[] = {"",""};
6667
#if defined(__MINGW32__) || defined(_MSC_VER)
67-
int unicodeFilenameLength = SystemImpl__stringToUnicodeSize(filename);
68-
wchar_t unicodeFilename[unicodeFilenameLength];
69-
SystemImpl__stringToUnicode(filename, unicodeFilename, unicodeFilenameLength);
70-
7168
struct _stat buf;
7269
#else
7370
struct stat buf = {0} /* Zero this or valgrind complains */;
7471
#endif
7572

7673
if (simresglob->curFileName && 0==strcmp(filename,simresglob->curFileName)) {
7774
/* Also check that the file was not modified */
78-
#if defined(__MINGW32__) || defined(_MSC_VER)
79-
if (_wstat(unicodeFilename, &buf)==0 && difftime(buf.st_mtime,simresglob->mtime)==0.0) {
80-
#else
81-
if (stat(filename, &buf)==0 && difftime(buf.st_mtime,simresglob->mtime)==0.0) {
82-
#endif
75+
if (omc_stat(filename, &buf)==0 && difftime(buf.st_mtime,simresglob->mtime)==0.0) {
8376
return simresglob->curFormat; // Super cache :)
8477
}
8578
}
@@ -104,11 +97,7 @@ static PlotFormat SimulationResultsImpl__openFile(const char *filename, Simulati
10497
}
10598
break;
10699
case PLT:
107-
#if defined(__MINGW32__) || defined(_MSC_VER)
108-
simresglob->pltReader = _wfopen(unicodeFilename, L"r");
109-
#else
110-
simresglob->pltReader = fopen(filename, "r");
111-
#endif
100+
simresglob->pltReader = omc_fopen(filename, "r");
112101
if (simresglob->pltReader==NULL) {
113102
msg[1] = filename;
114103
c_add_message(NULL,-1, ErrorType_scripting, ErrorLevel_error, gettext("Failed to open simulation result %s: %s"), msg, 2);

OMCompiler/Compiler/runtime/System_omc.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ extern "C"
4646
#include <limits.h>
4747
#include <stdlib.h>
4848
#include "omc_msvc.h"
49+
#include "omc_file.h"
4950
#include "openmodelica.h"
5051
#include "meta_modelica.h"
5152
#include "ModelicaUtilities.h"
@@ -826,9 +827,8 @@ extern const char* System_sprintff(const char *fmt, double d)
826827
extern const char* System_realpath(const char *path)
827828
{
828829
#if defined(__MINGW32__) || defined(_MSC_VER)
829-
int unicodePathLength = SystemImpl__stringToUnicodeSize(path);
830-
wchar_t unicodePath[unicodePathLength];
831-
SystemImpl__stringToUnicode(path, unicodePath, unicodePathLength);
830+
MULTIBYTE_TO_WIDECHAR_LENGTH(path, unicodePathLength);
831+
MULTIBYTE_TO_WIDECHAR_VAR(path, unicodePath, unicodePathLength);
832832

833833
DWORD bufLen = 0;
834834
bufLen = GetFullPathNameW(unicodePath, bufLen, NULL, NULL);
@@ -839,15 +839,18 @@ extern const char* System_realpath(const char *path)
839839

840840
WCHAR unicodeFullPath[bufLen];
841841
if (!GetFullPathNameW(unicodePath, bufLen, unicodeFullPath, NULL)) {
842+
MULTIBYTE_OR_WIDECHAR_VAR_FREE(unicodePath);
842843
fprintf(stderr, "GetFullPathNameW failed. %lu\n", GetLastError());
843844
MMC_THROW();
844845
}
846+
MULTIBYTE_OR_WIDECHAR_VAR_FREE(unicodePath);
845847

846-
int bufferLength = SystemImpl__unicodeToStringSize(unicodeFullPath);
847-
char buffer[bufferLength];
848-
SystemImpl__unicodeToString(unicodeFullPath, buffer, bufferLength);
848+
WIDECHAR_TO_MULTIBYTE_LENGTH(unicodeFullPath, bufferLength);
849+
WIDECHAR_TO_MULTIBYTE_VAR(unicodeFullPath, buffer, bufferLength);
849850
SystemImpl__toWindowsSeperators(buffer, bufferLength);
850-
return omc_alloc_interface.malloc_strdup(buffer);
851+
char *res = omc_alloc_interface.malloc_strdup(buffer);
852+
MULTIBYTE_OR_WIDECHAR_VAR_FREE(buffer);
853+
return res;
851854
#else
852855
char buf[PATH_MAX];
853856
if (realpath(path, buf) == NULL) {

OMCompiler/Compiler/runtime/systemimpl.c

Lines changed: 23 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -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 */
286264
void SystemImpl__toWindowsSeperators(char* buffer, int bufferLength)
287265
{
@@ -295,14 +273,15 @@ void SystemImpl__toWindowsSeperators(char* buffer, int bufferLength)
295273
int 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 */
477441
int 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 */
491447
int 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) {

OMCompiler/Compiler/runtime/systemimpl.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,6 @@ extern int SystemImpl__setCXXCompiler(const char *str);
8787
extern int SystemImpl__setLinker(const char *str);
8888
extern int SystemImpl__setCFlags(const char *str);
8989
extern int SystemImpl__setLDFlags(const char *str);
90-
#if defined(__MINGW32__) || defined(_MSC_VER)
91-
extern int SystemImpl__stringToUnicodeSize(const char* str);
92-
extern int SystemImpl__stringToUnicode(const char* str, wchar_t* unicode, int size);
93-
#endif
9490
extern char* SystemImpl__pwd(void);
9591
extern int SystemImpl__regularFileExists(const char* str);
9692
extern int SystemImpl__removeFile(const char* filename);

OMCompiler/Parser/parse.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545

4646
#include "errorext.h"
4747
#include "systemimpl.h"
48+
#include "omc_file.h"
4849

4950
pthread_once_t parser_once_create_key = PTHREAD_ONCE_INIT;
5051
pthread_key_t modelicaParserKey;
@@ -434,16 +435,11 @@ static void* parseFile(const char* fileName, const char* infoName, int flags, co
434435
* So we pass an empty string instead :)
435436
*/
436437
#if defined(__MINGW32__) || defined(_MSC_VER)
437-
int unicodeFileNameLength = SystemImpl__stringToUnicodeSize(fileName);
438-
wchar_t unicodeFileName[unicodeFileNameLength];
439-
SystemImpl__stringToUnicode(fileName, unicodeFileName, unicodeFileNameLength);
440-
441438
struct _stat st;
442-
_wstat(unicodeFileName, &st);
443439
#else
444440
struct stat st;
445-
stat(members.filename_C, &st);
446441
#endif
442+
omc_stat(members.filename_C, &st);
447443
members.timestamp = mmc_mk_rcon((double)st.st_mtime);
448444
if (0 == st.st_size) return parseString("",members.filename_C,ModelicaParser_flags, langStd, runningTestsuite);
449445

OMCompiler/SimulationRuntime/c/Makefile.common

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ RUNTIMEUTIL_HEADERS = \
107107
./util/modelica.h \
108108
./util/modelica_string.h \
109109
./util/omc_error.h \
110+
./util/omc_file.h \
110111
./util/omc_mmap.h \
111112
./util/omc_msvc.h \
112113
./util/omc_spinlock.h \

OMCompiler/SimulationRuntime/c/Makefile.objs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ else
2828
UTIL_OBJS_NO_FMI=
2929
endif
3030

31-
UTIL_OBJS_MINIMAL=base_array$(OBJ_EXT) boolean_array$(OBJ_EXT) omc_error$(OBJ_EXT) division$(OBJ_EXT) generic_array$(OBJ_EXT) index_spec$(OBJ_EXT) integer_array$(OBJ_EXT) list$(OBJ_EXT) modelica_string$(OBJ_EXT) real_array$(OBJ_EXT) ringbuffer$(OBJ_EXT) string_array$(OBJ_EXT) utility$(OBJ_EXT) varinfo$(OBJ_EXT) ModelicaUtilities$(OBJ_EXT) omc_msvc$(OBJ_EXT) simulation_options$(OBJ_EXT) rational$(OBJ_EXT) modelica_string_lit$(OBJ_EXT) omc_init$(OBJ_EXT) omc_mmap$(OBJ_EXT) $(UTIL_OBJS_NO_FMI)
32-
UTIL_HFILES_MINIMAL=base_array.h boolean_array.h division.h generic_array.h omc_error.h index_spec.h integer_array.h list.h modelica.h modelica_string.h read_write.h real_array.h ringbuffer.h rtclock.h string_array.h utility.h varinfo.h simulation_options.h omc_mmap.h modelica_string_lit.h omc_init.h
31+
UTIL_OBJS_MINIMAL=base_array$(OBJ_EXT) boolean_array$(OBJ_EXT) omc_error$(OBJ_EXT) omc_file$(OBJ_EXT) division$(OBJ_EXT) generic_array$(OBJ_EXT) index_spec$(OBJ_EXT) integer_array$(OBJ_EXT) list$(OBJ_EXT) modelica_string$(OBJ_EXT) real_array$(OBJ_EXT) ringbuffer$(OBJ_EXT) string_array$(OBJ_EXT) utility$(OBJ_EXT) varinfo$(OBJ_EXT) ModelicaUtilities$(OBJ_EXT) omc_msvc$(OBJ_EXT) simulation_options$(OBJ_EXT) rational$(OBJ_EXT) modelica_string_lit$(OBJ_EXT) omc_init$(OBJ_EXT) omc_mmap$(OBJ_EXT) $(UTIL_OBJS_NO_FMI)
32+
UTIL_HFILES_MINIMAL=base_array.h boolean_array.h division.h generic_array.h omc_error.h omc_file.h index_spec.h integer_array.h list.h modelica.h modelica_string.h read_write.h real_array.h ringbuffer.h rtclock.h string_array.h utility.h varinfo.h simulation_options.h omc_mmap.h modelica_string_lit.h omc_init.h
3333

3434
ifeq ($(OMC_MINIMAL_RUNTIME),)
3535
UTIL_OBJS=$(UTIL_OBJS_MINIMAL) java_interface$(OBJ_EXT) libcsv$(OBJ_EXT) read_csv$(OBJ_EXT) OldModelicaTables$(OBJ_EXT) tinymt64$(OBJ_EXT) write_csv$(OBJ_EXT) rtclock$(OBJ_EXT)

OMCompiler/SimulationRuntime/c/gc/omc_gc.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "../omc_simulation_settings.h"
4141
#include "omc_gc.h"
4242
#include "../util/omc_error.h"
43+
#include "../util/omc_file.h"
4344
#include "../util/omc_init.h"
4445

4546
static mmc_GC_state_type x_mmc_GC_state = {0};
@@ -67,7 +68,7 @@ static void print_words()
6768
pid_t pid = getpid();
6869
char str[50];
6970
sprintf(str, "omc-memory.%d.txt", pid);
70-
FILE *fout = fopen(str, "w");
71+
FILE *fout = omc_fopen(str, "w");
7172
HASH_ITER(hh, table, entry, tmp) {
7273
fprintf(fout, "%ld: %s\n", (long) entry->count, entry->pos);
7374
}

0 commit comments

Comments
 (0)