2002-01-10 01:39:16 +01:00
|
|
|
/* Copyright (C) 1999, 2002 Free Software Foundation
|
1999-04-07 16:42:40 +02:00
|
|
|
|
|
|
|
This file is part of libgcj.
|
|
|
|
|
|
|
|
This software is copyrighted work licensed under the terms of the
|
|
|
|
Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
package gnu.gcj.convert;
|
|
|
|
import java.io.*;
|
|
|
|
|
|
|
|
public class Convert
|
|
|
|
{
|
|
|
|
static void error (String message)
|
|
|
|
{
|
2002-01-10 01:39:16 +01:00
|
|
|
System.err.print("jv-convert: ");
|
1999-04-07 16:42:40 +02:00
|
|
|
System.err.println(message);
|
2002-01-10 01:39:16 +01:00
|
|
|
System.err.println("Try `jv-convert --help' for more information.");
|
|
|
|
System.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void help ()
|
|
|
|
{
|
|
|
|
System.out.println("Usage: jv-convert [OPTIONS] [INPUTFILE [OUTPUTFILE]]");
|
2002-01-10 18:56:26 +01:00
|
|
|
System.out.println();
|
2002-01-10 01:39:16 +01:00
|
|
|
System.out.println("Convert from one encoding to another.");
|
2002-01-10 18:56:26 +01:00
|
|
|
System.out.println();
|
2002-01-10 01:39:16 +01:00
|
|
|
System.out.println(" --encoding FROM");
|
|
|
|
System.out.println(" --from FROM use FROM as source encoding name");
|
|
|
|
System.out.println(" --to TO use TO as target encoding name");
|
|
|
|
System.out.println(" -i FILE read from FILE");
|
|
|
|
System.out.println(" -o FILE print output to FILE");
|
|
|
|
System.out.println(" --reverse swap FROM and TO encodings");
|
|
|
|
System.out.println(" --help print this help, then exit");
|
|
|
|
System.out.println(" --version print version number, then exit");
|
2002-01-10 18:56:26 +01:00
|
|
|
System.out.println();
|
2002-01-10 01:39:16 +01:00
|
|
|
System.out.println("`-' as a file name argument can be used to refer to stdin or stdout.");
|
|
|
|
System.exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void version ()
|
|
|
|
{
|
2002-03-25 23:08:27 +01:00
|
|
|
System.out.println("jv-convert ("
|
2002-01-10 18:56:26 +01:00
|
|
|
+ System.getProperty("java.vm.name")
|
|
|
|
+ ") "
|
2002-01-10 01:39:16 +01:00
|
|
|
+ System.getProperty("java.vm.version"));
|
2002-01-10 18:56:26 +01:00
|
|
|
System.out.println();
|
2002-02-23 00:24:45 +01:00
|
|
|
System.out.println("Copyright (C) 2002 Free Software Foundation, Inc.");
|
2002-01-10 01:39:16 +01:00
|
|
|
System.out.println("This is free software; see the source for copying conditions. There is NO");
|
|
|
|
System.out.println("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.");
|
|
|
|
System.exit(0);
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void missing (String arg)
|
|
|
|
{
|
|
|
|
error("missing arg after `" + arg + "' option");
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void main (String[] args)
|
|
|
|
{
|
|
|
|
String inName = "-";
|
|
|
|
String outName = "-";
|
|
|
|
String inEncodingName = null;
|
|
|
|
String outEncodingName = "JavaSrc";
|
|
|
|
int seenNames = 0;
|
|
|
|
boolean reverse = false;
|
|
|
|
|
|
|
|
for (int i = 0; i < args.length; i++)
|
|
|
|
{
|
|
|
|
String arg = args[i];
|
|
|
|
if (arg.length() == 0)
|
|
|
|
error("zero-length argument");
|
|
|
|
if (arg.charAt(0) == '-')
|
|
|
|
{
|
|
|
|
if (arg.equals("-encoding") || arg.equals("--encoding")
|
|
|
|
|| args.equals("-from") || arg.equals("--from"))
|
|
|
|
{
|
|
|
|
if (++i == args.length) missing(arg);
|
|
|
|
inEncodingName = args[i];
|
|
|
|
}
|
|
|
|
else if (arg.equals("-to") || arg.equals("--to"))
|
|
|
|
{
|
|
|
|
if (++i == args.length) missing(arg);
|
|
|
|
outEncodingName = args[i];
|
|
|
|
}
|
|
|
|
else if (arg.equals("-i"))
|
|
|
|
{
|
|
|
|
if (++i == args.length) missing(arg);
|
|
|
|
inName = args[i];
|
|
|
|
}
|
|
|
|
else if (arg.equals("-o"))
|
|
|
|
{
|
|
|
|
if (++i == args.length) missing(arg);
|
|
|
|
outName = args[i];
|
|
|
|
}
|
|
|
|
else if (arg.equals("-reverse") || arg.equals("--reverse"))
|
|
|
|
{
|
|
|
|
reverse = true;
|
|
|
|
}
|
2002-01-10 01:39:16 +01:00
|
|
|
else if (arg.equals("-help") || arg.equals("--help"))
|
|
|
|
{
|
|
|
|
help ();
|
|
|
|
}
|
|
|
|
else if (arg.equals("-version") || arg.equals("--version"))
|
|
|
|
{
|
|
|
|
version ();
|
|
|
|
}
|
1999-04-07 16:42:40 +02:00
|
|
|
else if (arg.equals("-"))
|
|
|
|
{
|
|
|
|
switch (seenNames)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
inName = "-";
|
|
|
|
seenNames++;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
outName = "-";
|
|
|
|
seenNames++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("too many `-' arguments");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
error("unrecognized argument `" + arg + "'");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (seenNames)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
inName = arg;
|
|
|
|
seenNames++;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
outName = arg;
|
|
|
|
seenNames++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("too many filename arguments");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reverse)
|
|
|
|
{
|
|
|
|
String tmp = inEncodingName;
|
|
|
|
inEncodingName = outEncodingName;
|
|
|
|
outEncodingName = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
BytesToUnicode inDecoder
|
|
|
|
= inEncodingName == null ? BytesToUnicode.getDefaultDecoder()
|
|
|
|
: BytesToUnicode.getDecoder(inEncodingName);
|
|
|
|
UnicodeToBytes outEncoder
|
|
|
|
= outEncodingName == null ? UnicodeToBytes.getDefaultEncoder()
|
|
|
|
: UnicodeToBytes.getEncoder(outEncodingName);
|
|
|
|
InputStream inStream = inName == "-" ? System.in
|
|
|
|
: new FileInputStream(inName);
|
|
|
|
OutputStream outStream;
|
|
|
|
if (outName == "-")
|
|
|
|
outStream = System.out;
|
|
|
|
else
|
|
|
|
outStream = new FileOutputStream(outName);
|
|
|
|
InputStreamReader in
|
|
|
|
= new InputStreamReader(inStream, inEncodingName);
|
|
|
|
OutputStreamWriter out
|
|
|
|
= new OutputStreamWriter(outStream, outEncodingName);
|
|
|
|
char[] buffer = new char[2048];
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
int count = in.read(buffer);
|
|
|
|
if (count < 0)
|
|
|
|
break;
|
|
|
|
out.write(buffer, 0, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
in.close();
|
|
|
|
out.close();
|
|
|
|
}
|
|
|
|
catch (java.io.IOException ex)
|
|
|
|
{
|
2002-01-10 01:39:16 +01:00
|
|
|
System.err.print("jv-convert exception: ");
|
1999-04-07 16:42:40 +02:00
|
|
|
System.err.println(ex);
|
|
|
|
System.exit(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|