Changeset 9dd81ea in OpenModelica


Ignore:
Timestamp:
2019-05-22T09:58:25+02:00 (5 years ago)
Author:
Adeel Asghar <adeel.asghar@…>
Parents:
0e102be
git-author:
Adeel Asghar <adeel.asghar@…> (05/21/19 11:07:44)
git-committer:
Adeel Asghar <adeel.asghar@…> (05/22/19 09:58:25)
Message:

Support non-ascii characters on Windows

Handle the file writing and removing code.
Handle reading the result files.
Use c_add_message instead of stderr.
Updated 3rdParty antlr.

Location:
OMCompiler
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • OMCompiler/3rdParty

    • Property commit changed from b1095643d59d7298388731a4c6dbc7d6fd9072e2 to 8718bd6164682b71dfa24978b3a7dbcb466b772b
  • OMCompiler/Compiler/Util/System.mo

    r83be5f2b r9dd81ea  
    366366  input String inString;
    367367  output Integer outInteger;
    368   external "C" outInteger=chdir(inString) annotation(Library = "omcruntime");
     368  external "C" outInteger=SystemImpl__chdir(inString) annotation(Library = "omcruntime");
    369369end cd;
    370370
  • OMCompiler/Compiler/runtime/SimulationResults.c

    r83be5f2b r9dd81ea  
    6464  int len = strlen(filename);
    6565  const char *msg[] = {"",""};
     66#if defined(__MINGW32__) || defined(_MSC_VER)
     67  int unicodeFilenameLength = SystemImpl__stringToUnicodeSize(filename);
     68  wchar_t unicodeFilename[unicodeFilenameLength];
     69  SystemImpl__stringToUnicode(filename, unicodeFilename, unicodeFilenameLength);
     70
     71  struct _stat buf;
     72#else
    6673  struct stat buf = {0} /* Zero this or valgrind complains */;
     74#endif
    6775
    6876  if (simresglob->curFileName && 0==strcmp(filename,simresglob->curFileName)) {
    6977    /* 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
    7081    if (stat(filename, &buf)==0 && difftime(buf.st_mtime,simresglob->mtime)==0.0) {
     82#endif
    7183      return simresglob->curFormat; // Super cache :)
    7284    }
     
    93105    break;
    94106  case PLT:
     107#if defined(__MINGW32__) || defined(_MSC_VER)
     108    simresglob->pltReader = _wfopen(unicodeFilename, L"r");
     109#else
    95110    simresglob->pltReader = fopen(filename, "r");
     111#endif
    96112    if (simresglob->pltReader==NULL) {
    97113      msg[1] = filename;
  • OMCompiler/Compiler/runtime/System_omc.c

    r83be5f2b r9dd81ea  
    826826extern const char* System_realpath(const char *path)
    827827{
     828#if defined(__MINGW32__) || defined(_MSC_VER)
     829  int unicodePathLength = SystemImpl__stringToUnicodeSize(path);
     830  wchar_t unicodePath[unicodePathLength];
     831  SystemImpl__stringToUnicode(path, unicodePath, unicodePathLength);
     832
     833  DWORD bufLen = 0;
     834  bufLen = GetFullPathNameW(unicodePath, bufLen, NULL, NULL);
     835  if (!bufLen) {
     836    fprintf(stderr, "GetFullPathNameW failed. %lu\n", GetLastError());
     837    MMC_THROW();
     838  }
     839
     840  WCHAR unicodeFullPath[bufLen];
     841  if (!GetFullPathNameW(unicodePath, bufLen, unicodeFullPath, NULL)) {
     842    fprintf(stderr, "GetFullPathNameW failed. %lu\n", GetLastError());
     843    MMC_THROW();
     844  }
     845
     846  int bufferLength = SystemImpl__unicodeToStringSize(unicodeFullPath);
     847  char buffer[bufferLength];
     848  SystemImpl__unicodeToString(unicodeFullPath, buffer, bufferLength);
     849  SystemImpl__toWindowsSeperators(buffer, bufferLength);
     850  return omc_alloc_interface.malloc_strdup(buffer);
     851#else
    828852  char buf[PATH_MAX];
    829853  if (realpath(path, buf) == NULL) {
     
    831855  }
    832856  return omc_alloc_interface.malloc_strdup(buf);
     857#endif
    833858}
    834859
  • OMCompiler/Compiler/runtime/systemimpl.c

    r83be5f2b r9dd81ea  
    262262}
    263263
     264#if defined(__MINGW32__) || defined(_MSC_VER)
     265extern int SystemImpl__stringToUnicodeSize(const char* str)
     266{
     267  return MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
     268}
     269
     270extern int SystemImpl__stringToUnicode(const char* str, wchar_t* unicode, int size)
     271{
     272  return MultiByteToWideChar(CP_UTF8, 0, str, -1, unicode, size);
     273}
     274
     275int SystemImpl__unicodeToStringSize(const wchar_t* unicode)
     276{
     277  return WideCharToMultiByte(CP_UTF8, 0, unicode, -1, NULL, 0, NULL, NULL);
     278}
     279
     280int 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
     285/* Make sure windows paths use frontslash and not backslash */
     286void SystemImpl__toWindowsSeperators(char* buffer, int bufferLength)
     287{
     288  int i;
     289  for (i=0; i<bufferLength && buffer[i]; i++) {
     290    if (buffer[i] == '\\') buffer[i] = '/';
     291  }
     292}
     293#endif
     294
     295int SystemImpl__chdir(const char* path)
     296{
     297#if defined(__MINGW32__) || defined(_MSC_VER)
     298  int unicodePathLength = SystemImpl__stringToUnicodeSize(path);
     299  wchar_t unicodePath[unicodePathLength];
     300  SystemImpl__stringToUnicode(path, unicodePath, unicodePathLength);
     301
     302  if (!SetCurrentDirectoryW(unicodePath)) {
     303    c_add_message(NULL,-1,ErrorType_scripting,ErrorLevel_error,gettext("SetCurrentDirectoryW failed."),NULL,0);
     304    return -1;
     305  }
     306  return 0;
     307#else
     308  if (chdir(path) != 0) {
     309    c_add_message(NULL,-1,ErrorType_scripting,ErrorLevel_error,gettext("chdir failed."),NULL,0);
     310    return -1;
     311  }
     312  return 0;
     313#endif
     314}
     315
    264316extern char* SystemImpl__pwd(void)
    265317{
     318#if defined(__MINGW32__) || defined(_MSC_VER)
     319  DWORD bufLen = 0;
     320  bufLen = GetCurrentDirectoryW(bufLen, 0);
     321  if (!bufLen) {
     322    c_add_message(NULL,-1,ErrorType_scripting,ErrorLevel_error,gettext("GetCurrentDirectoryW failed."),NULL,0);
     323    return NULL;
     324  }
     325
     326  WCHAR unicodePath[bufLen];
     327  if (!GetCurrentDirectoryW(bufLen, unicodePath)) {
     328    c_add_message(NULL,-1,ErrorType_scripting,ErrorLevel_error,gettext("GetCurrentDirectoryW failed."),NULL,0);
     329    return NULL;
     330  }
     331  int bufferLength = SystemImpl__unicodeToStringSize(unicodePath);
     332  char buffer[bufferLength];
     333  SystemImpl__unicodeToString(unicodePath, buffer, bufferLength);
     334  SystemImpl__toWindowsSeperators(buffer, bufferLength);
     335  return omc_alloc_interface.malloc_strdup(buffer);
     336#else
    266337  char buf[MAXPATHLEN];
     338  if (NULL == getcwd(buf,MAXPATHLEN)) {
     339    c_add_message(NULL,-1,ErrorType_scripting,ErrorLevel_error,gettext("System.pwd failed."),NULL,0);
     340    return NULL;
     341  }
     342  return omc_alloc_interface.malloc_strdup(buf);
     343#endif
     344}
     345
     346extern int SystemImpl__regularFileExists(const char* str)
     347{
    267348#if defined(__MINGW32__) || defined(_MSC_VER)
    268   int i;
    269   LPTSTR bufPtr=buf;
    270   DWORD bufLen = MAXPATHLEN;
    271   if (!GetCurrentDirectory(bufLen,bufPtr)) {
    272     fprintf(stderr, "System.pwd failed\n");
    273     return NULL;
    274   }
    275   /* Make sure windows paths use frontslash and not backslash */
    276   for (i=0; i<MAXPATHLEN && buf[i]; i++) {
    277     if (buf[i] == '\\') buf[i] = '/';
    278   }
    279   return omc_alloc_interface.malloc_strdup(buf);
    280 #else
    281   if (NULL == getcwd(buf,MAXPATHLEN)) {
    282     fprintf(stderr, "System.pwd failed\n");
    283     return NULL;
    284   }
    285   return omc_alloc_interface.malloc_strdup(buf);
    286 #endif
    287 }
    288 
    289 extern int SystemImpl__regularFileExists(const char* str)
    290 {
    291 #if defined(_MSC_VER)
    292   WIN32_FIND_DATA FileData;
     349  WIN32_FIND_DATAW FileData;
    293350  HANDLE sh;
    294351
    295   sh = FindFirstFile(str, &FileData);
     352  int unicodeFilenameLength = SystemImpl__stringToUnicodeSize(str);
     353  wchar_t unicodeFilename[unicodeFilenameLength];
     354  SystemImpl__stringToUnicode(str, unicodeFilename, unicodeFilenameLength);
     355
     356  sh = FindFirstFileW(unicodeFilename, &FileData);
    296357
    297358  if (sh == INVALID_HANDLE_VALUE) {
     
    323384  if (!SystemImpl__regularFileExists(str))
    324385    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
    325393  f = fopen(str, "a");
     394#endif
    326395  if (f == NULL)
    327396    return 0;
     
    335404  int res;
    336405  FILE * file = NULL;
     406#if defined(__MINGW32__) || defined(_MSC_VER)
     407  int unicodeFilenameLength = SystemImpl__stringToUnicodeSize(filename);
     408  wchar_t unicodeFilename[unicodeFilenameLength];
     409  SystemImpl__stringToUnicode(filename, unicodeFilename, unicodeFilenameLength);
     410
     411  struct _stat statstr;
     412  res = _wstat(unicodeFilename, &statstr);
     413#else
    337414  struct stat statstr;
    338415  res = stat(filename, &statstr);
     416#endif
    339417
    340418  if (res != 0) {
     
    363441#endif
    364442
     443#if defined(__MINGW32__) || defined(_MSC_VER)
     444  file = _wfopen(unicodeFilename, L"rb");
     445#else
    365446  file = fopen(filename,"rb");
     447#endif
    366448  if (file == NULL) {
    367449    const char *c_tokens[2]={strerror(errno),filename};
     
    396478{
    397479#if defined(__MINGW32__) || defined(_MSC_VER)
    398   return _unlink(filename);
     480  int unicodeFilenameLength = SystemImpl__stringToUnicodeSize(filename);
     481  wchar_t unicodeFilename[unicodeFilenameLength];
     482  SystemImpl__stringToUnicode(filename, unicodeFilename, unicodeFilenameLength);
     483
     484  return _wunlink(unicodeFilename);
    399485#else /* unix */
    400486  return unlink(filename);
     
    407493#if defined(__MINGW32__) || defined(_MSC_VER)
    408494  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);
    409503#else
    410504  const char *fileOpenMode = "wb";  /* on Unixes don't bother, do it binary mode */
     
    413507  int len = strlen(data); /* MMC_HDRSTRLEN(MMC_GETHDR(rmlA1)); */
    414508#if defined(__APPLE_CC__)||defined(__MINGW32__)||defined(__MINGW64__)
    415   unlink(filename);
     509  SystemImpl__removeFile(filename);
    416510#endif
    417511  /* 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
    418515  file = fopen(filename,fileOpenMode);
     516#endif
    419517  if (file == NULL) {
    420518    const char *c_tokens[1]={filename};
     
    546644int runProcess(const char* cmd)
    547645{
    548   STARTUPINFO si;
     646  STARTUPINFOW si;
    549647  PROCESS_INFORMATION pi;
    550648  char *c = "cmd /c";
     
    561659  /* fprintf(stderr, "%s\n", command); fflush(NULL); */
    562660
    563   if (CreateProcessA(NULL, command, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi))
     661  int unicodeCommandLength = SystemImpl__stringToUnicodeSize(command);
     662  wchar_t unicodeCommand[unicodeCommandLength];
     663  SystemImpl__stringToUnicode(command, unicodeCommand, unicodeCommandLength);
     664
     665  if (CreateProcessW(NULL, unicodeCommand, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi))
    564666  {
    565       WaitForSingleObject(pi.hProcess, INFINITE);
    566       // Get the exit code.
    567       GetExitCodeProcess(pi.hProcess, &exitCode);
    568       CloseHandle(pi.hProcess);
    569       CloseHandle(pi.hThread);
     667    WaitForSingleObject(pi.hProcess, INFINITE);
     668    // Get the exit code.
     669    GetExitCodeProcess(pi.hProcess, &exitCode);
     670    CloseHandle(pi.hProcess);
     671    CloseHandle(pi.hThread);
    570672  }
    571673  GC_free(command);
     
    11761278  int res,numCount;
    11771279  FILE * file = NULL;
     1280#if defined(__MINGW32__) || defined(_MSC_VER)
     1281  int unicodeFilenameLength = SystemImpl__stringToUnicodeSize(filename);
     1282  wchar_t unicodeFilename[unicodeFilenameLength];
     1283  SystemImpl__stringToUnicode(filename, unicodeFilename, unicodeFilenameLength);
     1284
     1285  struct _stat statstr;
     1286  res = _wstat(unicodeFilename, &statstr);
     1287#else
    11781288  struct stat statstr;
    11791289  res = stat(filename, &statstr);
     1290#endif
    11801291
    11811292  if(res!=0) {
     
    11901301  }
    11911302
     1303#if defined(__MINGW32__) || defined(_MSC_VER)
     1304  file = _wfopen(unicodeFilename, L"rb");
     1305#else
    11921306  file = fopen(filename,"rb");
     1307#endif
    11931308  buf = (char*) omc_alloc_interface.malloc_atomic(statstr.st_size+1);
    11941309  bufRes = (char*) omc_alloc_interface.malloc_atomic((statstr.st_size+70)*sizeof(char));
     
    23502465      const char *ignore = SystemImpl__iconv__ascii(str);
    23512466      const char *tokens[4] = {res,from,to,ignore};
    2352       c_add_message(NULL,-1,ErrorType_scripting,ErrorLevel_error,gettext("iconv(\"%s\",to=\"%s\",from=\"%s\") failed: %s"),tokens,4);
     2467      c_add_message(NULL,-1,ErrorType_scripting,ErrorLevel_error,gettext("iconv(\"%s\",from=\"%s\",to=\"%s\") failed: %s"),tokens,4);
    23532468      omc_alloc_interface.free_uncollectable((char*)ignore);
    23542469    }
  • OMCompiler/Compiler/runtime/systemimpl.h

    r83be5f2b r9dd81ea  
    8888extern int SystemImpl__setCFlags(const char *str);
    8989extern int SystemImpl__setLDFlags(const char *str);
     90#if defined(__MINGW32__) || defined(_MSC_VER)
     91extern int SystemImpl__stringToUnicodeSize(const char* str);
     92extern int SystemImpl__stringToUnicode(const char* str, wchar_t* unicode, int size);
     93#endif
    9094extern char* SystemImpl__pwd(void);
    9195extern int SystemImpl__regularFileExists(const char* str);
  • OMCompiler/Parser/parse.c

    r83be5f2b r9dd81ea  
    181181{
    182182  pANTLR3_PARSER      parser;
    183   pANTLR3_STRING      ttext;
    184183  pANTLR3_EXCEPTION      ex;
    185184  pANTLR3_COMMON_TOKEN   preToken,nextToken;
     
    196195  // Retrieve some info for easy reading.
    197196  ex      =    recognizer->state->exception;
    198   ttext   =    (pANTLR3_STRING) NULL;
    199197
    200198  switch  (recognizer->type)
     
    436434   * So we pass an empty string instead :)
    437435   */
     436#if defined(__MINGW32__) || defined(_MSC_VER)
     437  int unicodeFileNameLength = SystemImpl__stringToUnicodeSize(fileName);
     438  wchar_t unicodeFileName[unicodeFileNameLength];
     439  SystemImpl__stringToUnicode(fileName, unicodeFileName, unicodeFileNameLength);
     440
     441  struct _stat st;
     442  _wstat(unicodeFileName, &st);
     443#else
    438444  struct stat st;
    439445  stat(members.filename_C, &st);
     446#endif
    440447  members.timestamp = mmc_mk_rcon((double)st.st_mtime);
    441448  if (0 == st.st_size) return parseString("",members.filename_C,ModelicaParser_flags, langStd, runningTestsuite);
  • OMCompiler/SimulationRuntime/c/util/read_csv.c

    r83be5f2b r9dd81ea  
    3535#include "read_matlab4.h"
    3636#include "libcsv.h"
     37#if defined(__MINGW32__) || defined(_MSC_VER)
     38#include <windows.h>
     39#endif
    3740
    3841#if defined(__cplusplus)
     
    103106  size_t offset=0;
    104107  unsigned char delim = CSV_COMMA;
     108#if defined(__MINGW32__) || defined(_MSC_VER)
     109  int unicodeFilenameLength = MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0);
     110  wchar_t unicodeFilename[unicodeFilenameLength];
     111  MultiByteToWideChar(CP_UTF8, 0, filename, -1, unicodeFilename, unicodeFilenameLength);
     112
     113  f = _wfopen(unicodeFilename, L"r");
     114#else
    105115  f = fopen(filename,"r");
     116#endif
    106117  if (f == NULL) {
    107118    return -1;
     
    216227  struct csv_parser p;
    217228  struct csv_body body = {0};
     229#if defined(__MINGW32__) || defined(_MSC_VER)
     230  int unicodeFilenameLength = MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0);
     231  wchar_t unicodeFilename[unicodeFilenameLength];
     232  MultiByteToWideChar(CP_UTF8, 0, filename, -1, unicodeFilename, unicodeFilenameLength);
     233
     234  FILE *fin = _wfopen(unicodeFilename, L"r");
     235#else
    218236  FILE *fin = fopen(filename, "r");
     237#endif
    219238  size_t offset = 0;
    220239  unsigned char delim = CSV_COMMA;
     
    264283  size_t offset = 0;
    265284  unsigned char delim = CSV_COMMA;
     285#if defined(__MINGW32__) || defined(_MSC_VER)
     286  int unicodeFilenameLength = MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0);
     287  wchar_t unicodeFilename[unicodeFilenameLength];
     288  MultiByteToWideChar(CP_UTF8, 0, filename, -1, unicodeFilename, unicodeFilenameLength);
     289
     290  FILE *fin = _wfopen(unicodeFilename, L"r");
     291#else
    266292  FILE *fin = fopen(filename, "r");
     293#endif
    267294  if (!fin) {
    268295    return NULL;
  • OMCompiler/SimulationRuntime/c/util/read_matlab4.c

    r83be5f2b r9dd81ea  
    3636#include <ctype.h>
    3737#include "read_matlab4.h"
     38#if defined(__MINGW32__) || defined(_MSC_VER)
     39#include <windows.h>
     40#endif
    3841
    3942extern const char *omc_mat_Aclass;
     
    227230  char binTrans = 1;
    228231  memset(reader, 0, sizeof(ModelicaMatReader));
     232#if defined(__MINGW32__) || defined(_MSC_VER)
     233  int unicodeFilenameLength = MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0);
     234  wchar_t unicodeFilename[unicodeFilenameLength];
     235  MultiByteToWideChar(CP_UTF8, 0, filename, -1, unicodeFilename, unicodeFilenameLength);
     236
     237  reader->file = _wfopen(unicodeFilename, L"rb");
     238#else
    229239  reader->file = fopen(filename, "rb");
     240#endif
    230241  if(!reader->file) return strerror(errno);
    231242  reader->fileName = strdup(filename);
Note: See TracChangeset for help on using the changeset viewer.