From 94cae26a1556e6088a3995653fe5f7c6a9b76624 Mon Sep 17 00:00:00 2001 From: Volker Waurich Date: Fri, 3 Aug 2018 10:08:45 +0200 Subject: [PATCH] copyFile call for scripting api Belonging to [master]: - OpenModelica/OMCompiler#2584 --- Compiler/FrontEnd/ModelicaBuiltin.mo | 8 ++++++++ Compiler/Script/CevalScript.mo | 6 ++++++ Compiler/Util/System.mo | 7 +++++++ Compiler/runtime/systemimpl.c | 30 ++++++++++++++++++++++++++++ Compiler/runtime/systemimpl.h | 1 + 5 files changed, 52 insertions(+) diff --git a/Compiler/FrontEnd/ModelicaBuiltin.mo b/Compiler/FrontEnd/ModelicaBuiltin.mo index 20a83769e5..aa523c8bf7 100644 --- a/Compiler/FrontEnd/ModelicaBuiltin.mo +++ b/Compiler/FrontEnd/ModelicaBuiltin.mo @@ -1993,6 +1993,14 @@ external "builtin"; annotation(preferredView="text"); end mkdir; +function copy "copies the source file to the destined directory. Returns true if the file has been copied." + input String source; + input String destination; + output Boolean success; +external "builtin"; +annotation(preferredView="text"); +end copy; + function remove "removes a file or directory of given path (which may be either relative or absolute)." input String path; output Boolean success "Returns true on success."; diff --git a/Compiler/Script/CevalScript.mo b/Compiler/Script/CevalScript.mo index f26fa2dea6..f309d7b1cd 100644 --- a/Compiler/Script/CevalScript.mo +++ b/Compiler/Script/CevalScript.mo @@ -1062,6 +1062,12 @@ algorithm then (cache,Values.BOOL(b)); + case (cache,_,"copy",{Values.STRING(str1),Values.STRING(str2)},_) + equation + b = System.copyFile(str1,str2); + then + (cache,Values.BOOL(b)); + case (cache,_,"remove",{Values.STRING(str)},_) equation b = System.removeDirectory(str); diff --git a/Compiler/Util/System.mo b/Compiler/Util/System.mo index 00dae3be80..6de7945473 100644 --- a/Compiler/Util/System.mo +++ b/Compiler/Util/System.mo @@ -482,6 +482,13 @@ public function directoryExists external "C" outBool=SystemImpl__directoryExists(inString) annotation(Library = "omcruntime"); end directoryExists; +public function copyFile + input String source; + input String destination; + output Boolean outBool; + external "C" outBool=SystemImpl__copyFile(source, destination) annotation(Library = "omcruntime"); +end copyFile; + public function removeDirectory input String inString; diff --git a/Compiler/runtime/systemimpl.c b/Compiler/runtime/systemimpl.c index 4a5255ac3b..21cf6ef91a 100644 --- a/Compiler/runtime/systemimpl.c +++ b/Compiler/runtime/systemimpl.c @@ -893,6 +893,36 @@ extern int SystemImpl__createDirectory(const char *str) } } +extern int SystemImpl__copyFile(const char *str_1, const char *str_2) +{ + int rv = 1; + char ch; + FILE *source, *target; + + if (!SystemImpl__directoryExists(str_2)) + { + rv = SystemImpl__createDirectory(str_2); + } + + if (str_1 == "") + rv = 0; + + char targetFile[100]; + strcpy(targetFile,str_2); + strcat(targetFile,"/"); + strcat(targetFile,str_1); + + source = fopen(str_1, "r"); + target = fopen(targetFile, "w"); + + while( ( ch = fgetc(source) ) != EOF ) + rv=rv && fputc(ch, target); + + fclose(source); + fclose(target); + return rv; +} + static char * SystemImpl__NextDir(const char * path) { char * res = NULL; diff --git a/Compiler/runtime/systemimpl.h b/Compiler/runtime/systemimpl.h index 0fc1eed407..04562e201a 100644 --- a/Compiler/runtime/systemimpl.h +++ b/Compiler/runtime/systemimpl.h @@ -99,6 +99,7 @@ extern void SystemImpl__plotCallBack(threadData_t *threadData, int externalWindo const char* autoScale, const char* variables); extern double SystemImpl__time(void); extern int SystemImpl__directoryExists(const char* str); +extern int SystemImpl__copyFile(const char* str_1, const char* str_2); extern int SystemImpl__createDirectory(const char *str); extern int SystemImpl__removeDirectory(const char *str); extern const char* SystemImpl__readFileNoNumeric(const char* filename);