FROM : Clark Cox
DATE : Thu Oct 14 23:11:50 2004
On Thu, 14 Oct 2004 10:31:34 -0400, Brent Gulanowski
<<email_removed>> 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_removed>
http://www.livejournal.com/users/clarkcox3/
http://homepage.mac.com/clarkcox3/
DATE : Thu Oct 14 23:11:50 2004
On Thu, 14 Oct 2004 10:31:34 -0400, Brent Gulanowski
<<email_removed>> 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_removed>
http://www.livejournal.com/users/clarkcox3/
http://homepage.mac.com/clarkcox3/
| Related mails | Author | Date |
|---|---|---|
| Brent Gulanowski | Oct 14, 16:31 | |
| J Nozzi | Oct 14, 16:39 | |
| Stéphane Sudre | Oct 14, 22:44 | |
| Clark Cox | Oct 14, 23:11 | |
| Andreas Mayer | Oct 15, 04:31 | |
| Brent Gulanowski | Oct 15, 05:36 | |
| Stéphane Sudre | Oct 15, 08:45 |






Cocoa mail archive

