Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / gnu / gcj / convert / Convert.java
blob5d4a314d60950d64dd2aef695149d38849d552d1
1 /* Copyright (C) 1999, 2002, 2005 Free Software Foundation
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
7 details. */
9 package gnu.gcj.convert;
10 import java.io.*;
12 public class Convert
14 static void error (String message)
16 System.err.print("jv-convert: ");
17 System.err.println(message);
18 System.err.println("Try `jv-convert --help' for more information.");
19 System.exit(1);
22 static void help ()
24 System.out.println("Usage: jv-convert [OPTIONS] [INPUTFILE [OUTPUTFILE]]");
25 System.out.println();
26 System.out.println("Convert from one encoding to another.");
27 System.out.println();
28 System.out.println(" --encoding FROM");
29 System.out.println(" --from FROM use FROM as source encoding name");
30 System.out.println(" --to TO use TO as target encoding name");
31 System.out.println(" -i FILE read from FILE");
32 System.out.println(" -o FILE print output to FILE");
33 System.out.println(" --reverse swap FROM and TO encodings");
34 System.out.println(" --help print this help, then exit");
35 System.out.println(" --version print version number, then exit");
36 System.out.println();
37 System.out.println("`-' as a file name argument can be used to refer to stdin or stdout.");
38 System.exit(0);
41 static void version ()
43 System.out.println("jv-convert ("
44 + System.getProperty("java.vm.name")
45 + ") "
46 + System.getProperty("java.vm.version"));
47 System.out.println();
48 System.out.println("Copyright (C) 2005 Free Software Foundation, Inc.");
49 System.out.println("This is free software; see the source for copying conditions. There is NO");
50 System.out.println("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.");
51 System.exit(0);
54 static void missing (String arg)
56 error("missing arg after `" + arg + "' option");
59 public static void main (String[] args)
61 String inName = "-";
62 String outName = "-";
63 String inEncodingName = null;
64 String outEncodingName = "JavaSrc";
65 int seenNames = 0;
66 boolean reverse = false;
68 for (int i = 0; i < args.length; i++)
70 String arg = args[i];
71 if (arg.length() == 0)
72 error("zero-length argument");
73 if (arg.charAt(0) == '-')
75 if (arg.equals("-encoding") || arg.equals("--encoding")
76 || args.equals("-from") || arg.equals("--from"))
78 if (++i == args.length) missing(arg);
79 inEncodingName = args[i];
81 else if (arg.equals("-to") || arg.equals("--to"))
83 if (++i == args.length) missing(arg);
84 outEncodingName = args[i];
86 else if (arg.equals("-i"))
88 if (++i == args.length) missing(arg);
89 inName = args[i];
91 else if (arg.equals("-o"))
93 if (++i == args.length) missing(arg);
94 outName = args[i];
96 else if (arg.equals("-reverse") || arg.equals("--reverse"))
98 reverse = true;
100 else if (arg.equals("-help") || arg.equals("--help"))
102 help ();
104 else if (arg.equals("-version") || arg.equals("--version"))
106 version ();
108 else if (arg.equals("-"))
110 switch (seenNames)
112 case 0:
113 inName = "-";
114 seenNames++;
115 break;
116 case 1:
117 outName = "-";
118 seenNames++;
119 break;
120 default:
121 error("too many `-' arguments");
124 else
125 error("unrecognized argument `" + arg + "'");
127 else
129 switch (seenNames)
131 case 0:
132 inName = arg;
133 seenNames++;
134 break;
135 case 1:
136 outName = arg;
137 seenNames++;
138 break;
139 default:
140 error("too many filename arguments");
145 if (reverse)
147 String tmp = inEncodingName;
148 inEncodingName = outEncodingName;
149 outEncodingName = tmp;
154 BytesToUnicode inDecoder
155 = inEncodingName == null ? BytesToUnicode.getDefaultDecoder()
156 : BytesToUnicode.getDecoder(inEncodingName);
157 UnicodeToBytes outEncoder
158 = outEncodingName == null ? UnicodeToBytes.getDefaultEncoder()
159 : UnicodeToBytes.getEncoder(outEncodingName);
160 InputStream inStream = inName.equals("-") ? System.in
161 : new FileInputStream(inName);
162 OutputStream outStream;
163 if (outName.equals("-"))
164 outStream = System.out;
165 else
166 outStream = new FileOutputStream(outName);
167 InputStreamReader in
168 = new InputStreamReader(inStream, inEncodingName);
169 OutputStreamWriter out
170 = new OutputStreamWriter(outStream, outEncodingName);
171 char[] buffer = new char[2048];
172 for (;;)
174 int count = in.read(buffer);
175 if (count < 0)
176 break;
177 out.write(buffer, 0, count);
180 in.close();
181 out.close();
183 catch (java.io.IOException ex)
185 System.err.print("jv-convert exception: ");
186 System.err.println(ex);
187 System.exit(-1);