home *** CD-ROM | disk | FTP | other *** search
/ Hall of Fame / HallofFameCDROM.cdr / proglc / ddj1188.lzh / KING.ASC < prev    next >
Text File  |  1988-10-21  |  4KB  |  211 lines

  1. _DYNAMIC RUN-TIME STRUCTURES_
  2. by
  3. Todd King
  4.  
  5.  
  6. [LISTING ONE]
  7.  
  8.  
  9. /* Dynamic structure include file */
  10.  
  11. #ifndef _V_STRUCT_
  12. #define _V_STRUCT_
  13.  
  14. #include <stdio.h>
  15.  
  16. char *Var_types[] = { "int", "float", "double", ""};
  17. enum V_TYPES { UNKNOWN=-1, INT, FLOAT, DOUBLE };
  18.  
  19. #define MAX_VAR     256
  20. #define MAX_NAME    16
  21. #define MAX_DATA    1024
  22.  
  23. typedef struct {
  24.   char name[MAX_NAME];
  25.   enum V_TYPES type;
  26. } ELEMENT;
  27.  
  28. typedef struct {
  29.   ELEMENT element[MAX_VAR];
  30.   int count;
  31.   char data[MAX_DATA];
  32.   FILE *source;
  33. } D_STRUCT;
  34.  
  35. #endif 
  36.  
  37.  
  38.  
  39. [LISTING TWO]
  40.  
  41.  
  42. #include "d_struct.h"
  43.  
  44. /* Returns the number of definitions, -1 if any error occurs */
  45. int load_v_defs(v_file, d_struct)
  46. char v_file[];
  47. D_STRUCT *d_struct;
  48. {
  49.   int cnt;
  50.   char type_text[12];
  51.   char file_name[16];
  52.   FILE *fptr;
  53.  
  54. /* Open variable definition of variable names/record structure */
  55.   strcpy(file_name, v_file);
  56.   strcat(file_name, ".DEF");
  57.   fptr = fopen(file_name, "r");
  58.   if(fptr == NULL) return(-1);
  59.  
  60. /* Initialize */
  61.   d_struct->count = 0;
  62.  
  63. /* load in definitions */
  64.  
  65.   cnt = d_struct->count;
  66.   while(fscanf(fptr, "%s %s", type_text,
  67.     d_struct->element[cnt].name) != EOF)
  68.   {
  69.     d_struct->element[cnt].type = v_type(type_text);
  70.     cnt++;
  71.     if(cnt > MAX_VAR) return(-1);
  72.   }
  73.   d_struct->count = cnt;
  74.   fclose(fptr);
  75.  
  76. /* Now open data file - leave open for reading of data */
  77.   strcpy(file_name, v_file);
  78.   strcat(file_name, ".DAT");
  79.   d_struct->source = fopen(file_name, "r");
  80.   if(d_struct->source == NULL) return(-1);
  81.   return(d_struct->count);
  82. }
  83.  
  84.  
  85. /* Invalid type returns -1, otherwise index of type in 'Var_types' */
  86. int v_type(type_text)
  87. char type_text[];
  88. {
  89.   int i = 0;
  90.  
  91.   while(strlen(Var_types[i]) != 0)
  92.   {
  93.     if(strcmp(type_text, Var_types[i]) == 0) return(i);
  94.     i++;
  95.   }
  96.   return(-1);
  97. }
  98.  
  99. /* Brings a record into the data workspace */
  100. char *read_data(d_struct)
  101. D_STRUCT *d_struct;
  102. {
  103.   return(fgets(d_struct->data, MAX_DATA, d_struct->source));
  104. }
  105.  
  106.  
  107. /* extracts a data value from the record */
  108. /* which corresponds to a dynamic name   */
  109. double get_v_value(v_name, d_struct)
  110. char v_name[];
  111. D_STRUCT *d_struct;
  112. {
  113.   int i;
  114.   double dval;
  115.   char tmp_fmt[256], fmt[256];
  116.  
  117.   strcpy(fmt, "");
  118.   for(i=0; i< d_struct->count; i++)
  119.   {
  120.     if(strcmp(d_struct->element[i].name, v_name) == 0)
  121.     {
  122.       strcpy(tmp_fmt, fmt);
  123.       strcat(tmp_fmt, "%lf");
  124.       sscanf(d_struct->data, tmp_fmt, &dval);
  125.       return(dval);
  126.     }
  127.     strcat(fmt, "%*lf ");
  128.   }
  129.   return(0l);
  130. }
  131.  
  132. /* Terminates the all reads - clean up */
  133. int end_read(d_struct)
  134. D_STRUCT *d_struct;
  135. {
  136.   close(d_struct->source);
  137. }
  138.  
  139. /* Prints a variable - in a format according to its type */
  140. int print_v_value(v_name, d_struct)
  141. char v_name[];
  142. D_STRUCT *d_struct;
  143. {
  144.   int i;
  145.  
  146.   for(i=0; i<d_struct->count; i++)
  147.   {
  148.     if(strcmp(v_name, d_struct->element[i].name) == 0)
  149.     {
  150.       switch(d_struct->element[i].type)
  151.       {
  152.     case INT:
  153.       printf("%16d ", (int)get_v_value(v_name, d_struct));
  154.       break;
  155.     case FLOAT:
  156.       printf("%16.6f ", (float)get_v_value(v_name, d_struct));
  157.       break;
  158.     case DOUBLE:
  159.       printf("%16.6lf ", (double)get_v_value(v_name, d_struct));
  160.       break;
  161.     default:
  162.       printf("%16s ", "Unknown type");
  163.       break;
  164.       }
  165.       return(1);
  166.     }
  167.   }
  168.   printf("%16s ", "Unknown Variable");
  169.   return(0);
  170. }
  171.  
  172.  
  173. [LISTING THREE]
  174.  
  175. /* Example program - reads data and prints it on the screen */
  176. /* Command line arguments are:                              */
  177. /*      example <filename> <colname> [<colname> ...]        */
  178.  
  179. #include "vman_1.c"
  180.  
  181. main(argc, argv)
  182. int argc;
  183. char *argv[];
  184. {
  185.   int i;
  186.   double dval;
  187.   D_STRUCT d_struct;
  188.  
  189.   if(argc < 3) {
  190.     printf("Proper usage: example <filename> <colname>  [<colname> ...]\n");
  191.     exit(-1);
  192.   }
  193.  
  194.   load_v_defs(argv[1], &d_struct);
  195.   for(i=2; i<argc; i++) printf("  %12s   ", argv[i]);
  196.   printf("\n");
  197.   while(read_data(&d_struct))
  198.   {
  199.     for(i=2; i<argc; i++)
  200.     {
  201.       print_v_value(argv[i], &d_struct);
  202.     }
  203.     printf("\n");
  204.   }
  205.   end_read(&d_struct);
  206. }
  207.  
  208.  
  209.  
  210.  
  211.