OMCompiler/SimulationRuntime/cpp/Core/Utils/Modelica/ModelicaUtilities.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file belongs to the OpenModelica Run-Time System | ||
| 3 | * | ||
| 4 | * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC), c/o Linköpings | ||
| 5 | * universitet, Department of Computer and Information Science, SE-58183 Linköping, Sweden. All rights | ||
| 6 | * reserved. | ||
| 7 | * | ||
| 8 | * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THE BSD NEW LICENSE OR THE | ||
| 9 | * AGPL VERSION 3 LICENSE OR THE OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8. ANY | ||
| 10 | * USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S | ||
| 11 | * ACCEPTANCE OF THE BSD NEW LICENSE OR THE OSMC PUBLIC LICENSE OR THE AGPL | ||
| 12 | * VERSION 3, ACCORDING TO RECIPIENTS CHOICE. | ||
| 13 | * | ||
| 14 | * The OpenModelica software and the OSMC (Open Source Modelica Consortium) Public License | ||
| 15 | * (OSMC-PL) are obtained from OSMC, either from the above address, from the URLs: | ||
| 16 | * http://www.openmodelica.org or https://github.com/OpenModelica/ or | ||
| 17 | * http://www.ida.liu.se/projects/OpenModelica, and in the OpenModelica distribution. GNU | ||
| 18 | * AGPL version 3 is obtained from: https://www.gnu.org/licenses/licenses.html#GPL. The BSD NEW | ||
| 19 | * License is obtained from: http://www.opensource.org/licenses/BSD-3-Clause. | ||
| 20 | * | ||
| 21 | * This program is distributed WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY | ||
| 23 | * SET FORTH IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF | ||
| 24 | * OSMC-PL. | ||
| 25 | * | ||
| 26 | */ | ||
| 27 | |||
| 28 | /** @addtogroup coreUtils | ||
| 29 | * | ||
| 30 | * @{ | ||
| 31 | */ | ||
| 32 | #include <Core/ModelicaDefine.h> | ||
| 33 | #include <Core/Modelica.h> | ||
| 34 | #include <ModelicaUtilities.h> | ||
| 35 | #include <stdexcept> | ||
| 36 | #include <exception> | ||
| 37 | #include <string> | ||
| 38 | #include <stdio.h> | ||
| 39 | #include <stdarg.h> | ||
| 40 | #include <sstream> | ||
| 41 | #ifdef __cplusplus | ||
| 42 | extern "C" { | ||
| 43 | #endif | ||
| 44 | |||
| 45 | ✗ | void ModelicaMessage(const char* string) | |
| 46 | { | ||
| 47 | ✗ | fprintf(stdout, "%s", string); | |
| 48 | ✗ | fflush(stdout); | |
| 49 | ✗ | } | |
| 50 | |||
| 51 | 1 | void ModelicaVFormatMessage(const char* string, va_list args) | |
| 52 | { | ||
| 53 | 1 | vfprintf(stdout, string, args); | |
| 54 | 1 | fflush(stdout); | |
| 55 | 1 | } | |
| 56 | |||
| 57 | 1 | void ModelicaFormatMessage(const char* string, ...) | |
| 58 | { | ||
| 59 | va_list args; | ||
| 60 | 1 | va_start(args, string); | |
| 61 | 1 | ModelicaVFormatMessage(string, args); | |
| 62 | 1 | va_end(args); | |
| 63 | 1 | } | |
| 64 | |||
| 65 | ✗ | void ModelicaWarning(const char* string) | |
| 66 | { | ||
| 67 | ✗ | fprintf(stderr, "%s", string); | |
| 68 | ✗ | } | |
| 69 | |||
| 70 | ✗ | void ModelicaVFormatWarning(const char* string, va_list args) | |
| 71 | { | ||
| 72 | ✗ | vfprintf(stderr, string, args); | |
| 73 | ✗ | } | |
| 74 | |||
| 75 | ✗ | void ModelicaFormatWarning(const char* string, ...) | |
| 76 | { | ||
| 77 | va_list args; | ||
| 78 | ✗ | va_start(args, string); | |
| 79 | ✗ | ModelicaVFormatWarning(string, args); | |
| 80 | ✗ | va_end(args); | |
| 81 | ✗ | } | |
| 82 | |||
| 83 | ✗ | void ModelicaError(const char* string) | |
| 84 | { | ||
| 85 | ✗ | throw ModelicaSimulationError(UTILITY, string); | |
| 86 | } | ||
| 87 | |||
| 88 | ✗ | void ModelicaVFormatError(const char* string, va_list args) | |
| 89 | { | ||
| 90 | char buffer[256]; | ||
| 91 | vsnprintf(buffer, 256, string, args); | ||
| 92 | ✗ | ModelicaError(buffer); | |
| 93 | ✗ | } | |
| 94 | |||
| 95 | ✗ | void ModelicaFormatError(const char* string, ...) | |
| 96 | { | ||
| 97 | va_list args; | ||
| 98 | ✗ | va_start(args, string); | |
| 99 | ✗ | ModelicaVFormatError(string, args); | |
| 100 | ✗ | va_end(args); | |
| 101 | ✗ | } | |
| 102 | |||
| 103 | static std::map<const char*, char*> _allocatedStrings; | ||
| 104 | |||
| 105 | 137 | char* ModelicaAllocateString(size_t len) | |
| 106 | { | ||
| 107 | 137 | char *res = new char[len + 1]; | |
| 108 | if (!res) | ||
| 109 | ModelicaFormatError("%s:%d: ModelicaAllocateString failed", __FILE__, __LINE__); | ||
| 110 | 137 | _allocatedStrings[res] = res; | |
| 111 | 137 | res[len] = '\0'; | |
| 112 | 137 | return res; | |
| 113 | } | ||
| 114 | |||
| 115 | 15 | char* ModelicaAllocateStringWithErrorReturn(size_t len) | |
| 116 | { | ||
| 117 | 15 | char *res = new char[len + 1]; | |
| 118 | if (res) { | ||
| 119 | 15 | _allocatedStrings[res] = res; | |
| 120 | 15 | res[len] = '\0'; | |
| 121 | } | ||
| 122 | 15 | return res; | |
| 123 | } | ||
| 124 | |||
| 125 | 138 | void _ModelicaFreeStringIfAllocated(const char *str) | |
| 126 | { | ||
| 127 | std::map<const char*, char*>::iterator it; | ||
| 128 | it = _allocatedStrings.find(str); | ||
| 129 |
1/2✓ Branch 0 taken 138 times.
✗ Branch 1 not taken.
|
138 | if (it != _allocatedStrings.end()) { |
| 130 |
1/2✓ Branch 1 taken 138 times.
✗ Branch 2 not taken.
|
138 | delete [] _allocatedStrings[str]; |
| 131 | _allocatedStrings.erase(it); | ||
| 132 | } | ||
| 133 | 138 | } | |
| 134 | |||
| 135 | #ifdef __cplusplus | ||
| 136 | } | ||
| 137 | #endif | ||
| 138 | /** @} */ // end of coreUtils | ||
| 139 |