home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / STRLINK.ZIP / STROBJ.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1989-08-28  |  4.1 KB  |  152 lines

  1. {$A+,B-,D+,E+,F-,I-,L+,N-,O-,R-,S+,V+}
  2. {$DEFINE TPRO5}
  3.  
  4. UNIT StrObj;
  5.  
  6.    {Linked list holds strings.}
  7.  
  8.  
  9. INTERFACE {section}
  10.  
  11. USES
  12.    {$IFDEF TPRO5}
  13.        TpString,
  14.    {$ENDIF}
  15.    Objects;
  16.  
  17.  
  18. TYPE
  19.    StrObjectPtr = ^StrObject;
  20.    StrObject
  21.     = OBJECT(Node)
  22.       TheStrPtr : POINTER;
  23.  
  24.       CONSTRUCTOR Init(NewStr : STRING);
  25.       DESTRUCTOR  Done; VIRTUAL;
  26.  
  27.       FUNCTION  GetString : STRING;
  28.       PROCEDURE ChangeString(NewStr : STRING);
  29.       FUNCTION  SelfPtr : StrObjectPtr;
  30.       END;
  31.  
  32.  
  33. IMPLEMENTATION {section}
  34.  
  35.  
  36. {$IFDEF TPRO5}
  37. {============================================================================}
  38. CONSTRUCTOR StrObject.Init(NewStr : STRING);
  39.  
  40.    {Initialize the StrObject.}
  41.  
  42. BEGIN {StrObject.Init}
  43. TheStrPtr := StringToHeap(NewStr)
  44. END; {StrObject.Init}
  45. {============================================================================}
  46. {$ELSE}
  47. {============================================================================}
  48. CONSTRUCTOR StrObject.Init(NewStr : STRING);
  49.  
  50.    {Initialize the StrObject.  ...I originally used SUCC(LENGTH(NewStr)) to
  51. determine the size of the true string array, but I forgot that SUCC returns
  52. a value of the same type as the variable you sent it.  That's bad news if you
  53. try to put a 255-char string on the heap!  Think about it.}
  54.  
  55. VAR
  56.    TrueStrSize : WORD;
  57.  
  58. BEGIN {StrObject.Init}
  59. TrueStrSize := (LENGTH(NewStr) + 1); {must account for 0th byte!}
  60.  
  61. GETMEM({VAR} TheStrPtr,TrueStrSize);
  62. MOVE(NewStr,TheStrPtr^,TrueStrSize)
  63. END; {StrObject.Init}
  64. {============================================================================}
  65. {$ENDIF}
  66.  
  67. {$IFDEF TPRO5}
  68. {============================================================================}
  69. DESTRUCTOR  StrObject.Done;
  70.  
  71.    {De-initialize the StrObject.}
  72.  
  73. BEGIN {StrObject.Done}
  74. DisposeString(TheStrPtr)
  75. END; {StrObject.Done}
  76. {============================================================================}
  77. {$ELSE}
  78. {============================================================================}
  79. DESTRUCTOR  StrObject.Done;
  80.  
  81.    {De-initialize the StrObject.  See my comments in the Init routine!}
  82.  
  83. VAR
  84.    TrueStrSize : WORD;
  85.  
  86. BEGIN {StrObject.Done}
  87. TrueStrSize := (BYTE(TheStrPtr^) + 1);
  88. FREEMEM(TheStrPtr,TrueStrSize)
  89. END; {StrObject.Done}
  90. {============================================================================}
  91. {$ENDIF}
  92.  
  93. {$IFDEF TPRO5}
  94. {============================================================================}
  95. FUNCTION  StrObject.GetString : STRING;
  96.  
  97.    {Return the Str from the object.}
  98.  
  99. BEGIN {GetString}
  100. GetString := StringFromHeap(TheStrPtr)
  101. END; {GetString}
  102. {============================================================================}
  103. {$ELSE}
  104. {============================================================================}
  105. FUNCTION  StrObject.GetString : STRING;
  106.  
  107.    {Return the Str from the object.}
  108.  
  109. BEGIN {GetString}
  110. GetString := STRING(TheStrPtr^)
  111. END; {GetString}
  112. {============================================================================}
  113. {$ENDIF}
  114.  
  115. {$IFDEF TPRO5}
  116. {============================================================================}
  117. PROCEDURE StrObject.ChangeString(NewStr : STRING);
  118.  
  119.    {Changes the Str in the object to the NewStr.  This is much faster than
  120. calling Done() and New().}
  121.  
  122. BEGIN {ChangeString}
  123. DisposeString(TheStrPtr);
  124. TheStrPtr := StringToHeap(NewStr)
  125. END; {ChangeString}
  126. {============================================================================}
  127. {$ELSE}
  128. {============================================================================}
  129. PROCEDURE StrObject.ChangeString(NewStr : STRING);
  130.  
  131.    {Changes the Str in the object to the NewStr.}
  132.  
  133. BEGIN {ChangeString}
  134. Done;
  135. Init(NewStr)
  136. END; {ChangeString}
  137. {============================================================================}
  138. {$ENDIF}
  139.  
  140. {============================================================================}
  141. FUNCTION    StrObject.SelfPtr : StrObjectPtr;
  142.  
  143.    {Returns a pointer to this object.}
  144.  
  145. BEGIN {SelfPtr}
  146. SelfPtr := @Self
  147. END; {SelfPtr}
  148. {============================================================================}
  149.  
  150.  
  151. END. {StrObj}
  152.