home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Add-Ons / XCMDs / XCMDTools++ / xcmdBase.cp next >
Encoding:
Text File  |  1995-10-05  |  5.4 KB  |  207 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.  
  19. #include    <ctype.h>
  20. #include    "xcmdBase.h"
  21. #include    <Memory.h>
  22. #include    <String.h>
  23.  
  24. //
  25. //    This file defines all the methods in the base class.
  26. //    Parameters are passed to client functions as zero terminated strings. All the
  27. //    server calls involving strings expect pascal like string arrays. What to do...
  28. //
  29. //    I have just figured out why the parameters passed to this routine are null
  30. //    term strings: The parameters can be arb. long. The input strings, for example,
  31. //    can be the entire contents of a field container. Pascal strings are limited
  32. //    to 255 bytes. So...
  33. //
  34. //    Any server call made by the client, involving a string will expect a "c" like
  35. //    zero terminated string. The member function converts it to a pascal string, places
  36. //    the call, and converts it back on return. This may seem inefficient but "c" strings
  37. //    are more natural for this language and the alg. for conversion is pretty fast. 
  38. //    Besides most of the time we are not tied up in client requests anyway.
  39. //
  40. //    Here's the problem. For some bizare reason the developers of Hypercard
  41. //    pass zero terminated "C" like strings as parameters in the XCmdPtr.
  42. //    This would be OK by    itself until you realize that most of the callback
  43. //    routines want to see pascal like strings. We have created a special class
  44. //    to handle all the string operations.
  45. //
  46.  
  47. const    int pSiz    = 255;
  48.  
  49. //XCmdPtr    
  50. //xcmdBase::paramPtr    = nil;    // allocated when xcmdBase object is instantiated
  51.  
  52. //    Creation routine. Registers the XCmdPtr and locks all the parameters high.
  53. //    ____________________________________
  54. xcmdBase::xcmdBase( XCmdPtr xP, Boolean lockParameters ) : 
  55.     paramsLocked( lockParameters )
  56. {
  57.     paramPtr = xP;                //    our copy of the paramPtr
  58.     xcmdString::paramPtr = xP;    // common ptr for xcmdString objects.
  59.     
  60.     //if ( xP )    exitNow = false;  if xp is nil we probably wouldn't be here.
  61.  
  62.         // I don't automaticlly lock the handles to the parameters.
  63.         // if they are small strings then it may needlessly tie up high memory
  64.         // for other things.
  65.     if ( lockParameters )    lockParams();
  66.  
  67. }
  68.  
  69. //    Destruction routine. unlocks all the parameters.
  70. //    ____________________________________
  71. xcmdBase::~xcmdBase( void )
  72. {
  73.     if ( paramsLocked ) unlockParams();
  74. }
  75.  
  76. //    Unlocks all the parameters.
  77. //    ____________________________________
  78. void
  79. xcmdBase::unlockParams( void )
  80. {
  81.     if ( paramsLocked ) {
  82.         short i;
  83.  
  84.         for(i=0; i < paramPtr->paramCount; i++) {
  85.             HUnlock(paramPtr->params[i]);
  86.             }
  87.         paramsLocked = false;
  88.         }
  89. }
  90.  
  91. //    Locks all the parameters.
  92. //    ____________________________________
  93. void
  94. xcmdBase::lockParams( void )
  95. {
  96.     if ( !paramsLocked ) {
  97.         short i;
  98.  
  99.         for(i=0; i < paramPtr->paramCount; i++) {
  100.             MoveHHi(paramPtr->params[i]);
  101.             HLock(paramPtr->params[i]);
  102.             }
  103.         paramsLocked = true;
  104.         }
  105. }
  106.  
  107. //    Check the parameter count to see if it is expected.
  108. //    ____________________________________
  109. Boolean
  110. xcmdBase::checkParameters( int min, int max )
  111. {
  112.     if ( paramPtr==nil )    return false;    // no paramPtr so exit
  113.  
  114.     if (max<min) max=min;
  115.  
  116.     if ( nParams() < min ) {
  117.         errorMsg( "too few parameters." );
  118.         return false;
  119.         }
  120.     if ( nParams() > max ) {
  121.         errorMsg( "too many parameters." );
  122.         return false;
  123.         }
  124.     return true;
  125. }
  126.  
  127. //    Array overload. returns the parameter at position i.
  128. //    ____________________________________
  129. Handle
  130. xcmdBase::operator[]( const int i )
  131. {
  132.     if ( i>=0 && i<=16 ) return paramPtr->params[i];
  133.     else return nil;
  134. }
  135.  
  136. //    Return methods. alerts user of error.
  137. //    ____________________________________
  138. void
  139. xcmdBase::errorMsg( xcmdString msg )
  140. {
  141.     returnMsg( "••Error :" & msg );
  142. }
  143.  
  144. //    Return methods. returns string.
  145. //    ____________________________________
  146. void
  147. xcmdBase::returnMsg( xcmdString message )
  148. {
  149.     paramPtr->returnValue = Handle(message);
  150. }
  151.  
  152. /****  HyperTalk Utilities  ****/
  153. Handle
  154. xcmdBase::evalExpr( xcmdString expr )
  155. {
  156.     Handle h = EvalExpr( paramPtr, expr ); 
  157.     return h;
  158. }
  159.  
  160. void
  161. xcmdBase::sendCardMessage( xcmdString msg )
  162. {
  163.     // message goes to current card.
  164.     SendCardMessage( paramPtr, msg ); 
  165.  
  166. }
  167.  
  168. void
  169. xcmdBase::sendHCMessage( xcmdString msg )
  170. {
  171.     // message goes to hypercard itself.
  172.     SendHCMessage( paramPtr, msg );
  173. }
  174.  
  175. void
  176. xcmdBase::runScript( xcmdString handler )
  177. {
  178.     RunHandler( paramPtr, handler ); 
  179. }
  180.  
  181. /****  Memory Utilities  ****/
  182. void
  183. xcmdBase::getGlobal( xcmdString globName, xcmdString* rs )
  184. {
  185.     Handle        h = GetGlobal( paramPtr, globName );
  186.     xcmdString    gv( h );
  187.     DisposHandle( h );
  188.  
  189.     *rs = gv;
  190. }
  191.  
  192. void
  193. xcmdBase::setGlobal( xcmdString globName, xcmdString& globValue )
  194. {
  195.     Handle gVal = Handle(globValue); // create a handle of the value
  196.     SetGlobal( paramPtr, globName, gVal );
  197.     DisposHandle( gVal );            // dispose of it.
  198. }
  199.  
  200. void
  201. xcmdBase::zeroBytes( Ptr dstPtr, long longCount )
  202. {
  203.     ZeroBytes( paramPtr, dstPtr, longCount ); 
  204. }
  205.  
  206.  
  207.