home *** CD-ROM | disk | FTP | other *** search
/ No Fragments Archive 12: Textmags & Docs / nf_archive_12.iso / MAGS / SOURCES / ATARI_SRC.ZIP / atari source / DSHJ2 / BUFFER.C < prev    next >
Encoding:
C/C++ Source or Header  |  2001-02-10  |  5.8 KB  |  244 lines

  1. /*
  2.     This module contains the routines for manipulating the text buffer.
  3.     The text buffer looks as follows:
  4.  
  5.     +-----------------------+     <== buf_end
  6.     |                    |
  7. 2    |                    |
  8.     |                    |
  9.     +-----------------------+     <== current_char
  10.     |                    |
  11.     |    free_area            |
  12.     |                    |
  13.     +-----------------------+     <== free_start
  14.     |                    |
  15. 1    |                    |
  16.     |                    |
  17.     +-----------------------+     <== buf_start
  18.  
  19.     buf_end:    points at the last    character in the 2nd partition.
  20.     current_char:    points at the first    character in the 2nd partition.
  21.     free_start:    points at the first    free byte in the buffer.
  22.     buf_start:    points at the first    character in the 1st partition.
  23. */
  24. #include    <stdio.h>
  25. #include    <osbind.h>
  26. #include    "defs.h"
  27.  
  28. #define        MIN_LEFT    10    /* minimum edit space left    */
  29. #define        MIN_FREE    80L    /* minimum editing space    */
  30.  
  31. extern    unsigned char    **upd_ptr;    /* region pointer tag array    */
  32. extern    int        upd_tags;    /* number of ptrs in array    */
  33.  
  34. unsigned    cb_size = 0;        /* size of    copy buffer    */
  35. unsigned char    *cb_start = (char *)0;    /* pointer to    copy buffer    */
  36.  
  37. /*
  38.     Function to set Text Window tag pointer to current_char
  39. */
  40. unsigned char    *setwptr(tag)
  41. unsigned    tag;
  42. {
  43.     return(wdw_ptr[tag] = current_char);
  44. }
  45.  
  46. /*
  47.     Function to set Composition tag pointer to current_char
  48. */
  49. unsigned char    *setbptr(tag)
  50. unsigned    tag;
  51. {
  52.     return(buf_ptr[tag] = current_char);
  53. }
  54.  
  55. init_buffer(bp,ep)
  56. unsigned char    *bp, *ep;
  57. {
  58.     int    i;
  59.  
  60.     buf_start    = bp;        /* initialize buffer variables    */
  61.     buf_end        = ep;
  62.     free_start    = buf_start;
  63.     current_char    = buf_end;
  64.     for (i = 0;i < num_tags;) setbptr(i++);
  65.     for (i = 0;i < wdw_tags;) setwptr(i++);
  66. }
  67.  
  68. set_buffer(txtsize)
  69. unsigned long    txtsize;
  70. {
  71.     unsigned char    *start = free_start;
  72.  
  73.     setwptr(TMP4);                /* set end   marker    */
  74.     free_start += txtsize;            /* add new text size    */
  75.     TWrewindow(start);            /* to start position    */
  76.     setwptr(TMP1);                /* set begin marker    */
  77.     backup(0);                /* to start of line    */
  78.     word_wrap(current_char,0,0);        /* word wrap text    */
  79.     TWrewindow(wdw_ptr[TMP1]);        /* to start position    */
  80.     CPrewindow(wdw_ptr[TMP1]);        /* reset CP tag array    */
  81. }
  82.  
  83. unsigned long free_space()            /* number of free bytes */
  84. {
  85.     return((unsigned long)(current_char - free_start));
  86. }
  87.  
  88. /*
  89.     Function to read in a file to cuurent article...
  90.     Returns -1    if an I/O error occured
  91.         2    if no room in buffer
  92.         1    if file is larger than buffer
  93.         0    if read OK
  94. */
  95. read_file(file_ptr)
  96. char    *file_ptr;
  97. {
  98.     int        file;
  99.     unsigned long    rdsize, attempt;
  100.  
  101.     if (free_space() < MIN_FREE * 2)        /* no buffer space.. */
  102.         return(2);
  103.     if ((file = Fopen(file_ptr,FREAD)) == -1)    /* open read mode    */
  104.         return(-1);
  105.     attempt    = free_space() - MIN_FREE;
  106.     rdsize    = Fread(file,attempt,free_start);
  107.     Fclose(file);
  108.     if (rdsize == -1L)
  109.         return(-1);                /* file IO error.... */
  110.     set_buffer(rdsize);
  111.     return(rdsize == attempt);            /* test large file.. */
  112. }
  113.     
  114. /*
  115.     Function to write out an updated article...
  116.     Returns 0  if write OK
  117.         -1 if write error
  118. */
  119. write_file(file_ptr)
  120. char    *file_ptr;
  121. {
  122.     int    file;
  123.     long    status;
  124.  
  125.     file = Fcreate(file_ptr,NWFIL);
  126.     if (file == -1) return(-1);
  127.     CPrewindow(buf_end);
  128.     status = Fwrite(file,(long)(free_start - buf_start),buf_start);
  129.     Fclose(file);
  130.     if (status != (long)(free_start - buf_start))
  131.         return(-1);
  132.     else    return(0);
  133. }
  134.  
  135. /*
  136.     Move the free area window and adjust 'buf_ptr' accordingly.
  137. */
  138. CPrewindow(new_end)
  139. unsigned char    *new_end;
  140. {
  141.     mv_f_area(new_end,&buf_ptr[0],&buf_ptr[num_tags]);
  142. }
  143.  
  144. /*
  145.     Move the free area window and adjust 'wdw_ptr' accordingly.
  146. */
  147. TWrewindow(new_end)
  148. unsigned char    *new_end;
  149. {
  150.     unsigned char    **bptr, **eptr;
  151.  
  152.     mv_f_area(new_end,&wdw_ptr[0],&wdw_ptr[wdw_tags]);
  153.     if (upd_ptr) {
  154.         bptr = &upd_ptr[0];
  155.         eptr = &upd_ptr[upd_tags];
  156.         mv_f_area(new_end,bptr,eptr);
  157.         while (bptr < eptr) {
  158.             if (!*bptr) *bptr = current_char;
  159.             ++bptr;
  160.         }
  161.     }
  162. }
  163.  
  164. /*
  165.     Move the free area window with 'current_char' set to 'new_end'
  166.     and adjust all pointers in the array pointer.
  167. */
  168. mv_f_area(new_end,bptr,eptr)
  169. unsigned char    *new_end, **bptr, **eptr;
  170. {
  171.     if (!new_end)
  172.         new_end = current_char;
  173.     if (new_end < free_start) {        /* if in 1st partition    */
  174.       if (new_end < buf_start)
  175.         new_end = buf_start;
  176.       while (bptr < eptr) {
  177.         if (*bptr) {
  178.           if (*bptr >= new_end && *bptr < free_start)
  179.             *bptr = current_char - (long)(free_start - *bptr);
  180.           else
  181.           if (*bptr >= free_start && *bptr < current_char)
  182.             *bptr = 0L;
  183.         }
  184.         ++bptr;
  185.       }
  186.       Lr_move(free_start-1,current_char-1,(long)(free_start - new_end));
  187.       current_char -= (long)(free_start - new_end);
  188.       free_start    = new_end;
  189.     }
  190.     else
  191.     if (new_end > current_char) {        /* if in 2nd partition    */
  192.       if (new_end > buf_end)
  193.         new_end = buf_end;
  194.       while (bptr < eptr) {
  195.         if (*bptr) {
  196.           if (*bptr >= current_char && *bptr < new_end)
  197.             *bptr = free_start + (long)(*bptr - current_char);
  198.           else
  199.           if (*bptr >= free_start && *bptr < current_char)
  200.             *bptr = 0L;
  201.         }
  202.         ++bptr;
  203.       }
  204.       Lf_move(current_char,free_start,(long)(new_end - current_char));
  205.       free_start    += (long)(new_end - current_char);
  206.       current_char    = new_end;
  207.     }
  208.     else
  209.     for (;bptr < eptr;++bptr)
  210.         if (*bptr >= free_start && *bptr < current_char)
  211.             *bptr = 0L;
  212. }
  213.  
  214. /*
  215.     Function to copy data out of the copy buffer
  216. */
  217. cb_bufout(ptr)
  218. unsigned char *ptr;
  219. {
  220.     f_move(cb_start,ptr,cb_size);
  221. }
  222.  
  223. /*
  224.     Function to copy data into the copy buffer.
  225.     It will overwrite the previous contents of the copy buffer.
  226. */
  227. cb_bufin(ptr,len)
  228. unsigned char    *ptr;
  229. unsigned long    len;
  230. {
  231.     unsigned long    fsp;
  232.  
  233.     fsp = free_space();
  234.     if (len > 65535L || fsp <= MIN_FREE || len > fsp - MIN_LEFT)
  235.         return(1);            /* no space for command    */
  236.     if (cb_start)                /* free old one...    */
  237.         free(cb_start);
  238.     cb_start = malloc((unsigned)len + 2);    /* malloc save buffer    */
  239.     if (!cb_start)
  240.         return(1);            /* no memory buffer...    */
  241.     f_move(ptr,cb_start,cb_size = (unsigned)len);
  242.     return(0);
  243. }
  244.