home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / PJ8_3.ZIP / USTRITEM.PAS < prev    next >
Pascal/Delphi Source File  |  1990-02-15  |  2KB  |  86 lines

  1. (* ustritem.pas -- (c) 1989 by Tom Swan *)
  2.  
  3. unit ustritem;
  4.  
  5. interface
  6.  
  7. uses uitem;
  8.  
  9. type
  10.  
  11.    strItemPtr = ^strItem;
  12.    strItem = object( item )
  13.       sp : ^string;
  14.       constructor init( s : string );
  15.       destructor done; virtual;
  16.       function getString : string;
  17.       function putString( s : string ) : Boolean; virtual;
  18.       procedure upperString; virtual;
  19.    end;
  20.  
  21. implementation
  22.  
  23. { ----- Initialize a new string item, allocating heap space for the
  24. string's characters and addressing that space with the object's sp
  25. pointer. }
  26.  
  27. constructor strItem.init( s : string );
  28. begin
  29.    sp := nil;
  30.    if putstring( s ) then item.init else
  31.    begin
  32.       done;    { Clean up any partially allocated structures }
  33.       fail;    { Out of memory error }
  34.    end;
  35. end;
  36.  
  37. { ----- Dispose of the allocated heap space and any memory assigned
  38. to the string item object. }
  39.  
  40. destructor strItem.done;
  41. begin
  42.    if sp <> nil
  43.       then freemem( sp, length( sp^ ) + 1 );
  44.    item.done;
  45. end;
  46.  
  47. { ----- Return the value of the string. }
  48.  
  49. function strItem.getString : string;
  50. begin
  51.    getString := sp^;
  52. end;
  53.  
  54. { ----- Assign or replace the object's character string. Disposes any
  55. current string and returns true for success; false if out of memory. }
  56.  
  57. function strItem.putString( s : string ) : Boolean;
  58. begin
  59.    if sp <> nil
  60.       then freemem( sp, length( sp^ ) + 1 );
  61.    getmem( sp, length( s ) + 1 );
  62.    if sp = nil then putString := false else
  63.    begin
  64.       sp^ := s;
  65.       putString := true;
  66.    end;
  67. end;
  68.  
  69. { ----- Convert string to all uppercase. }
  70.  
  71. procedure strItem.upperString;
  72. var
  73.    i : integer;
  74. begin
  75.    if sp <> nil then
  76.       for i := 1 to length( sp^ ) do
  77.          sp^[ i ] := upcase( sp^[ i ] );
  78. end;
  79.  
  80. end.
  81.  
  82.  
  83.  
  84.  
  85.  
  86.