home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / MISC / LO241SRV.ZIP / VDISK.C < prev    next >
C/C++ Source or Header  |  1998-05-17  |  4KB  |  168 lines

  1.  
  2. // LoraBBS Version 2.41 Free Edition
  3. // Copyright (C) 1987-98 Marco Maccaferri
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 2 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. #include <alloc.h>
  20. #include <stdio.h>
  21. #include <stdarg.h>
  22. #include <string.h>
  23.  
  24. #include "sched.h"
  25. #include "lsetup.h"
  26.  
  27. #define MAX_DISK_BUFFER  16384
  28.  
  29. static char *buffer = NULL;
  30. static int  inbuffer, firstaction;
  31. static long indisk, maxread, readed, dupindisk;
  32.  
  33. FILE *mopen (char *filename, char *mode);
  34. int mclose (FILE *fp);
  35. int mputs (char *s, FILE *fp);
  36. void mprintf (FILE *fp, char *format, ...);
  37. long mseek (FILE *fp, long position, int offset);
  38. int mread (char *s, int n, int e, FILE *fp);
  39. long memlength (void);
  40.  
  41. FILE *mopen (char *filename, char *mode)
  42. {
  43.    if (buffer != NULL)
  44.       free (buffer);
  45.  
  46.    buffer = (char *)malloc (MAX_DISK_BUFFER);
  47.    if (buffer == NULL)
  48.       return (NULL);
  49.  
  50.    inbuffer = 0;
  51.    maxread = readed = dupindisk = indisk = 0L;
  52.    firstaction = 1;
  53.  
  54.    return (fopen (filename, mode));
  55. }
  56.  
  57. int mclose (FILE *fp)
  58. {
  59.    if (buffer != NULL) {
  60.       free (buffer);
  61.       buffer = NULL;
  62.    }
  63.  
  64.    if (fp != NULL)
  65.       return (fclose (fp));
  66.  
  67.    return (EOF);
  68. }
  69.  
  70. int mputs (char *s, FILE *fp)
  71. {
  72.    if (firstaction) {
  73.       maxread = 0L;
  74.       firstaction = 0;
  75.       dupindisk = indisk = 0L;
  76.       fseek (fp, 0L, SEEK_SET);
  77.    }
  78.  
  79.    if (inbuffer + strlen (s) >= MAX_DISK_BUFFER) {
  80.       fwrite (buffer, inbuffer, 1, fp);
  81.       indisk += inbuffer;
  82.       dupindisk += inbuffer;
  83.       inbuffer = 0;
  84.    }
  85.  
  86.    strcpy (&buffer[inbuffer], s);
  87.    inbuffer += strlen (s);
  88.    maxread += strlen (s);
  89.  
  90.    return (s[strlen (s)]);
  91. }
  92.  
  93. void mprintf (FILE *fp, char *format, ...)
  94. {
  95.    va_list var_args;
  96.    char string[256];
  97.  
  98.    if (strlen (format) > 256)
  99.       return;
  100.  
  101.    va_start(var_args,format);
  102.    vsprintf(string,format,var_args);
  103.    va_end(var_args);
  104.  
  105.    mputs (string, fp);
  106. }
  107.  
  108. long mseek (FILE *fp, long position, int offset)
  109. {
  110.    if (dupindisk) {
  111.       if (indisk) {
  112.          fwrite (buffer, inbuffer, 1, fp);
  113.          indisk += inbuffer;
  114.          dupindisk += inbuffer;
  115.          inbuffer = 0;
  116.       }
  117.  
  118.       fseek (fp, position, offset);
  119.  
  120.       fread (buffer, MAX_DISK_BUFFER, 1, fp);
  121.       indisk = dupindisk - (long)MAX_DISK_BUFFER;
  122.    }
  123.  
  124.    inbuffer = 0;
  125.    readed = 0L;
  126.    firstaction = 1;
  127.  
  128.    return (0L);
  129. }
  130.  
  131. int mread (char *s, int n, int e, FILE *fp)
  132. {
  133.    if (firstaction)
  134.       firstaction = 0;
  135.  
  136.    if (readed + e > maxread)
  137.       e = (int)(maxread - readed);
  138.  
  139.    if (inbuffer + e >= MAX_DISK_BUFFER) {
  140.       n = MAX_DISK_BUFFER - inbuffer;
  141.       memcpy (s, &buffer[inbuffer], n);
  142.       if (indisk > (long)MAX_DISK_BUFFER) {
  143.          fread (buffer, MAX_DISK_BUFFER, 1, fp);
  144.          indisk -= (long)MAX_DISK_BUFFER;
  145.       }
  146.       else {
  147.          fread (buffer, (int)indisk, 1, fp);
  148.          indisk = 0L;
  149.       }
  150.       memcpy (&s[n], buffer, e - n);
  151.       inbuffer = e - n;
  152.    }
  153.    else {
  154.       memcpy (s, &buffer[inbuffer], e);
  155.       inbuffer += e;
  156.    }
  157.  
  158.    readed += e;
  159.  
  160.    return (e);
  161. }
  162.  
  163. long memlength (void)
  164. {
  165.    return (maxread);
  166. }
  167.  
  168.