OMCompiler/Compiler/Util/File.mo
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file is part of OpenModelica. | ||
| 3 | * | ||
| 4 | * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC), | ||
| 5 | * c/o Linköpings universitet, Department of Computer and Information Science, | ||
| 6 | * SE-58183 Linköping, Sweden. | ||
| 7 | * | ||
| 8 | * All rights reserved. | ||
| 9 | * | ||
| 10 | * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF AGPL VERSION 3 LICENSE OR | ||
| 11 | * THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8. | ||
| 12 | * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES | ||
| 13 | * RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GNU AGPL | ||
| 14 | * VERSION 3, ACCORDING TO RECIPIENTS CHOICE. | ||
| 15 | * | ||
| 16 | * The OpenModelica software and the OSMC (Open Source Modelica Consortium) | ||
| 17 | * Public License (OSMC-PL) are obtained from OSMC, either from the above | ||
| 18 | * address, from the URLs: | ||
| 19 | * http://www.openmodelica.org or | ||
| 20 | * https://github.com/OpenModelica/ or | ||
| 21 | * http://www.ida.liu.se/projects/OpenModelica, | ||
| 22 | * and in the OpenModelica distribution. | ||
| 23 | * | ||
| 24 | * GNU AGPL version 3 is obtained from: | ||
| 25 | * https://www.gnu.org/licenses/licenses.html#GPL | ||
| 26 | * | ||
| 27 | * This program is distributed WITHOUT ANY WARRANTY; without | ||
| 28 | * even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 29 | * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH | ||
| 30 | * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL. | ||
| 31 | * | ||
| 32 | * See the full OSMC Public License conditions for more details. | ||
| 33 | * | ||
| 34 | */ | ||
| 35 | |||
| 36 | encapsulated package File | ||
| 37 | |||
| 38 | class File | ||
| 39 | extends ExternalObject; | ||
| 40 | 17398421 | function constructor<T> "File constructor." | |
| 41 | input Option<Integer> fromID = noReference() "Never pass this an actual Option<Integer>. Only use File.getReference(file) or File.noReference(). Determines if we should restore from another File object or create a new File."; | ||
| 42 | output File file; | ||
| 43 | external "C" file=om_file_new(fromID) annotation(IncludeDirectory="modelica://File/", Include="#include \"omc_file_ext.h\""); | ||
| 44 | end constructor; | ||
| 45 | |||
| 46 | 17398421 | function destructor | |
| 47 | input File file; | ||
| 48 | external "C" om_file_free(file) annotation(IncludeDirectory="modelica://File/", Include="#include \"omc_file_ext.h\""); | ||
| 49 | end destructor; | ||
| 50 | end File; | ||
| 51 | |||
| 52 | type Mode = enumeration(Read,Write); | ||
| 53 | |||
| 54 |
2/2✓ Branch 0 taken 34114 times.
✓ Branch 1 taken 17364307 times.
|
17398421 | function open |
| 55 | input File file; | ||
| 56 | input String filename; | ||
| 57 | input Mode mode = Mode.Read; | ||
| 58 | external "C" om_file_open(file,filename,mode) annotation(IncludeDirectory="modelica://File/", Include="#include \"omc_file_ext.h\""); | ||
| 59 | end open; | ||
| 60 | |||
| 61 | 34114 | function write | |
| 62 | input File file; | ||
| 63 | input String data; | ||
| 64 | external "C" om_file_write(file,data) annotation(IncludeDirectory="modelica://File/", Include="#include \"omc_file_ext.h\""); | ||
| 65 | end write; | ||
| 66 | |||
| 67 | 98596261 | function writeInt | |
| 68 | input File file; | ||
| 69 | input Integer data; | ||
| 70 | input String format="%d"; | ||
| 71 | external "C" om_file_write_int(file,data,format) annotation(IncludeDirectory="modelica://File/", Include="#include \"omc_file_ext.h\""); | ||
| 72 | end writeInt; | ||
| 73 | |||
| 74 | 7178808 | function writeReal | |
| 75 | input File file; | ||
| 76 | input Real data; | ||
| 77 | input String format="%.15g"; | ||
| 78 | external "C" om_file_write_real(file,data,format) annotation(IncludeDirectory="modelica://File/", Include="#include \"omc_file_ext.h\""); | ||
| 79 | end writeReal; | ||
| 80 | |||
| 81 | type Escape = enumeration(None "No escape string", | ||
| 82 | C "Escapes C strings (minimally): \\n and \"", | ||
| 83 | JSON "Escapes JSON strings (quotes and control characters)", | ||
| 84 | XML "Escapes strings to XML text"); | ||
| 85 | |||
| 86 | 4628 | function writeEscape | |
| 87 | input File file; | ||
| 88 | input String data; | ||
| 89 | input Escape escape; | ||
| 90 | external "C" om_file_write_escape(file,data,escape) annotation(IncludeDirectory="modelica://File/", Include="#include \"omc_file_ext.h\""); | ||
| 91 | end writeEscape; | ||
| 92 | |||
| 93 | type Whence = enumeration(Set "SEEK_SET 0=start of file",Current "SEEK_CUR 0=current byte",End "SEEK_END 0=end of file"); | ||
| 94 | |||
| 95 | 9812286 | function seek | |
| 96 | input File file; | ||
| 97 | input Integer offset; | ||
| 98 | input Whence whence = Whence.Set; | ||
| 99 | output Boolean success; | ||
| 100 | external "C" success = om_file_seek(file,offset,whence) annotation(IncludeDirectory="modelica://File/", Include="#include \"omc_file_ext.h\""); | ||
| 101 | end seek; | ||
| 102 | |||
| 103 | ✗ | function tell | |
| 104 | input File file; | ||
| 105 | output Integer pos; | ||
| 106 | external "C" pos = om_file_tell(file) annotation(IncludeDirectory="modelica://File/", Include="#include \"omc_file_ext.h\""); | ||
| 107 | end tell; | ||
| 108 | |||
| 109 |
1/2✓ Branch 0 taken 743914 times.
✗ Branch 1 not taken.
|
743914 | function getFilename |
| 110 | input Option<Integer> file; | ||
| 111 | output String fileName2; | ||
| 112 | external "C" fileName2 = om_file_get_filename(file) annotation(IncludeDirectory="modelica://File/", Include="#include \"omc_file_ext.h\""); | ||
| 113 | end getFilename; | ||
| 114 | |||
| 115 | ✗ | function noReference "Returns NULL (an opaque pointer; not actually Option<Integer>)" | |
| 116 | output Option<Integer> reference; | ||
| 117 | external "C" reference = om_file_no_reference() annotation(IncludeDirectory="modelica://File/", Include="#include \"omc_file_ext.h\""); | ||
| 118 | end noReference; | ||
| 119 | |||
| 120 | 34114 | function getReference "Returns an opaque pointer (not actually Option<Integer>)" | |
| 121 | input File file; | ||
| 122 | output Option<Integer> reference; | ||
| 123 | external "C" reference = om_file_get_reference(file) annotation(IncludeDirectory="modelica://File/", Include="#include \"omc_file_ext.h\""); | ||
| 124 | end getReference; | ||
| 125 | |||
| 126 | 31787 | function releaseReference | |
| 127 | input File file; | ||
| 128 | external "C" om_file_release_reference(file) annotation(IncludeDirectory="modelica://File/", Include="#include \"omc_file_ext.h\""); | ||
| 129 | end releaseReference; | ||
| 130 | |||
| 131 | function writeSpace | ||
| 132 | input File file; | ||
| 133 | input Integer n; | ||
| 134 | algorithm | ||
| 135 |
2/2✓ Branch 0 taken 3675730 times.
✓ Branch 1 taken 5533697 times.
|
23644017 | for i in 1:n loop |
| 136 | 14434590 | File.write(file, " "); | |
| 137 | end for; | ||
| 138 | end writeSpace; | ||
| 139 | |||
| 140 | package Examples | ||
| 141 | |||
| 142 | model WriteToFile | ||
| 143 | File file = File(); | ||
| 144 | initial algorithm | ||
| 145 | open(file,"abc.txt",Mode.Write); | ||
| 146 | write(file,"def.fafaf\n"); | ||
| 147 | writeEscape(file,"xx<def.\"\nfaf>af\n",escape=Escape.JSON); | ||
| 148 | annotation(experiment(StopTime=0)); | ||
| 149 | end WriteToFile; | ||
| 150 | |||
| 151 | end Examples; | ||
| 152 | |||
| 153 | annotation(__OpenModelica_Interface="util"); | ||
| 154 | end File; | ||
| 155 |