Skip to content

Commit

Permalink
refs #21: Add functions to get sheet range for XLS/XLSX
Browse files Browse the repository at this point in the history
  • Loading branch information
tbeu committed Sep 29, 2017
1 parent bde9869 commit 4738dee
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 26 deletions.
3 changes: 3 additions & 0 deletions ExternData/Examples/package.mo
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ package Examples "Test examples"
Modelica.Blocks.Math.Gain gain1(k=jsonfile.getReal("set1.gain.k")) annotation(Placement(transformation(extent={{-15,60},{5,80}})));
Modelica.Blocks.Math.Gain gain2(k=Modelica.Utilities.Strings.scanReal(jsonfile.getString("set2.gain.k"))) annotation(Placement(transformation(extent={{-15,30},{5,50}})));
Modelica.Blocks.Sources.Clock clock annotation(Placement(transformation(extent={{-50,60},{-30,80}})));
parameter Integer m = jsonfile.getArrayRows2D("table1") "Number of rows in 2D array";
Modelica.Blocks.Sources.TimeTable timeTable(table=jsonfile.getRealArray2D("table1", 3, 2)) annotation(Placement(transformation(extent={{-50,30},{-30,50}})));
equation
connect(clock.y,gain1.u) annotation(Line(points={{-29,70},{-17,70}}, color={0,0,127}));
Expand All @@ -77,6 +78,7 @@ package Examples "Test examples"
Modelica.Blocks.Math.Gain gain1(k=xlsfile.getReal("B2", "set1")) annotation(Placement(transformation(extent={{-15,60},{5,80}})));
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}})));
parameter Integer m = xlsfile.getArrayRows2D("table1") "Number of rows in 2D array";
Modelica.Blocks.Sources.TimeTable timeTable(table=xlsfile.getRealArray2D("A1", "table1", 3, 2)) annotation(Placement(transformation(extent={{-50,30},{-30,50}})));
parameter Real sumB = computeColSum(xlsfile, "B") "Sum of column B";
function computeColSum "Compute column sum"
Expand Down Expand Up @@ -116,6 +118,7 @@ package Examples "Test examples"
Modelica.Blocks.Math.Gain gain1(k=xlsxfile.getReal("B2", "set1")) annotation(Placement(transformation(extent={{-15,60},{5,80}})));
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}})));
parameter Integer m = xlsxfile.getArrayRows2D("table1") "Number of rows in 2D array";
Modelica.Blocks.Sources.TimeTable timeTable(table=xlsxfile.getRealArray2D("A1", "table1", 3, 2)) annotation(Placement(transformation(extent={{-50,30},{-30,50}})));
parameter Real sumB = computeColSum(xlsxfile, "B") "Sum of column B";
function computeColSum "Compute column sum"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ EXPORTS
ED_getStringFromXLS
ED_getIntFromXLS
ED_getDoubleArray2DFromXLS
ED_getArray2DDimensionsFromXLS
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ EXPORTS
ED_getStringFromXLSX
ED_getIntFromXLSX
ED_getDoubleArray2DFromXLSX
ED_getArray2DDimensionsFromXLSX
60 changes: 41 additions & 19 deletions ExternData/Resources/C-Sources/ED_JSONFile.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ typedef struct {
ED_LOCALE_TYPE loc;
} JSONFile;

