home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / toswinsc.zoo / scrap.c < prev    next >
C/C++ Source or Header  |  1992-10-27  |  3KB  |  181 lines

  1. /*
  2.  * Copyright 1992 Eric R. Smith. All rights reserved.
  3.  * Redistribution is permitted only if the distribution
  4.  * is not for profit, and only if all documentation
  5.  * (including, in particular, the file "copying")
  6.  * is included in the distribution in unmodified form.
  7.  * THIS PROGRAM COMES WITH ABSOLUTELY NO WARRANTY, NOT
  8.  * EVEN THE IMPLIED WARRANTIES OF MERCHANTIBILITY OR
  9.  * FITNESS FOR A PARTICULAR PURPOSE. USE AT YOUR OWN
  10.  * RISK.
  11.  */
  12. #include <osbind.h>
  13. #include <gemfast.h>
  14. #include <aesbind.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include "xgem.h"
  18. #include "toswin.h"
  19. #include "twdefs.h"
  20. #include "twproto.h"
  21.  
  22. static char spath[128];
  23. static char sname[128];
  24.  
  25. static char *default_spath[] =
  26. {
  27.     "C:\\CLIPBRD",
  28.     "U:\\CLIPBRD",
  29.     "A:\\CLIPBRD",
  30.     0
  31. };
  32.  
  33. static void
  34. massage(path)
  35.     char *path;
  36. {
  37.     char *s;
  38.  
  39.     if (!*path) {
  40.         *path++ = '\\';
  41.         *path = 0;
  42.     }
  43.     for (s = path; *s; s++) ;
  44.  
  45.     if (s[-1] == '\\')
  46.         return;        /* already canonical form */
  47.  
  48.     strcpy(s, "\\*.*");
  49.     if (Fsfirst(s, 0xff) == 0) {    /* OK, this is a real directory */
  50.         s[1] = 0;
  51.         return;
  52.     }
  53.     while (s > path && *s != '\\') --s;
  54.     if (s == path)
  55.         *s++ = '\\';
  56.     else
  57.         s++;
  58.  
  59.     *s = 0;
  60. }
  61.  
  62. static int
  63. exists(x)
  64.     char *x;
  65. {
  66.     return Fsfirst(x, 0xff) == 0;
  67. }
  68.  
  69. void
  70. write_scrap(file, data, len)
  71.     char *file, *data;
  72.     int len;
  73. {
  74.     int i, fd;
  75.     char *s, *t;
  76.     char *dta;
  77.  
  78.     spath[0] = 0;
  79.     i = scrp_read(spath);
  80.     if (i == 0 || !spath[0]) {    /* error -- no scrap directory */
  81.         for (i = 0; default_spath[i]; i++) {
  82.             if (exists(default_spath[i])) {
  83.                 strcpy(spath, default_spath[i]);
  84.                 break;
  85.             }
  86.         }
  87.         if (!default_spath[i]) {
  88.             strcpy(spath, default_spath[0]);
  89.             if ((Dsetdrv(Dgetdrv()) & 0x4) == 0)
  90.                 spath[0] = 'A';
  91.             (void)Dcreate(spath);
  92.         }
  93.     }
  94.     massage(spath);        /* get it into canonical format */
  95.  
  96. /* clear the scrapboard directory */
  97.     dta = (char *)Fgetdta();
  98.     s = sname;
  99.     t = spath;
  100.     while (*t) {
  101.         *s++ = *t++;
  102.     }
  103.     strcpy(s, "SCRAP.*");
  104.     i = Fsfirst(sname, 2);
  105.     while (i == 0) {
  106.         strcpy(s, dta+30);
  107.         (void)Fdelete(sname);
  108.         i = Fsnext();
  109.     }
  110.     strcpy(s, file);
  111.     fd = Fcreate(sname, 0);
  112.     if (fd < 0) {
  113.         form_alert(1, AlertStrng(NOSCRAP));
  114.         return;
  115.     }
  116.     i = Fwrite(fd, (long)len, data);
  117.     if (i != len) {
  118.         form_alert(1, AlertStrng(SCRAPERR));
  119.     }
  120.     (void)Fclose(fd);
  121.     scrp_write(spath);
  122. }
  123.  
  124. char *
  125. read_scrap(name)
  126.     char *name;
  127. {
  128.     int i;
  129.     int fd;
  130.     long len, r;
  131.     char *data;
  132.  
  133.     i = scrp_read(spath);
  134.     if (i == 0) return 0;
  135.  
  136.     massage(spath);
  137.     strcpy(sname, spath);
  138.     strcat(sname, name);
  139.  
  140.     fd = Fopen(sname, 0);
  141.     if (fd < 0)
  142.         return 0;
  143.  
  144.     len = Fseek(0L, fd, 2);
  145.     (void)Fseek(0L, fd, 0);
  146.     if (len >= 0) {
  147.         data = malloc((size_t)len+1);
  148.         if (data) {
  149.             r = Fread(fd, len, data);
  150.             if (r <= 0) {
  151.                 free(data);
  152.                 data = 0;
  153.             } else {
  154.                 data[r] = 0;
  155.             }
  156.         }
  157.     } else {
  158.         data = 0;
  159.     }
  160.     (void)Fclose(fd);
  161.     return data;
  162. }
  163.  
  164. int
  165. scrap_exists(name)
  166.     char *name;
  167. {
  168.     int i, fd;
  169.  
  170.     i = scrp_read(spath);
  171.     if (i == 0) return 0;
  172.     massage(spath);
  173.     strcpy(sname, spath);
  174.     strcat(sname, name);
  175.     fd = Fopen(sname, 0);
  176.     if (fd < 0)
  177.         return 0;
  178.     (void)Fclose(fd);
  179.     return 1;
  180. }
  181.