• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: IPAddressFormatter?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: IPAddressFormatter?


  • Subject: Re: IPAddressFormatter?
  • From: Brent Gulanowski <email@hidden>
  • Date: Thu, 14 Oct 2004 23:36:00 -0400
  • Domainkeys-signature: a=rsa-sha1; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:references; b=JFKH+SjpgA1k9ZBY9sZiZ0Lpvrh5kC2u5bPZtj14DpKcqVvflyE/iw0iEdEkZIME06Axe7DxGX+2vlFTsVSPDkdVxW06c4oOl9vm9MXhLHZf37WuCGQbeRJckPih2KGonB/Q7yJEp7AJHmmRKZbq7Kz1ry9dKOxwjbQf/+qpOYE

Thanks, Clark, I'll give this a good reading over. Only thing I'm not
sure of is why I'd ever want to store the IP address as an integer.
Seems better to always treat it as a string and insert dots as
required. Unless maybe as four integers (as for an IPv6 version, I'm
not that far along in the book). Plust a colon and another integer for
the port.

Someone was asking "input or output" -- sorry I meant input; it's to
control what user's can type.


On Thu, 14 Oct 2004 17:11:50 -0400, Clark Cox <email@hidden> wrote:
> 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/
>


--

Brent Gulanowski
http://www.boredastronaut.com
_______________________________________________
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

  • Follow-Ups:
    • Re: IPAddressFormatter?
      • From: Stéphane Sudre <email@hidden>
References: 
 >IPAddressFormatter? (From: Brent Gulanowski <email@hidden>)
 >Re: IPAddressFormatter? (From: Clark Cox <email@hidden>)

  • Prev by Date: supplying default values to popup in table
  • Next by Date: -[NSUserDefaultsController save:]
  • Previous by thread: Re: IPAddressFormatter?
  • Next by thread: Re: IPAddressFormatter?
  • Index(es):
    • Date
    • Thread