home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / Control Panel 0.9.4 / TC 5 / Notifier.c < prev    next >
Encoding:
Text File  |  1993-11-17  |  2.7 KB  |  90 lines  |  [TEXT/KAHL]

  1. /*
  2.  * Notifier.c
  3.  *
  4.  * Completed on 11/14/93 by Ken Worley using Symantec Think C 6.0.1.
  5.  *
  6.  * Contains the StrFormat and StrNotify routines.  Note that the ANSI, ANSI-small,
  7.  * or ANSI-A4 library must be included in your project for these routines to work.
  8.  *
  9.  */
  10.  
  11. /*    StrFormat
  12.  *
  13.  * This routine creates a string out of a format string containing type specifiers
  14.  * and an undetermined number of other arguments just like the printf group of
  15.  * library routines.
  16.  *
  17.  * The second argument is a format string just like the kind used with the library
  18.  * routine printf and the following arguments are inserted in the string based on
  19.  * the specifiers in the format string.  For more info on this, see the explanation
  20.  * for the printf routine in the standard libraries.  Besides the extra string
  21.  * argument and the fact that the result is put into a Pascal string, the routines
  22.  * are very alike.  (In fact, the library routine vsprintf is used to format the
  23.  * string which is then transferred to a Pascal string.)  Make sure you do NOT send
  24.  * the address of the Str255 variable.
  25.  */
  26. void    StrFormat( Str255 theString, const char* formatString, ... )
  27. {
  28.     va_list        arguments;
  29.     char        tempString[256];
  30.     short        x, theLength;
  31.     
  32.     va_start( arguments, formatString );
  33.     vsprintf( tempString, formatString, arguments );
  34.     va_end( arguments );
  35.  
  36.     theLength = strlen( tempString );
  37.     theString[0] = theLength;
  38.     
  39.     if ( theLength )
  40.         for ( x=1; x<=theLength; x++ )
  41.             theString[x] = tempString[x-1];
  42. }
  43.  
  44. /*
  45.  * StrNotify
  46.  *
  47.  * This routine uses the Notification manager to send a message to the user.  The
  48.  * memory for the NMRec and string are allocated by the calling program and pointers
  49.  * to those items are sent to StrNotify.
  50.  *
  51.  * You must send a pointer to a struct of type NMRec (a notification record) and a
  52.  * POINTER to a string of type Str255.  The memory pointed to by these two arguments
  53.  * must be LOCKED (non-relocatable) and must continue to exist until after the
  54.  * notification is made.  Since there is no mechanism here to determine when the
  55.  * notification occurs, it would be best for the memory to be static or permanently
  56.  * allocated.
  57.  */
  58. void    StrNotify( NMRec *nm, Str255 *str )
  59. {    
  60.     nm->nmMark = 0;
  61.     nm->nmIcon = NULL;
  62.     nm->nmSound = 0;
  63.     nm->nmStr = (StringPtr)str;
  64.     nm->nmResp = (ProcPtr)-1L;
  65.     nm->nmRefCon = 0L;
  66.     nm->qType = nmType;
  67.     
  68.     NMInstall( nm );
  69. }
  70.  
  71. /* EXAMPLE */
  72. /*
  73.  * This code:
  74.  *
  75.  * int        x;
  76.  *    long    l;
  77.  * static NMRec  myNMRec;
  78.  * static Str255 myString;
  79.  *
  80.  * x = 5;
  81.  * y = 250;
  82.  *
  83.  * StrFormat( myString, "The value %ld divided by 50 is %d.", y, x );
  84.  * StrNotify( &myNMRec, &myString );
  85.  *
  86.  * Produces a Notification dialog box containing the following text:
  87.  *
  88.  *     The value 250 divided by 50 is 5.
  89.  */
  90.