• 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: checking for line ending types
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: checking for line ending types


  • Subject: Re: checking for line ending types
  • From: Andrew Merenbach <email@hidden>
  • Date: Sat, 27 Nov 2004 15:40:12 -0800

Good day, Hunter. Your code looks fine, but you should note that the method -[NSString cString] will, according too Apple's NSString class docs, be 'deprecated in the near future.' They instead recommend that one use 'UTF8String to convert arbitrary NSStrings to a lossless 8-bit representation.'

On the other hand, you might not have to deal with C strings at all. If you read the data file into an NSString, you can use -[NSString rangeOfString:], as follows, as well as using an enumeration instead of checking for chars:

typedef enum
{
	GTCLineEndingTypeDOS,
	GTCLineEndingTypeMac,
	GTCLineEndingTypeUnix,
} GTCLineEndingType;

- (GTCLineEndingType)lineEndingTypeForFile:(NSString *)path
{
	NSString *fileContents = [NSString stringWithContentsOfFile:path];

	/* Is this a DOS format? */
	NSRange dosRange = [fileContents rangeOfString:@"\r\n"];
	if (dosRange.location != NSNotFound) return GTCLineEndingTypeDOS;

	/* Not DOS; is this the Mac format? */
	NSRange macRange = [fileContents rangeOfString:@"\r"];
	if (macRange.location != NSNotFound) return GTCLineEndingTypeMac;

	/* Neither DOS nor Mac, so return Unix */
	return GTCLineEndingTypeUnix;
}

(Written in Mail.app--hopefully there are no typos.)

Although each constant in the GTCLineEndingType enumeration will by default be assigned a unique integer, you can manually assign unique numbers if you wish to add some contextual information. You can use a switch() statement on the return of this method--since GTCLineEndingType is a number--and put the line ending type constants in for the case selectors.

Cheers,
	Andrew Merenbach

Attachment: smime.p7s
Description: S/MIME cryptographic signature

 _______________________________________________
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: checking for line ending types
      • From: Andrew Farmer <email@hidden>
  • Prev by Date: Re: Nib files and outlets
  • Next by Date: Re: Nib files and outlets
  • Previous by thread: Re: checking for line ending types
  • Next by thread: Re: checking for line ending types
  • Index(es):
    • Date
    • Thread