Skip to content

Commit

Permalink
Add a scripting function to access stat
Browse files Browse the repository at this point in the history
It is otherwise very inconvenient to access file sizes from the
scripting environment.

Belonging to [master]:
  - OpenModelica/OMCompiler#1913
  - OpenModelica/OpenModelica-testsuite#746
  • Loading branch information
sjoelund authored and OpenModelica-Hudson committed Oct 17, 2017
1 parent aeb9725 commit 702e338
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
10 changes: 10 additions & 0 deletions Compiler/FrontEnd/ModelicaBuiltin.mo
Expand Up @@ -1637,6 +1637,16 @@ algorithm
exists := Internal.stat(dirName) == Internal.FileType.Directory;
end directoryExists;

impure function stat
input String fileName;
output Boolean success;
output Real fileSize;
output Real mtime;
external "builtin" annotation(__OpenModelica_Impure=true,Documentation(info="<html>
<p>Like <a href=\"http://linux.die.net/man/2/stat\">stat(2)</a>, except the output is of type real because of limited precision of Integer.</p>
</html>"));
end stat;

impure function readFile
"The contents of the given file are returned.
Note that if the function fails, the error message is returned as a string instead of multiple output or similar."
Expand Down
5 changes: 5 additions & 0 deletions Compiler/Script/CevalScriptBackend.mo
Expand Up @@ -1871,6 +1871,11 @@ algorithm
then
(cache,Values.BOOL(false),st);

case (cache,_,"stat",{Values.STRING(str)},st as GlobalScript.SYMBOLTABLE(ast=p),_)
algorithm
(b,r1,r2) := System.stat(str);
then (cache,Values.TUPLE({Values.BOOL(b),Values.REAL(r1),Values.REAL(r2)}),st);

case (cache,_,"isType",{Values.CODE(Absyn.C_TYPENAME(classpath))},st as GlobalScript.SYMBOLTABLE(ast=p),_)
equation
b = Interactive.isType(classpath, p);
Expand Down
2 changes: 1 addition & 1 deletion Compiler/Util/System.mo
Expand Up @@ -1176,7 +1176,7 @@ public function stat
output Real st_size; /* An integer stored as double for higher precision */
output Real st_mtime; /* An integer stored as double for higher precision */
external "C" success=SystemImpl__stat(filename,st_size,st_mtime) annotation(Library = {"omcruntime"},Documentation(info="<html>
<p>Like <a href=\"http://linux.die.net/man/3/ctime\">ctime(3)</a>, except the input is of type real because of limited precision of Integer.</p>
<p>Like <a href=\"http://linux.die.net/man/2/stat\">stat(2)</a>, except the output is of type real because of limited precision of Integer.</p>
</html>"));
end stat;

Expand Down

0 comments on commit 702e338

Please sign in to comment.