2004-03-29 03:37:46 +00:00
|
|
|
/** This tool converts a file containing a string to a C String encoding.
|
2002-04-05 16:26:47 +00:00
|
|
|
Copyright (C) 2002 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Written by: Richard Frith-Macdonald <richard@brainstorm.co.uk>
|
|
|
|
Created: April 2002
|
|
|
|
|
|
|
|
This file is part of the GNUstep Project
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
2005-02-22 11:22:44 +00:00
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public
|
2002-04-05 16:26:47 +00:00
|
|
|
License along with this library; see the file COPYING.LIB.
|
|
|
|
If not, write to the Free Software Foundation,
|
2005-05-22 03:32:16 +00:00
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2002-04-05 16:26:47 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include <Foundation/Foundation.h>
|
|
|
|
#include <Foundation/NSArray.h>
|
|
|
|
#include <Foundation/NSData.h>
|
|
|
|
#include <Foundation/NSException.h>
|
|
|
|
#include <Foundation/NSString.h>
|
|
|
|
#include <Foundation/NSProcessInfo.h>
|
|
|
|
#include <Foundation/NSUserDefaults.h>
|
|
|
|
#include <Foundation/NSDebug.h>
|
|
|
|
#include <Foundation/NSFileHandle.h>
|
|
|
|
#include <Foundation/NSAutoreleasePool.h>
|
2004-01-15 04:07:08 +00:00
|
|
|
#ifdef NeXT_Foundation_LIBRARY
|
|
|
|
#include "GNUstepBase/GSCategories.h"
|
|
|
|
#endif
|
2002-04-05 16:26:47 +00:00
|
|
|
|
2002-04-07 18:56:08 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
|
2004-03-29 03:37:46 +00:00
|
|
|
/** Return whether value ch between min and max. */
|
2002-04-07 18:56:08 +00:00
|
|
|
#define inrange(ch,min,max) ((ch)>=(min) && (ch)<=(max))
|
2004-03-29 03:37:46 +00:00
|
|
|
/** Convert hex digit in ascii to decimal equivalent. */
|
2002-04-07 18:56:08 +00:00
|
|
|
#define char2num(ch) \
|
|
|
|
inrange(ch,'0','9') \
|
|
|
|
? ((ch)-0x30) \
|
|
|
|
: (inrange(ch,'a','f') \
|
|
|
|
? ((ch)-0x57) : ((ch)-0x37))
|
2002-04-05 16:26:47 +00:00
|
|
|
|
2004-03-29 03:37:46 +00:00
|
|
|
|
|
|
|
/** <p>Converts a file encoded in a specified or default non-unicode encoding
|
|
|
|
* to unicode, or, if the file is already in unicode, converts it to a
|
|
|
|
* specified or default non-unicode encoding. The converted text is
|
|
|
|
* printed to standard out.</p>
|
|
|
|
*/
|
2002-04-05 16:26:47 +00:00
|
|
|
int
|
|
|
|
main(int argc, char** argv, char **env)
|
|
|
|
{
|
|
|
|
NSAutoreleasePool *pool;
|
|
|
|
NSProcessInfo *proc;
|
|
|
|
NSArray *args;
|
|
|
|
unsigned i;
|
2002-04-06 06:33:34 +00:00
|
|
|
BOOL found = NO;
|
2002-04-07 18:56:08 +00:00
|
|
|
BOOL eIn;
|
|
|
|
BOOL eOut;
|
2002-04-06 06:33:34 +00:00
|
|
|
NSString *n;
|
|
|
|
NSStringEncoding enc = 0;
|
2002-04-05 16:26:47 +00:00
|
|
|
|
|
|
|
#ifdef GS_PASS_ARGUMENTS
|
|
|
|
[NSProcessInfo initializeWithArguments: argv count: argc environment: env];
|
|
|
|
#endif
|
|
|
|
pool = [NSAutoreleasePool new];
|
|
|
|
proc = [NSProcessInfo processInfo];
|
|
|
|
if (proc == nil)
|
|
|
|
{
|
|
|
|
NSLog(@"defaults: unable to get process information!\n");
|
|
|
|
[pool release];
|
2003-05-12 20:42:47 +00:00
|
|
|
exit(EXIT_SUCCESS);
|
2002-04-05 16:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
args = [proc arguments];
|
|
|
|
|
2002-04-07 18:56:08 +00:00
|
|
|
eIn = [[NSUserDefaults standardUserDefaults] boolForKey: @"EscapeIn"];
|
|
|
|
eOut = [[NSUserDefaults standardUserDefaults] boolForKey: @"EscapeOut"];
|
2002-04-06 06:33:34 +00:00
|
|
|
n = [[NSUserDefaults standardUserDefaults] stringForKey: @"Encoding"];
|
|
|
|
if (n == nil)
|
2002-04-05 16:26:47 +00:00
|
|
|
{
|
2002-04-06 06:33:34 +00:00
|
|
|
enc = [NSString defaultCStringEncoding];
|
2002-04-05 16:26:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-01-15 04:07:08 +00:00
|
|
|
const NSStringEncoding *e;
|
2002-04-06 06:33:34 +00:00
|
|
|
NSMutableString *names;
|
2002-04-05 16:26:47 +00:00
|
|
|
|
2002-04-06 06:33:34 +00:00
|
|
|
names = [NSMutableString stringWithCapacity: 1024];
|
|
|
|
e = [NSString availableStringEncodings];
|
|
|
|
while (*e != 0)
|
2002-04-05 16:26:47 +00:00
|
|
|
{
|
2002-04-06 06:33:34 +00:00
|
|
|
NSString *name = [NSString localizedNameOfStringEncoding: *e];
|
2002-04-05 16:26:47 +00:00
|
|
|
|
2002-04-06 06:33:34 +00:00
|
|
|
[names appendFormat: @" %@\n", name];
|
|
|
|
if ([n isEqual: name] == YES)
|
2002-04-05 16:26:47 +00:00
|
|
|
{
|
2002-04-06 06:33:34 +00:00
|
|
|
enc = *e;
|
|
|
|
break;
|
2002-04-05 16:26:47 +00:00
|
|
|
}
|
2002-04-06 06:33:34 +00:00
|
|
|
e++;
|
2002-04-05 16:26:47 +00:00
|
|
|
}
|
2002-04-06 06:33:34 +00:00
|
|
|
if (enc == 0)
|
|
|
|
{
|
|
|
|
NSLog(@"defaults: unable to find encoding '%@'!\n"
|
|
|
|
@"Known encoding names are -\n%@", n, names);
|
|
|
|
[pool release];
|
2003-05-12 20:42:47 +00:00
|
|
|
exit(EXIT_SUCCESS);
|
2002-04-06 06:33:34 +00:00
|
|
|
}
|
|
|
|
}
|
2002-04-05 16:26:47 +00:00
|
|
|
|
2002-04-06 06:33:34 +00:00
|
|
|
for (i = 1; found == NO && i < [args count]; i++)
|
|
|
|
{
|
|
|
|
NSString *file = [args objectAtIndex: i];
|
|
|
|
|
2002-04-07 18:56:08 +00:00
|
|
|
if ([file hasPrefix: @"-"] == YES)
|
2002-04-06 06:33:34 +00:00
|
|
|
{
|
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
found = YES;
|
|
|
|
NS_DURING
|
2002-04-05 16:26:47 +00:00
|
|
|
{
|
2002-04-06 06:33:34 +00:00
|
|
|
NSData *myData;
|
2002-04-05 16:26:47 +00:00
|
|
|
|
2002-04-06 06:33:34 +00:00
|
|
|
myData = [[NSData alloc] initWithContentsOfFile: file];
|
|
|
|
if (myData == nil)
|
2002-04-05 16:26:47 +00:00
|
|
|
{
|
2002-04-06 06:33:34 +00:00
|
|
|
NSLog(@"File read operation failed for %@.", file);
|
2002-04-05 16:26:47 +00:00
|
|
|
}
|
2002-04-06 06:33:34 +00:00
|
|
|
else
|
2002-04-05 16:26:47 +00:00
|
|
|
{
|
2002-04-06 06:33:34 +00:00
|
|
|
unsigned l = [myData length];
|
2002-04-07 18:56:08 +00:00
|
|
|
const unichar *b = (const unichar*)[myData bytes];
|
2002-04-06 06:33:34 +00:00
|
|
|
NSStringEncoding iEnc;
|
|
|
|
NSStringEncoding oEnc;
|
|
|
|
NSString *myString;
|
2002-04-05 16:26:47 +00:00
|
|
|
|
2002-04-06 06:33:34 +00:00
|
|
|
if (l > 1 && (*b == 0xFFFE || *b == 0xFEFF))
|
2002-04-05 16:26:47 +00:00
|
|
|
{
|
2002-04-06 06:33:34 +00:00
|
|
|
iEnc = NSUnicodeStringEncoding;
|
|
|
|
oEnc = enc;
|
2002-04-05 16:26:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-04-06 06:33:34 +00:00
|
|
|
iEnc = enc;
|
|
|
|
oEnc = NSUnicodeStringEncoding;
|
|
|
|
}
|
2005-02-22 11:22:44 +00:00
|
|
|
|
2002-04-06 06:33:34 +00:00
|
|
|
myString = [[NSString alloc] initWithData: myData
|
|
|
|
encoding: iEnc];
|
|
|
|
RELEASE(myData);
|
|
|
|
if (myString == nil)
|
|
|
|
{
|
|
|
|
NSLog(@"Encoding input conversion failed for %@.", file);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-04-07 18:56:08 +00:00
|
|
|
if (eIn == YES)
|
|
|
|
{
|
|
|
|
unsigned l = [myString length];
|
|
|
|
unichar *u;
|
|
|
|
NSZone *z = NSDefaultMallocZone();
|
|
|
|
unsigned i = 0;
|
|
|
|
unsigned o = 0;
|
|
|
|
|
|
|
|
u = NSZoneMalloc(z, sizeof(unichar)*l);
|
|
|
|
[myString getCharacters: u];
|
|
|
|
|
|
|
|
while (i < l)
|
|
|
|
{
|
|
|
|
unichar c = u[i++];
|
|
|
|
|
|
|
|
if (c == '\\' && i <= l - 6)
|
|
|
|
{
|
|
|
|
c = u[i++];
|
|
|
|
|
|
|
|
if (c == 'u' || c == 'U')
|
|
|
|
{
|
|
|
|
unichar v;
|
|
|
|
|
|
|
|
v = 0;
|
|
|
|
c = u[i++];
|
|
|
|
v |= char2num(c);
|
|
|
|
|
|
|
|
v <<= 4;
|
|
|
|
c = u[i++];
|
|
|
|
v |= char2num(c);
|
2005-02-22 11:22:44 +00:00
|
|
|
|
2002-04-07 18:56:08 +00:00
|
|
|
v <<= 4;
|
|
|
|
c = u[i++];
|
|
|
|
v |= char2num(c);
|
2005-02-22 11:22:44 +00:00
|
|
|
|
2002-04-07 18:56:08 +00:00
|
|
|
v <<= 4;
|
|
|
|
c = u[i++];
|
|
|
|
v |= char2num(c);
|
|
|
|
|
|
|
|
c = v;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
u[o++] = '\\';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
u[o++] = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
RELEASE(myString);
|
|
|
|
myString = [[NSString alloc] initWithCharactersNoCopy: u
|
|
|
|
length: o freeWhenDone: YES];
|
|
|
|
}
|
|
|
|
if (eOut == YES)
|
|
|
|
{
|
|
|
|
unsigned l = [myString length];
|
|
|
|
unichar *u;
|
|
|
|
char *c;
|
|
|
|
NSZone *z = NSDefaultMallocZone();
|
|
|
|
unsigned o = 0;
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
u = NSZoneMalloc(z, sizeof(unichar)*l);
|
|
|
|
c = NSZoneMalloc(z, 6*l);
|
|
|
|
[myString getCharacters: u];
|
|
|
|
for (i = 0; i < l; i++)
|
|
|
|
{
|
|
|
|
if (u[i] < 128)
|
|
|
|
{
|
|
|
|
c[o++] = u[i];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-07-10 19:38:41 +00:00
|
|
|
sprintf(&c[o], "\\U%04x", u[i]);
|
2002-04-07 18:56:08 +00:00
|
|
|
o += 6;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NSZoneFree(z, u);
|
|
|
|
myData = [[NSData alloc] initWithBytesNoCopy: c
|
|
|
|
length: o];
|
|
|
|
}
|
2003-09-22 03:00:21 +00:00
|
|
|
else if (eIn == YES)
|
|
|
|
{
|
|
|
|
myData = [myString dataUsingEncoding: iEnc
|
|
|
|
allowLossyConversion: NO];
|
|
|
|
}
|
2002-04-07 18:56:08 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
myData = [myString dataUsingEncoding: oEnc
|
|
|
|
allowLossyConversion: NO];
|
|
|
|
}
|
2002-04-06 06:33:34 +00:00
|
|
|
RELEASE(myString);
|
|
|
|
if (myData == nil)
|
|
|
|
{
|
|
|
|
NSLog(@"Encoding output conversion failed for %@.",
|
|
|
|
file);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NSFileHandle *out;
|
|
|
|
|
|
|
|
out = [NSFileHandle fileHandleWithStandardOutput];
|
|
|
|
[out writeData: myData];
|
|
|
|
[out synchronizeFile];
|
|
|
|
}
|
2002-04-05 16:26:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-04-06 06:33:34 +00:00
|
|
|
NS_HANDLER
|
|
|
|
{
|
|
|
|
NSLog(@"Converting '%@' - %@", file, [localException reason]);
|
|
|
|
}
|
|
|
|
NS_ENDHANDLER
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found == NO)
|
|
|
|
{
|
|
|
|
NSLog(@"\nThis utility expects a filename as an argument.\n"
|
|
|
|
@"It reads the file, and writes it to STDOUT after converting it\n"
|
|
|
|
@"to unicode from C string encoding or vice versa.\n"
|
|
|
|
@"You can supply a '-Encoding name' option to specify the C string\n"
|
2002-04-07 18:56:08 +00:00
|
|
|
@"encoding to be used, if you don't want to use the default.\n"
|
|
|
|
@"You can supply a '-EscapeIn YES' option to specify that input\n"
|
2004-07-10 19:38:41 +00:00
|
|
|
@"should be parsed for \\U escape sequences (as in property lists).\n"
|
2002-04-07 18:56:08 +00:00
|
|
|
@"You can supply a '-EscapeOut YES' option to specify that output\n"
|
2004-07-10 19:38:41 +00:00
|
|
|
@"should be ascii with \\U escape sequences (for property lists).\n");
|
2002-04-05 16:26:47 +00:00
|
|
|
}
|
|
|
|
[pool release];
|
|
|
|
return 0;
|
|
|
|
}
|