Skip to content

Commit af48c1c

Browse files
author
henjo
committed
Introduced type information to input file
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@720 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent 0861c21 commit af48c1c

File tree

5 files changed

+160
-34
lines changed

5 files changed

+160
-34
lines changed

c_runtime/modelica.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@
1414
typedef real_array_t real_array;
1515
typedef integer_array_t integer_array;
1616

17+
typedef modelica_integer size_real_array_rettype;
18+
typedef modelica_integer size_integer_array_rettype;
1719
#endif

c_runtime/read_write.c

Lines changed: 116 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,124 @@
11

22
#include "read_write.h"
33

4-
void read_modelica_real(FILE* file, modelica_real* data)
4+
#include <stdlib.h>
5+
6+
void cleanup_description(type_description* desc)
7+
{
8+
if (desc->ndims > 0)
9+
{
10+
free(desc->dim_size);
11+
}
12+
}
13+
14+
int read_type_description(FILE* file, type_description* desc)
15+
{
16+
int c;
17+
int i;
18+
do
19+
{
20+
if ((c = fgetc(file)) == EOF) return 1;
21+
if (c != '#') return 1;
22+
if ((c = fgetc(file)) == EOF) return 1;
23+
if (c != ' ') return 1;
24+
if ((c = fgetc(file)) == EOF) return 1;
25+
switch (c)
26+
{
27+
case 'i': /* integer */
28+
case 'r': /* real */
29+
case 'b': /* boolean */
30+
case 's': /* string */
31+
desc->type = c;
32+
break;
33+
default:
34+
return 1;
35+
}
36+
if ((c = fgetc(file)) == EOF) return 1;
37+
if (c == '!') /* scalar */
38+
{
39+
desc->ndims = 0;
40+
desc->dim_size = 0;
41+
break;
42+
}
43+
if (c != '[') return 1;
44+
/* now is an array dim description */
45+
if (fscanf(file,"%d",&desc->ndims) != 1) return 1;
46+
if (desc->ndims < 0) return 1;
47+
if (desc->ndims > 0)
48+
{
49+
desc->dim_size = (int*)malloc(desc->ndims*sizeof(int));
50+
if (!desc->dim_size) return 1;
51+
}
52+
else
53+
{
54+
desc->dim_size = 0;
55+
}
56+
for (i = 0; i < desc->ndims; ++i)
57+
{
58+
if (fscanf(file,"%d",&desc->dim_size[i]) != 1)
59+
{
60+
free(desc->dim_size);
61+
return 1;
62+
}
63+
}
64+
break;
65+
66+
} while (0);
67+
68+
/* read to end of line */
69+
while (((c = fgetc(file)) != '\n') && (c != EOF));
70+
return 0;
71+
}
72+
73+
int read_modelica_real(FILE* file, modelica_real* data)
574
{
675
float f;
7-
fscanf(file,"%e",&f);
76+
type_description desc;
77+
if (read_type_description(file,&desc)) return 1;
78+
if ((desc.type != 'r') && (desc.type != 'i')) { cleanup_description(&desc); return 1; }
79+
if (desc.ndims != 0) { cleanup_description(&desc); return 1; }
80+
if (fscanf(file,"%e",&f) != 1) { cleanup_description(&desc); return 1; }
881
*data = f;
82+
cleanup_description(&desc);
83+
return 0;
984
}
1085

11-
void read_modelica_integer(FILE* file, modelica_integer* data)
86+
int read_modelica_integer(FILE* file, modelica_integer* data)
1287
{
13-
fscanf(file,"%d",data);
88+
type_description desc;
89+
if (read_type_description(file,&desc)) return 1;
90+
if (desc.type != 'i') { cleanup_description(&desc); return 1; }
91+
if (desc.ndims != 0) { cleanup_description(&desc); return 1; }
92+
if (fscanf(file,"%d",data) != 1) { cleanup_description(&desc); return 1; }
93+
cleanup_description(&desc);
94+
return 0;
1495
}
1596

