home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / educatio / xcoral16.zip / KILL_BUF.C < prev    next >
C/C++ Source or Header  |  1993-01-15  |  5KB  |  207 lines

  1. /*
  2. ** Copyright 1989, 1992 by Lionel Fournigault
  3. **
  4. ** Permission to use, copy, and distribute for non-commercial purposes,
  5. ** is hereby granted without fee, providing that the above copyright
  6. ** notice appear in all copies and that both the copyright notice and this
  7. ** permission notice appear in supporting documentation.
  8. ** The software may be modified for your own purposes, but modified versions
  9. ** may not be distributed.
  10. ** This software is provided "as is" without any expressed or implied warranty.
  11. **
  12. **
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <malloc.h>
  17. #include <string.h>
  18. #include <X11/Xlib.h>
  19.  
  20. #include "text.h"
  21. #include "flist.h"
  22.  
  23. #define KB_SIZE_BUF 256
  24. #define KB_SIZE 20
  25.  
  26. typedef struct {
  27.     char *p;        /* Le buffer */
  28.     unsigned int size;    /* La taille du buffer */
  29.     unsigned int s_len;    /* Longueur de s dans le buffer */
  30.     unsigned int s_lines;    /* Nb de lignes */
  31. } kb;
  32.  
  33. static kb killbuf [KB_SIZE];
  34. static void ShiftBuffers ();
  35.  
  36.  
  37. /*
  38. **    Function name : InitKillBuf
  39. **
  40. **    Description : Creation de la pile des buffers pour ranger ce
  41. **        qui a ete efface.
  42. **    Input : 
  43. **    Ouput :
  44. */
  45. void InitKillBuf ()
  46. {
  47.     register int i;
  48.  
  49.     for ( i=0; i < KB_SIZE; i++ ) {
  50.         killbuf [i].p = (char *) malloc ( KB_SIZE_BUF );
  51.         killbuf [i].size = KB_SIZE_BUF;
  52.         killbuf [i].s_len = 0;
  53.         killbuf [i].s_lines = 0;
  54.        }
  55. }
  56.  
  57.  
  58. /*
  59. **    Function name : StoreInKillBuf
  60. **
  61. **    Description : Met la chaine s de longeur n et
  62. **        contenant n lignes dans la pile.
  63. **
  64. **    Input : La chaine, sa longueur, le nb de lignes.
  65. **    Ouput :
  66. */
  67. void StoreInKillBuf ( s, len, n )
  68.     register char *s;
  69.     register int len;
  70.     register int n;
  71. {
  72.     /*
  73.      * Pas les lignes vides
  74.      */
  75.     if ( (len == 1) && (*s == '\n' ))
  76.         return;
  77.  
  78.     /*
  79.      * Si ya deja quelque chose, on decale.
  80.      */
  81.     if ( killbuf [0].s_len != NULL ) 
  82.         ShiftBuffers ();
  83.  
  84.     /*
  85.      * Si ya pas assez de place, on change
  86.      * le pointeur courant.
  87.      */
  88.     if ( len > killbuf [0].size ) { 
  89.               if ( killbuf [0].p != 0 )
  90.             (void) free ( killbuf [0].p );
  91.         killbuf [0].p = (char *) malloc ( (unsigned) len + 1 );
  92.         killbuf [0].size = len + 1;
  93.     }
  94.  
  95.     /*
  96.      * Copie et mis a jour des infos.
  97.      */       
  98.     (void) strncpy ( killbuf [0].p, s, len ); 
  99.     killbuf [0].s_len = len;
  100.     killbuf [0].s_lines = n;
  101.  
  102. #ifdef DEBUG
  103.        (void) fprintf ( stderr, "Store len = %d size = %d lines = %d\n", 
  104.            killbuf [0].s_len, killbuf [0].size, killbuf [0].s_lines );
  105. #endif DEBUG
  106. }
  107.  
  108.  
  109. /*
  110. **    Function name : ShiftBuffers
  111. **
  112. **    Description :  On decale les buffers pour liberer le premier.
  113. **        Le buffer no 20 est perdu.
  114. **    Input : 
  115. **    Ouput :
  116. */
  117. static void ShiftBuffers ()
  118. {
  119.       register int i;
  120.  
  121.       for ( i=KB_SIZE-1; i > 0; i-- ) { /* On commence par le dernier */
  122.  
  123.          if ( killbuf [i-1].size > killbuf [i].size ) {
  124.                  if ( killbuf [i].p != 0 )
  125.             free ( killbuf [i].p );
  126.         killbuf [i].p = (char *) malloc ( killbuf [i-1].size );
  127.          }
  128.  
  129.              (void) strncpy ( killbuf [i].p, killbuf [i-1].p, (int) killbuf [i-1].s_len );
  130.  
  131.          killbuf [i].size = killbuf [i-1].size;
  132.          killbuf [i].s_len = killbuf [i-1].s_len;
  133.          killbuf [i].s_lines = killbuf [i-1].s_lines;
  134.  
  135. #ifdef DEBUG
  136.          fprintf ( stderr, "Store i = %d p = %d len = %d\n",
  137.               i, killbuf [i].p, killbuf [i].s_len );
  138. #endif DEBUG
  139.       }
  140. }
  141.  
  142.  
  143. /*
  144. **    Function name : RestoreKillBuff
  145. **
  146. **    Description : Restore le la chaine contenu dans
  147. **        le buffer i.
  148. **
  149. **    Input : Le numero dans la pile, longueur, et nb lignes.
  150. **    Ouput : La chaine.
  151. */
  152. char *RestoreKillBuf ( i, len, dn )
  153. register int i;
  154. register int *len; /* Return */
  155. register int *dn;  /* Return */
  156. {
  157.        if ( (i < NULL) || (i > (KB_SIZE-1)) || (killbuf [i].s_len == NULL))
  158.               return NULL;
  159.  
  160.        *len = killbuf [i].s_len;
  161.        *dn =  killbuf [i].s_lines;
  162.        return ( (char *) killbuf [i].p );
  163. }
  164.  
  165.  
  166. /*
  167. **    Function name : LoadKillBuffer
  168. **
  169. **    Description : Charge le contenu de la pile dans
  170. **        un buffer. ( uniquement les debuts de lignes ).
  171. **
  172. **    Input : Le buffer
  173. **    Ouput :
  174. */
  175. void LoadKillBuffer ( buf )
  176. Buf *buf;
  177. {
  178.     register int i, len;
  179.     char tmp[8];
  180.        register char *p;
  181.  
  182. #define MAX_LEN    30
  183.  
  184.        for ( i=0; i < KB_SIZE; i++ ) {
  185.               bzero ( tmp, 8 );
  186.               (void) sprintf ( tmp, "%d  ", i + 1 );
  187.         InsertNchar ( buf, tmp, strlen(tmp) );
  188.         if ( killbuf [i].s_lines < 2 ) {
  189.             if ( (len = killbuf [i].s_len) > MAX_LEN )
  190.                 len = MAX_LEN;
  191.                 }
  192.               else {
  193.                   p = killbuf [i].p;
  194.                   while ( *p != '\n' ) p++;
  195.                   len = p - killbuf [i].p;
  196.             if ( len > MAX_LEN )
  197.                 len = MAX_LEN;
  198.         } 
  199.         if ( len != 0 )
  200.             InsertNchar ( buf, killbuf [i].p, len );
  201.  
  202.         InsertNchar ( buf, " ...\n", 5 ); 
  203.     }
  204. }
  205.  
  206.  
  207.