Created
February 8, 2010 05:18
-
-
Save atr000/297909 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
int main (int argc, const char * argv[]) { | |
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; | |
// arguments array | |
NSArray *args = [[NSProcessInfo processInfo] arguments]; | |
// check if all arguments have been supplied | |
if ( argc != 3 && argc != 4 ) | |
printf("Usage:\nSource directory [path]\ntag [string]\nencoding [MacRoman / ISO8859-1 / UTF8 / UTF16]\n"); | |
else | |
{ | |
NSFileManager *NSFm; | |
NSStringEncoding textEncoding; | |
NSFm = [NSFileManager defaultManager]; | |
NSString *sourceDirectory = [args objectAtIndex:1]; | |
if ([NSFm fileExistsAtPath:sourceDirectory] == NO) { | |
printf("source directory doesn't exist.\n"); | |
return 1; } | |
NSString *tag = [args objectAtIndex:2]; | |
NSString *inTag = [NSString stringWithFormat:@"<%@>", tag]; | |
NSString *outTag = [NSString stringWithFormat:@"</%@>", tag]; | |
if (argc == 4) { | |
NSString *enc = [args objectAtIndex:3]; | |
if ([enc isEqualToString:@"MacRoman"]) | |
textEncoding = NSMacOSRomanStringEncoding; | |
else if ([enc isEqualToString:@"ISO8859-1"]) | |
textEncoding = NSISOLatin1StringEncoding; | |
else if ([enc isEqualToString:@"UTF8"]) | |
textEncoding = NSUTF8StringEncoding; | |
else if ([enc isEqualToString:@"UTF16"]) | |
textEncoding = NSUnicodeStringEncoding; | |
else | |
{ | |
printf("Invalid text encoding.\n"); | |
return 2; | |
} | |
} | |
else | |
textEncoding = NSMacOSRomanStringEncoding; | |
NSString *fileName, *sourcePath, *value, *destinationPath; | |
NSScanner *fileScanner; | |
NSArray *fileArray; | |
int i, n; | |
fileArray = [NSFm directoryContentsAtPath:sourceDirectory]; | |
n = [fileArray count]; | |
for (i=0; i < n; ++i) | |
{ | |
fileName = [fileArray objectAtIndex:i]; | |
if ([fileName hasPrefix:@"."] == NO) { | |
sourcePath = [sourceDirectory stringByAppendingPathComponent:fileName]; | |
NSString *sourceText = [[NSString alloc] | |
initWithContentsOfFile:sourcePath | |
encoding:textEncoding | |
error:NULL]; | |
if (sourceText != nil) | |
{ | |
fileScanner = [NSScanner scannerWithString:sourceText]; | |
if ([fileScanner scanUpToString:inTag intoString:NULL] == YES) | |
{ | |
[fileScanner scanString:inTag intoString:NULL]; | |
[fileScanner scanUpToString:outTag intoString:&value]; | |
destinationPath = [sourceDirectory stringByAppendingPathComponent:value]; | |
destinationPath = [destinationPath stringByAppendingString:@".txt"]; | |
[NSFm movePath:sourcePath toPath:destinationPath handler:nil]; | |
} | |
[sourceText release]; | |
} | |
} | |
} | |
} | |
[pool release]; | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment