Skip to content

Commit

Permalink
refs #21: Do not error if cell/node/key is not found
Browse files Browse the repository at this point in the history
* Report validity by new Boolean output flag ''exist'' (on scalar get functions only)
* Use ModelicaMessage (and not ModelicaError) (Can be changed to ModelicaWarning once there is proper tool support).
  • Loading branch information
tbeu committed Aug 10, 2017
1 parent ed24314 commit 7a8eb1e
Show file tree
Hide file tree
Showing 11 changed files with 373 additions and 203 deletions.
72 changes: 51 additions & 21 deletions ExternData/Resources/C-Sources/ED_INIFile.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,98 +160,128 @@ void ED_destroyINI(void* _ini)
}
}

double ED_getDoubleFromINI(void* _ini, const char* varName, const char* section)
double ED_getDoubleFromINI(void* _ini, const char* varName, const char* section, int* exist)
{
double ret = 0.;
INIFile* ini = (INIFile*)_ini;
*exist = 1;
if (ini != NULL) {
INISection* _section = findSection(ini, section);
if (_section != NULL) {
INIPair* pair = findKey(_section, varName);
if (pair != NULL) {
if (ED_strtod(pair->value, ini->loc, &ret)) {
ModelicaFormatError("Cannot read double value \"%s\" from file \"%s\"\n",
pair->value, ini->fileName);
if (NULL != pair->value) {
if (ED_strtod(pair->value, ini->loc, &ret)) {
ModelicaFormatError("Cannot read double value \"%s\" from file \"%s\"\n",
pair->value, ini->fileName);
}
}
else {
ModelicaFormatError("Cannot read value for key \"%s\" from file \"%s\"\n",
varName, ini->fileName);
*exist = 0;
}
}
else {
ModelicaFormatError("Cannot read key \"%s\" from file \"%s\"\n",
ModelicaFormatMessage("Cannot read key \"%s\" from file \"%s\"\n",
varName, ini->fileName);
*exist = 0;
}
}
else {
if (strlen(section) > 0) {
ModelicaFormatError("Cannot read section \"%s\" from file \"%s\"\n",
ModelicaFormatMessage("Cannot read section \"%s\" from file \"%s\"\n",
section, ini->fileName);
}
else {
ModelicaFormatError("Cannot read empty section from file \"%s\"\n",
ModelicaFormatMessage("Cannot read empty section from file \"%s\"\n",
ini->fileName);
}
*exist = 0;
}
}
return ret;
}

const char* ED_getStringFromINI(void* _ini, const char* varName, const char* section)
const char* ED_getStringFromINI(void* _ini, const char* varName, const char* section, int* exist)
{
INIFile* ini = (INIFile*)_ini;
*exist = 1;
if (ini != NULL) {
INISection* _section = findSection(ini, section);
if (_section != NULL) {
INIPair* pair = findKey(_section, varName);
if (pair != NULL) {
char* ret = ModelicaAllocateString(strlen(pair->value));
strcpy(ret, pair->value);
return (const char*)ret;
if (NULL != pair->value) {
char* ret = ModelicaAllocateString(strlen(pair->value));
strcpy(ret, pair->value);
return (const char*)ret;
}
else {
ModelicaFormatError("Cannot read value for key \"%s\" from file \"%s\"\n",
varName, ini->fileName);
*exist = 0;
}
}
else {
ModelicaFormatError("Cannot read key \"%s\" from file \"%s\"\n",
ModelicaFormatMessage("Cannot read key \"%s\" from file \"%s\"\n",
varName, ini->fileName);
*exist = 0;
}
}
else {
if (strlen(section) > 0) {
ModelicaFormatError("Cannot read section \"%s\" from file \"%s\"\n",
ModelicaFormatMessage("Cannot read section \"%s\" from file \"%s\"\n",
section, ini->fileName);
}
else {
ModelicaFormatError("Cannot read empty section from file \"%s\"\n",
ModelicaFormatMessage("Cannot read empty section from file \"%s\"\n",
ini->fileName);
}
*exist = 0;
}
}
return "";
}

int ED_getIntFromINI(void* _ini, const char* varName, const char* section)
int ED_getIntFromINI(void* _ini, const char* varName, const char* section, int* exist)
{
long ret = 0;
INIFile* ini = (INIFile*)_ini;
*exist = 1;
if (ini != NULL) {
INISection* _section = findSection(ini, section);
if (_section != NULL) {
INIPair* pair = findKey(_section, varName);
if (pair != NULL) {
if (ED_strtol(pair->value, ini->loc, &ret)) {
ModelicaFormatError("Cannot read int value \"%s\" from file \"%s\"\n",
pair->value, ini->fileName);
if (NULL != pair->value) {
if (ED_strtol(pair->value, ini->loc, &ret)) {
ModelicaFormatError("Cannot read int value \"%s\" from file \"%s\"\n",
pair->value, ini->fileName);
}
}
else {
ModelicaFormatError("Cannot read value for key \"%s\" from file \"%s\"\n",
varName, ini->fileName);
*exist = 0;
}
}
else {
ModelicaFormatError("Cannot read key \"%s\" from file \"%s\"\n",
ModelicaFormatMessage("Cannot read key \"%s\" from file \"%s\"\n",
varName, ini->fileName);
*exist = 0;
}
}
else {
if (strlen(section) > 0) {
ModelicaFormatError("Cannot read section \"%s\" from file \"%s\"\n",
ModelicaFormatMessage("Cannot read section \"%s\" from file \"%s\"\n",
section, ini->fileName);
}
else {
ModelicaFormatError("Cannot read empty section from file \"%s\"\n",
ModelicaFormatMessage("Cannot read empty section from file \"%s\"\n",
ini->fileName);
}
*exist = 0;
}
}
return (int)ret;
Expand Down
79 changes: 45 additions & 34 deletions ExternData/Resources/C-Sources/ED_JSONFile.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,40 +116,36 @@ static char* findValue(JsonNodeRef* root, const char* varName, const char* fileN
char* token = NULL;
char* buf = strdup(varName);
if (buf != NULL) {
int elementError = 0;
char* key = NULL;
char* nextToken = NULL;
token = strtok_r(buf, ".", &nextToken);
if (token == NULL) {
elementError = 1;
}
while (token != NULL && elementError == 0) {
size_t i;
int foundToken = 0;
for (i = 0; i < JsonNode_getChildCount(*root); i++) {
JsonNodeRef child = JsonNode_findChild(*root, token, JSON_OBJ);
if (child != NULL) {
*root = child;
token = strtok_r(NULL, ".", &nextToken);
foundToken = 1;
break;
}
while (token != NULL) {
JsonNodeRef iter = JsonNode_findChild(*root, token, JSON_OBJ);
if (NULL != iter) {
*root = iter;
token = strtok_r(NULL, ".", &nextToken);
}
if (foundToken == 0) {
elementError = 1;
else {
key = token;
token = strtok_r(NULL, ".", &nextToken);
break;
}
}
if (token == NULL) {
if (NULL != key && NULL != *root && NULL == token) {
token = JsonNode_getPairValue(*root, key);
free(buf);
ModelicaFormatError("Cannot read element \"%s\" from file \"%s\"\n",
varName, fileName);
if (NULL == token) {
ModelicaFormatMessage("Cannot read element \"%s\" from file \"%s\"\n",
varName, fileName);
*root = NULL;
}
}
else {
token = JsonNode_getPairValue(*root, token);
free(buf);
if (token == NULL) {
ModelicaFormatError("Cannot read element \"%s\" from file \"%s\"\n",
varName, fileName);
}
ModelicaFormatMessage("Cannot read element \"%s\" from file \"%s\"\n",
varName, fileName);
*root = NULL;
token = NULL;
}
}
else {
Expand All @@ -158,10 +154,11 @@ static char* findValue(JsonNodeRef* root, const char* varName, const char* fileN
return token;
}

double ED_getDoubleFromJSON(void* _json, const char* varName)
double ED_getDoubleFromJSON(void* _json, const char* varName, int* exist)
{
double ret = 0.;
JSONFile* json = (JSONFile*)_json;
*exist = 1;
if (json != NULL) {
JsonNodeRef root = json->root;
char* token = findValue(&root, varName, json->fileName);
Expand All @@ -171,17 +168,22 @@ double ED_getDoubleFromJSON(void* _json, const char* varName)
token, json->fileName);
}
}
else {
ModelicaFormatError("Cannot read double value from file \"%s\"\n",
else if (NULL != root) {
ModelicaFormatMessage("Cannot read double value from file \"%s\"\n",
json->fileName);
*exist = 0;
}
else {
*exist = 0;
}
}
return ret;
}

const char* ED_getStringFromJSON(void* _json, const char* varName)
const char* ED_getStringFromJSON(void* _json, const char* varName, int* exist)
{
JSONFile* json = (JSONFile*)_json;
*exist = 1;
if (json != NULL) {
JsonNodeRef root = json->root;
char* token = findValue(&root, varName, json->fileName);
Expand All @@ -190,18 +192,23 @@ const char* ED_getStringFromJSON(void* _json, const char* varName)
strcpy(ret, token);
return (const char*)ret;
}
else {
ModelicaFormatError("Cannot read value from file \"%s\"\n",
else if (NULL != root) {
ModelicaFormatMessage("Cannot read value from file \"%s\"\n",
json->fileName);
*exist = 0;
}
else {
*exist = 0;
}
}
return "";
}

int ED_getIntFromJSON(void* _json, const char* varName)
int ED_getIntFromJSON(void* _json, const char* varName, int* exist)
{
long ret = 0;
JSONFile* json = (JSONFile*)_json;
*exist = 1;
if (json != NULL) {
JsonNodeRef root = json->root;
char* token = findValue(&root, varName, json->fileName);
Expand All @@ -211,9 +218,13 @@ int ED_getIntFromJSON(void* _json, const char* varName)
token, json->fileName);
}
}
else {
ModelicaFormatError("Cannot read int value from file \"%s\"\n",
else if (NULL != root) {
ModelicaFormatMessage("Cannot read int value from file \"%s\"\n",
json->fileName);
*exist = 0;
}
else {
*exist = 0;
}
}
return (int)ret;
Expand Down
Loading

0 comments on commit 7a8eb1e

Please sign in to comment.