[comp.sys.mac.programmer] C to Pascal strings

MJSCHMEL@pucc.Princeton.EDU (Michael J. Schmelzer) (03/01/88)

========================================================================
Newsgroups: comp.sys.mac
Date: Saturday, 27-Feb-88 15:04:33 EST
Subject: strings (HELP!)
From: Me <MJSCHMEL@PUCC>
To: Netnews
 
Hello again, everybody.
 
The MPW manual was no help concerning the conversion of C to Pascal
strings. The manual also cryptically states that the function is
"c2pstr" and doesn't provide an example of how to convert a
normal, null terminated C string into a Str255.
 
 
char *p2cstr();
char *c2psstr(); /* typo in the prototype or in the manual */
 
I have written some applications in LSC and would like to become
competent in MPW before I enter the job market (!)
If someone could please provide a working example of C to Pascal string
conversion in MPW I would be eternally grateful.
(By the way, I remember reading here that someone was completely
disgusted with the way LSC handled these conversions. I'd like to know
why.)
 
Let's get comp.sys.mac.programmer off to a good start and give me an
answer! (This posting was completely ignored on comp.sys.mac)
 
If I sound desperate, I am. (Even if I don't, I am.)
==============================================================================
comes around    Mike Schmelzer MJSCHMEL@PUCC.Princeton.Edu    what goes around
==============================================================================

rmh@Apple.COM (Rick Holzgrafe) (03/03/88)

In article <4619@pucc.Princeton.EDU> MJSCHMEL@pucc.Princeton.EDU writes:
>If someone could please provide a working example of C to Pascal string
>conversion in MPW I would be eternally grateful.
> 
>Let's get comp.sys.mac.programmer off to a good start and give me an
>answer! (This posting was completely ignored on comp.sys.mac)
> 
>==============================================================================
>comes around    Mike Schmelzer MJSCHMEL@PUCC.Princeton.Edu    what goes around
>==============================================================================

Completely ignored? Gosh - for once, everyone must have done like I did,
and assumed someone else would answer. Ok, here it is:

	char buf[] = "Fee fie foe fum";
	c2pstr (buf);

Buf now contains "\17Fee fie foe fum" with no terminating null byte.
To convert it back to a C string:

	p2cstr (buf);

These conversions work "in place": your original string is munged into
the new format. This works, of course, since both pascal and C strings
require the same amount of space: one character extra, either at the end
for the null byte in C, or at the beginning for the length byte in Pascal.

Judging from the declarations, these routines also return a pointer to
the same string you pass them. This is so that you can use these calls
as parameters to some other call, such as:

	printf ("%s\n", p2cstr (a_pascal_string));

My first examples above ignored the returned value.

==========================================================================
Rick Holzgrafe			 | {sun,voder,nsc,mtxinu,dual}!apple!rmh
Communications Software Engineer | AppleLink HOLZGRAFE1    rmh@apple.com
Apple Computer, Inc.		 | "All opinions expressed are mine, and
20525 Mariani Ave. MS: 27-Y	 | do not necessarily represent those of
Cupertino, CA 95014		 | my employer, Apple Computer Inc."

gam3@well.UUCP (G. Allen Morris III) (03/03/88)

a pascal string looks like:

struct pascalString {
	char stringSize;
	char string[255];
}

Hope that helps.

saf@moss.ATT.COM (03/07/88)

In MPW 2.0, there is a \p escape which makes Pascal strings:
	"\phello world\n"
is a pascal string.

A Str255 is a structure:

struct {
	char length;
	char data[255];
}

(The member names are probably wrong - this is from memory)  Anyway, because
it is a structure, you have to take its address explicitly.

	Steve Falco   moss!saf   saf@moss.ATT.COM