Skip to content

Commit

Permalink
Fixed indexing operations
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@727 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
henjo committed Feb 12, 2002
1 parent 328bb14 commit f82100c
Show file tree
Hide file tree
Showing 6 changed files with 268 additions and 67 deletions.
2 changes: 1 addition & 1 deletion c_runtime/Makefile
@@ -1,6 +1,6 @@
CC = gcc
AR = ar -ru
CFLAGS = -Wall -ansi -pedantic
CFLAGS = -g -Wall -ansi -pedantic



Expand Down
26 changes: 25 additions & 1 deletion c_runtime/index_spec.c
@@ -1,8 +1,32 @@
#include "index_spec.h"
#include "memory_pool.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

int index_spec_ok(index_spec_t* s)
{
int i;
if (!s) return 0;
if (s->ndims < 0) return 0;
if (!s->dim_size) return 0;
if (!s->index) return 0;
for (i = 0; i < s->ndims; ++i)
{

if (s->dim_size[i] < 0) return 0;
if ((s->index[i] == 0) && (s->dim_size[i] != 1))
{
fprintf(stderr,"index[%d] == 0, size == %d\n",i,(unsigned int)s->dim_size[i]);
return 0;
}

}
return 1;
}



void alloc_index_spec(index_spec_t* s)
{
int i;
Expand Down Expand Up @@ -31,7 +55,7 @@ void create_index_spec(index_spec_t* dest, int nridx, ...)
dest->index = index_alloc(nridx);
for (i = 0; i < nridx; ++i)
{
dest->dim_size[i] = va_arg(ap,int);
dest->dim_size[i] = va_arg(ap,int);
dest->index[i] = va_arg(ap,int*);
}
va_end(ap);
Expand Down
1 change: 1 addition & 0 deletions c_runtime/index_spec.h
Expand Up @@ -10,6 +10,7 @@ struct index_spec_s

typedef struct index_spec_s index_spec_t;

int index_spec_ok(index_spec_t* s);
void alloc_index_spec(index_spec_t* s);
void create_index_spec(index_spec_t* dest, int nridx, ...);
int* make_index_array(int nridx,...);
Expand Down
4 changes: 2 additions & 2 deletions c_runtime/memory_pool.c
Expand Up @@ -127,7 +127,7 @@ int* size_alloc(int n)

start = current_state.size_buffer_ptr;
current_state.size_buffer_ptr += n;
return size_buffer+n;
return size_buffer+start;

/* return start;*/
}
Expand All @@ -141,7 +141,7 @@ int** index_alloc(int n)

start = current_state.index_buffer_ptr;
current_state.index_buffer_ptr += n;
return index_buffer+n;
return index_buffer+start;

/* return start;*/
}
Expand Down
44 changes: 30 additions & 14 deletions c_runtime/read_write.c
@@ -1,6 +1,7 @@

#include "read_write.h"

#include <stdio.h>
#include <stdlib.h>

void cleanup_description(type_description* desc)
Expand All @@ -11,6 +12,16 @@ void cleanup_description(type_description* desc)
}
}

void in_report(const char *str)
{
fprintf(stderr,"input failed: %s\n",str);
}

void read_to_eol(FILE* file)
{
int c;
while (((c = fgetc(file)) != '\n') && (c != EOF));
}
int read_type_description(FILE* file, type_description* desc)
{
int c;
Expand Down Expand Up @@ -65,31 +76,35 @@ int read_type_description(FILE* file, type_description* desc)

} while (0);

/* read to end of line */
while (((c = fgetc(file)) != '\n') && (c != EOF));
read_to_eol(file);

return 0;
}



int read_modelica_real(FILE* file, modelica_real* data)
{
float f;
type_description desc;
if (read_type_description(file,&desc)) return 1;
if ((desc.type != 'r') && (desc.type != 'i')) { cleanup_description(&desc); return 1; }
if (desc.ndims != 0) { cleanup_description(&desc); return 1; }
if (fscanf(file,"%e",&f) != 1) { cleanup_description(&desc); return 1; }
if (read_type_description(file,&desc)) { in_report("rs type_desc"); return 1; }
if ((desc.type != 'r') && (desc.type != 'i')) { cleanup_description(&desc); in_report("rs type"); return 1; }
if (desc.ndims != 0) { cleanup_description(&desc); in_report("rs dims"); return 1; }
if (fscanf(file,"%e",&f) != 1) { cleanup_description(&desc); in_report("rs parse"); return 1; }
*data = f;
read_to_eol(file);
cleanup_description(&desc);
return 0;
}

int read_modelica_integer(FILE* file, modelica_integer* data)
{
type_description desc;
if (read_type_description(file,&desc)) return 1;
if (desc.type != 'i') { cleanup_description(&desc); return 1; }
if (desc.ndims != 0) { cleanup_description(&desc); return 1; }
if (fscanf(file,"%d",data) != 1) { cleanup_description(&desc); return 1; }
if (read_type_description(file,&desc)) { in_report("is type_desc"); return 1; }
if (desc.type != 'i') { cleanup_description(&desc); in_report("is type"); return 1; }
if (desc.ndims != 0) { cleanup_description(&desc); in_report("is ndims"); return 1; }
if (fscanf(file,"%d",data) != 1) { cleanup_description(&desc); in_report("is parse"); return 1; }
read_to_eol(file);
cleanup_description(&desc);
return 0;
}
Expand All @@ -102,9 +117,9 @@ int read_real_array(FILE* file, real_array_t* arr)
real_array_t tmp;
type_description desc;

if (read_type_description(file,&desc)) return 1;
if ((desc.type != 'r') && (desc.type != 'i')) return 1;
if (desc.ndims <= 0) return 1;
if (read_type_description(file,&desc)) { in_report("ra type_desc"); return 1; }
if ((desc.type != 'r') && (desc.type != 'i')) { in_report("ra type"); return 1; }
if (desc.ndims <= 0) { in_report("ra ndims"); return 1; }

tmp.ndims = desc.ndims;
tmp.dim_size = desc.dim_size;
Expand All @@ -115,9 +130,10 @@ int read_real_array(FILE* file, real_array_t* arr)
nr_elements = real_array_nr_of_elements(arr);
for (i = 0; i < nr_elements; ++i)
{
if (fscanf(file,"%e",&f) != 1) return 1;
if (fscanf(file,"%e",&f) != 1) { in_report("ra parse"); return 1; }
arr->data[i] = f;
}
read_to_eol(file);
return 0;
}
/*
Expand Down

0 comments on commit f82100c

Please sign in to comment.