home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0010 - 0019 / ibm0010-0019 / ibm0010.tar / ibm0010 / PROCWRKB.ZIP / BENCH1.ZIP / HELP / GARBAGE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-07  |  3.9 KB  |  172 lines

  1. /* ==( help/garbage.c )== */
  2. /* ----------------------------------------------- */
  3. /* Pro-C  Copyright (C) 1988 - 1990 Vestronix Inc. */
  4. /* Modification to this source is not supported    */
  5. /* by Vestronix Inc.                               */
  6. /*            All Rights Reserved                  */
  7. /* ----------------------------------------------- */
  8. /* Written   JPK  26-Sep-88                        */
  9. /* Modified  Geo  18-Jan-90  See comments below    */
  10. /* ----------------------------------------------- */
  11. /* %W%  (%H% %T%) */
  12.  
  13. /*
  14.  *  Modifications
  15.  *
  16.  *  18-Jan-90  Geo - Allowed access for renfile for dirw()
  17.  *  12-Dec-89  Geo - V2 version with variable lines
  18.  *  25-Oct-89  Geo - 1.32 Merge
  19.  *
  20. */
  21.  
  22. /*
  23.  * contains the routines that monitor and clean the garbage from
  24.  * the help text file
  25. */
  26.  
  27. /* 
  28.  * Routines in this file :
  29.  *
  30.  * static int renfile(char *, char *);
  31.  *     renames a file
  32.  *
  33.  * void put_null_rec(void);
  34.  *    forces an initial null record in index file
  35.  *
  36.  * int eat_garbage(char *);
  37.  *    cleans the fragmentation out of the help text file
  38.  *
  39.  */
  40.  
  41. # include <stdio.h> 
  42. # include <bench.h>
  43. # include <fileio.h>
  44. # include "help.h"
  45.  
  46. PROTO (int renfile, (char *, char *));
  47. PROTO (long fsize, (int));
  48.  
  49. /* This is required by dirw() as well */
  50.  
  51. int renfile(old, new)
  52.     char    * old;
  53.     char    * new;
  54. {
  55. # ifdef UNIX
  56.     char buffer[80];
  57.  
  58.     (void) sprintf(buffer, "/bin/mv %s %s", old, new);
  59.     return(system(buffer));
  60.  
  61. # else
  62.  
  63.     return(rename(old, new));
  64.  
  65. # endif
  66. }
  67.  
  68. void get_garbage_rec(garbage_ndx)
  69. struct help_ndx *garbage_ndx;
  70. {
  71.     /* write total amount of garbage */
  72.     get_rec(nfd, 0, (char *)garbage_ndx);
  73. }
  74.  
  75. void put_garbage_rec(garbage_ndx)
  76. struct help_ndx *garbage_ndx;
  77. {
  78.     /* write total amount of garbage */
  79.     put_rec(nfd, 0, (char *)garbage_ndx);
  80. }
  81.  
  82. int eat_garbage(file)
  83.     char     * file;
  84. {
  85.     FILE     *newt;
  86.     int    newi = -1;
  87.     char    oldhlp[128];
  88.     char    newhlp[128];
  89.     char    oldndx[128];
  90.     char    newndx[128];
  91.     struct    help_ndx h_new;
  92.     int     num = 1;
  93.     int     size = 0;
  94.     int      ch;
  95.  
  96.     get_garbage_rec(&h_new);
  97.     if (h_new.size < fsize(fileno(hfptr))/5)
  98.         return(TRUE);
  99.  
  100. /* clean all the fragmentation out of the help file by copying the help */
  101. /* nodes from the old help file to a new one at newly calculated seek   */
  102. /* positions                                                            */
  103.  
  104.     (void) strcpy(newndx, file);
  105.     (void) strcat(newndx, ".onf");
  106.     (void) strcpy(newhlp, file);
  107.     (void) strcat(newhlp, ".ohf");
  108.  
  109.     (void) strcpy(oldndx, file);
  110.     (void) strcat(oldndx, ".ndx");
  111.     (void) strcpy(oldhlp, file);
  112.     (void) strcat(oldhlp, ".hlp");
  113.  
  114.     openf(newndx, SH_OPENRWC, sizeof(struct help_ndx), &newi);
  115.     if ((newt = fopen(newhlp, "w+")) == NULL)
  116.         return(FALSE);
  117.  
  118.     /* Should be no Garbage on a new file */
  119.     h_new.size = 0;
  120.     h_new.seekpos = 0L;
  121.     put_rec(newi, 0, (char *)&h_new);
  122.  
  123.     /* loop through existing help messages */
  124.     while (read_index(num) != FALSE)
  125.     {
  126.         h_new.size = h_ndx.size;
  127.         h_new.seekpos = fsize(fileno(newt));
  128.  
  129.         /* Help text */
  130.         fseek(hfptr, h_ndx.seekpos, SEEK_SET);
  131.         fseek(newt, h_new.seekpos, SEEK_SET);
  132.  
  133.         /* copy the help stuff to the new file */
  134.         while(((ch = getc(hfptr)) != EOF)  && ch != '\f'/*  && 
  135.             (size < h_ndx.size + HSTRSIZE) */)
  136.         {
  137.             putc(ch, newt);
  138.             size++;
  139.         }
  140.             putc('\f', newt);
  141.         fflush(newt);
  142.  
  143.         /* write the new index record */
  144.         put_rec(newi, num, (char *)&h_new);
  145.  
  146.         num++;
  147.         size = 0;
  148.     }
  149.     /* close the files */
  150.     closef(nfd);
  151.     closef(newi);
  152.     nfd = -1;
  153.     (void) fclose(hfptr);
  154.     (void) fclose(newt);
  155.  
  156.     /* erase the old files */
  157.     unlink(oldhlp);
  158.     unlink(oldndx);
  159.  
  160.     /* rename the new files to that of the old files */
  161.     (void) renfile(newhlp, oldhlp);
  162.     (void) renfile(newndx, oldndx);
  163.  
  164.     /* reopen the updated help files */
  165.     openf(oldndx, SH_OPENRWNC, sizeof(struct help_ndx), &nfd);
  166.     lock_index_file(WLOK);
  167.     if ((hfptr = fopen(oldhlp, "r+")) == NULL)
  168.         return(FALSE);
  169.  
  170.     return(TRUE);
  171. }
  172.