OMCompiler/SimulationRuntime/c/util/omc_str_utils.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 | /* Plain C string helpers: no Modelica or MetaModelica string type involved, so | ||
| 30 | both runtimes use them. */ | ||
| 31 | |||
| 32 | #include "omc_str_utils.h" | ||
| 33 | #include "../gc/omc_gc.h" | ||
| 34 | #include <stdio.h> | ||
| 35 | #include <stdlib.h> | ||
| 36 | #include <string.h> | ||
| 37 | |||
| 38 | 544664 | extern int omc__escapedStringLength(const char* str, int nl, int *hasEscape) | |
| 39 | { | ||
| 40 | int i=0; | ||
| 41 |
2/2✓ Branch 0 taken 26373764 times.
✓ Branch 1 taken 544664 times.
|
26918428 | while(*str) { |
| 42 |
3/3✓ Branch 0 taken 90464 times.
✓ Branch 1 taken 81651 times.
✓ Branch 2 taken 26201649 times.
|
26373764 | switch (*str) { |
| 43 | 90464 | case '"': | |
| 44 | case '\\': | ||
| 45 | case '\a': | ||
| 46 | case '\b': | ||
| 47 | case '\f': | ||
| 48 | 90464 | case '\v': i++; *hasEscape=1; break; | |
| 49 | 81651 | case '\r': | |
| 50 |
2/2✓ Branch 0 taken 1737 times.
✓ Branch 1 taken 79914 times.
|
81651 | case '\n': if(nl) {i++; *hasEscape=1;} break; |
| 51 | default: break; | ||
| 52 | } | ||
| 53 | 26373764 | i++; | |
| 54 | 26373764 | str++; | |
| 55 | } | ||
| 56 | 544664 | return i; | |
| 57 | } | ||
| 58 | |||
| 59 | /* "\b",_ => "\\b" | ||
| 60 | * "\n",true => "\\n" | ||
| 61 | */ | ||
| 62 | 544106 | extern char* omc__escapedString(const char* str, int nl) | |
| 63 | { | ||
| 64 | int len; | ||
| 65 | char *res; | ||
| 66 | const char *origstr = str; | ||
| 67 | int i=0; | ||
| 68 | 544106 | int hasEscape = 0; | |
| 69 | 544106 | len = omc__escapedStringLength(str,nl,&hasEscape); | |
| 70 |
2/2✓ Branch 0 taken 4227 times.
✓ Branch 1 taken 539879 times.
|
544106 | if (!hasEscape) { |
| 71 | return NULL; | ||
| 72 | } | ||
| 73 | 4227 | res = (char*) omc_alloc_interface.malloc_atomic(len+1); | |
| 74 |
2/2✓ Branch 0 taken 3315169 times.
✓ Branch 1 taken 4227 times.
|
3319396 | while(*str) { |
| 75 |
9/9✓ Branch 0 taken 86094 times.
✓ Branch 1 taken 4362 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 8 times.
✓ Branch 7 taken 61849 times.
✓ Branch 8 taken 3162848 times.
|
3315169 | switch (*str) { |
| 76 | 86094 | case '"': res[i++] = '\\'; res[i++] = '"'; break; | |
| 77 | 4362 | case '\\': res[i++] = '\\'; res[i++] = '\\'; break; | |
| 78 | 1 | case '\a': res[i++] = '\\'; res[i++] = 'a'; break; | |
| 79 | 1 | case '\b': res[i++] = '\\'; res[i++] = 'b'; break; | |
| 80 | 3 | case '\f': res[i++] = '\\'; res[i++] = 'f'; break; | |
| 81 | 3 | case '\v': res[i++] = '\\'; res[i++] = 'v'; break; | |
| 82 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | case '\r': if(nl) {res[i++] = '\\'; res[i++] = 'r';} else {res[i++] = *str;} break; |
| 83 |
2/2✓ Branch 0 taken 1729 times.
✓ Branch 1 taken 60120 times.
|
61849 | case '\n': if(nl) {res[i++] = '\\'; res[i++] = 'n';} else {res[i++] = *str;} break; |
| 84 | 3162848 | default: res[i++] = *str; | |
| 85 | } | ||
| 86 | 3315169 | str++; | |
| 87 | } | ||
| 88 | 4227 | res[i] = '\0'; | |
| 89 | 4227 | return res; | |
| 90 | } | ||
| 91 | |||
| 92 | 4 | int GC_vasprintf(const char **strp, const char *fmt, va_list ap) { | |
| 93 | int len; | ||
| 94 | char *tmp; | ||
| 95 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 3 times.
|
4 | if (0==strstr(fmt, "%")) { |
| 96 | /* A no-op; we don't actually create a copy of the string which might be unexpected... */ | ||
| 97 | 1 | *strp = fmt; | |
| 98 | 1 | return strlen(fmt); | |
| 99 | } | ||
| 100 | va_list ap2; | ||
| 101 | 3 | va_copy(ap2, ap); | |
| 102 | len = vsnprintf(NULL, 0, fmt, ap); | ||
| 103 | 3 | tmp = mmc_check_out_of_memory(omc_alloc_interface.malloc_atomic(len+1)); | |
| 104 | len = vsnprintf(tmp, len+1, fmt, ap2); | ||
| 105 | 3 | *strp = tmp; | |
| 106 | 3 | return len; | |
| 107 | } | ||
| 108 | |||
| 109 | 3 | int GC_asprintf(const char **strp, const char *fmt, ...) { | |
| 110 | int len; | ||
| 111 | va_list ap; | ||
| 112 | 3 | va_start(ap, fmt); | |
| 113 | |||
| 114 | 3 | len = GC_vasprintf(strp, fmt, ap); | |
| 115 | |||
| 116 | 3 | va_end(ap); | |
| 117 | 3 | return len; | |
| 118 | } | ||
| 119 | |||
| 120 |