Added +[dataFromCString:] and +[dataFromCString:]; much cleanup
This commit is contained in:
parent
782654d444
commit
eada9d3cd1
@ -90,6 +90,16 @@
|
|||||||
/** Returns an NSData object containing a blank C string (i.e. a byte sequence
|
/** Returns an NSData object containing a blank C string (i.e. a byte sequence
|
||||||
of length 1, containing the null character '\0').
|
of length 1, containing the null character '\0').
|
||||||
*/
|
*/
|
||||||
+(NSData *)dataWithBlankCString;
|
+(NSData *) dataWithBlankCString;
|
||||||
|
|
||||||
|
/** Returns an NSData object containing bytes copied from the given C string
|
||||||
|
(sans the null terminator).
|
||||||
|
*/
|
||||||
|
+(NSData *) dataFromCString:(const char *)cString;
|
||||||
|
|
||||||
|
/** Returns an NSData object containing the bytes of the given C string
|
||||||
|
(sans the null terminator).
|
||||||
|
*/
|
||||||
|
+(NSData *) dataWithCString:(char *)cString;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|||||||
@ -25,59 +25,65 @@
|
|||||||
|
|
||||||
@implementation NSData (SA_NSDataExtensions)
|
@implementation NSData (SA_NSDataExtensions)
|
||||||
|
|
||||||
-(BOOL)isNullTerminated
|
-(BOOL) isNullTerminated {
|
||||||
{
|
if (self.length == 0)
|
||||||
|
return NO;
|
||||||
|
|
||||||
return (((char*) self.bytes)[self.length - 1] == '\0');
|
return (((char*) self.bytes)[self.length - 1] == '\0');
|
||||||
}
|
}
|
||||||
|
|
||||||
-(const char *)SA_terminatedCString
|
-(const char *) SA_terminatedCString {
|
||||||
{
|
|
||||||
return self.SA_dataWithTerminatedCString.bytes;
|
return self.SA_dataWithTerminatedCString.bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
-(NSData *)SA_dataWithTerminatedCString
|
-(NSData *) SA_dataWithTerminatedCString {
|
||||||
{
|
if (self.length == 0) {
|
||||||
if(self.length == 0)
|
return [NSData dataWithBytes:"\0"
|
||||||
{
|
length:1];
|
||||||
return [NSData dataWithBytes:"\0" length:1];
|
} else if (self.isNullTerminated) {
|
||||||
}
|
|
||||||
else if(self.isNullTerminated)
|
|
||||||
{
|
|
||||||
return self;
|
return self;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
char* terminated_string_buffer = malloc(self.length + 1);
|
char* terminated_string_buffer = malloc(self.length + 1);
|
||||||
[self getBytes:terminated_string_buffer length:self.length];
|
[self getBytes:terminated_string_buffer
|
||||||
|
length:self.length];
|
||||||
terminated_string_buffer[self.length] = '\0';
|
terminated_string_buffer[self.length] = '\0';
|
||||||
|
|
||||||
return [NSData dataWithBytesNoCopy:terminated_string_buffer length:(self.length + 1) freeWhenDone:YES];
|
return [NSData dataWithBytesNoCopy:terminated_string_buffer
|
||||||
|
length:(self.length + 1)
|
||||||
|
freeWhenDone:YES];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
-(const char *)SA_unterminatedByteString
|
-(const char *) SA_unterminatedByteString {
|
||||||
{
|
|
||||||
return self.SA_dataWithUnterminatedByteString.bytes;
|
return self.SA_dataWithUnterminatedByteString.bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
-(NSData *)SA_dataWithUnterminatedByteString
|
-(NSData *) SA_dataWithUnterminatedByteString {
|
||||||
{
|
if (self.length == 0 || self.isNullTerminated == NO) {
|
||||||
if(self.length == 0 || self.isNullTerminated == NO)
|
|
||||||
{
|
|
||||||
return self;
|
return self;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
char* unterminated_string_buffer = malloc(self.length - 1);
|
char* unterminated_string_buffer = malloc(self.length - 1);
|
||||||
[self getBytes:unterminated_string_buffer length:self.length - 1];
|
[self getBytes:unterminated_string_buffer length:self.length - 1];
|
||||||
|
|
||||||
return [NSData dataWithBytesNoCopy:unterminated_string_buffer length:(self.length - 1) freeWhenDone:YES];
|
return [NSData dataWithBytesNoCopy:unterminated_string_buffer
|
||||||
|
length:(self.length - 1)
|
||||||
|
freeWhenDone:YES];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
+(NSData *)dataWithBlankCString
|
+(NSData *) dataWithBlankCString {
|
||||||
{
|
return [NSData dataWithBytes:"\0"
|
||||||
return [NSData dataWithBytes:"\0" length:1];
|
length:1];
|
||||||
|
}
|
||||||
|
|
||||||
|
+(NSData *) dataFromCString:(const char *)cString {
|
||||||
|
return [NSData dataWithBytes:cString
|
||||||
|
length:strlen(cString)];
|
||||||
|
}
|
||||||
|
|
||||||
|
+(NSData *) dataWithCString:(char *)cString {
|
||||||
|
return [NSData dataWithBytesNoCopy:cString
|
||||||
|
length:strlen(cString)];
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|||||||
@ -2,6 +2,6 @@ NSData+SA_NSDataExtensions
|
|||||||
|
|
||||||
Adds utility functions to NSData, that help deal with null termination of C strings.
|
Adds utility functions to NSData, that help deal with null termination of C strings.
|
||||||
|
|
||||||
This category on NSData adds properties that allow you to get the null-terminated or non-null-terminated versions of byte arrays stored as NSData objects, and to easily check whether an NSData's byte array is, or is not, null-terminated (that is, whether its last byte is a null).
|
This category on NSData adds properties that allow you to get the null-terminated or non-null-terminated versions of byte arrays stored as NSData objects, and to easily check whether an NSData’s byte array is, or is not, null-terminated (that is, whether its last byte is a null).
|
||||||
|
|
||||||
Copyright (c) 2015 Said Achmiz.
|
Copyright (c) 2015 Said Achmiz.
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user