views:

94

answers:

2

Do I have to read the files and iterate manually? I'd like to be able to change between LF and CRLF.

+1  A: 

You can use the "tr" command in the terminal.

Matthieu Cormier
No good way to do that in Cocoa without using NSTask to run a command?
zekel
+1  A: 

I'm sure there are more memory-efficient ways, but this might do the job for you:

NSStringEncoding usedEncoding;
NSMutableString *fileContents = [[NSMutableString alloc] initWithContentsOfFile:pathToFile usedEncoding:&usedEncoding error:nil];

// Normally you'd pass in an error and do the checking thing.

[fileContents replaceOccurrencesOfString:@"\n" withString:@"\r\n" options:NSLiteralSearch range:NSMakeRange(0, [fileContents length])];
// The other direction: [fileContents replaceOccurrencesOfString:@"\r\n" withString:@"\n" options:NSLiteralSearch range:NSMakeRange(0, [fileContents length])];

// Assumes you want to overwrite the file; again, normally you'd check for errors and such.
[fileContents writeToFile:filePath atomically:YES encoding:usedEncoding error:nil];
[fileContents release];

pathToFile is obviously the path to the file; substitute the initWithContentsOfURL:.../writeToURL:... versions if you prefer.

Wevah
I just realized that this will cause issues if you read in a file that already uses CRLF line-endings; hopefully you already know this ahead of time/are detecting this. >_<
Wevah