_DYNAMIC RUN-TIME STRUCTURES_
by
Todd King


[LISTING ONE]


/* Dynamic structure include file */

#ifndef _V_STRUCT_
#define _V_STRUCT_

#include <stdio.h>

char *Var_types[] = { "int", "float", "double", ""};
enum V_TYPES { UNKNOWN=-1, INT, FLOAT, DOUBLE };

#define MAX_VAR     256
#define MAX_NAME    16
#define MAX_DATA    1024

typedef struct {
  char name[MAX_NAME];
  enum V_TYPES type;
} ELEMENT;

typedef struct {
  ELEMENT element[MAX_VAR];
  int count;
  char data[MAX_DATA];
  FILE *source;
} D_STRUCT;

#endif 



[LISTING TWO]


#include "d_struct.h"

/* Returns the number of definitions, -1 if any error occurs */
int load_v_defs(v_file, d_struct)
char v_file[];
D_STRUCT *d_struct;
{
  int cnt;
  char type_text[12];
  char file_name[16];
  FILE *fptr;

/* Open variable definition of variable names/record structure */
  strcpy(file_name, v_file);
  strcat(file_name, ".DEF");
  fptr = fopen(file_name, "r");
  if(fptr == NULL) return(-1);

/* Initialize */
  d_struct->count = 0;

/* load in definitions */

  cnt = d_struct->count;
  while(fscanf(fptr, "%s %s", type_text,
    d_struct->element[cnt].name) != EOF)
  {
    d_struct->element[cnt].type = v_type(type_text);
    cnt++;
    if(cnt > MAX_VAR) return(-1);
  }
  d_struct->count = cnt;
  fclose(fptr);

/* Now open data file - leave open for reading of data */
  strcpy(file_name, v_file);
  strcat(file_name, ".DAT");
  d_struct->source = fopen(file_name, "r");
  if(d_struct->source == NULL) return(-1);
  return(d_struct->count);
}


/* Invalid type returns -1, otherwise index of type in 'Var_types' */
int v_type(type_text)
char type_text[];
{
  int i = 0;

  while(strlen(Var_types[i]) != 0)
  {
    if(strcmp(type_text, Var_types[i]) == 0) return(i);
    i++;
  }
  return(-1);
}

/* Brings a record into the data workspace */
char *read_data(d_struct)
D_STRUCT *d_struct;
{
  return(fgets(d_struct->data, MAX_DATA, d_struct->source));
}


/* extracts a data value from the record */
/* which corresponds to a dynamic name   */
double get_v_value(v_name, d_struct)
char v_name[];
D_STRUCT *d_struct;
{
  int i;
  double dval;
  char tmp_fmt[256], fmt[256];

  strcpy(fmt, "");
  for(i=0; i< d_struct->count; i++)
  {
    if(strcmp(d_struct->element[i].name, v_name) == 0)
    {
      strcpy(tmp_fmt, fmt);
      strcat(tmp_fmt, "%lf");
      sscanf(d_struct->data, tmp_fmt, &dval);
      return(dval);
    }
    strcat(fmt, "%*lf ");
  }
  return(0l);
}

/* Terminates the all reads - clean up */
int end_read(d_struct)
D_STRUCT *d_struct;
{
  close(d_struct->source);
}

/* Prints a variable - in a format according to its type */
int print_v_value(v_name, d_struct)
char v_name[];
D_STRUCT *d_struct;
{
  int i;

  for(i=0; i<d_struct->count; i++)
  {
    if(strcmp(v_name, d_struct->element[i].name) == 0)
    {
      switch(d_struct->element[i].type)
      {
    case INT:
      printf("%16d ", (int)get_v_value(v_name, d_struct));
      break;
    case FLOAT:
      printf("%16.6f ", (float)get_v_value(v_name, d_struct));
      break;
    case DOUBLE:
      printf("%16.6lf ", (double)get_v_value(v_name, d_struct));
      break;
    default:
      printf("%16s ", "Unknown type");
      break;
      }
      return(1);
    }
  }
  printf("%16s ", "Unknown Variable");
  return(0);
}


[LISTING THREE]

/* Example program - reads data and prints it on the screen */
/* Command line arguments are:                              */
/*      example <filename> <colname> [<colname> ...]        */

#include "vman_1.c"

main(argc, argv)
int argc;
char *argv[];
{
  int i;
  double dval;
  D_STRUCT d_struct;

  if(argc < 3) {
    printf("Proper usage: example <filename> <colname>  [<colname> ...]\n");
    exit(-1);
  }

  load_v_defs(argv[1], &d_struct);
  for(i=2; i<argc; i++) printf("  %12s   ", argv[i]);
  printf("\n");
  while(read_data(&d_struct))
  {
    for(i=2; i<argc; i++)
    {
      print_v_value(argv[i], &d_struct);
    }
    printf("\n");
  }
  end_read(&d_struct);
}