void* ED_createJSON(const char* fileName, int verbose) {
void* ED_createJSON(const char* fileName, int verbose)
{
JSONFile* json = (JSONFile*)malloc(sizeof(JSONFile));
if (json == NULL) {
ModelicaError("Memory allocation error\n");
Expand Down Expand Up @@ -83,7 +84,8 @@ void* ED_createJSON(const char* fileName, int verbose) {
return json;
}

void ED_destroyJSON(void* _json) {
void ED_destroyJSON(void* _json)
{
JSONFile* json = (JSONFile*)_json;
if (json != NULL) {
if (json->fileName != NULL) {
Expand All @@ -96,7 +98,8 @@ void ED_destroyJSON(void* _json) {
}
}

double ED_getDoubleFromJSON(void* _json, const char* varName, int* exist) {
double ED_getDoubleFromJSON(void* _json, const char* varName, int* exist)
{
double ret = 0.;
JSONFile* json = (JSONFile*)_json;
if (json != NULL) {
Expand Down Expand Up @@ -136,7 +139,8 @@ double ED_getDoubleFromJSON(void* _json, const char* varName, int* exist) {
return ret;
}

const char* ED_getStringFromJSON(void* _json, const char* varName, int* exist) {
const char* ED_getStringFromJSON(void* _json, const char* varName, int* exist)
{
JSONFile* json = (JSONFile*)_json;
if (json != NULL) {
*exist = 1;
Expand All @@ -163,7 +167,8 @@ const char* ED_getStringFromJSON(void* _json, const char* varName, int* exist) {
return "";
}

int ED_getIntFromJSON(void* _json, const char* varName, int* exist) {
int ED_getIntFromJSON(void* _json, const char* varName, int* exist)
{
long ret = 0;
JSONFile* json = (JSONFile*)_json;
if (json != NULL) {
Expand Down Expand Up @@ -203,7 +208,8 @@ int ED_getIntFromJSON(void* _json, const char* varName, int* exist) {
return (int)ret;
}

int ED_getBooleanFromJSON(void* _json, const char* varName, int* exist) {
int ED_getBooleanFromJSON(void* _json, const char* varName, int* exist)
{
int ret = 0;
JSONFile* json = (JSONFile*)_json;
if (json != NULL) {
Expand Down Expand Up @@ -241,8 +247,10 @@ int ED_getBooleanFromJSON(void* _json, const char* varName, int* exist) {
return ret;
}

void ED_getArray1DDimensionFromJSON(void* _json, const char* varName, int* n) {
void ED_getArray1DDimensionFromJSON(void* _json, const char* varName, int* n)
{
JSONFile* json = (JSONFile*)_json;
*n = 0;
if (json != NULL) {
if (json_object_dothas_value_of_type(json->root, varName, JSONArray)) {
const JSON_Array* jsonArray = json_object_dotget_array(json->root, varName);
Expand All @@ -255,8 +263,11 @@ void ED_getArray1DDimensionFromJSON(void* _json, const char* varName, int* n) {
}
}

void ED_getArray2DDimensionsFromJSON(void* _json, const char* varName, int* m, int* n) {
void ED_getArray2DDimensionsFromJSON(void* _json, const char* varName, int* m, int* n)
{
JSONFile* json = (JSONFile*)_json;
*m = 0;
*n = 0;
if (json != NULL) {
if (json_object_dothas_value_of_type(json->root, varName, JSONArray)) {
const JSON_Array* jsonArray = json_object_dotget_array(json->root, varName);
Expand All @@ -280,7 +291,8 @@ void ED_getArray2DDimensionsFromJSON(void* _json, const char* varName, int* m, i
}
}

void ED_getDoubleArray1DFromJSON(void* _json, const char* varName, double* a, size_t n) {
void ED_getDoubleArray1DFromJSON(void* _json, const char* varName, double* a, size_t n)
{
JSONFile* json = (JSONFile*)_json;
if (json != NULL) {
if (json_object_dothas_value_of_type(json->root, varName, JSONArray)) {
Expand Down Expand Up @@ -328,7 +340,8 @@ void ED_getDoubleArray1DFromJSON(void* _json, const char* varName, double* a, si
}
}

void ED_getStringArray1DFromJSON(void* _json, const char* varName, char** a, size_t n) {
void ED_getStringArray1DFromJSON(void* _json, const char* varName, char** a, size_t n)
{
JSONFile* json = (JSONFile*)_json;
if (json != NULL) {
if (json_object_dothas_value_of_type(json->root, varName, JSONArray)) {
Expand Down Expand Up @@ -361,7 +374,8 @@ void ED_getStringArray1DFromJSON(void* _json, const char* varName, char** a, siz
}
}

void ED_getIntArray1DFromJSON(void* _json, const char* varName, int* a, size_t n) {
void ED_getIntArray1DFromJSON(void* _json, const char* varName, int* a, size_t n)
{
JSONFile* json = (JSONFile*)_json;
if (json != NULL) {
if (json_object_dothas_value_of_type(json->root, varName, JSONArray)) {
Expand Down Expand Up @@ -413,7 +427,8 @@ void ED_getIntArray1DFromJSON(void* _json, const char* varName, int* a, size_t n
}
}

void ED_getBooleanArray1DFromJSON(void* _json, const char* varName, int* a, size_t n) {
void ED_getBooleanArray1DFromJSON(void* _json, const char* varName, int* a, size_t n)
{
JSONFile* json = (JSONFile*)_json;
if (json != NULL) {
if (json_object_dothas_value_of_type(json->root, varName, JSONArray)) {
Expand Down Expand Up @@ -459,7 +474,8 @@ void ED_getBooleanArray1DFromJSON(void* _json, const char* varName, int* a, size
}
}

void ED_getDoubleArray2DFromJSON(void* _json, const char* varName, double* a, size_t m, size_t n) {
void ED_getDoubleArray2DFromJSON(void* _json, const char* varName, double* a, size_t m, size_t n)
{
JSONFile* json = (JSONFile*)_json;
if (json != NULL) {
if (json_object_dothas_value_of_type(json->root, varName, JSONArray)) {
Expand Down Expand Up @@ -513,7 +529,8 @@ void ED_getDoubleArray2DFromJSON(void* _json, const char* varName, double* a, si
}
}

void ED_getStringArray2DFromJSON(void* _json, const char* varName, char** a, size_t m, size_t n) {
void ED_getStringArray2DFromJSON(void* _json, const char* varName, char** a, size_t m, size_t n)
{
JSONFile* json = (JSONFile*)_json;
if (json != NULL) {
if (json_object_dothas_value_of_type(json->root, varName, JSONArray)) {
Expand Down Expand Up @@ -549,7 +566,8 @@ void ED_getStringArray2DFromJSON(void* _json, const char* varName, char** a, siz
}
}

void ED_getIntArray2DFromJSON(void* _json, const char* varName, int* a, size_t m, size_t n) {
void ED_getIntArray2DFromJSON(void* _json, const char* varName, int* a, size_t m, size_t n)
{
JSONFile* json = (JSONFile*)_json;
if (json != NULL) {
if (json_object_dothas_value_of_type(json->root, varName, JSONArray)) {
Expand Down Expand Up @@ -607,7 +625,8 @@ void ED_getIntArray2DFromJSON(void* _json, const char* varName, int* a, size_t m
}
}

void ED_getBooleanArray2DFromJSON(void* _json, const char* varName, int* a, size_t m, size_t n) {
void ED_getBooleanArray2DFromJSON(void* _json, const char* varName, int* a, size_t m, size_t n)
{
JSONFile* json = (JSONFile*)_json;
if (json != NULL) {
if (json_object_dothas_value_of_type(json->root, varName, JSONArray)) {
Expand Down Expand Up @@ -659,7 +678,8 @@ void ED_getBooleanArray2DFromJSON(void* _json, const char* varName, int* a, size
}
}

static JSON_Value_Type json_array_get_type(const JSON_Array *array) {
static JSON_Value_Type json_array_get_type(const JSON_Array *array)
{
JSON_Value_Type type = JSONNull;
size_t i;
size_t n = json_array_get_count(array);
Expand All @@ -675,7 +695,8 @@ static JSON_Value_Type json_array_get_type(const JSON_Array *array) {
return type;
}

static JSON_Value_Type json_array_get_type2D(const JSON_Array *array) {
static JSON_Value_Type json_array_get_type2D(const JSON_Array *array)
{
JSON_Value_Type type = JSONNull;
if (JSONArray == json_array_get_type(array)) {
size_t i;
Expand All @@ -693,7 +714,8 @@ static JSON_Value_Type json_array_get_type2D(const JSON_Array *array) {
return type;
}

static int json_array_check_dimensions2D(const JSON_Array *array) {
static int json_array_check_dimensions2D(const JSON_Array *array)
{
int n = 0;
if (JSONArray == json_array_get_type(array)) {
size_t i;
Expand Down
15 changes: 15 additions & 0 deletions ExternData/Resources/C-Sources/ED_XLSFile.c
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,18 @@ void ED_getDoubleArray2DFromXLS(void* _xls, const char* cellAddress, const char*
}
}
}

void ED_getArray2DDimensionsFromXLS(void* _xls, const char* sheetName, int* m, int* n)
{
XLSFile* xls = (XLSFile*)_xls;
*m = 0;
*n = 0;
if (xls != NULL) {
char* _sheetName = (char*)sheetName;
const xlsWorkSheet* pWS = findSheet(xls, &_sheetName);
if (NULL != pWS) {
*m = pWS->rows.lastrow + 1;
*n = pWS->rows.lastcol;
}
}
}
36 changes: 31 additions & 5 deletions ExternData/Resources/C-Sources/ED_XLSXFile.c
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ static void findBlankCell(WORD row, WORD col, XmlNodeRef root, int* isBlank)
const XmlNodeRef dim = XmlNode_findChild(root, "dimension");
*isBlank = 0;
if (NULL != dim) {
const char* ref = XmlNode_getAttributeValue(dim, "ref");
char* ref = XmlNode_getAttributeValue(dim, "ref");
if (NULL != ref) {
char* colon = strchr(ref, ':');
if (NULL != colon) {
Expand All @@ -398,7 +398,7 @@ double ED_getDoubleFromXLSX(void* _xlsx, const char* cellAddress, const char* sh
XLSXFile* xlsx = (XLSXFile*)_xlsx;
if (xlsx != NULL) {
char* _sheetName = (char*)sheetName;
XmlNodeRef root = findSheet(xlsx, &_sheetName);
const XmlNodeRef root = findSheet(xlsx, &_sheetName);
if (root != NULL) {
char* token = findCellValue(xlsx, cellAddress, root, _sheetName);
*exist = 1;
Expand Down Expand Up @@ -439,7 +439,7 @@ const char* ED_getStringFromXLSX(void* _xlsx, const char* cellAddress, const cha
XLSXFile* xlsx = (XLSXFile*)_xlsx;
if (xlsx != NULL) {
char* _sheetName = (char*)sheetName;
XmlNodeRef root = findSheet(xlsx, &_sheetName);
const XmlNodeRef root = findSheet(xlsx, &_sheetName);
if (root != NULL) {
char* token = findCellValue(xlsx, cellAddress, root, _sheetName);
*exist = 1;
Expand Down Expand Up @@ -478,7 +478,7 @@ int ED_getIntFromXLSX(void* _xlsx, const char* cellAddress, const char* sheetNam
XLSXFile* xlsx = (XLSXFile*)_xlsx;
if (xlsx != NULL) {
char* _sheetName = (char*)sheetName;
XmlNodeRef root = findSheet(xlsx, &_sheetName);
const XmlNodeRef root = findSheet(xlsx, &_sheetName);
if (root != NULL) {
char* token = findCellValue(xlsx, cellAddress, root, _sheetName);
*exist = 1;
Expand Down Expand Up @@ -517,7 +517,7 @@ void ED_getDoubleArray2DFromXLSX(void* _xlsx, const char* cellAddress, const cha
XLSXFile* xlsx = (XLSXFile*)_xlsx;
if (xlsx != NULL) {
char* _sheetName = (char*)sheetName;
XmlNodeRef root = findSheet(xlsx, &_sheetName);
const XmlNodeRef root = findSheet(xlsx, &_sheetName);
if (root != NULL) {
WORD row = 0, col = 0;
WORD i, j;
Expand Down Expand Up @@ -555,3 +555,29 @@ void ED_getDoubleArray2DFromXLSX(void* _xlsx, const char* cellAddress, const cha
}
}
}

void ED_getArray2DDimensionsFromXLSX(void* _xlsx, const char* sheetName, int* m, int* n)
{
XLSXFile* xlsx = (XLSXFile*)_xlsx;
*m = 0;
*n = 0;
if (xlsx != NULL) {
char* _sheetName = (char*)sheetName;
const XmlNodeRef root = findSheet(xlsx, &_sheetName);
if (root != NULL) {
const XmlNodeRef dim = XmlNode_findChild(root, "dimension");
if (NULL != dim) {
char* ref = XmlNode_getAttributeValue(dim, "ref");
if (NULL != ref) {
char* colon = strchr(ref, ':');
if (NULL != colon) {
WORD row = 0, col = 0;
rc(++colon, &row, &col);
*m = (int)row;
*n = (int)col;
}
}
}
}
}
}
1 change: 1 addition & 0 deletions ExternData/Resources/Include/ED_XLSFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ double ED_getDoubleFromXLS(void* _xls, const char* cellAddress, const char* shee
const char* ED_getStringFromXLS(void* _xls, const char* cellAddress, const char* sheetName, int* exist);
int ED_getIntFromXLS(void* _xls, const char* cellAddress, const char* sheetName, int* exist);
void ED_getDoubleArray2DFromXLS(void* _xls, const char* cellAddress, const char* sheetName, double* a, size_t m, size_t n);
void ED_getArray2DDimensionsFromXLS(void* _xls, const char* sheetName, int* m, int* n);

#endif
1 change: 1 addition & 0 deletions ExternData/Resources/Include/ED_XLSXFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ double ED_getDoubleFromXLSX(void* _xlsx, const char* cellAddress, const char* sh
const char* ED_getStringFromXLSX(void* _xlsx, const char* cellAddress, const char* sheetName, int* exist);
int ED_getIntFromXLSX(void* _xlsx, const char* cellAddress, const char* sheetName, int* exist);
void ED_getDoubleArray2DFromXLSX(void* _xlsx, const char* cellAddress, const char* sheetName, double* a, size_t m, size_t n);
void ED_getArray2DDimensionsFromXLSX(void* _xlsx, const char* sheetName, int* m, int* n);

#endif
Loading

0 comments on commit 4738dee

Please sign in to comment.