[comp.sys.mac.programmer] char strings and TextEdit

topix@gpu.utcs.utoronto.ca (R. Munroe) (07/15/90)

I have a rudimentary problem that I've been racking my brain trying to solve.
Basically, I want to send a struct of character strings to a TextEdit function.
Is there a proper (i.e. elegant) way to pass a struct of char strings to a 
TE function and insert the strings into a TE record?

I also have a couple more simple questions.  I am using THINK C 4.0.2. Why does
the following work OK ...

TEHandle textHdl;
..... /* more stuff */
char buffer[255] = "This is a test.";
TEInsert(&buffer, strlen(buffer), textHdl);

while the following does not?

TEHandle textHdl;
..... /* more stuff */
char buffer[255];
PtoCstr(buffer);
sprintf(buffer, "This is a test.");
CtoPstr(buffer);
TEInsert(&buffer, strlen(buffer), textHdl);

In the last case, I get a couple of extraneous characters printed before and
after my string.

Another minor question - how can I get a newline character from a string to
display properly?  The following ...

TEHandle textHdl;
..... /* more stuff */
char buffer[255] = "This is a test.\nSo is this.";
TEInsert(&buffer, strlen(buffer), textHdl);

displays a non-printing character where the newline should be.

I know these questions are basic, but, as I said, I've been going nuts trying
to find answers on my own (THINK C manuals, Inside Mac, etc.).  Thanks in
advance for any help/hints/suggestions.


Bob Munroe
topix@utcs.utoronto.ca

mneerach@c.inf.ethz.ch (Matthias Ulrich Neeracher) (07/16/90)

In article <1990Jul15.085520.14428@gpu.utcs.utoronto.ca> topix@gpu.utcs.utoronto.ca (R. Munroe) writes:

>Why does the following work OK ...

>char buffer[255] = "This is a test.";
>TEInsert(&buffer, strlen(buffer), textHdl);

>while the following does not?

>char buffer[255];
>PtoCstr(buffer);
>sprintf(buffer, "This is a test.");
>CtoPstr(buffer);
>TEInsert(&buffer, strlen(buffer), textHdl);

In the first case, you are dealing with a C string and are giving a pointer
to the first character and the correct length, which works fine.
  In the second case, you work with a *Pascal* string, i.e. you give a pointer
to the length byte, which is wrong, and you are using strlen on a Pascal 
string which is *very* wrong, because strlen looks for a '\0' character and
after using CtoPstr, no '\0' character is guaranteed to be around.
  So, leave out the CtoPstr and everything should work. Oh yes, and leave
out the PtoCstr also: At best it does nothing (buffer is overwritten by the
sprintf anyway).

>Another minor question - how can I get a newline character from a string to
>display properly?  The following ...

>TEHandle textHdl;
>..... /* more stuff */
>char buffer[255] = "This is a test.\nSo is this.";
>TEInsert(&buffer, strlen(buffer), textHdl);
>
>displays a non-printing character where the newline should be.

I don't know about Think C, but I understand many compiler differentiate
between '\n' (ASCII 10) and '\r' (ASCII 13). Try '\r', maybe this works.

Matthias

***************************************************************************
* Matthias Neeracher   * I wouldn't recommend sex, drugs or insanity for  *
* mneerach@inf.ethz.ch * everyone, but they've always worked for me - HST *
***************************************************************************