home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 June / SIMTEL_0692.cdr / msdos / modem / cvt100.arc / FILEIO.C < prev    next >
Text File  |  1988-08-02  |  11KB  |  316 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <io.h>
  4. #include <fcntl.h>
  5.  
  6. #define LOGBUFFSIZE 1024
  7.  
  8. /**************************************************************************/
  9. /* Function prototypes                                                    */
  10.  
  11. void Fileinit( void );            /* Initialize the file system */
  12. void SaveSetup( void );           /* Save setup information to file */
  13. int  GetTTSetup( void );          /* Set Communications setup values */
  14. int  GetVidSetup( void );         /* Set Video setup values */
  15.  
  16. void OpenLogFile( void );         /* Open the logfile */
  17. void WriteLog( char );            /* Write a character to the log file */
  18. void FlushLogBuff( void );        /* Flush the log file buffer to disk */
  19. void CloseLogFile( void );        /* Close the log file */
  20.  
  21.  
  22. /**************************************************************************/
  23. /* Global Variables                                                       */
  24.  
  25. char setupfile[] = "CVT100.SET";  /* Filename of setup file */
  26. char logfile[] = "CVT100.LOG";    /* Filename of log file */
  27.  
  28. /**************************************************************************/
  29. /* External Variables                                                     */
  30.  
  31. extern unsigned int port;         /* COM port */
  32. extern unsigned int speed;        /* BAUD rate */
  33. extern char parity[5];            /* Parity setting */
  34. extern unsigned int databits;     /* Number of Data bits */
  35. extern unsigned int stopbits;     /* Number of Stop bits */
  36.  
  37. extern unsigned char retracemode; /* Video snow retrace wait mode flag */
  38. extern unsigned char forecolor;   /* Default foreground color */
  39. extern unsigned char backcolor;   /* Default background color */
  40.  
  41. extern unsigned char backspace;   /* Backspace interpretation flag */
  42. extern unsigned char keyclick;    /* Keyclick on/off flag */
  43. extern unsigned char applkeypad;  /* Application key pad mode flag */
  44.  
  45. extern unsigned originmode;       /* Origin mode, relative or absolute */
  46. extern unsigned insertmode;       /* Insert mode, off or on */
  47. extern unsigned autowrap;         /* Automatic wrap mode, off or on */
  48. extern unsigned newline;          /* Newline mode, off or on,  GLOBAL data!*/
  49. extern unsigned cursorvisible;    /* Cursor visibility, on or hidden */
  50. extern unsigned reversebackground;/* Reverse background attribute, on or off*/
  51. extern unsigned screenwid;        /* Screen column width */
  52. extern unsigned char log;         /* Flag to indicate status of Log */
  53.  
  54. /**************************************************************************/
  55. /* Local Static Data                                                      */
  56.  
  57. static char initfilefound = 0;    /* Flag to indication Setup File found */
  58. static int loghandle;             /* Handle of Log file */
  59. static unsigned char logfileopen = 0; /* Flag to indicate log file is open */
  60.  
  61. static char logbuff[LOGBUFFSIZE]; /* Log file buffer */
  62. static int logcount;              /* Count of characters written to logfile*/
  63.  
  64. static struct com_struct {        /* Structure to hold com parameters */
  65.     int port;
  66.     unsigned speed;
  67.     char parity[5];
  68.     int databits;
  69.     int stopbits;
  70. } com;
  71.  
  72. static struct vid_struct {        /* Structure to hold video parameters */
  73.     unsigned char retracemode;
  74.     unsigned char forecolor;
  75.     unsigned char backcolor;
  76. } vid;
  77.  
  78. static struct key_struct {        /* Structure to hold keyboard parameters */
  79.     unsigned char keyclick;
  80.     unsigned char backspace;
  81.     unsigned char applkeypad;
  82. } key;
  83.  
  84. static struct emul_struct {       /* Structure to hold emulation parameters */
  85.     unsigned int originmode;
  86.     unsigned int insertmode;
  87.     unsigned int autowrap;
  88.     unsigned int newline;
  89.     unsigned int cursorvisible;
  90.     unsigned int reversebackground;
  91.     unsigned int screenwid;
  92.     unsigned char log;
  93. } emul;
  94.  
  95.  
  96. static struct setup_save_struct { /* Setup File block definition */
  97.     char crecord[128];               /* space for communications record */
  98.     char vrecord[128];               /* space for video record */
  99.     char krecord[128];               /* space for keyboard record */
  100.     char erecord[128];               /* space for emulation record */
  101.     } setup;
  102.  
  103.  
  104. /**************************************************************************/
  105. /**************************************************************************/
  106.  
  107.  
  108. /* F I L E I N I T -- initialize the file system */
  109.  
  110. void Fileinit() {
  111.     int handle;
  112.     int bytes;
  113.                                   /* Try and open setup file for input */
  114.     if ( (handle = _open(setupfile,O_RDONLY)) == -1)
  115.         return;                      /* If failed then just return */
  116.  
  117.                                   /* Try and read a setup block from file */
  118.     bytes = _read(handle,&setup,sizeof(setup));
  119.     if (bytes < 0)                   /* If failed then this is a fatal error */
  120.        badexit("IO error reading Setup file");
  121.     else if (bytes != sizeof(setup))
  122.        badexit("Number of bytes read error on Setup file");
  123.     if (_close(handle) != 0)      /* Close the setup file */
  124.        badexit("File Close error on Setup file");
  125.  
  126.     initfilefound = 1;            /* Set setup information read flag */
  127.  
  128.                                   /* Move saved information to parm struct's*/
  129.     memcpy(&com,setup.crecord,sizeof(com));
  130.     memcpy(&vid,setup.vrecord,sizeof(vid));
  131.     memcpy(&key,setup.krecord,sizeof(key));
  132.     memcpy(&emul,setup.erecord,sizeof(emul));
  133.  
  134. }
  135.  
  136.  
  137. /* S A V E S E T U P -- save the setup information */
  138.  
  139. void SaveSetup() {
  140.     int handle;
  141.     int bytes;
  142.  
  143.  
  144.     com.port = port;              /* Save communications parameters */
  145.     com.speed = speed;
  146.     memcpy(com.parity,parity,sizeof(com.parity));
  147.     com.databits = databits;
  148.     com.stopbits = stopbits;
  149.  
  150.     vid.retracemode = retracemode;/* Save video parameters */
  151.     vid.forecolor = forecolor;
  152.     vid.backcolor = backcolor;
  153.  
  154.     key.backspace = backspace;    /* Save KeyBoard parameters */
  155.     key.keyclick = keyclick;
  156.     key.applkeypad = applkeypad;
  157.  
  158.     emul.originmode = originmode; /* Save VT emulation values */
  159.     emul.insertmode = insertmode;
  160.     emul.autowrap = autowrap;
  161.     emul.newline = newline;
  162.     emul.cursorvisible = cursorvisible;
  163.     emul.reversebackground = reversebackground;
  164.     emul.screenwid = screenwid;
  165.     emul.log = log;
  166.  
  167.                                   /* Initialize setup block to zeros */
  168.     memset(&setup,'\0',sizeof(setup));
  169.                                   /* Move parm information to setup block */
  170.     memcpy(setup.crecord,&com,sizeof(com));
  171.     memcpy(setup.vrecord,&vid,sizeof(vid));
  172.     memcpy(setup.krecord,&key,sizeof(key));
  173.     memcpy(setup.erecord,&emul,sizeof(emul));
  174.  
  175.                                   /* Try and create a setup file for writing */
  176.     if ( (handle = _creat(setupfile,0)) == -1)
  177.         badexit("IO error Opening setup file for output");
  178.  
  179.  
  180.                                   /* Try and write the setup block to disk*/
  181.     bytes = _write(handle,&setup,sizeof(setup));
  182.     if (bytes != sizeof(setup))
  183.        badexit("Number of bytes write error on Setup file");
  184.     if (_close(handle) != 0)      /* Try and close the output file */
  185.        badexit("File Close error on Setup file");
  186.  
  187.     initfilefound = 1;            /* Set flag indicating setup is good */
  188.  
  189. }
  190.  
  191.  
  192.  
  193.  
  194. /* G E T T T S E T U P -- Get the communications setup saved in a file */
  195.  
  196. int GetTTSetup( void ) {
  197.  
  198.     if (initfilefound == 0)       /* If no setup file was found */
  199.         return(0);                   /* Then return 0 for no action taken */
  200.  
  201.     port = com.port;              /* Set communications parameters */
  202.     speed = com.speed;            /* to saved values */
  203.     memcpy(parity,com.parity,sizeof(com.parity));
  204.     databits = com.databits;
  205.     stopbits = com.stopbits;
  206.     return(1);                    /* Return 1 for action taken */
  207. }
  208.  
  209. /* G E T V I D S E T U P -- Get the video setup saved in file */
  210.  
  211. int GetVidSetup( void ) {
  212.  
  213.     if (initfilefound == 0)       /* If no setup file was found */
  214.         return(0);                   /* Then return 0 for no action taken */
  215.  
  216.     retracemode = vid.retracemode;/* Set video parameters */
  217.     forecolor = vid.forecolor;    /* to saved values */
  218.     backcolor = vid.backcolor;
  219.     return(1);                    /* Return 1 for action taken */
  220. }
  221.  
  222. /* G E T K E Y S E T U P -- Get the keyboard setup saved in file */
  223.  
  224. int GetKeySetup( void ) {
  225.  
  226.     if (initfilefound == 0)       /* If no setup file was found */
  227.         return(0);                   /* Then return 0 for no action taken */
  228.  
  229.     backspace = key.backspace;    /* Set KeyBoard parameters */
  230.     keyclick = key.keyclick;      /* to saved values */
  231.     applkeypad = key.applkeypad;
  232.     return(1);                    /* Return 1 for action taken */
  233. }
  234.  
  235.  
  236. /* G E T V T S E T U P -- Get the emulation setup saved in file */
  237.  
  238. int GetVTSetup( void ) {
  239.  
  240.     if (initfilefound == 0)       /* If no setup file was found */
  241.         return(0);                   /* Then return 0 for no action taken */
  242.  
  243.     originmode = emul.originmode; /* Set VT emulation values */
  244.     insertmode = emul.insertmode;    /* to saved values */
  245.     autowrap = emul.autowrap;
  246.     newline = emul.newline;
  247.     cursorvisible = emul.cursorvisible;
  248.     reversebackground = emul.reversebackground;
  249.     screenwid = emul.screenwid;
  250.     log = emul.log;
  251.  
  252.     if (log)                      /* Check if logging specified in setup file*/
  253.         OpenLogFile();
  254.  
  255.     return(1);                    /* Return 1 for action taken */
  256. }
  257.  
  258.  
  259. /* O P E N L O G F I L E -- Open the logfile */
  260.  
  261. void OpenLogFile() {
  262.  
  263.     if (logfileopen)              /* Ignore if already open */
  264.         return;
  265.                                   /* Try and open log file for output */
  266.     if ( (loghandle = open(logfile,O_RDWR | O_APPEND)) == -1)
  267.         if ( (loghandle = _creat(logfile,0)) == -1)
  268.             badexit("IO error opening log file for output");
  269.  
  270.     logfileopen = 1;              /* Save Flag indicating log file is open */
  271.     logcount = 0;
  272. }
  273.  
  274.  
  275. /* W R I T E L O G -- Put a char in the logfile buffer, flush if necessary */
  276.  
  277. void WriteLog( char c) {
  278.  
  279.     logbuff[logcount++] = c;      /* Store this character in the buffer */
  280.  
  281.     if (logcount == LOGBUFFSIZE)  /* Check if time to flush buffer */
  282.         FlushLogBuff();
  283. }
  284.  
  285. /* F L U S H L O G B U F -- Flush the log buffer to disk */
  286.  
  287. void FlushLogBuff( ) {
  288.  
  289.     if (logcount > 0)             /* Try and write the chars if any exist */
  290.         if ( write(loghandle,logbuff,logcount) != logcount)
  291.             badexit("IO error writing to log file");
  292.  
  293.     logcount = 0;                 /* Reset the count of logged characters */
  294. }
  295.  
  296. /* C L O S E L O G F I L E -- Close the logfile */
  297.  
  298. void CloseLogFile() {
  299.  
  300.     if (!logfileopen)             /* Ignore if not open */
  301.         return;
  302.  
  303.     FlushLogBuff();               /* Flush any remaining characters */
  304.                                   /* Try and close the log file */
  305.     if ( close(loghandle) == -1)
  306.        badexit("IO error closing log file");
  307.  
  308.     logfileopen = 0;              /* Save Flag indicating log file is closed */
  309. }
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.