home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / PASCAL / EDITWIN / O_TBUF.PAS < prev    next >
Pascal/Delphi Source File  |  1992-07-01  |  6KB  |  184 lines

  1. {==============================================================}
  2. {                                                              }
  3. {           Saved as: O_TBUF.PAS                               }
  4. {            Purpose: Provide text buffer that can be          }
  5. {                     treated as array of strings, with        }
  6. {                     random access to elements through        }
  7. {                     array index, manipulate strings in       }
  8. {                     buffer                                   }
  9. {             Author: Pat Anderson                             }
  10. {           Language: TP 6.0                                   }
  11. {      Last modified: 06/20/92                                 }
  12. {                                                              }
  13. {==============================================================}
  14.  
  15. unit O_Tbuf;
  16.  
  17. {--------------------------------------------------------------}
  18.                         INTERFACE
  19. {--------------------------------------------------------------}
  20.  
  21. type
  22.   PTextArray = ^TTextArray;
  23.   TTextArray = ARRAY [1..1] OF pointer;
  24.  
  25.   PTextBuffer = ^TTextBuffer;
  26.   TTextBuffer = object
  27.     TextArray : PTextArray;
  28.     MaxLines : integer;
  29.     TotalLines : integer;
  30.     constructor Init (Lines : integer);
  31.     function StringToArray (PascalStr : string; Index : integer) : boolean;
  32.     function StringFromArray (Index : integer) : string;
  33.     function InsertLine (PascalStr : string; Index : integer) : boolean;
  34.     procedure DeleteLineFromArray (Index : integer);
  35.     procedure SetTotalLines (NumberOfLines : integer);
  36.     function GetTotalLines : integer;
  37.     procedure ClearTextArray;
  38.     destructor Done;
  39.   end;
  40.  
  41. function StringToHeap (PascalStr : string) : pointer;
  42. function StringFromHeap (HeapStr : pointer) : string;
  43. procedure DisposeString (var PHeapStr : pointer);
  44.  
  45. {--------------------------------------------------------------}
  46.                         IMPLEMENTATION
  47. {--------------------------------------------------------------}
  48.  
  49. function StringToHeap (PascalStr : string) : pointer;
  50. {function that copies a Pascal string to the heap, creating
  51.  a dynamic string ("heapstring"), and returns pointer to it}
  52.  var
  53.     SpaceNeeded : byte;
  54.     HeapStr : pointer;
  55.   begin
  56.     SpaceNeeded := Length (PascalStr) + 1;  {1 is for length byte itself}
  57.     if MaxAvail < SpaceNeeded then          {check if sufficient memory}
  58.       StringToHeap := nil                   {if not, return nil pointer}
  59.     else                                    {if enough memory}
  60.       begin
  61.         GetMem (HeapStr, SpaceNeeded);      {allocate storage for heapstring}
  62.         string (HeapStr^) := PascalStr;     {assign Pascal string to heapstring}
  63.         StringToHeap := HeapStr;            {return pointer to heapstring}
  64.       end;
  65.   end; {of StringToHeap function}
  66.  
  67. function StringFromHeap (HeapStr : pointer) : string;
  68. {function that retrieves dynamic string created with and
  69.  StringToHeap function and returns it as a Pascal string}
  70.   var
  71.     PascalStr : string;
  72.   begin
  73.     if HeapStr = nil then                {if pointer is nil}
  74.       StringFromHeap := ''               { return NULL string}
  75.     else
  76.       StringFromHeap := string (HeapStr^);
  77.   end; {of StringFromHeap function}
  78.  
  79. procedure DisposeString (var PHeapStr : pointer);
  80. {procedure to deallocate space for a dynamic string created with
  81.  the StringToHeap function}
  82.   var
  83.     BytesToFree : byte;
  84.   begin
  85.     if PHeapStr = nil then
  86.       Exit
  87.     else
  88.       begin
  89.         BytesToFree := byte (PHeapStr^) + 1;
  90.         FreeMem (PHeapStr, BytesToFree);
  91.         PHeapStr := nil;
  92.       end;
  93.   end; {of DisposeString procedure}
  94.  
  95. constructor TTextBuffer.Init;
  96.   var
  97.     count : integer;
  98.   begin
  99.     MaxLines := Lines;
  100.     New (TextArray);
  101.     GetMem (TextArray^[1], MaxLines * SizeOf (pointer));
  102.     for count := 1 TO MaxLines do
  103.       TextArray^[Count] := nil;
  104.     TotalLines := 0;
  105.   end; { of Init }
  106.  
  107. function TTextBuffer.StringToArray (PascalStr : string; Index : integer) : boolean;
  108.   begin
  109.     StringToArray := true;
  110.     if TextArray^[Index] <> nil then
  111.       DisposeString (TextArray^[Index])
  112.     else
  113.       Inc (TotalLines);
  114.     TextArray^[Index] := StringToHeap (PascalStr);
  115.     if TextArray^[Index] = nil then
  116.       StringToArray := false
  117.   end; { procedure StringToArray }
  118.  
  119. function TTextBuffer.StringFromArray (Index : integer) : string;
  120. { Retrieve the string pointed to by the given array index
  121.   array, for assignment to Turbo Pascal string }
  122.   begin
  123.     StringFromArray := StringFromHeap (TextArray^[Index]);
  124.   end; { function StringFromArray }
  125.  
  126. function TTextBuffer.InsertLine (PascalStr : string; Index : integer) : boolean;
  127. { Insert a string into array at specified index, move following
  128.   elements to next higher index }
  129.   var
  130.     count : integer;
  131.   begin
  132.     InsertLine := true;
  133.     for Count := TotalLines + 1 doWNTO Index + 1 do       { Open up space }
  134.       TextArray^[Count] := TextArray^[Count - 1];
  135.     TextArray^[Index] := StringToHeap (PascalStr);
  136.     if TextArray^[Index] = nil then
  137.       InsertLine := false
  138.     else
  139.       Inc (TotalLines);
  140.   end; { procedure InsertLine }
  141.  
  142. procedure TTextBuffer.DeleteLineFromArray (Index : integer);
  143. { Delete string pointed to by given array index }
  144.   var
  145.     count : integer;
  146.   begin
  147.     if TotalLines = 0 then
  148.       Exit;
  149.     DisposeString (TextArray^[Index]);        { Dispose of string }
  150.     for Count := Index TO TotalLines do       { Close up  space   }
  151.       TextArray^[Count] := TextArray^[Count + 1];
  152.     Dec (TotalLines);
  153.   end; { procedure DeleteLine }
  154.  
  155. procedure TTextBuffer.SetTotalLines (NumberOfLines : integer);
  156.   begin
  157.     TotalLines := NumberOfLines;
  158.   end;
  159.  
  160. function TTextBuffer.GetTotalLines : integer;
  161.   begin
  162.     GetTotalLines := TotalLines;
  163.   end;
  164.  
  165. procedure TTextBuffer.ClearTextArray;
  166.   var
  167.     Count : integer;
  168.   begin
  169.     for Count := 1 to MaxLines do
  170.       if TextArray^[Count] <> nil then begin
  171.         DisposeString (TextArray^[Count]);
  172.         TextArray^[Count] := nil;
  173.       end;
  174.   end;
  175.  
  176. destructor TTextBuffer.Done;
  177.   var
  178.     count : integer;
  179.   begin
  180.     ClearTextArray;
  181.     Dispose (TextArray);
  182.   end;
  183. end.
  184.