home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / cprog / actlib12.zip / TOOLS.ZIP / OPTIONS.C < prev    next >
C/C++ Source or Header  |  1993-02-25  |  3KB  |  103 lines

  1. /*  Copyright (C) 1993   Marc Stern  (internet: stern@mble.philips.be)  */
  2.  
  3. #include "tools.h"
  4. #include <direct.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <dos.h>
  8.  
  9.  
  10. extern char **_argv;  /* if not in Borland */
  11.  
  12. char optfilename[_MAX_PATH];
  13.  
  14.  
  15. /***
  16.  *
  17.  *  Function   :    load_options
  18.  *
  19.  *  Topics     :    Load options from a file.
  20.  *
  21.  *  Decisions  :    The file has the same name as the program
  22.  *                  with extension .OPT and is searched in
  23.  *                  current directory, then in program directory.
  24.  *                  If option file is not found, options are not modified.
  25.  *
  26.  *  Parameters :    out  void * options     ptr to variable containing options
  27.  *                  in   size_t size        size of data to read
  28.  *
  29.  *  Return     :    number of bytes read
  30.  *                  -1 if file was not found
  31.  *
  32.  ***/
  33.  
  34. int load_options( void *options, size_t size )
  35.  
  36. { char *ptr;
  37.   FILE *optfile;
  38.   int r;
  39.  
  40.   strcpy( optfilename, _argv[0] );
  41.   fnreduce( optfilename );
  42.   ptr = strrchr( optfilename, '.' ) + 1;
  43.   strcpy( ptr, "OPT" );
  44.  
  45.   ptr = strrchr( optfilename, '\\' ) + 1;
  46.   optfile = fopen( ptr, "rb" );
  47.   if ( optfile ) { strcpy( optfilename, ptr );
  48.                    fnreduce( optfilename );
  49.                  }
  50.             else {
  51.                    optfile = fopen( optfilename, "rb" );
  52.                    if ( ! optfile ) { *optfilename = '\0';
  53.                                       return -1;
  54.                                     }
  55.                  }
  56.  
  57.   r = fread( options, 1, size, optfile );
  58.   fclose( optfile );
  59.   return r;
  60. }
  61.  
  62.  
  63.  
  64. /***
  65.  *
  66.  *  Function   :    save_options
  67.  *
  68.  *  Topics     :    Save options into a file.
  69.  *
  70.  *  Decisions  :    If options were previously loaded, this file is used.
  71.  *                  Otherwise, the file has the same name as the program
  72.  *                  with extension .OPT and is located in program directory.
  73.  *
  74.  *  Parameters :    in  void * options     ptr to variable containing options
  75.  *                  in  size_t size        size of data to read
  76.  *
  77.  *  Return     :    number of bytes written
  78.  *                  -1 if file not opened
  79.  *
  80.  ***/
  81.  
  82. int save_options( const void *options, size_t size )
  83.  
  84. { char *ptr;
  85.   FILE *optfile;
  86.   int r;
  87.  
  88.   if ( ! *optfilename )
  89.      {
  90.        strcpy( optfilename, _argv[0] );
  91.        fnreduce( optfilename );
  92.        ptr = strrchr( optfilename, '.' ) + 1;
  93.        strcpy( ptr, "OPT" );
  94.      }
  95.  
  96.   optfile = fopen( optfilename, "wb" );
  97.   if ( ! optfile ) return -1;
  98.  
  99.   r = fwrite( options, 1, size, optfile );
  100.   fclose( optfile );
  101.   return r;
  102. }
  103.