[comp.sys.mac.programmer] STR# rsrc question

gf0c+@andrew.cmu.edu (Gregory S. Fox) (01/12/89)

Howdy.

I've been working with a STR# that I use to in conjunction with the
List Manager.  That's unremarkable.  More interesting have been my
attempts to add and deleted from this list.  On the list manager end,
no problem.  On the STR# end, not so trivial.

For the add, I have been experimenting with HandAndHand, and incrementing
the counter at the head of the list.  For remove, I am clueless.

Am I missing something incredibly obvious?

Well, thanks...
-Greg

oster@dewey.soe.berkeley.edu (David Phillip Oster) (01/12/89)

Here is what I use to modify strings in a STR# (change them, add and delete
strings:
/* Note that these string list functions, unlike the systems, are zero based.
 */

/* GetStrPtr - returns a pointer to the data of list.
   since we do no functions, we know that our calling
   won't move data.
 */
StringPtr GetStrPtr(list, where)StrList list;Integer where;{
	register Integer i;
	register char *p;

	for(i = 0, p = (Ptr) &(**list).strs;i < where;i++){
		p += Length(p) + 1;
	}
	return (StringPtr) p;
}


/* GetStr - retrieve a copy of a string from the list 
 */
GetStr(list, where, s)StrList list;Integer where;Str255 s;{
	StringPtr p;

	p = GetStrPtr(list, where);
	HLock(list);
	StrMove(p, s);
	HUnlock(list);
}

/* SetStr - change the ith string in the list. (Note: the list
   may change length as a result of this operation.)
 */
SetStr(list, where, s)StrList list;Integer where;Str255 s;{
	StringPtr p;

	p = GetStrPtr(list, where);
	if(p == NIL)
		return;

	if(p[0] == s[0]){
		HLock(list);
		StrMove(s, p);
		HUnlock(list);
	}else{
		Munger(list, StripAddress(p) - StripAddress(*list),
			NIL, Length(p) + 1,
			s, Length(s) + 1);
	}
}

/* InsertStr - insert s in the list as the new 'where'th item
 */
InsertStr(list, where, s)IntList list;Integer where;Str255 s;{
	StringPtr p;

	p = GetStrPtr(list, where);
	Munger(list, StripAddress(p) - StripAddress(*list),
		NIL, 0,
		s, Length(s) + 1);
	(**list).cnt++;
}

/* DeleteStr - remove the whereth item
 */
DeleteStr(list, where)StrList list;Integer where;{
	StringPtr p;

	p = GetStrPtr(list, where);
	Munger(list, StripAddress(p) - StripAddress(*list),
		NIL, Length(p) + 1,
		p, 0);
	(**list).cnt--;
}

--- 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

cpyang@ccnysci.UUCP (Chao Ping Yang) (01/12/89)

	I have posted codes on the STR# handling to this
	news group not long ago, and I have added the
	DelIndString and they have been working great for me.
	Sorry I did not answer by mail but I know I have 
	trouble sending to andrew.cmu.edu.

	==Chaoping