home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Add-Ons / XCMDs / XCMDTools++ / xcmdStrings.cp < prev    next >
Encoding:
Text File  |  1995-10-05  |  5.2 KB  |  253 lines  |  [TEXT/CWIE]

  1. //    © Paul B. Beeken, Work In Progress, 1994-5
  2. //    Knowledge Software Consulting.
  3. //
  4. //    These files are a mindlessly simple wrapper class for the
  5. //    basic XCMD operations.  Only one instance of the xcmdBase class is
  6. //    generated per XCMD call but there may be many instances of the XCMDString
  7. //    class.  I have used these classes to whip out XCMD/XFCNs within hours of
  8. //    receiving the specs.  They work great for me but I will always consider 
  9. //    suggestions.
  10. //
  11. //    Please, please, please, in the unlikely event you should use this stuff
  12. //    for some commercial application I would appreciate you contacting me.  If
  13. //    its for your own use, use away. Send email: knowsoft@ios.com
  14. //
  15. //    As always: this file is presented as is with no warrantees expressed or implied.
  16. //    Swim at your own risk, etc. etc.
  17.  
  18. #include    <strings.h>
  19. #include    <string.h>
  20. #include    "xcmdStrings.h"
  21.  
  22. XCmdPtr    
  23. xcmdString::paramPtr    = nil;    // allocated when xcmdBase object is instantiated
  24.  
  25. // sometimes we need the string explicitly.
  26. inline int
  27. xcmdString::length( void ) const
  28. {
  29.     if ( isPascal ) return str.p[0];
  30.     return strlen( str.c );
  31. }
  32.  
  33. inline char*
  34. xcmdString::cString( void )
  35. {
  36.     if ( isPascal ) p2cstr( str.p );
  37.     isPascal = false;
  38.     return str.c;
  39. }
  40.  
  41. inline StringPtr
  42. xcmdString::pString( void )
  43. {
  44.     if ( !isPascal ) c2pstr( str.c );
  45.     isPascal = true;
  46.     return str.p;
  47. }
  48.  
  49.  
  50. xcmdString::xcmdString( Handle s ) : isPascal( false )
  51. {
  52.     str.c = new char[strlen(*s)+1];
  53.     HLock( s );
  54.     strcpy( str.c, *s );
  55.     HUnlock( s );
  56. }
  57.  
  58. xcmdString::~xcmdString()
  59. {
  60.     if ( str.c != nil )    delete [] str.c;
  61. }
  62.  
  63. xcmdString::xcmdString( const xcmdString& s ) : isPascal( false )
  64. {    // pointer may not have been created on free store. 
  65.     str.c = new char[s.length()+1];
  66.     strcpy( str.c, s );
  67. }
  68.  
  69. xcmdString::xcmdString( char* s ) : isPascal( false )
  70. {    // pointer may not have been created on free store. 
  71.     str.c = new char[strlen(s)+1];
  72.     strcpy( str.c, s );
  73. }
  74.  
  75. xcmdString::xcmdString( StringPtr s ) : isPascal( true )
  76. {    // pointer may not have been created on free store. 
  77.     str.c = new char[1+s[0]];
  78.     strncpy( str.c, (char*)s, 1+s[0] );
  79. }
  80.  
  81. /****  String Conversions  ****/
  82. xcmdString::xcmdString( Boolean bool ) : isPascal( true )
  83. {
  84.     str.c = new char[8];
  85.     
  86.     BoolToStr( paramPtr, bool, str.p );
  87. }
  88.  
  89. xcmdString::xcmdString( extended num ) : isPascal( true )
  90. {
  91.     str.c = new char[12];
  92.  
  93.     ExtToStr( paramPtr, num, str.p ); 
  94.  
  95. }
  96.  
  97. xcmdString::xcmdString( unsigned long posNum ) : isPascal( true )
  98. {
  99.     str.c = new char[12];
  100.  
  101.     LongToStr( paramPtr, long(posNum), str.p ); 
  102. }
  103.  
  104. xcmdString::xcmdString( long num, short nd ) : isPascal( true )
  105. {
  106.     str.c = new char[nd+4];
  107.  
  108.     NumToHex( paramPtr, num, nd, str.p );
  109. }
  110.  
  111. xcmdString::xcmdString( long num ) : isPascal( true )
  112. {
  113.     str.c = new char[12];
  114.  
  115.     NumToStr( paramPtr, num, str.p ); 
  116. }
  117.  
  118. xcmdString::xcmdString( Point pt ) : isPascal( true )
  119. {
  120.     str.c = new char[15];
  121.  
  122.     PointToStr( paramPtr, pt, str.p );
  123. }
  124.  
  125. xcmdString::xcmdString( Rect& rct ) : isPascal( true )
  126. {
  127.     str.c = new char[30];
  128.  
  129.     RectToStr( paramPtr, &rct, str.p ); 
  130. }
  131.  
  132. // conversion operators for useful types:
  133. xcmdString::operator Boolean()
  134. {
  135.     Boolean rc = StrToBool( paramPtr, pString() );
  136.     return rc;
  137. }
  138.  
  139. xcmdString::operator Rect()
  140. {
  141.         Rect    rct;
  142.     StrToRect( paramPtr, pString(), &rct );
  143.     return rct;
  144. }
  145.  
  146. xcmdString::operator Point()
  147. {
  148.         Point    pt;
  149.     StrToPoint( paramPtr, pString(), &pt );
  150.     return pt;
  151. }
  152.  
  153. xcmdString::operator extended()
  154. {
  155.     extended e = StrToExt( paramPtr, pString() ); 
  156.     return e;
  157. }
  158.  
  159. xcmdString::operator unsigned long()
  160. {
  161.     unsigned long d = StrToLong( paramPtr, pString() ); 
  162.     return d;
  163. }
  164.  
  165. xcmdString::operator long()
  166. {
  167.     unsigned long d = StrToNum( paramPtr, pString() ); 
  168.     return d;
  169. }
  170.  
  171. xcmdString::operator StringPtr()
  172. {
  173.     return pString();
  174. }
  175.  
  176. xcmdString::operator char*()
  177. {
  178.     return cString();
  179. }
  180.  
  181.  
  182. xcmdString::operator Handle()
  183. {
  184.     Handle h;
  185.     
  186.     h = NewHandleClear (length() + 1);
  187.     if ( h )
  188.         strcpy( (char *) *h, cString() );
  189.     
  190.     return h;
  191. }
  192.  
  193. // Some key testing operators. == != contains
  194. //    ____________________________________
  195. int            
  196. xcmdString::contains( xcmdString& s2 )
  197. {
  198.     char*    op    =    StringMatch( paramPtr, s2, cString() );
  199.     if ( op == nil ) op = cString();
  200.     return op-cString();
  201. }
  202.  
  203. Boolean
  204. operator!=( const xcmdString& s1, const xcmdString& s2 )
  205. {
  206.     Boolean    rc    =    StringEqual( xcmdString::paramPtr, s1, s2 );
  207.     return !rc;
  208. }
  209.  
  210. // Equate.  Is a friend so that "" clauses are handled automaticlly
  211. //    ____________________________________
  212. Boolean
  213. operator==( const xcmdString& s1, const xcmdString& s2 )
  214. {
  215.     Boolean    rc    =    StringEqual( xcmdString::paramPtr, s1, s2 );
  216.     return rc;
  217. }
  218.  
  219. // overloading of assignment.
  220. //    ____________________________________
  221. xcmdString&
  222. xcmdString::operator=( const xcmdString& s2 )
  223. {
  224.     if ( this != &s2 ) {
  225.         delete [] str.c;
  226.         str.c = new char[ 1 + s2.length() ];
  227.         strcpy( cString(), s2 );
  228.         }
  229.     return *this;
  230. }
  231.  
  232. // overloading of index to return specific element.
  233. //    ____________________________________
  234. char
  235. xcmdString::operator[]( const int i )
  236. {
  237.     if ( i>length() ) return '\0';
  238.     return cString()[i];
  239. }
  240.  
  241. // Catenation.  Is a friend so that "" are handled automaticlly
  242. //    ____________________________________
  243. xcmdString
  244. operator&( const xcmdString& s1, const xcmdString& s2 )
  245. {
  246.     char*    s    =    new    char[ s1.length() + s2.length() + 1 ];
  247.     strcpy( s, s1 );
  248.     xcmdString    rs( strcat( s, s2 ) );
  249.     delete [] s;
  250.     return rs;
  251. }
  252.  
  253.