Merged revisions 143552,143554,143557,143560,143562,143564-143567,143570-143573,14357...
[official-gcc.git] / libjava / gnu / gcj / convert / Convert.java
blob96fe28d42e4c17a80a74ac4c4a6f9c8f9a5c5d7c
1 /* Copyright (C) 1999, 2002, 2005, 2006, 2007, 2008, 2009
2 Free Software Foundation
4 This file is part of libgcj.
6 This software is copyrighted work licensed under the terms of the
7 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
8 details. */
10 package gnu.gcj.convert;
11 import java.io.*;
13 public class Convert
15 static void error (String message)
17 System.err.print("jv-convert: ");
18 System.err.println(message);
19 System.err.println("Try `jv-convert --help' for more information.");
20 System.exit(1);
23 static void help ()
25 System.out.println("Usage: jv-convert [OPTIONS] [INPUTFILE [OUTPUTFILE]]");
26 System.out.println();
27 System.out.println("Convert from one encoding to another.");
28 System.out.println();
29 System.out.println(" --encoding FROM");
30 System.out.println(" --from FROM use FROM as source encoding name");
31 System.out.println(" --to TO use TO as target encoding name");
32 System.out.println(" -i FILE read from FILE");
33 System.out.println(" -o FILE print output to FILE");
34 System.out.println(" --reverse swap FROM and TO encodings");
35 System.out.println(" --help print this help, then exit");
36 System.out.println(" --version print version number, then exit");
37 System.out.println();
38 System.out.println("`-' as a file name argument can be used to refer to stdin or stdout.");
39 System.exit(0);
42 static void version ()
44 System.out.println("jv-convert ("
45 + System.getProperty("java.vm.name")
46 + ") "
47 + System.getProperty("java.vm.version"));
48 System.out.println();
49 System.out.println("Copyright (C) 2009 Free Software Foundation, Inc.");
50 System.out.println("This is free software; see the source for copying conditions. There is NO");
51 System.out.println("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.");
52 System.exit(0);
55 static void missing (String arg)
57 error("missing arg after `" + arg + "' option");
60 public static void main (String[] args)
62 String inName = "-";
63 String outName = "-";
64 String inEncodingName = null;
65 String outEncodingName = "JavaSrc";
66 int seenNames = 0;
67 boolean reverse = false;
69 for (int i = 0; i < args.length; i++)
71 String arg = args[i];
72 if (arg.length() == 0)
73 error("zero-length argument");
74 if (arg.charAt(0) == '-')
76 if (arg.equals("-encoding") || arg.equals("--encoding")
77 || args.equals("-from") || arg.equals("--from"))
79 if (++i == args.length) missing(arg);
80 inEncodingName = args[i];
82 else if (arg.equals("-to") || arg.equals("--to"))
84 if (++i == args.length) missing(arg);
85 outEncodingName = args[i];
87 else if (arg.equals("-i"))
89 if (++i == args.length) missing(arg);
90 inName = args[i];
92 else if (arg.equals("-o"))
94 if (++i == args.length) missing(arg);
95 outName = args[i];
97 else if (arg.equals("-reverse") || arg.equals("--reverse"))
99 reverse = true;
101 else if (arg.equals("-help") || arg.equals("--help"))
103 help ();
105 else if (arg.equals("-version") || arg.equals("--version"))
107 version ();
109 else if (arg.equals("-"))
111 switch (seenNames)
113 case 0:
114 inName = "-";
115 seenNames++;
116 break;
117 case 1:
118 outName = "-";
119 seenNames++;
120 break;
121 default:
122 error("too many `-' arguments");
125 else
126 error("unrecognized argument `" + arg + "'");
128 else
130 switch (seenNames)
132 case 0:
133 inName = arg;
134 seenNames++;
135 break;
136 case 1:
137 outName = arg;
138 seenNames++;
139 break;
140 default:
141 error("too many filename arguments");
146 if (reverse)
148 String tmp = inEncodingName;
149 inEncodingName = outEncodingName;
150 outEncodingName = tmp;
155 InputStream inStream = inName.equals("-") ? System.in
156 : new FileInputStream(inName);
157 OutputStream outStream;
158 if (outName.equals("-"))
159 outStream = System.out;
160 else
161 outStream = new FileOutputStream(outName);
162 InputStreamReader in
163 = (inEncodingName == null
164 ? new InputStreamReader(inStream)
165 : new InputStreamReader(inStream, inEncodingName));
166 OutputStreamWriter out
167 = (outEncodingName == null
168 ? new OutputStreamWriter(outStream)
169 : new OutputStreamWriter(outStream, outEncodingName));
170 char[] buffer = new char[2048];
171 for (;;)
173 int count = in.read(buffer);
174 if (count < 0)
175 break;
176 out.write(buffer, 0, count);
179 in.close();
180 out.close();
182 catch (java.io.IOException ex)
184 System.err.print("jv-convert exception: ");
185 System.err.println(ex);
186 System.exit(-1);