16-
void read_real_array(FILE* file, real_array_t* arr)
97+
int read_real_array(FILE* file, real_array_t* arr)
1798
{
1899
int nr_elements;
19100
int i;
20101
float f;
102+
real_array_t tmp;
103+
type_description desc;
104+
105+
if (read_type_description(file,&desc)) return 1;
106+
if ((desc.type != 'r') && (desc.type != 'i')) return 1;
107+
if (desc.ndims <= 0) return 1;
108+
109+
tmp.ndims = desc.ndims;
110+
tmp.dim_size = desc.dim_size;
111+
clone_real_array_spec(&tmp,arr);
112+
alloc_real_array_data(arr);
113+
cleanup_description(&desc);
114+
21115
nr_elements = real_array_nr_of_elements(arr);
22116
for (i = 0; i < nr_elements; ++i)
23117
{
24-
fscanf(file,"%e",&f);
118+
if (fscanf(file,"%e",&f) != 1) return 1;
25119
arr->data[i] = f;
26120
}
27-
fprintf(stderr,"Real array read\n");
121+
return 0;
28122
}
29123
/*
30124
void read_integer_array(FILE* file, integer_array_t* arr)
@@ -38,35 +132,47 @@ void read_integer_array(FILE* file, integer_array_t* arr)
38132
}
39133
}
40134
*/
41-
void write_modelica_real(FILE* file, modelica_real* data)
135+
int write_modelica_real(FILE* file, modelica_real* data)
42136
{
137+
fprintf(file,"# r!\n");
43138
fprintf(file,"%e\n",*data);
139+
return 0;
44140
}
45141

46-
void write_modelica_integer(FILE* file, modelica_integer* data)
142+
int write_modelica_integer(FILE* file, modelica_integer* data)
47143
{
144+
fprintf(file,"# i!\n");
48145
fprintf(file,"%d\n",*data);
146+
return 0;
49147
}
50148

51-
void write_real_array(FILE* file, real_array_t* arr)
149+
int write_real_array(FILE* file, real_array_t* arr)
52150
{
53151
int nr_elements;
54152
int i;
153+
fprintf(file,"# r[ %d",arr->ndims);
154+
for (i = 0; i < arr->ndims; ++i) fprintf(file," %d",arr->dim_size[i]);
155+
fprintf(file,"\n");
55156
nr_elements = real_array_nr_of_elements(arr);
56157
for (i = 0; i < nr_elements; ++i)
57158
{
58159
fprintf(file,"%e\n",arr->data[i]);
59160
}
161+
return 0;
60162
}
61163
/*
62164
void write_integer_array(FILE* file, integer_array_t* arr)
63165
{
64166
int nr_elements;
65167
int i;
168+
fprintf(file,"# i[ %d",arr->ndims);
169+
for (i = 0; i < arr->ndims; ++i) fprintf(file," %d",arr->dim_size[i]);
170+
fprintf(file,"\n");
66171
nr_elements = integer_array_nr_of_elements(arr);
67172
for (i = 0; i < nr_elements; ++i)
68173
{
69174
fprintf(file,"%d\n",arr->data[i]);
70175
}
176+
return 0;
71177
}
72178
*/

c_runtime/read_write.h

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#ifndef READ_WRITE_H_
2-
#define READ_WIRTE_H_
2+
#define READ_WRITE_H_
33

44
#include <stdio.h>
55
#include <errno.h>
@@ -13,14 +13,22 @@
1313
#define PRE_READ_DONE if (close_file) fclose(in_file);
1414
#define PRE_WRITE_DONE if (close_file) fclose(out_file);
1515

16-
void read_modelica_real(FILE*,modelica_real*);
17-
void read_real_array(FILE*,real_array_t*);
18-
void write_modelica_real(FILE*,modelica_real*);
19-
void write_real_array(FILE*,real_array_t*);
16+
struct type_desc_s {
17+
char type;
18+
int ndims;
19+
int *dim_size;
20+
};
2021

21-
void read_modelica_integer(FILE*,modelica_integer*);
22-
void read_integer_array(FILE*,integer_array_t*);
23-
void write_modelica_integer(FILE*,modelica_integer*);
24-
void write_integer_array(FILE*,integer_array_t*);
22+
typedef struct type_desc_s type_description;
23+
24+
int read_modelica_real(FILE*,modelica_real*);
25+
int read_real_array(FILE*,real_array_t*);
26+
int write_modelica_real(FILE*,modelica_real*);
27+
int write_real_array(FILE*,real_array_t*);
28+
29+
int read_modelica_integer(FILE*,modelica_integer*);
30+
int read_integer_array(FILE*,integer_array_t*);
31+
int write_modelica_integer(FILE*,modelica_integer*);
32+
int write_integer_array(FILE*,integer_array_t*);
2533

2634
#endif

c_runtime/real_array.c

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ void alloc_real_array_data(real_array_t* a)
101101
{
102102
size_t array_size;
103103

104-
array_size = real_array_nr_of_elements(a) * sizeof(modelica_real);
104+
array_size = real_array_nr_of_elements(a);
105105
a->data = real_alloc(array_size);
106106

107107
/* size_t array_size;
@@ -437,12 +437,12 @@ void array_alloc_real_array(real_array_t* dest,int n,real_array_t* first,...)
437437

438438
}
439439

440-
void array_scalar_real_array(real_array_t* dest,int n,real* first,...)
440+
void array_scalar_real_array(real_array_t* dest,int n,real first,...)
441441
{
442442

443443
}
444444

445-
void array_alloc_scalar_real_array(real_array_t* dest,int n,real* first,...)
445+
void array_alloc_scalar_real_array(real_array_t* dest,int n,real first,...)
446446
{
447447

448448
}
@@ -459,11 +459,17 @@ real* real_array_element_addr(real_array_t* source,int ndims,...)
459459
return tmp;
460460
}
461461

462-
void modelica_builtin_cat_real_array(int k, real_array_t* A, real_array_t* B)
463-
{
464462

463+
void cat_real_array(int k, real_array_t* dest, int n, real_array_t* first,...)
464+
{
465+
assert(0 && "Not implemented yet");
466+
}
467+
void cat_alloc_real_array(int k, real_array_t* dest, int n, real_array_t* first,...)
468+
{
469+
assert(0 && "Not implemented yet");
465470
}
466471

472+
467473
void range_alloc_real_array(real start, real stop, real inc, real_array_t* dest)
468474
{
469475
int n;
@@ -773,7 +779,7 @@ void promote_real_array(real_array_t* a, int n,real_array_t* dest)
773779
}
774780
}
775781

776-
void promote_real_scalar(double s,int n,real_array_t* dest)
782+
void promote_scalar_real_array(double s,int n,real_array_t* dest)
777783
{
778784
size_t i;
779785

@@ -798,14 +804,15 @@ int ndims_real_array(real_array_t* a)
798804
return a->ndims;
799805
}
800806

801-
int size_of_dimension_real_array(real_array_t* a, int i)
807+
int size_of_dimension_real_array(real_array_t a, int i)
802808
{
803-
assert(real_array_ok(a));
804-
assert((i > 0) && (i <= a->ndims));
809+
assert(real_array_ok(&a));
810+
assert((i > 0) && (i <= a.ndims));
805811

806-
return a->dim_size[i];
812+
return a.dim_size[i-1];
807813
}
808814

815+
809816
void size_real_array(real_array_t* a, real_array_t* dest)
810817
{
811818
/* This should be an integer data instead */

c_runtime/real_array.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,13 @@ void simple_index_real_array2(real_array_t* source,
8484
void array_real_array(real_array_t* dest,int n,real_array_t* first,...);
8585
void array_alloc_real_array(real_array_t* dest,int n,real_array_t* first,...);
8686

87-
void array_scalar_real_array(real_array_t* dest,int n,real* first,...);
88-
void array_alloc_scalar_real_array(real_array_t* dest,int n,real* first,...);
87+
void array_scalar_real_array(real_array_t* dest,int n,real first,...);
88+
void array_alloc_scalar_real_array(real_array_t* dest,int n,real first,...);
8989

9090
real* real_array_element_addr(real_array_t* source,int ndims,...);
9191

92-
void modelica_builtin_cat_real_array(int k, real_array_t* A, real_array_t* B);
92+
void cat_real_array(int k,real_array_t* dest, int n, real_array_t* first,...);
93+
void cat_alloc_real_array(int k,real_array_t* dest, int n, real_array_t* first,...);
9394

9495
void range_alloc_real_array(real start,real stop,real inc,real_array_t* dest);
9596
void range_real_array(real start,real stop, real inc,real_array_t* dest);
@@ -121,10 +122,12 @@ void exp_real_array(real_array_t* a, modelica_integer b, real_array_t* dest);
121122
void exp_alloc_real_array(real_array_t* a, modelica_integer b, real_array_t* dest);
122123

123124
void promote_real_array(real_array_t* a, int n,real_array_t* dest);
124-
void promote_real_scalar(double s,int n,real_array_t* dest);
125+
void promote_scalar_real_array(double s,int n,real_array_t* dest);
125126

126127
int ndims_real_array(real_array_t* a);
127-
int size_of_dimension_real_array(real_array_t* a, int i);
128+
int size_of_dimension_real_array(real_array_t a, int i);
129+
typedef modelica_integer size_of_dimension_real_array_rettype;
130+
128131
void size_real_array(real_array_t* a,real_array_t* dest);
129132
double scalar_real_array(real_array_t* a);
130133
void vector_real_array(real_array_t* a, real_array_t* dest);

0 commit comments

Comments
 (0)