home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume29 / persim / part01 / read.c < prev    next >
C/C++ Source or Header  |  1992-04-06  |  6KB  |  204 lines

  1. /*Copyright  (c)   1992  Adam  Stein.   All  Rights Reserved.   
  2.  
  3.   Permission to use,  copy,  modify  and  distribute  without
  4.   charge this software, documentation, images, etc. is grant-
  5.   ed, provided that this copyright and the author's  name  is  
  6.   retained.   
  7.   
  8.   A fee may be charged for this program ONLY to recover costs   
  9.   for distribution (i.e. media costs).  No profit can be made            
  10.   on this program.   
  11.    
  12.   The author assumes no responsibility for disasters (natural   
  13.   or otherwise) as a consequence of use of this software.      
  14.    
  15.   Adam Stein (stein.wbst129@xerox.com)         
  16. */ 
  17.  
  18. #include <stdio.h>
  19. #include <fcntl.h>
  20. #include "persim.h"
  21.  
  22. double atof();
  23.  
  24. /*This routine will read in values for 1D arrays from a file or keyboard.
  25.  
  26.   Inputs:  filename - name of the file containing the values
  27.        filetype - what type this file it is
  28.        numnodes - number of values to read in
  29.        var      - which variable these values are for
  30.   Outputs: 1 if successful, 0 if an error occurred
  31.   Locals:  data     - used to read in data from binary files
  32.        dummy    - used to check if there are too many values in the file
  33.        fp       - file pointer
  34.        loop     - loop through reading in values
  35.   Globals: ASCII    - file is in ascii format
  36.        EOF      - user terminated input
  37.        NULL     - 0
  38. */
  39. readdata(var,numnodes,filename,filetype)
  40. register int numnodes,filetype;
  41. register double var[];
  42. register char *filename;
  43. {
  44.     register int loop;
  45.     register unsigned char *data,dummy[21];
  46.     register FILE *fp;
  47.     char *malloc();
  48.  
  49.     /*If filename isn't NULL, read in values from a file*/
  50.     if(filename) {
  51.       if((fp = fopen(filename,"r")) == NULL) {
  52.         printf("can't open {%s}\n",filename);
  53.         return(0);
  54.       }
  55.  
  56.       /*If binary, read in a line of values at a time*/
  57.       if(filetype != ASCII) {
  58.         if((data = (unsigned char *) malloc(numnodes)) == NULL)
  59.           error();
  60.  
  61.         if(fread(data,1,numnodes,fp) != numnodes) error();
  62.  
  63.         if(fread(&dummy[0],1,1,fp) || !feof(fp))
  64.           puts("warning: too many data points in file");
  65.       }
  66.  
  67.       /*Read values from ascii file or convert from binary array*/
  68.       for(loop = 0;loop < numnodes;++loop)
  69.         if(filetype == ASCII) {
  70.           if(fgets(dummy,21,fp) == NULL) {
  71.         puts("not enough data points in file");
  72.         break;
  73.           } else var[loop] = atof(dummy);
  74.         } else var[loop] = (double) data[loop];
  75.  
  76.       fclose(fp);
  77.     } else for(loop = 0;loop < numnodes;++loop) { /*Get data from keyboard*/
  78.          printf("Data Point #%d? ");
  79.  
  80.          if(fgets(dummy,21,stdin) == (char *) EOF) {
  81.            puts("input ended early at request of the user");
  82.            break;
  83.          } else var[loop] = atof(dummy);
  84.            }
  85.  
  86.     return(1);
  87. }
  88.  
  89. /*This routine will read in the system variables.
  90.  
  91.   Inputs:  filename - name of the file containing the values
  92.   Outputs: none
  93.   Locals:  fd       - file descriptor
  94.        tmp      - temporary holding place for read in values
  95.   Globals: state    - system variables
  96.        O_RDONLY - open file for reading
  97. */
  98. readstate(filename)
  99. register char *filename;
  100. {
  101.     register int fd;
  102.     STATE tmp;
  103.     extern STATE state;
  104.  
  105.     if((fd = open(filename,O_RDONLY)) == -1) {
  106.       printf("can't open {%s}\n",filename);
  107.       return;
  108.     }
  109.  
  110.     if(read(fd,(char *) &tmp,sizeof(tmp)) != sizeof(tmp))
  111.       puts("reading system variables failed");
  112.     else bcopy((char *) &tmp,(char *) &state,sizeof(state));
  113.  
  114.     close(fd);
  115. }
  116.  
  117. /*This routine will read in values for 2D arrays from a file or keyboard.
  118.  
  119.   Inputs:  filename - name of the file containing the values
  120.        filetype - what type this file it is
  121.        innodes  - number of input nodes
  122.        outnodes - number of output nodes
  123.        var      - which variable these values are for
  124.   Outputs: 1 if successful, 0 if an error occurred
  125.   Locals:  fp       - file pointer
  126.        from     - from which input node value
  127.        dummy    - line of input from keyboard
  128.        to       - to which output node value
  129.        value    - weight value
  130.        weight   - read in values (from, to, value)
  131.   Globals: ASCII    - file is in ascii format
  132.        NULL     - 0
  133. */
  134. readwts(var,innodes,outnodes,filename,filetype)
  135. register int innodes,outnodes,filetype;
  136. register double *var[];
  137. register char *filename;
  138. {
  139.     int from,to;
  140.     double value;
  141.     register char dummy[21];
  142.     register FILE *fp;
  143.     WEIGHT weight;
  144.  
  145.     /*If filename isn't NULL, read values from a file*/
  146.     if(filename) {
  147.       if((fp = fopen(filename,"r")) == NULL) {
  148.         printf("can't open {%s}\n",filename);
  149.         return(0);
  150.       }
  151.  
  152.       if(filetype != ASCII)
  153.         while(fread((char *) &weight,sizeof(weight),1,fp) == 1) {
  154.           if((weight.from < 1) || (weight.from > innodes))
  155.         printf("\nthe specified input node #%d is out of bounds\n\n",
  156.                weight.from);
  157.           else if((weight.to < 1) || (weight.to > outnodes))
  158.              printf("\nthe specified output node #%d is out of bounds\n\n",
  159.                     weight.to);
  160.                else var[weight.from-1][weight.to-1] = weight.value;
  161.         }
  162.       else while(fscanf(fp,"%d %d %lf",&from,&to,&value) == 3) {
  163.              if((from < 1) || (from > innodes))
  164.            printf("\nthe specified input node #%d is out of bounds\n\n",
  165.                   from);
  166.              else if((to < 1) || (to > outnodes))
  167.                 printf("\nthe specified output node #%d is out of bounds\n\n",
  168.                        to);
  169.               else var[from-1][to-1] = value;
  170.            }
  171.  
  172.       fclose(fp);
  173.     } else {
  174.          /*Read from keyboard until user quits*/
  175.          while(1) {
  176.            printf("From Input Node? ");
  177.            fgets(dummy,21,stdin);
  178.            if(dummy[0] == '\n') break;
  179.            from = atoi(dummy);
  180.  
  181.            printf("To Output Node? ");
  182.            fgets(dummy,21,stdin);
  183.            if(dummy[0] == '\n') break;
  184.            to = atoi(dummy);
  185.  
  186.            printf("Weight Value? ");
  187.            fgets(dummy,21,stdin);
  188.            if(dummy[0] == '\n') break;
  189.            puts("");
  190.  
  191.                if((from < 1) || (from > innodes))
  192.              printf("\nthe specified input node #%d is out of bounds\n\n",
  193.                     from);
  194.                else if((to < 1) || (to > outnodes))
  195.                   printf("\nthe specified output node #%d is out of bounds\n\n",
  196.                          to);
  197.                 else var[from-1][to-1] = atof(dummy);
  198.          }
  199.            }
  200.  
  201.     return(1);
  202. }
  203.  
  204.