Re: NSString array to -> char**
Re: NSString array to -> char**
- Subject: Re: NSString array to -> char**
- From: Seth Delackner <email@hidden>
- Date: Thu, 20 Mar 2003 07:23:23 +0000
On Wed, Mar 19, 2003 at 01:48:22PM +0100, Thomas Lachand-Robert wrote:
>
No, but my code was plain wrong here (too quickly typed). Back to the
>
point, we now have a char** (not a char*) at the beginning of the
>
array, but it's filled with the lengths of the strings (as a int*),
>
instead of the locations of the strings. So we have to change that:
>
char** p = [data mutableBytes], **q = p;
>
char* firststring = (char*)[data mutableBytes] + (n+1)*sizeof(char*);
>
// location of the first string
>
while (k--) {// there is k strings remaining
>
int length = *(int*)q; // get the length of the string
>
*q = firststring + length;
>
q++;
>
}
>
*q = NULL;
>
>
Hope it's easier to read now (and less buggy).
Well, nearly done. Since I've managed to build what I 'need' using just a class
with an init method that callocs the space, this NSMutableData method is purely
for learning at this point.
Below is code based on your suggestions. The first while loop seems to
properly generate an NSMutableData containing first the lengths of each string,
then pointers to each string. And yes, you have to append a zero byte yourself
when encoding strings. The second while loop is basically what I have quoted
above, but it definitely doesn't work.
First though, some gdb output for fun:
after we have finished the first while loop: (on 3 strings)
<00000004 00000005 00000005 00000000 74696c6c 0031393a 3437006c 61746572 00>
then after the first iteration of the second loop:
<01cfa6c4 00000005 00000005 00000000 74696c6c 0031393a 3437006c 61746572 00>
(gdb) p (char*)(p)
$1 = 0x1cfa6b0 "\001\317\246\304\000"...
Which is definitely not what the first string was.
-(NSMutableData*) arrayOfCStringsWithEncoding:(NSStringEncoding)encoding {
NSRange r= NSMakeRange(0, sizeof(char*));
NSData* tempData;
char zero = 0;
int n = [self count];
NSMutableData *data = [NSMutableData dataWithLength: (n+1)*sizeof(char*)];
NSEnumerator* e = [self objectEnumerator];
NSString* s;
int tempLength;
while (s = [e nextObject]) {
tempData = [s dataUsingEncoding:encoding allowLossyConversion:YES];
[data append
Data:tempData];
[data appendBytes: &zero length: sizeof(char)];
tempLength = [tempData length];
[data replaceBytesInRange: r withBytes: (void*)&tempLength length: r.length];
r.location += r.length;
}
char** p = [data mutableBytes];
char* firstString = (char*)p + (n+1)*sizeof(char*);
while (n--) {
int length = *(int*)p;
//// Is this right? the first string is at firstString,
//// and the second should start at firstString + length
//// of first string, no?
*p = firstString + length;
p++;
}
return data;
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.