Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- Handle escaping strings where the string-length does not change


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@19323 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Feb 26, 2014
1 parent c9f5ab7 commit 4a0ef18
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 18 deletions.
46 changes: 37 additions & 9 deletions Compiler/runtime/FMIImpl.c
Expand Up @@ -346,8 +346,15 @@ void FMIImpl__initializeFMI1Import(fmi1_import_t* fmi, void** fmiInfo, fmi_versi
const char* guid = fmi1_import_get_GUID(fmi);
/* Read the FMI description from FMU's modelDescription.xml file. */
const char* description = fmi1_import_get_description(fmi);
description = ((description != NULL) && (omc__escapedStringLength(description,0) > strlen(description))) ? (const char*)omc__escapedString(description,0) : description;
description = (description != NULL) ? description : "";
if (description != NULL) {
int hasEscape = 0;
omc__escapedStringLength(description,0,&hasEscape);
if (hasEscape) {
description = (const char*)omc__escapedString(description,0);
}
} else {
description = "";
}
/* Read the FMI generation tool from FMU's modelDescription.xml file. */
const char* generationTool = fmi1_import_get_generation_tool(fmi);
/* Read the FMI generation date and time from FMU's modelDescription.xml file. */
Expand Down Expand Up @@ -399,8 +406,15 @@ void FMIImpl__initializeFMI1Import(fmi1_import_t* fmi, void** fmiInfo, fmi_versi
void* variable_name = mk_scon(name);
free(name);
const char* description = fmi1_import_get_variable_description(model_variable);
description = ((description != NULL) && (omc__escapedStringLength(description,0) > strlen(description))) ? (const char*)omc__escapedString(description,0) : description;
description = (description != NULL) ? description : "";
if (description != NULL) {
int hasEscape = 0;
omc__escapedStringLength(description,0,&hasEscape);
if (hasEscape) {
description = (const char*)omc__escapedString(description,0);
}
} else {
description = "";
}
void* variable_description = mk_scon(description);
const char* base_type = getFMI1ModelVariableBaseType(model_variable);
void* variable_base_type = mk_scon(base_type);
Expand Down Expand Up @@ -482,9 +496,16 @@ void FMIImpl__initializeFMI2Import(fmi2_import_t* fmi, void** fmiInfo, fmi_versi
const char* guid = fmi2_import_get_GUID(fmi);
/* Read the FMI description from FMU's modelDescription.xml file. */
const char* description = fmi2_import_get_description(fmi);
description = ((description != NULL) && (omc__escapedStringLength(description,0) > strlen(description))) ? (const char*)omc__escapedString(description,0) : description;
description = (description != NULL) ? description : "";
/* Read the FMI generation tool from FMU's modelDescription.xml file. */
if (description != NULL) {
int hasEscape = 0;
omc__escapedStringLength(description,0,&hasEscape);
if (hasEscape) {
description = (const char*)omc__escapedString(description,0);
}
} else {
description = "";
}
/* Read the FMI generation tool from FMU's modelDescription.xml file. */
const char* generationTool = fmi2_import_get_generation_tool(fmi);
/* Read the FMI generation date and time from FMU's modelDescription.xml file. */
const char* generationDateAndTime = fmi2_import_get_generation_date_and_time(fmi);
Expand Down Expand Up @@ -539,8 +560,15 @@ void FMIImpl__initializeFMI2Import(fmi2_import_t* fmi, void** fmiInfo, fmi_versi
void* variable_name = mk_scon(name);
free(name);
const char* description = fmi2_import_get_variable_description(model_variable);
description = ((description != NULL) && (omc__escapedStringLength(description,0) > strlen(description))) ? (const char*)omc__escapedString(description,0) : description;
description = (description != NULL) ? description : "";
if (description != NULL) {
int hasEscape = 0;
omc__escapedStringLength(description,0,&hasEscape);
if (hasEscape) {
description = (const char*)omc__escapedString(description,0);
}
} else {
description = "";
}
void* variable_description = mk_scon(description);
const char* base_type = getFMI2ModelVariableBaseType(model_variable);
void* variable_base_type = mk_scon(base_type);
Expand Down
19 changes: 11 additions & 8 deletions SimulationRuntime/c/util/modelica_string.c
Expand Up @@ -172,7 +172,7 @@ modelica_string_const cat_modelica_string(modelica_string_const s1, modelica_str
return dest;
}

extern int omc__escapedStringLength(const char* str, int nl)
extern int omc__escapedStringLength(const char* str, int nl, int *hasEscape)
{
int i=0;
while(*str) {
Expand All @@ -182,9 +182,9 @@ extern int omc__escapedStringLength(const char* str, int nl)
case '\a':
case '\b':
case '\f':
case '\v': i++; break;
case '\r': if(nl) {i++; if(str[1] == '\n') str++;} break;
case '\n': if(nl) {i++; if(str[1] == '\r') str++;} break;
case '\v': i++; *hasEscape=1; break;
case '\r': if(nl) {i++; *hasEscape=1; if(str[1] == '\n') str++;} break;
case '\n': if(nl) {i++; *hasEscape=1;} break;
default: break;
}
i++;
Expand All @@ -199,11 +199,14 @@ extern int omc__escapedStringLength(const char* str, int nl)
extern char* omc__escapedString(const char* str, int nl)
{
int len1,len2;
char *res;
char *res,*origstr = str;
int i=0;
int hasEscape = 0;
len1 = strlen(str);
len2 = omc__escapedStringLength(str,nl);
if(len1 == len2) return NULL;
len2 = omc__escapedStringLength(str,nl,&hasEscape);
if (!hasEscape) {
return NULL;
}
res = (char*) malloc(len2+1);
while(*str) {
switch (*str) {
Expand All @@ -214,7 +217,7 @@ extern char* omc__escapedString(const char* str, int nl)
case '\f': res[i++] = '\\'; res[i++] = 'f'; break;
case '\v': res[i++] = '\\'; res[i++] = 'v'; break;
case '\r': if(nl) {res[i++] = '\\'; res[i++] = 'n'; if(str[1] == '\n') str++;} else {res[i++] = *str;} break;
case '\n': if(nl) {res[i++] = '\\'; res[i++] = 'n'; if(str[1] == '\r') str++;} else {res[i++] = *str;} break;
case '\n': if(nl) {res[i++] = '\\'; res[i++] = 'n';} else {res[i++] = *str;} break;
default: res[i++] = *str;
}
str++;
Expand Down
2 changes: 1 addition & 1 deletion SimulationRuntime/c/util/modelica_string.h
Expand Up @@ -78,7 +78,7 @@ extern modelica_string_const copy_modelica_string(modelica_string_const source);
extern modelica_string_const cat_modelica_string(modelica_string_const s1, modelica_string_const s2);

/* Escape string */
int omc__escapedStringLength(const char* str, int nl);
int omc__escapedStringLength(const char* str, int nl, int *hasEscape);
extern char* omc__escapedString(const char* str, int nl);

#endif
Expand Down

0 comments on commit 4a0ef18

Please sign in to comment.