home *** CD-ROM | disk | FTP | other *** search
/ Computer Select (Limited Edition) / Computer Select.iso / dobbs / v17n02 / turtle.exe / TSAVE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-03  |  3.3 KB  |  119 lines

  1. /*****************************************************************
  2.  *                                                               *
  3.  * TSAVE.C   Save DLL for TURTLE.EXE                             *
  4.  *                                                               *
  5.  * These functions link with DLLSTART.ASM to form                *
  6.  * TSAVE.DLL                                                     *
  7.  *                                                               *
  8.  * Al Williams                                                   *
  9.  *                                                               *
  10.  *****************************************************************/
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <malloc.h>
  14. #include "xci.h"
  15. #include "turtle.h"
  16.  
  17. #define EXPORT _far _loadds _export
  18.  
  19. /* locate which buffer number is indicated 0-9 is user buffer
  20.    buffer 10 is the screen. fn gets the file name from the
  21.    command line */
  22. getbuf(char *line,char **fn)
  23.   {
  24.   char *fnn,*buf;
  25.   int bufno;
  26.   bufno=11;        /* default to screen (11-1=10) */
  27. /* read file name */
  28.   fnn=strtok(line," \t");
  29.   if (!fnn) { printf("Syntax error\n"); return -1; }
  30. /* get buffer # (maybe) */
  31.   buf=strtok(NULL," \t");
  32. /* Call getval() in TCMDS.C (imported) to read number */
  33.   if (buf) if (getval(buf,0,1,10,&bufno)) return -1;
  34.   *fn=fnn;
  35.   return --bufno;
  36.   }
  37.  
  38.  
  39. /* save buffer to file */
  40. XCICMD EXPORT save(int cmd, char far *cmds,struct udata *data)
  41.   {
  42.   FILE *f;
  43.   char *fn;
  44.   int bufno,i;
  45.   if (cmd)
  46.     {
  47.     printf("Save the screen (or buffer) to disk\n");
  48.     if (cmd==1)
  49.       printf("Usage: save filename [buffer]\n");
  50.     return;
  51.     }
  52. /* get arguments */
  53.   bufno=getbuf(cmds,&fn);
  54.   if (bufno==-1) return;
  55. /* if in text mode, get screen from buffer
  56.    if in graphics mode, get screen directly from screen */
  57.   if (bufno==10) bufno+=data->textgraph;
  58. /* insure buffer not empty */
  59.   if (!data->store[bufno])
  60.     {
  61.     printf("Buffer %d is empty\n",bufno+1);
  62.     return;
  63.     }
  64. /* open file for writing */
  65.   f=fopen(fn,"wb");
  66. /* write in one chunk */
  67.   if (fwrite(data->store[bufno],(unsigned)64000,1,f)!=1)
  68.      {
  69.      printf("Error writing to file\n");
  70.      }
  71.   if (fclose(f)) printf("Can't close file\n");
  72.   }
  73.  
  74. /* load command in */
  75. XCICMD EXPORT load(int cmd, char far *cmds,struct udata *data)
  76.   {
  77.   int bufno;
  78.   FILE *f;
  79.   char *fn;
  80.   if (cmd)
  81.     {
  82.     printf("Load the screen (or buffer) from disk\n");
  83.     if (cmd==1)
  84.       printf("Usage: load filename [buffer]\n");
  85.     return;
  86.     }
  87. /* call getbuf to interpret arguments */
  88.   bufno=getbuf(cmds,&fn);
  89.   if (bufno==-1) return;
  90. /* if in text mode, get screen from buffer
  91.    if in graphics mode, get screen directly from screen */
  92.   if (bufno==10) bufno+=data->textgraph;
  93. /* if buffer empty allocate space for it */
  94.   if (!data->store[bufno])
  95.     {
  96.     if (!(data->store[bufno]=malloc((unsigned)64000)))
  97.       {
  98.       printf("Out of memory",bufno+1);
  99.       return;
  100.       }
  101.     }
  102. /* read buffer in one big chunk */
  103.   f=fopen(fn,"rb");
  104.   if (fread(data->store[bufno],(unsigned)64000,1,f)!=1)
  105.      {
  106.      printf("Error reading file\n");
  107.      }
  108.   fclose(f);
  109.   }
  110.  
  111.  
  112. /* Init DLL so we can use C library */
  113. unsigned far pascal _loadds dllstart()
  114.   {
  115.   extern void far pascal C_INIT(void);
  116.   C_INIT();
  117.   return 1;
  118.   }
  119.