[comp.sys.mac.programmer] How to use ParamText's

wilson@csli.STANFORD.EDU (Nathan Wilson) (03/14/89)

I was working on being a good person and switching all the strings in
my code to calls to GetIndString when I came across the following
problem.  Under some conditions I would like to title a window (not a
dialog or alert) that displays a PICT resource something like

<name of resource> ID = <number of resource> in <name of file>

with the stuff in <> replaced with the appropriate info.  Now it
occurred to me that the best way to generate this string would be to
store it as a parameterized string as per alerts, that is

^0 ID = ^1 in ^2

and then use the ParamText mechanism.  However I can't find any way to
invoke the string substitution stuff used by the Dialog Manager.  I
could of course do it all myself, but to do that correctly* I would
probably have to add more globals to my program and then write and
debug the code.  This seems kind of a waste of effort given that the
ability is at least marginally there already.  Did I miss something?
Any ideas, experience or code? (prefer Pascal or C)

(* Read: I'd rather not mess directly with the DAStrings global
mentioned on IM I-421.)

	Nathan Wilson
	Teleos Research
	nathan%teleos.com@ai.sri.com

lippin@wish-bone.berkeley.edu (The Apathist) (03/14/89)

Recently wilson@csli.stanford.edu (Nathan Wilson) wrote:
[...]
>Now it
>occurred to me that the best way to generate this string would be to
>store it as a parameterized string as per alerts, that is
>
>^0 ID = ^1 in ^2
>
>and then use the ParamText mechanism.
[...]

There's no need to bother the dialog manager with this; Munger, the
general-purpose trap, will make short work of this job.

					--Tom Lippincott
					  lippin@math.berkeley.edu

	"It's a multi-purpose shape: a box."
					--David Byrne, True Stories

jln@accuvax.nwu.edu (John Norstad) (03/15/89)

Try the following (MPW C 3.0).  It's cryptic (in the most hallowed C 
tradition), and it's not incredibly effecient, but it works.

/*______________________________________________________________________

   PlugParams - Plug parameters into message.
   
   Entry:   line1 = input line.
            p0, p1, p2, p3 = parameters.
               
   Exit:    line2 = output line.
               
   This routine works just like the toolbox routine ParamText.
   The input line may contain place-holders ^0, ^1, ^2, and ^3, 
   which are replaced by the parameters p0-p3.  The input line
   must not contain any other ^ characters.  The input and output lines
   may not be the same string.  Pass nil for parameters which don't
   occur in line1.  If the output line exceeds 255 characters it's
   truncated.
_____________________________________________________________________*/


void PlugParams (Str255 line1, Str255 line2, Str255 p0, 
   Str255 p1, Str255 p2, Str255 p3)

{
   char            *in;         /* pointer to cur pos in input line */
   char            *out;        /* pointer to cur pos in output line */
   char            *inEnd;      /* pointer to end of input line */
   char            *outEnd;     /* pointer to end of output line */
   char            *param;      /* pointer to param to be plugged */
   short            len;        /* length of param */
   
   in = line1+1;
   out = line2+1;
   inEnd = line1 + 1 + *line1;
   outEnd = line2 + 256;
   while (in < inEnd ) {
      if (*in == '^') {
         in++;
         if (in >= inEnd) break;
         switch (*in++) {
            case '0':
               param = p0;
               break;
            case '1':
               param = p1;
               break;
            case '2':
               param = p2;
               break;
            case '3':
               param = p3;
               break;
            default:
               continue;
         };
         if (!param) continue;
         len = *param;
         if (out + len > outEnd) len = outEnd - out;
         memcpy(out, param+1, len);
         out += len;
      } else {
         if (out >= outEnd) break;
         *out++ = *in++;
      };
   };
   *line2 = out - (line2+1);
};

John Norstad
Academic Computing and Network Services
Northwestern University

Bitnet: jln@nuacc
Internet: jln@acns.nwu.edu
Applelink: a0173

beard@ux3.lbl.gov (Patrick C Beard) (03/15/89)

In article <8079@csli.STANFORD.EDU> wilson@csli.stanford.edu (Nathan Wilson) writes:
>
>I was working on being a good person and switching all the strings in
>my code to calls to GetIndString when I came across the following
>problem.  Under some conditions I would like to title a window (not a
>dialog or alert) that displays a PICT resource something like
>
><name of resource> ID = <number of resource> in <name of file>
>
>with the stuff in <> replaced with the appropriate info.  Now it
>occurred to me that the best way to generate this string would be to
>store it as a parameterized string as per alerts, that is
>
>^0 ID = ^1 in ^2
>

How about:
	Str255 title;
	String format;
	
	GetIndString(format, FORMAT_STRING_ID, 1);
	.
	.
	PtoCstr(format);
sprintf(title,format, PtoCstr(resName), PtoCstr(resNumberStr),PtoCstr(resFile));
	SetWTitle(window, CtoPstr(title));


+----------------------------------------------------------------+
 \   Patrick Beard                 "Badges?                       \
  \    Berkeley Systems, Inc.        I ain't got to show you...    \
   \      PCBeard@lbl.gov                 ...NO STINKING BADGES!"   \
    + ---------------------------------------------------------------+

oster@dewey.soe.berkeley.edu (David Phillip Oster) (03/15/89)

In article <2110@helios.ee.lbl.gov> beard@ux3.lbl.gov (Patrick C Beard) writes:
_>In article <8079@csli.STANFORD.EDU> wilson@csli.stanford.edu (Nathan Wilson) writes:
_>>
_>>I was working on being a good person and switching all the strings in
_>>my code to calls to GetIndString when I came across the following
_>>problem.  Under some conditions I would like to title a window (not a
_>>dialog or alert) that displays a PICT resource something like
_>>
_>><name of resource> ID = <number of resource> in <name of file>
_>>
_>>with the stuff in <> replaced with the appropriate info.  Now it
_>>occurred to me that the best way to generate this string would be to
_>>store it as a parameterized string as per alerts, that is
_>>
_>>^0 ID = ^1 in ^2
_>>
_>
_>How about:
_>	Str255 title;
_>	String format;
_>	
_>	GetIndString(format, FORMAT_STRING_ID, 1);
_>	.
_>	.
_>	PtoCstr(format);
_>sprintf(title,format, PtoCstr(resName), PtoCstr(resNumberStr),PtoCstr(resFile));
_>	SetWTitle(window, CtoPstr(title));

or even better:

#define PS(s) (int)(unsigned)(unsigned char)(s)[0], &(s)[1]
	Str255 title;
	sprintf(title, "%.*s ID = %.*s in %.*s",
		PS(resName), PS(rseNumberStr), PS(resFile));
	CtoPstr(title);
	SetWTitle(window, title);

That way, you aren't modifying the Str255s resName, resNumberStr, or
resFile, so you don't have to worry about forgetting to turn them back
into pascal strings.  Note, that this clever trick uses code already built
into sprintf to handle pascal strings directly. The only caveat is that
the PS macro evaluates the argument twice, so it had better not have side
effects.

--- David Phillip Oster            --"When we replace the mouse with a pen,
Arpa: oster@dewey.soe.berkeley.edu --3 button mouse fans will need saxophone
Uucp: {uwvax,decvax}!ucbvax!oster%dewey.soe.berkeley.edu --lessons." - Gasee