home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / SNNSV32.ZIP / SNNSv3.2 / tools / sources / pat_sel.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-25  |  9.2 KB  |  337 lines

  1. /*****************************************************************************
  2.   FILE           : pat_sel.c
  3.   SHORTNAME      : 
  4.   SNNS VERSION   : 3.2
  5.  
  6.   PURPOSE        : Pattern Selector
  7.   NOTES          :
  8.  
  9.   AUTHOR         : Stefan Broeckel, Tobias Soyez 
  10.   DATE           : 30.07.92
  11.   VERSION        : 1.0
  12.  
  13.   CHANGED BY     : 
  14.   IDENTIFICATION : @(#)pat_sel.c    1.3 2/28/94
  15.   SCCS VERSION   : 1.3
  16.   LAST CHANGE    : 2/28/94
  17.  
  18.              Copyright (c) 1990-1994  SNNS Group, IPVR, Univ. Stuttgart, FRG
  19.  
  20. ******************************************************************************/
  21.  
  22.  
  23. /******************************************************************************/
  24. /* included headers                                                           */
  25. /******************************************************************************/
  26.  
  27. #include <stdio.h>
  28. #include <time.h>
  29.  
  30.  
  31. /******************************************************************************/
  32. /* constants                                                                  */
  33. /******************************************************************************/
  34.  
  35. #define NO_ERROR      0
  36. #define BAD_PAT_FILE  1
  37. #define WRITE_ERR     2
  38. #define UNEXP_EOF     3
  39.  
  40.  
  41. /******************************************************************************/
  42. /* global variables                                                           */
  43. /******************************************************************************/
  44.  
  45. FILE  *in_no_file   ;
  46. FILE  *in_pat_file  ;
  47. FILE  *out_pat_file ;
  48.  
  49.  
  50. static char *patHeader[] =
  51. {  
  52.   "SNNS pattern definition file %s\n",
  53.   "generated at",
  54.   "No. of patterns     : %d\n",
  55.   "No. of input units  : %d\n",
  56.   "No. of output units : %d\n"
  57. } ;
  58.  
  59.  
  60.  
  61. /******************************************************************************/
  62. /* function skip_comment                                                      */
  63. /******************************************************************************/
  64.  
  65. skip_comment ()
  66.   
  67.   {
  68.     register int  c ;
  69.     int           q ;
  70.  
  71.     q    = 1 ;
  72.  
  73.     while (q != 3)
  74.     {
  75.       c = getc (in_pat_file) ;
  76.  
  77.       switch (c)
  78.       {
  79.         case ' '  : break ;  
  80.         case '\n' : if (q == 2) q = 3 ; break ;
  81.         case '#'  : if (q == 1) q = 2 ; break ;
  82.         case EOF  :             q = 3 ; break ;
  83.         default   : if (q == 1) q = 3 ; break ;
  84.       }
  85.     }
  86.  
  87.     ungetc (c, in_pat_file) ;
  88.   }
  89.  
  90.  
  91.  
  92. /******************************************************************************/
  93. /* function read_file_header                                                  */
  94. /******************************************************************************/
  95.  
  96. int read_file_header (no_of_patterns, no_of_input_units, no_of_output_units,
  97.                       version)
  98.  
  99.   int  *no_of_patterns     ;
  100.   int  *no_of_input_units  ;
  101.   int  *no_of_output_units ;
  102.   char *version            ;
  103.  
  104.   {
  105.     char  dstring [255] ;
  106.  
  107.  
  108.     skip_comment() ;
  109.  
  110.     if (fscanf(in_pat_file, patHeader[0], version) == 1)
  111.     { 
  112.       if (fscanf(in_pat_file, patHeader[1]) != 0)    return (BAD_PAT_FILE) ;
  113.       if (fgets (dstring, 254, in_pat_file) == NULL) return (BAD_PAT_FILE) ;
  114.       skip_comment() ;
  115.       if (fscanf(in_pat_file, patHeader[2], no_of_patterns)     != 1)
  116.          return (BAD_PAT_FILE) ;
  117.       if (fscanf(in_pat_file, patHeader[3], no_of_input_units)  != 1)
  118.          return (BAD_PAT_FILE) ;
  119.       if (fscanf(in_pat_file, patHeader[4], no_of_output_units) != 1)
  120.          return (BAD_PAT_FILE) ;
  121.     }
  122.     else
  123.     {
  124.       sprintf (version, "%s", "V1.4-3D") ;
  125.       if (fscanf (in_pat_file, "%d%d%d", no_of_patterns, no_of_input_units,
  126.           no_of_output_units) != 3) return (BAD_PAT_FILE) ;
  127.     }
  128.  
  129.     return (NO_ERROR) ;
  130.   }
  131.  
  132.  
  133.  
  134. /******************************************************************************/
  135. /* function write_file_header                                                 */
  136. /******************************************************************************/
  137.  
  138. int write_file_header (no_of_patterns, no_of_input_units, no_of_output_units,
  139.                        version)
  140.  
  141.   int  no_of_patterns     ;
  142.   int  no_of_input_units  ;
  143.   int  no_of_output_units ;
  144.   char *version           ;
  145.  
  146.  
  147.   {
  148.     time_t  clock ;
  149.  
  150.  
  151.     fprintf(out_pat_file, patHeader[0], version) ;
  152.     fprintf(out_pat_file, patHeader[1]) ;
  153.     (void) time (&clock) ;
  154.     fprintf(out_pat_file, " %s\n", ctime (&clock)) ;
  155.     fprintf(out_pat_file, patHeader[2], no_of_patterns    ) ;
  156.     fprintf(out_pat_file, patHeader[3], no_of_input_units ) ;
  157.     fprintf(out_pat_file, patHeader[4], no_of_output_units) ;
  158.     fprintf(out_pat_file, "\n") ;
  159.     return (NO_ERROR) ;
  160.   }
  161.  
  162.  
  163.  
  164. /******************************************************************************/
  165. /* main program                                                               */
  166. /******************************************************************************/
  167.       
  168. main (argc, argv)
  169.  
  170. int  argc    ;
  171. char *argv[] ;
  172.  
  173.   float  *output_pattern    ;
  174.   float  *input_pattern     ;
  175.   char   version[255]       ;
  176.   int    no_of_patterns     ;
  177.   int    no_of_input_units  ;
  178.   int    no_of_output_units ;
  179.   int    no_of_sel_pat      ;
  180.   int    pat_no             ;
  181.   int    act_pat            ;
  182.   int    error, i, j        ;
  183.   
  184.  
  185.   error = NO_ERROR ;
  186.  
  187.   if (argc != 4)
  188.   {  
  189.     fprintf (stderr, "usage: %s <no_file> <in_pat_file> <out_pat_file>\n",
  190.              argv[0]); 
  191.     return ;
  192.   }
  193.  
  194.   
  195.   if ((in_no_file   = fopen(argv[1], "r")) == (FILE *) NULL) 
  196.   { 
  197.     fprintf(stderr, "error: can't read file %s\n", argv[1]) ;
  198.     return ;
  199.   }
  200.   
  201.   if ((in_pat_file  = fopen(argv[2], "r")) == (FILE *) NULL)
  202.   { 
  203.     fprintf(stderr, "error: can't read file %s\n", argv[2]) ;
  204.     fclose (in_no_file) ;
  205.     return ;
  206.   }
  207.  
  208.   if ((out_pat_file = fopen(argv[3], "r")) != (FILE *) NULL)
  209.   {
  210.     fclose(out_pat_file) ;
  211.     fprintf (stderr, "overwrite %s (y/n) ? ", argv[3]) ;
  212.     if (getc(stdin) != 'y') return ; 
  213.   }
  214.  
  215.   if ((out_pat_file = fopen(argv[3], "w")) == (FILE *) NULL)
  216.   {
  217.     fprintf(stderr, "error: can't create file %s\n", argv[3]) ;
  218.     fclose (in_no_file)  ;
  219.     fclose (in_pat_file) ;
  220.     return ;
  221.   }
  222.  
  223.   
  224.   act_pat       = 1 ; 
  225.   no_of_sel_pat = 0 ;
  226.  
  227.   /* number of entries in the number file */
  228.   while (fscanf(in_no_file, "%d", &pat_no) == 1) no_of_sel_pat++ ;
  229.   fseek (in_no_file, 0, 0) ;
  230.  
  231.   if (read_file_header  (&no_of_patterns, &no_of_input_units,
  232.       &no_of_output_units, version) != 0) error = BAD_PAT_FILE ;
  233.    
  234.   if (write_file_header (no_of_sel_pat, no_of_input_units,
  235.       no_of_output_units, version) != 0)  error = WRITE_ERR ; 
  236.  
  237.   input_pattern  = (float *) malloc(no_of_input_units  * sizeof(float)) ;
  238.   output_pattern = (float *) malloc(no_of_output_units * sizeof(float)) ;
  239.  
  240.   /* select patterns */
  241.   for (i = 1 ; (i <= no_of_sel_pat) && (error == NO_ERROR) ; i++)
  242.   { 
  243.     if (fscanf (in_no_file, "%d", &pat_no) == EOF) error == UNEXP_EOF ;
  244.   
  245.     /* search pattern */
  246.     while ((act_pat != pat_no) && (error == NO_ERROR))
  247.     {  
  248.       for (j = 0 ; j < no_of_input_units ; j++)
  249.       {
  250.         skip_comment () ;
  251.         if (fscanf (in_pat_file, "%*f") == EOF) error = UNEXP_EOF ;
  252.       }  
  253.       for (j = 0 ; j < no_of_output_units ; j++)
  254.       {
  255.         skip_comment () ;
  256.         if (fscanf (in_pat_file, "%*f") == EOF) error = UNEXP_EOF ;
  257.       } 
  258.       act_pat++ ;       
  259.     }
  260.     
  261.     /* read pattern */
  262.     if (error == NO_ERROR)
  263.     {
  264.       for (j = 0 ; j < no_of_input_units ; j++)
  265.       {
  266.         skip_comment () ;
  267.         if (fscanf (in_pat_file, "%f", &input_pattern[j]) == EOF)
  268.             error = UNEXP_EOF ;
  269.       }  
  270.       for (j = 0 ; j < no_of_output_units ; j++)
  271.       {
  272.         skip_comment () ;
  273.         if (fscanf (in_pat_file, "%f", &output_pattern[j]) == EOF)
  274.             error = UNEXP_EOF ;
  275.       }
  276.       
  277.       act_pat++ ;
  278.  
  279.       /* write pattern */
  280.       if (error == NO_ERROR)
  281.       {
  282.         fprintf (out_pat_file, "#%d\n", i) ;
  283.  
  284.         for (j = 0 ; j < no_of_input_units ; j++)
  285.         {
  286.           if      (input_pattern[j] == 0.0) fprintf (out_pat_file, "0 ") ;
  287.           else if (input_pattern[j] == 1.0) fprintf (out_pat_file, "1 ") ;
  288.           else    fprintf (out_pat_file, "%f ", input_pattern[j]) ;
  289.  
  290.           if ((j % 10) == 9) fprintf (out_pat_file, "\n") ;
  291.         } 
  292.         fprintf (out_pat_file, "\n") ;
  293.  
  294.         for (j = 0 ; j < no_of_output_units ; j++)
  295.         {
  296.           if      (output_pattern[j] == 0.0) fprintf (out_pat_file, "0 ") ;
  297.           else if (output_pattern[j] == 1.0) fprintf (out_pat_file, "1 ") ;
  298.           else    fprintf (out_pat_file, "%f ", output_pattern[j]) ;
  299.  
  300.           if ((j % 10) == 9) fprintf (out_pat_file, "\n") ;
  301.         } 
  302.         fprintf (out_pat_file, "\n") ;
  303.       }
  304.     }
  305.   }
  306.  
  307.   switch (error)
  308.   {
  309.     case BAD_PAT_FILE : fprintf(stderr, "error: bad pattern file\n")       ;
  310.                         break ;
  311.     case WRITE_ERR    : fprintf(stderr, "error: write error\n")            ;
  312.                         break ;
  313.     case UNEXP_EOF    : fprintf(stderr, "error: unexpected end of file\n") ;
  314.                         break ;
  315.     default           : break ;
  316.   }
  317.  
  318.   free (input_pattern)  ;
  319.   free (output_pattern) ;
  320.  
  321.   fclose (in_no_file)   ;
  322.   fclose (in_pat_file)  ;
  323.   fclose (out_pat_file) ;
  324. }
  325.  
  326.  
  327. /******************************************************************************/
  328. /* end of file                                                                */
  329. /******************************************************************************/
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.