home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 10.ddi / TVSRC.ZIP / HISTLIST.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  4.7 KB  |  196 lines

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