OMCompiler/SimulationRuntime/c/util/utility.c
| 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 | |||
| 29 | #include "../ModelicaUtilities.h" | ||
| 30 | #include "utility.h" | ||
| 31 | #include "omc_init.h" | ||
| 32 | #include "modelica_string.h" | ||
| 33 | #if defined(OMC_METAMODELICA_RUNTIME) | ||
| 34 | /* The string vocabulary of this runtime; util/omc_string.h stands down here. */ | ||
| 35 | #include "../meta/meta_modelica_string.h" | ||
| 36 | #else | ||
| 37 | #include "omc_box.h" | ||
| 38 | #endif | ||
| 39 | #include "omc_file.h" | ||
| 40 | #include "omc_strdup.h" | ||
| 41 | #include "../simulation_data.h" | ||
| 42 | #include "../simulation/options.h" | ||
| 43 | #include <string.h> | ||
| 44 | #include <errno.h> | ||
| 45 | #include <float.h> | ||
| 46 | #include <limits.h> | ||
| 47 | #include <limits.h> | ||
| 48 | |||
| 49 | 21821176 | modelica_real real_int_pow(threadData_t *threadData, modelica_real base, modelica_integer n) | |
| 50 | { | ||
| 51 | modelica_real result = 1.0; | ||
| 52 | modelica_integer m = n < 0; | ||
| 53 |
2/2✓ Branch 0 taken 8239715 times.
✓ Branch 1 taken 13581461 times.
|
21821176 | if(m) |
| 54 | { | ||
| 55 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8239715 times.
|
8239715 | if(base == 0.0) |
| 56 | ✗ | omc_assert(threadData, omc_dummyFileInfo, "Model error. 0^(%i) is not defined", n); | |
| 57 | 8239715 | n = -n; | |
| 58 | } | ||
| 59 |
2/2✓ Branch 0 taken 64414075 times.
✓ Branch 1 taken 21821176 times.
|
86235251 | while(n != 0) |
| 60 | { | ||
| 61 |
2/2✓ Branch 0 taken 36119042 times.
✓ Branch 1 taken 28295033 times.
|
64414075 | if((n % 2) != 0) |
| 62 | { | ||
| 63 | 36119042 | result *= base; | |
| 64 | 36119042 | n--; | |
| 65 | } | ||
| 66 | 64414075 | base *= base; | |
| 67 | 64414075 | n /= 2; | |
| 68 | } | ||
| 69 |
2/2✓ Branch 0 taken 8239715 times.
✓ Branch 1 taken 13581461 times.
|
21821176 | return m ? (1 / result) : result; |
| 70 | } | ||
| 71 | |||
| 72 | #if !defined(OMC_MINIMAL_RUNTIME) | ||
| 73 | |||
| 74 | #include <regex.h> | ||
| 75 | |||
| 76 | 2826105 | extern int OpenModelica_regexImpl(const char* str, const char* re, const int maxn, int extended, int ignoreCase, void*(*mystrdup)(const char*), void **outMatches) | |
| 77 | { | ||
| 78 | regex_t myregex; | ||
| 79 | int nmatch=0,i,rc,res; | ||
| 80 |
4/4✓ Branch 0 taken 2826104 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1198 times.
✓ Branch 3 taken 2824907 times.
|
5652209 | int flags = (extended ? REG_EXTENDED : 0) | (ignoreCase ? REG_ICASE : 0) | (maxn ? 0 : REG_NOSUB); |
| 81 | regmatch_t *matches; | ||
| 82 | 2826105 | matches = (regmatch_t*)malloc(maxn*sizeof(regmatch_t)); | |
| 83 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2826105 times.
|
2826105 | assert(matches != NULL); |
| 84 | |||
| 85 | memset(&myregex, 1, sizeof(regex_t)); | ||
| 86 | 2826105 | rc = regcomp(&myregex, re, flags); | |
| 87 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2826104 times.
|
2826105 | if (rc && maxn == 0) { |
| 88 | 1 | free(matches); | |
| 89 | 1 | return 0; | |
| 90 | } | ||
| 91 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2826104 times.
|
2826104 | if (rc) { |
| 92 | ✗ | char err_buf[2048] = {0}; | |
| 93 | int len = 0; | ||
| 94 | len += snprintf(err_buf+len,2040-len,"Failed to compile regular expression: %s with error: ", re); | ||
| 95 | ✗ | regerror(rc, &myregex, err_buf+len, 2048-len); | |
| 96 | ✗ | regfree(&myregex); | |
| 97 | ✗ | if (maxn) { | |
| 98 | ✗ | outMatches[0] = mystrdup(err_buf); | |
| 99 | ✗ | for (i=1; i<maxn; i++) | |
| 100 | ✗ | outMatches[i] = mystrdup(""); | |
| 101 | } | ||
| 102 | |||
| 103 | ✗ | free(matches); | |
| 104 | return 0; | ||
| 105 | } | ||
| 106 | 2826104 | res = regexec(&myregex, str, maxn, matches, 0); | |
| 107 |
2/2✓ Branch 0 taken 1197 times.
✓ Branch 1 taken 2824907 times.
|
2826104 | if (!maxn) |
| 108 | 1197 | nmatch += res == 0 ? 1 : 0; | |
| 109 | else if (maxn) { | ||
| 110 | 2824907 | char *dup = omc_strdup(str); | |
| 111 |
2/2✓ Branch 0 taken 18252464 times.
✓ Branch 1 taken 2824907 times.
|
21077371 | for (i=0; i<maxn; i++) { |
| 112 |
4/4✓ Branch 0 taken 12642672 times.
✓ Branch 1 taken 5609792 times.
✓ Branch 2 taken 4229484 times.
✓ Branch 3 taken 8413188 times.
|
18252464 | if (!res && matches[i].rm_so != -1) { |
| 113 | 4229484 | memcpy(dup, str + matches[i].rm_so, matches[i].rm_eo - matches[i].rm_so); | |
| 114 | 4229484 | dup[matches[i].rm_eo - matches[i].rm_so] = '\0'; | |
| 115 | 4229484 | outMatches[nmatch++] = mystrdup(dup); | |
| 116 | } | ||
| 117 | } | ||
| 118 |
2/2✓ Branch 0 taken 14022980 times.
✓ Branch 1 taken 2824907 times.
|
16847887 | for (i=nmatch; i<maxn; i++) { |
| 119 | 14022980 | outMatches[i] = mystrdup(""); | |
| 120 | } | ||
| 121 | 2824907 | free(dup); | |
| 122 | } | ||
| 123 | |||
| 124 | 2826104 | regfree(&myregex); | |
| 125 | 2826104 | free(matches); | |
| 126 | |||
| 127 | 2826104 | return nmatch; | |
| 128 | } | ||
| 129 | |||
| 130 | 3 | static char* Modelica_strdup(const char *str) | |
| 131 | { | ||
| 132 | 3 | char *res = ModelicaAllocateString(strlen(str)); | |
| 133 | strcpy(res, str); | ||
| 134 | 3 | return res; | |
| 135 | } | ||
| 136 | |||
| 137 | 1 | extern int OpenModelica_regex(const char* str, const char* re, int maxn, int extended, int sensitive, const char **outMatches) | |
| 138 | { | ||
| 139 | 1 | return OpenModelica_regexImpl(str,re,maxn,extended,sensitive,(void*(*)(const char*)) Modelica_strdup,(void**)outMatches); | |
| 140 | } | ||
| 141 | |||
| 142 | #endif /* OMC_MINIMAL_RUNTIME */ | ||
| 143 | |||
| 144 | /* TODO: What is the ifdef for filesystem availability? */ | ||
| 145 | |||
| 146 | 7224 | void OpenModelica_updateUriMapping(threadData_t *threadData, void *namesAndDirs) | |
| 147 | { | ||
| 148 | int i; | ||
| 149 | 7224 | threadData->localRoots[LOCAL_ROOT_URI_LOOKUP] = namesAndDirs; /* This should keep the names from being garbage collected */ | |
| 150 | 7224 | } | |
| 151 | |||
| 152 | #include <sys/stat.h> | ||
| 153 | |||
| 154 | #if defined(_MSC_VER) | ||
| 155 | #define stat _stat | ||
| 156 | #else | ||
| 157 | #include <unistd.h> | ||
| 158 | #endif | ||
| 159 | |||
| 160 | static const char *PATH_NOT_IN_FMU_RESOURCES = "Returning path (%s) not in the resources directory. The FMU might not work as expected if you send it to a different system"; | ||
| 161 | |||
| 162 | /* if uri starts with X:\ or X:/ */ | ||
| 163 | ✗ | static int hasDriveLetter(const char* uri) | |
| 164 | { | ||
| 165 | ✗ | return strlen(uri) > 2 && | |
| 166 | ✗ | ((uri[0] >= 'A' && uri[0] <= 'Z') || (uri[0] >= 'a' && uri[0] <= 'z')) && | |
| 167 | ✗ | uri[1]==':' && (uri[2] == '/' || uri[2] == '\\'); | |
| 168 | } | ||
| 169 | |||
| 170 | 2142 | static modelica_string uriToFilenameRegularPaths(modelica_string uri_om, const char *uri, char buf[PATH_MAX], const char *origUri, const char *resourcesDir) | |
| 171 | { | ||
| 172 | omc_stat_t stat_buf; | ||
| 173 | size_t len, i, j = 0; | ||
| 174 | 2142 | int uriExists = 0==omc_stat(uri, &stat_buf); | |
| 175 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2142 times.
|
2142 | if (resourcesDir) { |
| 176 | ✗ | if (strlen(resourcesDir)+strlen(uri)+2 < PATH_MAX) { | |
| 177 | ✗ | if (hasDriveLetter(uri)) { | |
| 178 | sprintf(buf, "%s/", resourcesDir); | ||
| 179 | ✗ | len = strlen(buf); | |
| 180 | ✗ | for (i = 0; i < strlen(uri); i++) | |
| 181 | ✗ | if (uri[i] != ':') | |
| 182 | { | ||
| 183 | ✗ | buf[len+j] = (uri[i] == '\\') ? '/' : uri[i]; | |
| 184 | ✗ | j++; | |
| 185 | } | ||
| 186 | ✗ | buf[len+j]='\0'; | |
| 187 | } else { | ||
| 188 | sprintf(buf, "%s/%s", resourcesDir, uri); | ||
| 189 | } | ||
| 190 | ✗ | if (!uriExists || 0==omc_stat(buf, &stat_buf)) { | |
| 191 | /* The path with resources prepended either exists or the path without resources does not exist | ||
| 192 | * So re-run uriToFilenameRegularPaths with resourcesDir prepended to the URI | ||
| 193 | */ | ||
| 194 | char newbuf[PATH_MAX]; | ||
| 195 | ✗ | return uriToFilenameRegularPaths(NULL, buf, newbuf, origUri, NULL); | |
| 196 | } | ||
| 197 | } else { | ||
| 198 | ✗ | omc_assert_warning(omc_dummyFileInfo, "Path longer than PATH_MAX: %s/%s", resourcesDir, uri); | |
| 199 | } | ||
| 200 | } | ||
| 201 |
2/2✓ Branch 0 taken 272 times.
✓ Branch 1 taken 1870 times.
|
2142 | if (uriExists) { |
| 202 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 272 times.
|
272 | if (resourcesDir) { |
| 203 | ✗ | omc_assert_warning(omc_dummyFileInfo, PATH_NOT_IN_FMU_RESOURCES, uri); | |
| 204 | } | ||
| 205 | /* This is a file, directory, etc. Can't use open to check this. */ | ||
| 206 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 272 times.
|
272 | if (0==realpath(uri, buf)) { |
| 207 | /* Unexpected; we know the file exists, but realpath failed. Just return the URI */ | ||
| 208 | ✗ | omc_assert_warning(omc_dummyFileInfo, "realpath failed for existing path %s: %s", uri, strerror(errno)); | |
| 209 | ✗ | return uri_om ? omc_string_retain(uri_om) : omc_string_new(uri); | |
| 210 | } | ||
| 211 | /* Use the realpath result */ | ||
| 212 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 272 times.
|
272 | if (S_ISDIR(stat_buf.st_mode)) { |
| 213 | /* Make directories end with a / if the original URI ends with a / */ | ||
| 214 | ✗ | len = strlen(buf); | |
| 215 | ✗ | if (buf[len-1]!='/' && origUri[strlen(origUri)-1]=='/') { | |
| 216 | ✗ | if (len+1 >= PATH_MAX) { | |
| 217 | /* Can't fit the path; just return the original URI */ | ||
| 218 | ✗ | omc_assert_warning(omc_dummyFileInfo, "Path longer than PATH_MAX: %s/, returning %s", buf, buf); | |
| 219 | ✗ | return uri_om ? omc_string_retain(uri_om) : omc_string_new(uri); | |
| 220 | } | ||
| 221 | ✗ | strcpy(buf+len, "/"); | |
| 222 | } | ||
| 223 | } | ||
| 224 |
4/4✓ Branch 0 taken 16 times.
✓ Branch 1 taken 256 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 14 times.
|
272 | return (0==strcmp(uri, buf) && uri_om) ? omc_string_retain(uri_om) : omc_string_new(buf); |
| 225 | } | ||
| 226 | |||
| 227 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1870 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
1870 | if (uri[0]=='/' || hasDriveLetter(uri)) { |
| 228 | /* Absolute path */ | ||
| 229 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1870 times.
|
1870 | return uri_om ? omc_string_retain(uri_om) : omc_string_new(uri); |
| 230 | } | ||
| 231 | ✗ | if (0==realpath("./", buf)) { | |
| 232 | /* Failed to resolve ./ */ | ||
| 233 | ✗ | omc_assert_warning(omc_dummyFileInfo, "realpath failed to resolve ./"); | |
| 234 | ✗ | return uri_om ? omc_string_retain(uri_om) : omc_string_new(uri); | |
| 235 | } | ||
| 236 | ✗ | len = strlen(buf); | |
| 237 | ✗ | if (len+strlen(uri)+1 >= PATH_MAX) { | |
| 238 | /* Can't fit the path; just return the original URI */ | ||
| 239 | ✗ | omc_assert_warning(omc_dummyFileInfo, "Path longer than PATH_MAX: %s/%s, returning %s", buf, uri, uri); | |
| 240 | ✗ | return uri_om ? omc_string_retain(uri_om) : omc_string_new(uri); | |
| 241 | } | ||
| 242 | /* Copy the rest of the URI onto the buffer */ | ||
| 243 | ✗ | if (buf[len-1]!='/') { | |
| 244 | ✗ | buf[len++]='/'; | |
| 245 | } | ||
| 246 | ✗ | strcpy(buf+len, uri); | |
| 247 | ✗ | return omc_string_new(buf); | |
| 248 | } | ||
| 249 | |||
| 250 | /* (name, directory) pairs sorted by name, for modelica:// URIs: the class list | ||
| 251 | omc hands over from its symbol table, or the literal the code generator bakes | ||
| 252 | into a simulation. Both are an array box, and differ only in where its length | ||
| 253 | comes from. */ | ||
| 254 | #if defined(OMC_METAMODELICA_RUNTIME) | ||
| 255 | #define OMC_URI_TABLE_LEN(X) MMC_HDRSLOTS(MMC_GETHDR(X)) | ||
| 256 | #define OMC_URI_TABLE_DATA(X) ((void**) MMC_STRUCTDATA(X)) | ||
| 257 | #define OMC_URI_NAME_DATA(X) MMC_STRINGDATA(X) | ||
| 258 | #else | ||
| 259 | #define OMC_URI_TABLE_LEN(X) OMC_BOX_ARRAY_LEN(X) | ||
| 260 | #define OMC_URI_TABLE_DATA(X) (&OMC_BOX_FIELD((X), 1)) | ||
| 261 | #define OMC_URI_NAME_DATA(X) omc_string_data((modelica_string)(X)) | ||
| 262 | #endif | ||
| 263 | |||
| 264 | 2050 | static int findString(const void *name, const void *entry) | |
| 265 | { | ||
| 266 | 2050 | return strcmp((const char *) name, OMC_URI_NAME_DATA(((void**)entry)[0])); | |
| 267 | } | ||
| 268 | |||
| 269 | 1078 | static modelica_string lookupDirectoryFromName(const char *name, void *nameDirArray) | |
| 270 | { | ||
| 271 | size_t len; | ||
| 272 | void **strs, **obj; | ||
| 273 | |||
| 274 |
1/2✓ Branch 0 taken 1078 times.
✗ Branch 1 not taken.
|
1078 | if (!nameDirArray) { |
| 275 | return NULL; | ||
| 276 | } | ||
| 277 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1076 times.
|
1078 | len = OMC_URI_TABLE_LEN(nameDirArray); |
| 278 | 1076 | strs = OMC_URI_TABLE_DATA(nameDirArray); | |
| 279 | 1078 | obj = bsearch(name, strs, len/2, 2*sizeof(void*), findString); | |
| 280 |
1/2✓ Branch 0 taken 1078 times.
✗ Branch 1 not taken.
|
1078 | return obj ? (modelica_string) obj[1] : NULL; |
| 281 | } | ||
| 282 | |||
| 283 | static void getIdent(char *str, char *this, char **next) | ||
| 284 | { | ||
| 285 |
3/8✓ Branch 0 taken 9653 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8575 times.
✓ Branch 3 taken 1078 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
9653 | while (*str != 0 && *str != '.' && *str != '/') { |
| 286 | 8575 | *(this++) = *(str++); | |
| 287 | } | ||
| 288 | 1078 | *this = '\0'; | |
| 289 | *next = str; | ||
| 290 | } | ||
| 291 | |||
| 292 | 1078 | extern void OpenModelica_decode_uri_inplace(char *src) | |
| 293 | { | ||
| 294 | char *tmp = src; | ||
| 295 |
2/2✓ Branch 0 taken 47555 times.
✓ Branch 1 taken 1078 times.
|
48633 | while (*src) { |
| 296 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 47555 times.
|
47555 | if (*src == '+') *(tmp++) = ' '; |
| 297 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 47555 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
47555 | else if (*src == '%' && src[1]) { |
| 298 | char buf[3]; | ||
| 299 | int i; | ||
| 300 | ✗ | buf[0] = src[1]; | |
| 301 | ✗ | buf[1] = src[2]; | |
| 302 | ✗ | buf[2] = '\0'; | |
| 303 | ✗ | errno = 0; | |
| 304 | ✗ | i = strtol(buf,NULL,16); | |
| 305 | ✗ | if (errno) { | |
| 306 | ✗ | *(tmp++) = *src; | |
| 307 | ✗ | errno = 0; | |
| 308 | } else { | ||
| 309 | ✗ | *(tmp++) = i; | |
| 310 | ✗ | *tmp = 0; | |
| 311 | ✗ | src += 2; | |
| 312 | } | ||
| 313 | 47555 | } else *(tmp++) = *src; | |
| 314 | 47555 | src++; | |
| 315 | } | ||
| 316 | 1078 | *tmp = '\0'; | |
| 317 | 1078 | } | |
| 318 | |||
| 319 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2142 | extern modelica_string OpenModelica_uriToFilename_impl(threadData_t *threadData, modelica_string uri_om, const char *resourcesDir) |
| 320 | { | ||
| 321 | #if defined(_MSC_VER) | ||
| 322 | #define strncasecmp _strnicmp | ||
| 323 | #endif | ||
| 324 | |||
| 325 | char buf[PATH_MAX]; | ||
| 326 | |||
| 327 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2142 times.
|
2142 | if (omc_string_len(uri_om) <= 0) { |
| 328 | ✗ | omc_assert(threadData, omc_dummyFileInfo, "Malformed URI (got an empty string)"); | |
| 329 | ✗ | OMC_THROW(); | |
| 330 | } | ||
| 331 | |||
| 332 | 2142 | char* uri = (char*)omc_alloc_interface.malloc_atomic(sizeof(char) * (omc_string_len(uri_om)+1)); | |
| 333 |
2/2✓ Branch 0 taken 1076 times.
✓ Branch 1 taken 1064 times.
|
2140 | strcpy(uri, omc_string_data(uri_om)); |
| 334 | |||
| 335 | modelica_string dir; | ||
| 336 |
2/2✓ Branch 0 taken 1078 times.
✓ Branch 1 taken 1064 times.
|
2142 | if (0==strncasecmp(uri, "modelica://", 11)) { |
| 337 | omc_stat_t stat_buf; | ||
| 338 | 1078 | uri += 11; | |
| 339 | getIdent(uri, buf, &uri); | ||
| 340 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1078 times.
|
1078 | if (0 == *buf) { |
| 341 | ✗ | omc_assert(threadData, omc_dummyFileInfo, "Malformed URI (couldn't get a class name): %s", omc_string_data(uri_om)); | |
| 342 | ✗ | OMC_THROW(); | |
| 343 | } | ||
| 344 | 1078 | dir = lookupDirectoryFromName(buf, threadData->localRoots[LOCAL_ROOT_URI_LOOKUP]); | |
| 345 |
2/4✓ Branch 0 taken 1078 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1078 times.
|
1078 | if (dir==NULL || omc_string_len(dir)==0) { |
| 346 | ✗ | omc_assert(threadData, omc_dummyFileInfo, "Failed to lookup URI (is the package loaded?) %s", omc_string_data(uri_om)); | |
| 347 | ✗ | OMC_THROW(); | |
| 348 | } | ||
| 349 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1078 times.
|
1078 | if (resourcesDir) { |
| 350 | ✗ | if (omc_string_len(dir)+2+strlen(resourcesDir) >= PATH_MAX) { | |
| 351 | ✗ | omc_assert_warning(omc_dummyFileInfo, "Path longer than PATH_MAX: %s/%s, ignoring the resourcesDir", omc_string_data(dir), resourcesDir); | |
| 352 | } else { | ||
| 353 | ✗ | int dirExists = 0==omc_stat(omc_string_data(dir), &stat_buf); | |
| 354 | sprintf(buf, "%s/%s", omc_string_data(dir), resourcesDir); | ||
| 355 | ✗ | if (!dirExists || 0==omc_stat(buf, &stat_buf)) { | |
| 356 | ✗ | dir = omc_string_new(buf); | |
| 357 | } else { | ||
| 358 | ✗ | omc_assert_warning(omc_dummyFileInfo, PATH_NOT_IN_FMU_RESOURCES, omc_string_data(dir)); | |
| 359 | } | ||
| 360 | } | ||
| 361 | } | ||
| 362 | 1078 | OpenModelica_decode_uri_inplace(uri); | |
| 363 | /* We found where the package is stored */ | ||
| 364 | while (1) { | ||
| 365 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1078 times.
|
1078 | if (*uri == '.') { |
| 366 | ✗ | uri++; | |
| 367 | } else { | ||
| 368 | break; | ||
| 369 | } | ||
| 370 | getIdent(uri, buf, &uri); | ||
| 371 | ✗ | if (0 == *buf) { | |
| 372 | ✗ | if (*uri == '.') { | |
| 373 | ✗ | omc_assert(threadData, omc_dummyFileInfo, "Malformed URI (double dot in class name): %s", omc_string_data(uri_om)); | |
| 374 | ✗ | OMC_THROW(); | |
| 375 | } | ||
| 376 | break; /* / or end of string */ | ||
| 377 | } | ||
| 378 | ✗ | if (omc_string_len(dir)+strlen(buf)+1 >= PATH_MAX) { | |
| 379 | ✗ | omc_assert(threadData, omc_dummyFileInfo, "Failed to resolve URI; path longer than PATH_MAX(%d): %s", PATH_MAX, omc_string_data(uri_om)); | |
| 380 | ✗ | OMC_THROW(); | |
| 381 | } | ||
| 382 | /* Move the found ident last in the path */ | ||
| 383 | ✗ | memmove(buf+omc_string_len(dir)+1, buf, strlen(buf)+1); | |
| 384 | /* Copy the old directory in there */ | ||
| 385 | ✗ | strcpy(buf, omc_string_data(dir)); | |
| 386 | ✗ | buf[omc_string_len(dir)]='/'; | |
| 387 | ✗ | if (!(0==omc_stat(buf, &stat_buf) && S_ISDIR(stat_buf.st_mode))) { | |
| 388 | break; | ||
| 389 | } | ||
| 390 | ✗ | dir = omc_string_new(buf); | |
| 391 | } | ||
| 392 |
2/4✓ Branch 0 taken 1078 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1078 times.
|
1078 | while (*uri && *(uri++) != '/') /* Ignore */; |
| 393 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1078 times.
|
1078 | if (0 == strlen(uri)) { |
| 394 | /* realpath, etc */ | ||
| 395 | ✗ | return uriToFilenameRegularPaths(dir, omc_string_data(dir), buf, omc_string_data(uri_om), NULL); | |
| 396 | } | ||
| 397 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1078 times.
|
1078 | if (omc_string_len(dir)+strlen(uri-1) >= PATH_MAX) { |
| 398 | ✗ | return omc_string_empty; | |
| 399 | } | ||
| 400 | 1076 | strcpy(buf, omc_string_data(dir)); | |
| 401 | 1078 | strcpy(buf+omc_string_len(dir), uri-1); | |
| 402 | 1078 | dir = omc_string_new(buf); | |
| 403 | 1078 | return uriToFilenameRegularPaths(dir, omc_string_data(dir), buf, omc_string_data(uri_om), NULL); | |
| 404 | } | ||
| 405 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1064 times.
|
1064 | if (0==strncasecmp(uri, "file://", 7)) { |
| 406 | ✗ | return uriToFilenameRegularPaths(NULL, uri+7, buf, omc_string_data(uri_om), resourcesDir); | |
| 407 | } | ||
| 408 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1064 times.
|
1064 | if (strstr(uri, "://")) { |
| 409 | ✗ | omc_assert(threadData, omc_dummyFileInfo, "Unknown URI schema: %s", omc_string_data(uri_om)); | |
| 410 | ✗ | OMC_THROW(); | |
| 411 | } | ||
| 412 | |||
| 413 | 1064 | return uriToFilenameRegularPaths(uri_om, uri, buf, omc_string_data(uri_om), resourcesDir); | |
| 414 | } | ||
| 415 | |||
| 416 | /* TODO: Remove this function after @sjoelund is done prototyping */ | ||
| 417 | ✗ | extern void uriToFilename(threadData_t *threadData) | |
| 418 | { | ||
| 419 | ✗ | abort(); | |
| 420 | } | ||
| 421 | |||
| 422 | 38 | modelica_real realMaxLit(void) | |
| 423 | { | ||
| 424 | 38 | return DBL_MAX / 2048; /* in case some non-linear or ODE solver tries to add eps to this value */ | |
| 425 | } | ||
| 426 | |||
| 427 | 30742 | modelica_integer intMaxLit(void) | |
| 428 | { | ||
| 429 | 30742 | return LONG_MAX / 2; | |
| 430 | } | ||
| 431 |