Re: IPAddressFormatter?
Re: IPAddressFormatter?
- Subject: Re: IPAddressFormatter?
- From: Clark Cox <email@hidden>
- Date: Thu, 14 Oct 2004 17:11:50 -0400
On Thu, 14 Oct 2004 10:31:34 -0400, Brent Gulanowski
<email@hidden> wrote:
>
I scanned the 'net and the archives and check in Omni and Misc, but I
>
can't seem to find a freebie IP Address formatter. I know I'm being
>
lazy, but if anyone has one lying around it and wanted to share I'd be
>
your friend forever. Or larn me how to search better ;-).
It shouldn't be that tough, here's something just hacked together in
Mail, handles the input of IP addresses as XXX.XXX.XXX.XXX in hex, oct
or decimal; as well as IP addresses specified as a single 32-bit
number:
-----------
@interface IPFormatter : NSFormatter
@end
@implementation IPFormatter
- (NSString *)stringForObjectValue:(id)obj
{
unsigned int ipNumber;
if([obj isKindOfClass:[NSNumber class]])
{
ipNumber = [obj unsignedIntValue];
}
else
{
ipNumber = [obj intValue];
}
return [NSString stringWithFormat: @"%u.%u.%u.%u",
(ipNumber >> 24) & 0xFF,
(ipNumber >> 16) & 0xFF,
(ipNumber >> 8) & 0xFF,
ipNumber & 0xFF];
}
unsigned long ConvertPart(NSString *part)
{
if(part)
return strtoul([part UTF8String], NULL, 0);
else
return 0;
}
- (BOOL)getObjectValue:(id *)obj forString:(NSString *)string
errorDescription:(NSString **)error
{
//Remove any characters that are not in [0-9xX.]
NSMutableString *tempString = [NSMutableString
stringWithString: string];
NSCharacterSet *illegalCharacters = [[NSCharacterSet
characterSetWithCharactersInString:@"0123456789xX."] invertedSet];
NSRange illegalCharacterRange = [tempString
rangeOfCharacterFromSet: illegalCharacters];
while(illegalCharacterRange.location != NSNotFound)
{
[tempString deleteCharactersInRange: illegalCharacterRange];
illegalCharacterRange = [tempString rangeOfCharacterFromSet:
illegalCharacters];
}
string = tempString;
NSArray *parts = [string componentsSeparatedByString: @"."];
switch([parts count])
{
case 0:
*obj = [NSNumber numberWithUnsignedLong: 0];
return TRUE;
case 1:
*obj = [NSNumber numberWithUnsignedLong: ConvertPart([parts
objectAtIndex: 0])];
return TRUE;
case 2:
*obj = [NSNumber numberWithUnsignedLong: (((ConvertPart([parts
objectAtIndex: 0]) & 0xFF) << 24) |
((ConvertPart([parts
objectAtIndex: 1]) & 0xFF) << 16))];
return TRUE;
case 3:
*obj = [NSNumber numberWithUnsignedLong: (((ConvertPart([parts
objectAtIndex: 0]) & 0xFF) << 24) |
((ConvertPart([parts
objectAtIndex: 1]) & 0xFF) << 16) |
((ConvertPart([parts
objectAtIndex: 2]) & 0xFF) << 8))];
return TRUE;
case 4:
*obj = [NSNumber numberWithUnsignedLong: (((ConvertPart([parts
objectAtIndex: 0]) & 0xFF) << 24) |
((ConvertPart([parts
objectAtIndex: 1]) & 0xFF) << 16) |
((ConvertPart([parts
objectAtIndex: 2]) & 0xFF) << 8) |
(ConvertPart([parts
objectAtIndex: 3]) & 0xFF))];
return TRUE;
default:
return FALSE;
}
}
@end
-----------
--
Clark S. Cox III
email@hidden
http://www.livejournal.com/users/clarkcox3/
http://homepage.mac.com/clarkcox3/
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden