Skip to content
This repository has been archived by the owner on May 18, 2019. It is now read-only.

Commit

Permalink
copyFile call for scripting api
Browse files Browse the repository at this point in the history
Belonging to [master]:
  - #2584
  • Loading branch information
vwaurich authored and OpenModelica-Hudson committed Aug 3, 2018
1 parent bab4ecd commit 94cae26
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Compiler/FrontEnd/ModelicaBuiltin.mo
Expand Up @@ -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.";
Expand Down
6 changes: 6 additions & 0 deletions Compiler/Script/CevalScript.mo
Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions Compiler/Util/System.mo
Expand Up @@ -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;
Expand Down
30 changes: 30 additions & 0 deletions Compiler/runtime/systemimpl.c
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions Compiler/runtime/systemimpl.h
Expand Up @@ -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);
Expand Down

0 comments on commit 94cae26

Please sign in to comment.