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 Sep 29, 2017
1 parent c3ec486 commit e19bd32
Show file tree
Hide file tree
Showing 13 changed files with 829 additions and 414 deletions.
34 changes: 34 additions & 0 deletions ExternData/Examples/package.mo
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,23 @@ package Examples "Test examples"
Modelica.Blocks.Math.Gain gain2(k=Modelica.Utilities.Strings.scanReal(xlsfile.getString("B2", "set2"))) annotation(Placement(transformation(extent={{-15,30},{5,50}})));
Modelica.Blocks.Sources.Clock clock annotation(Placement(transformation(extent={{-50,60},{-30,80}})));
Modelica.Blocks.Sources.TimeTable timeTable(table=xlsfile.getRealArray2D("A1", "table1", 3, 2)) annotation(Placement(transformation(extent={{-50,30},{-30,50}})));
Real sumB "Sum of column B";
protected
constant Integer startRow = 2 "Start row";
Integer row;
Real val;
Boolean exist;
algorithm
sumB := 0.0;
row := startRow;
exist := true;
while exist loop
(val, exist) := xlsfile.getReal("B" + String(row));
if exist then
sumB := sumB + val;
row := row + 1;
end if;
end while;
equation
connect(clock.y,gain1.u) annotation(Line(points={{-29,70},{-17,70}}, color={0,0,127}));
connect(clock.y,gain2.u) annotation(Line(points={{-29,70},{-22,70},{-22,40},{-17,40}}, color={0,0,127}));
Expand All @@ -92,6 +109,23 @@ package Examples "Test examples"
Modelica.Blocks.Math.Gain gain2(k=Modelica.Utilities.Strings.scanReal(xlsxfile.getString("B2", "set2"))) annotation(Placement(transformation(extent={{-15,30},{5,50}})));
Modelica.Blocks.Sources.Clock clock annotation(Placement(transformation(extent={{-50,60},{-30,80}})));
Modelica.Blocks.Sources.TimeTable timeTable(table=xlsxfile.getRealArray2D("A1", "table1", 3, 2)) annotation(Placement(transformation(extent={{-50,30},{-30,50}})));
Real sumB "Sum of column B";
protected
constant Integer startRow = 2 "Start row";
Integer row;
Real val;
Boolean exist;
algorithm
sumB := 0.0;
row := startRow;
exist := true;
while exist loop
(val, exist) := xlsxfile.getReal("B" + String(row));
if exist then
sumB := sumB + val;
row := row + 1;
end if;
end while;
equation
connect(clock.y,gain1.u) annotation(Line(points={{-29,70},{-17,70}}, color={0,0,127}));
connect(clock.y,gain2.u) annotation(Line(points={{-29,70},{-22,70},{-22,40},{-17,40}}, color={0,0,127}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ EXPORTS
ED_getDoubleFromJSON
ED_getStringFromJSON
ED_getIntFromJSON
ED_getBooleanFromJSON
ED_getArray1DDimensionFromJSON
ED_getArray2DDimensionsFromJSON
ED_getDoubleArray1DFromJSON
Expand Down
81 changes: 60 additions & 21 deletions ExternData/Resources/C-Sources/ED_INIFile.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,99 +160,138 @@ void ED_destroyINI(void* _ini)
}
}

double ED_getDoubleFromINI(void* _ini, const char* varName, const char* section, int strict)
double ED_getDoubleFromINI(void* _ini, const char* varName, const char* section, int strict, int* exist)
{
double ret = 0.;
INIFile* ini = (INIFile*)_ini;
if (ini != NULL) {
INISection* _section = findSection(ini, section);
if (_section != NULL) {
INIPair* pair = findKey(_section, varName);
*exist = 1;
if (pair != NULL) {
if (ED_strtod(pair->value, ini->loc, &ret, strict)) {
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, strict)) {
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;
}
}
else {
*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;
if (ini != NULL) {
INISection* _section = findSection(ini, section);
if (_section != NULL) {
INIPair* pair = findKey(_section, varName);
*exist = 1;
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;
}
}
else {
*exist = 0;
}
return "";
}

int ED_getIntFromINI(void* _ini, const char* varName, const char* section, int strict)
int ED_getIntFromINI(void* _ini, const char* varName, const char* section, int strict, int* exist)
{
long ret = 0;
INIFile* ini = (INIFile*)_ini;
if (ini != NULL) {
INISection* _section = findSection(ini, section);
if (_section != NULL) {
INIPair* pair = findKey(_section, varName);
*exist = 1;
if (pair != NULL) {
if (ED_strtol(pair->value, ini->loc, &ret, strict)) {
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, strict)) {
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;
}
}
else {
*exist = 0;
}
return (int)ret;
}
Loading

0 comments on commit e19bd32

Please sign in to comment.