home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tv20cpp.zip / src / histlist.cpp < prev    next >
C/C++ Source or Header  |  1998-01-19  |  3KB  |  180 lines

  1. /*
  2.  * histlist.cc
  3.  *
  4.  * Turbo Vision - Version 2.0
  5.  *
  6.  * Copyright (c) 1994 by Borland International
  7.  * All Rights Reserved.
  8.  *
  9.  * Modified by Sergio Sigala <ssigala@globalnet.it>
  10.  */
  11.  
  12. #include <tvision/tv.h>
  13.  
  14. #include <stdlib.h>
  15. #include <string.h>
  16.  
  17. class HistRec
  18. {
  19.  
  20. public:
  21.  
  22.     HistRec( uchar nId, const char *nStr );
  23.  
  24.     void *operator new( size_t );
  25.     void *operator new( size_t, HistRec * );
  26.  
  27.     uchar id;
  28.     uchar len;
  29.     char str[1];
  30.  
  31. };
  32.  
  33. void *HistRec::operator new( size_t, HistRec *hr )
  34. {
  35.     return hr;
  36. }
  37.  
  38. void *HistRec::operator new( size_t )
  39. {
  40.     abort();
  41.     return 0;
  42. }
  43.  
  44. inline HistRec::HistRec( uchar nId, const char *nStr ) :
  45.     id( nId ),
  46.     len( strlen( nStr ) + 3 )
  47. {
  48.     strcpy( str, nStr );
  49. }
  50.  
  51.  
  52. inline HistRec *advance( HistRec *ptr, size_t s )
  53. {
  54.     return (HistRec *)((char *)ptr + s);
  55. }
  56.  
  57. inline HistRec *backup( HistRec *ptr, size_t s )
  58. {
  59.     return (HistRec *)((char *)ptr - s);
  60. }
  61.  
  62. inline HistRec *next( HistRec *ptr )
  63. {
  64.     return advance( ptr, ptr->len );
  65. }
  66.  
  67. inline HistRec *prev( HistRec *ptr )
  68. {
  69.     return backup( ptr, ptr->len );
  70. }
  71.  
  72. ushort historySize = 1024;  // initial size of history block
  73.  
  74. static uchar curId;
  75. static HistRec *curRec;
  76. static HistRec *historyBlock;
  77. static HistRec *lastRec;
  78.  
  79. void advanceStringPointer()
  80. {
  81.     curRec = next( curRec );
  82.     while( curRec < lastRec && curRec->id != curId )
  83.         curRec = next( curRec );
  84.     if( curRec >= lastRec )
  85.         curRec = 0;
  86. }
  87.  
  88. void deleteString()
  89. {
  90.     size_t len = curRec->len;
  91.  
  92. #ifndef __UNPATCHED
  93.     // BUG FIX - EFW - Mon 10/30/95
  94.     // This insures that if n = lastRec, no bytes are copied and
  95.     // a GPF is prevented.
  96.     HistRec *n = next(curRec);
  97.     memcpy(curRec, n, size_t((char *)lastRec - (char *)n));
  98. #else
  99.     memcpy(curRec, next(curRec),    size_t( (char *)lastRec - (char *)curRec ) );
  100. #endif
  101.     lastRec = backup( lastRec, len );
  102. }
  103.  
  104. void insertString( uchar id, const char *str )
  105. {
  106.     ushort len = strlen( str ) + 3;
  107.     while( len > historySize - ( (char *)lastRec - (char *)historyBlock ) )
  108.         {
  109.         ushort firstLen = historyBlock->len;
  110.         HistRec *dst = historyBlock;
  111.         HistRec *src = next( historyBlock );
  112.         memcpy( dst, src,  size_t( (char *)lastRec - (char *)src ) );
  113.         lastRec = backup( lastRec, firstLen );
  114.         }
  115.     new( lastRec ) HistRec( id, str );
  116.     lastRec = next( lastRec );
  117. }
  118.  
  119. void startId( uchar id )
  120. {
  121.     curId = id;
  122.     curRec = historyBlock;
  123. }
  124.  
  125. ushort historyCount( uchar id )
  126. {
  127.     startId( id );
  128.     ushort count =  0;
  129.     advanceStringPointer();
  130.     while( curRec != 0 )
  131.         {
  132.         count++;
  133.         advanceStringPointer();
  134.         }
  135.     return count;
  136. }
  137.  
  138. void historyAdd( uchar id, const char *str )
  139. {
  140.     if( str[0] == EOS )
  141.         return;
  142.     startId( id );
  143.     advanceStringPointer();
  144.     while( curRec != 0 )
  145.         {
  146.         if( strcmp( str, curRec->str ) == 0 )
  147.             deleteString();
  148.         advanceStringPointer();
  149.         }
  150.     insertString( id, str );
  151. }
  152.  
  153. const char *historyStr( uchar id, int index )
  154. {
  155.     startId( id );
  156.     for( short i = 0; i <= index; i++ )
  157.         advanceStringPointer();
  158.     if( curRec != 0 )
  159.         return curRec->str;
  160.     else
  161.         return 0;
  162. }
  163.  
  164. void clearHistory()
  165. {
  166.     new (historyBlock) HistRec( 0, "" );
  167.     lastRec = next( historyBlock );
  168. }
  169.  
  170. void initHistory()
  171. {
  172.     historyBlock = (HistRec *) new char[historySize];
  173.     clearHistory();
  174. }
  175.  
  176. void doneHistory()
  177. {
  178.     delete historyBlock;
  179. }
  180.