OMCompiler/Compiler/Util/IOStream.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 IOStream | ||
| 37 | "file: IOStream.mo | ||
| 38 | package: IOStream | ||
| 39 | description: IOStream Utilities | ||
| 40 | @author: Adrian Pop [adrpo@ida.liu.se] | ||
| 41 | @date: 2010-05-19 | ||
| 42 | |||
| 43 | |||
| 44 | This package implement these stream types: | ||
| 45 | - file streams (stream as file) | ||
| 46 | - list streams (stream as list<String>) | ||
| 47 | - buffer streams (stream as an external C buffer) | ||
| 48 | |||
| 49 | A stream has a type and several functions to create, | ||
| 50 | append, delete, close, print, or transform to string" | ||
| 51 | |||
| 52 | uniontype IOStreamType "TODO! change these to X_TYPE" | ||
| 53 | record FILE String name; end FILE; | ||
| 54 | record LIST end LIST; | ||
| 55 | record BUFFER end BUFFER; | ||
| 56 | end IOStreamType; | ||
| 57 | |||
| 58 | uniontype IOStreamData | ||
| 59 | record FILE_DATA | ||
| 60 | Integer data; | ||
| 61 | end FILE_DATA; | ||
| 62 | |||
| 63 | record LIST_DATA | ||
| 64 | list<String> data; | ||
| 65 | end LIST_DATA; | ||
| 66 | |||
| 67 | record BUFFER_DATA | ||
| 68 | Integer data; | ||
| 69 | end BUFFER_DATA; | ||
| 70 | end IOStreamData; | ||
| 71 | |||
| 72 | uniontype IOStream | ||
| 73 | record IOSTREAM | ||
| 74 | String name; | ||
| 75 | IOStreamType ty; | ||
| 76 | IOStreamData data; | ||
| 77 | end IOSTREAM; | ||
| 78 | end IOStream; | ||
| 79 | |||
| 80 | constant Integer stdInput = 0; | ||
| 81 | constant Integer stdOutput = 1; | ||
| 82 | constant Integer stdError = 2; | ||
| 83 | |||
| 84 | constant IOStream emptyStreamOfTypeList = IOSTREAM("emptyStreamOfTypeList", LIST(), LIST_DATA({})); | ||
| 85 | |||
| 86 | protected import IOStreamExt; | ||
| 87 | protected import List; | ||
| 88 | |||
| 89 | public | ||
| 90 | function create | ||
| 91 | input String streamName; | ||
| 92 | input IOStreamType streamType = LIST(); | ||
| 93 | output IOStream outStream; | ||
| 94 | algorithm | ||
| 95 | outStream := match streamType | ||
| 96 | local | ||
| 97 | String fileName; | ||
| 98 | Integer fileID, bufferID; | ||
| 99 | |||
| 100 | case FILE(fileName) | ||
| 101 | algorithm | ||
| 102 | ✗ | fileID := IOStreamExt.createFile(fileName); | |
| 103 | ✗ | then | |
| 104 | IOSTREAM(streamName, streamType, FILE_DATA(fileID)); | ||
| 105 | |||
| 106 | case LIST() | ||
| 107 | 2301 | then | |
| 108 | IOSTREAM(streamName, streamType, LIST_DATA({})); | ||
| 109 | |||
| 110 | case BUFFER() | ||
| 111 | algorithm | ||
| 112 | ✗ | bufferID := IOStreamExt.createBuffer(); | |
| 113 | ✗ | then | |
| 114 | IOSTREAM(streamName, streamType, BUFFER_DATA(bufferID)); | ||
| 115 | end match; | ||
| 116 | end create; | ||
| 117 | |||
| 118 | function append | ||
| 119 | input IOStream inStream; | ||
| 120 | input String inString; | ||
| 121 | output IOStream outStream; | ||
| 122 | algorithm | ||
| 123 | outStream := match inStream | ||
| 124 | local | ||
| 125 | list<String> listData; | ||
| 126 | Integer fileID, bufferID; | ||
| 127 | IOStream fStream, bStream; | ||
| 128 | String streamName; | ||
| 129 | IOStreamType streamType; | ||
| 130 | |||
| 131 | case fStream as IOSTREAM(data = FILE_DATA(fileID)) | ||
| 132 | algorithm | ||
| 133 | ✗ | IOStreamExt.appendFile(fileID, inString); | |
| 134 | then | ||
| 135 | fStream; | ||
| 136 | |||
| 137 | case IOSTREAM(streamName, streamType, LIST_DATA(listData)) | ||
| 138 | 22354 | then | |
| 139 | IOSTREAM(streamName, streamType, LIST_DATA(inString::listData)); | ||
| 140 | |||
| 141 | case bStream as IOSTREAM(data = BUFFER_DATA(bufferID)) | ||
| 142 | algorithm | ||
| 143 | ✗ | IOStreamExt.appendBuffer(bufferID, inString); | |
| 144 | then | ||
| 145 | bStream; | ||
| 146 | end match; | ||
| 147 | end append; | ||
| 148 | |||
| 149 | function appendList | ||
| 150 | input IOStream inStream; | ||
| 151 | input list<String> inStringList; | ||
| 152 | output IOStream outStream; | ||
| 153 | algorithm | ||
| 154 | 80 | outStream := List.foldr(inStringList, append, inStream); | |
| 155 | end appendList; | ||
| 156 | |||
| 157 | function appendListReverse | ||
| 158 | input output IOStream s; | ||
| 159 | input list<String> data; | ||
| 160 | protected | ||
| 161 | IOStreamData s_data = s.data; | ||
| 162 | algorithm | ||
| 163 | () := match s_data | ||
| 164 | case IOStreamData.FILE_DATA() | ||
| 165 | algorithm | ||
| 166 | ✗ | for str in data loop | |
| 167 | ✗ | IOStreamExt.appendFile(s_data.data, str); | |
| 168 | end for; | ||
| 169 | then | ||
| 170 | (); | ||
| 171 | |||
| 172 | case IOStreamData.LIST_DATA() | ||
| 173 | algorithm | ||
| 174 | 17 | s_data.data := listAppend(data, s_data.data); | |
| 175 | 17 | s.data := s_data; | |
| 176 | then | ||
| 177 | (); | ||
| 178 | |||
| 179 | case IOStreamData.BUFFER_DATA() | ||
| 180 | algorithm | ||
| 181 | ✗ | for str in data loop | |
| 182 | ✗ | IOStreamExt.appendBuffer(s_data.data, str); | |
| 183 | end for; | ||
| 184 | then | ||
| 185 | (); | ||
| 186 | end match; | ||
| 187 | end appendListReverse; | ||
| 188 | |||
| 189 | function appendListStream | ||
| 190 | input IOStream srcStream; | ||
| 191 | input output IOStream dstStream; | ||
| 192 | protected | ||
| 193 | list<String> data; | ||
| 194 | algorithm | ||
| 195 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
|
17 | IOStream.IOSTREAM(data = IOStreamData.LIST_DATA(data = data)) := srcStream; |
| 196 | 17 | dstStream := appendListReverse(dstStream, data); | |
| 197 | end appendListStream; | ||
| 198 | |||
| 199 | function close | ||
| 200 | input IOStream inStream; | ||
| 201 | output IOStream outStream; | ||
| 202 | algorithm | ||
| 203 | outStream := matchcontinue inStream | ||
| 204 | local | ||
| 205 | Integer fileID; | ||
| 206 | IOStream fStream; | ||
| 207 | |||
| 208 | case fStream as IOSTREAM(data = FILE_DATA(fileID)) | ||
| 209 | algorithm | ||
| 210 | ✗ | IOStreamExt.closeFile(fileID); | |
| 211 | then | ||
| 212 | fStream; | ||
| 213 | |||
| 214 | // close does nothing for list or buffer streams | ||
| 215 | else inStream; | ||
| 216 | end matchcontinue; | ||
| 217 | end close; | ||
| 218 | |||
| 219 | function delete | ||
| 220 | input IOStream inStream; | ||
| 221 | algorithm | ||
| 222 | () := match inStream | ||
| 223 | local | ||
| 224 | Integer fileID, bufferID; | ||
| 225 | |||
| 226 | case IOSTREAM(data = FILE_DATA(fileID)) | ||
| 227 | algorithm | ||
| 228 | ✗ | IOStreamExt.deleteFile(fileID); | |
| 229 | then | ||
| 230 | (); | ||
| 231 | |||
| 232 | case IOSTREAM(data = LIST_DATA()) | ||
| 233 | then | ||
| 234 | (); | ||
| 235 | |||
| 236 | case IOSTREAM(data = BUFFER_DATA(bufferID)) | ||
| 237 | algorithm | ||
| 238 | ✗ | IOStreamExt.deleteBuffer(bufferID); | |
| 239 | then | ||
| 240 | (); | ||
| 241 | end match; | ||
| 242 | end delete; | ||
| 243 | |||
| 244 | function clear | ||
| 245 | input IOStream inStream; | ||
| 246 | output IOStream outStream; | ||
| 247 | algorithm | ||
| 248 | outStream := matchcontinue inStream | ||
| 249 | local | ||
| 250 | Integer fileID, bufferID; | ||
| 251 | IOStream fStream, bStream; | ||
| 252 | String name; | ||
| 253 | IOStreamData data; | ||
| 254 | IOStreamType ty; | ||
| 255 | |||
| 256 | case fStream as IOSTREAM(data = FILE_DATA(fileID)) | ||
| 257 | algorithm | ||
| 258 | ✗ | IOStreamExt.clearFile(fileID); | |
| 259 | then | ||
| 260 | fStream; | ||
| 261 | |||
| 262 | case IOSTREAM(name, ty, _) | ||
| 263 | ✗ | then | |
| 264 | IOSTREAM(name, ty, LIST_DATA({})); | ||
| 265 | |||
| 266 | case bStream as IOSTREAM(data = BUFFER_DATA(bufferID)) | ||
| 267 | algorithm | ||
| 268 | ✗ | IOStreamExt.clearBuffer(bufferID); | |
| 269 | then | ||
| 270 | bStream; | ||
| 271 | end matchcontinue; | ||
| 272 | end clear; | ||
| 273 | |||
| 274 | function empty | ||
| 275 | input IOStream inStream; | ||
| 276 | output Boolean res; | ||
| 277 | protected | ||
| 278 | IOStreamData data = inStream.data; | ||
| 279 | algorithm | ||
| 280 | res := match data | ||
| 281 | 394 | case LIST_DATA() then listEmpty(data.data); | |
| 282 | end match; | ||
| 283 | end empty; | ||
| 284 | |||
| 285 | function string | ||
| 286 | input IOStream inStream; | ||
| 287 | output String string; | ||
| 288 | algorithm | ||
| 289 | string := match inStream | ||
| 290 | local | ||
| 291 | list<String> listData; | ||
| 292 | Integer fileID, bufferID; | ||
| 293 | String str; | ||
| 294 | |||
| 295 | case IOSTREAM(data = FILE_DATA(fileID)) | ||
| 296 | algorithm | ||
| 297 | ✗ | str := IOStreamExt.readFile(fileID); | |
| 298 | then | ||
| 299 | str; | ||
| 300 | |||
| 301 | case IOSTREAM(data = LIST_DATA(listData)) | ||
| 302 | algorithm | ||
| 303 | 1901 | str := IOStreamExt.appendReversedList(listData); | |
| 304 | then | ||
| 305 | str; | ||
| 306 | |||
| 307 | case IOSTREAM(data = BUFFER_DATA(bufferID)) | ||
| 308 | algorithm | ||
| 309 | ✗ | str := IOStreamExt.readBuffer(bufferID); | |
| 310 | then | ||
| 311 | str; | ||
| 312 | end match; | ||
| 313 | end string; | ||
| 314 | |||
| 315 | function print | ||
| 316 | "@author: adrpo | ||
| 317 | This function will print a string depending on the second argument | ||
| 318 | to the standard output (1) or standard error (2). | ||
| 319 | Use IOStream.stdOutput, IOStream.stdError constants" | ||
| 320 | input IOStream inStream; | ||
| 321 | input Integer whereToPrint; | ||
| 322 | algorithm | ||
| 323 | () := match inStream | ||
| 324 | local | ||
| 325 | list<String> listData; | ||
| 326 | Integer fileID, bufferID; | ||
| 327 | |||
| 328 | case IOSTREAM(data = FILE_DATA(fileID)) | ||
| 329 | algorithm | ||
| 330 | ✗ | IOStreamExt.printFile(fileID, whereToPrint); | |
| 331 | then | ||
| 332 | (); | ||
| 333 | |||
| 334 | case IOSTREAM(data = BUFFER_DATA(bufferID)) | ||
| 335 | algorithm | ||
| 336 | ✗ | IOStreamExt.printBuffer(bufferID, whereToPrint); | |
| 337 | then | ||
| 338 | (); | ||
| 339 | |||
| 340 | case IOSTREAM(data = LIST_DATA(listData)) | ||
| 341 | algorithm | ||
| 342 | 6 | IOStreamExt.printReversedList(listData, whereToPrint); | |
| 343 | then | ||
| 344 | (); | ||
| 345 | |||
| 346 | end match; | ||
| 347 | end print; | ||
| 348 | |||
| 349 | /* | ||
| 350 | TODO! Global Streams to be implemented later | ||
| 351 | IOStream.remember(IOStream, id); | ||
| 352 | IOStream = IOStream.aquire(id); | ||
| 353 | IOStream.forget(IOStream, id); | ||
| 354 | */ | ||
| 355 | |||
| 356 | annotation(__OpenModelica_Interface="util"); | ||
| 357 | end IOStream; | ||
| 358 |