mirror of
https://github.com/gnustep/libs-base.git
synced 2025-05-31 00:30:53 +00:00
Conversion to Unicode files
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@10819 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
9760449718
commit
00b897f684
4 changed files with 85 additions and 2 deletions
|
@ -20,6 +20,7 @@
|
|||
|
||||
#include "config.h"
|
||||
#include <Foundation/NSArray.h>
|
||||
#include <Foundation/NSData.h>
|
||||
#include <Foundation/NSException.h>
|
||||
#include <Foundation/NSString.h>
|
||||
#include <Foundation/NSProcessInfo.h>
|
||||
|
@ -27,6 +28,71 @@
|
|||
#include <Foundation/NSDebug.h>
|
||||
#include <Foundation/NSAutoreleasePool.h>
|
||||
|
||||
int
|
||||
convert_unicode(NSArray *args)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 2; i < [args count]; i++)
|
||||
{
|
||||
NSString *file = [args objectAtIndex: i];
|
||||
|
||||
NS_DURING
|
||||
{
|
||||
NSData *data;
|
||||
NSString *myString;
|
||||
NSString *output;
|
||||
|
||||
data = [NSData dataWithContentsOfFile: file];
|
||||
myString = [[NSString alloc] initWithData: data
|
||||
encoding: NSUTF8StringEncoding];
|
||||
AUTORELEASE(myString);
|
||||
if ([myString length] == 0)
|
||||
myString = [[NSString alloc] initWithData: data
|
||||
encoding: [NSString defaultCStringEncoding]];
|
||||
output = [[file lastPathComponent]
|
||||
stringByAppendingPathExtension: @"unicode"];
|
||||
data = [myString dataUsingEncoding: NSUnicodeStringEncoding];
|
||||
[data writeToFile: output atomically: YES];
|
||||
}
|
||||
NS_HANDLER
|
||||
{
|
||||
NSLog(@"Converting '%@' - %@", file, [localException reason]);
|
||||
}
|
||||
NS_ENDHANDLER
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
convert_utf8(NSArray *args)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 2; i < [args count]; i++)
|
||||
{
|
||||
NSString *file = [args objectAtIndex: i];
|
||||
|
||||
NS_DURING
|
||||
{
|
||||
NSData *data;
|
||||
NSString *myString;
|
||||
NSString *output;
|
||||
|
||||
myString = [NSString stringWithContentsOfFile: file];
|
||||
output = [[file lastPathComponent]
|
||||
stringByAppendingPathExtension: @"utf8"];
|
||||
data = [myString dataUsingEncoding: NSUTF8StringEncoding];
|
||||
[data writeToFile: output atomically: YES];
|
||||
}
|
||||
NS_HANDLER
|
||||
{
|
||||
NSLog(@"Converting '%@' - %@", file, [localException reason]);
|
||||
}
|
||||
NS_ENDHANDLER
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char** argv, char **env)
|
||||
|
@ -50,9 +116,20 @@ main(int argc, char** argv, char **env)
|
|||
|
||||
args = [proc arguments];
|
||||
|
||||
if ([args count] <= 1)
|
||||
if ([args count] <= 1 || [[args objectAtIndex: 1] isEqual: @"--help"]
|
||||
|| [[args objectAtIndex: 1] isEqual: @"-h"])
|
||||
{
|
||||
NSLog(@"No file names given to parse.");
|
||||
printf("Usage: sfparse [--utf8] filename.\n");
|
||||
printf("--unicode - convert an ASCII or UTF8 file to Unicode\n");
|
||||
printf("--utf8 - convert an ASCII or Unicode to UTF8\n");
|
||||
}
|
||||
else if ([[args objectAtIndex: 1] isEqual: @"--unicode"])
|
||||
{
|
||||
convert_unicode(args);
|
||||
}
|
||||
else if ([[args objectAtIndex: 1] isEqual: @"--utf8"])
|
||||
{
|
||||
convert_utf8(args);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue