OSDN Git Service

fe18a67c97f37ccd198640a05a19a45e97b2f762
[pf3gnuchains/gcc-fork.git] / libjava / gnu / gcj / convert / Convert.java
1 /* Copyright (C) 1999, 2002  Free Software Foundation
2
3    This file is part of libgcj.
4
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
7 details.  */
8
9 package gnu.gcj.convert;
10 import java.io.*;
11
12 public class Convert
13 {
14   static void error (String message)
15   {
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);
20   }
21
22   static void help ()
23   {
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);
39   }
40
41   static void version ()
42   {
43     System.out.println("jv-convert (GNU "
44                        + System.getProperty("java.vm.name")
45                        + ") "
46                        + System.getProperty("java.vm.version"));
47     System.out.println();
48     System.out.println("Copyright 1999, 2002 Free Software Foundation");
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);
52   }
53
54   static void missing (String arg)
55   {
56     error("missing arg after `" + arg + "' option");
57   }
58
59   public static void main (String[] args)
60   {
61     String inName = "-";
62     String outName = "-";
63     String inEncodingName = null;
64     String outEncodingName = "JavaSrc";
65     int seenNames = 0;
66     boolean reverse = false;
67
68     for (int i = 0;  i < args.length;  i++)
69       {
70         String arg = args[i];
71         if (arg.length() == 0)
72           error("zero-length argument");
73         if (arg.charAt(0) == '-')
74           {
75             if (arg.equals("-encoding") || arg.equals("--encoding")
76                 || args.equals("-from") || arg.equals("--from"))
77               {
78                 if (++i == args.length) missing(arg);
79                 inEncodingName = args[i];
80               }
81             else if (arg.equals("-to") || arg.equals("--to"))
82               {
83                 if (++i == args.length) missing(arg);
84                 outEncodingName = args[i];
85               }
86             else if (arg.equals("-i"))
87               {
88                 if (++i == args.length) missing(arg);
89                 inName = args[i];
90               }
91             else if (arg.equals("-o"))
92               {
93                 if (++i == args.length) missing(arg);
94                 outName = args[i];
95               }
96             else if (arg.equals("-reverse") || arg.equals("--reverse"))
97               {
98                 reverse = true;
99               }
100             else if (arg.equals("-help") || arg.equals("--help"))
101               {
102                 help ();
103               }
104             else if (arg.equals("-version") || arg.equals("--version"))
105               {
106                 version ();
107               }
108             else if (arg.equals("-"))
109               {
110                 switch (seenNames)
111                   {
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");
122                   }
123               }
124             else
125               error("unrecognized argument `" + arg + "'");
126           }
127         else
128           {
129             switch (seenNames)
130               {
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");
141               }
142           }
143       }
144
145     if (reverse)
146       {
147         String tmp = inEncodingName;
148         inEncodingName = outEncodingName;
149         outEncodingName = tmp;
150       }
151
152     try
153       {
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 == "-" ? System.in
161           : new FileInputStream(inName);
162         OutputStream outStream;
163         if (outName == "-")
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 (;;)
173           {
174             int count = in.read(buffer);
175             if (count < 0)
176               break;
177             out.write(buffer, 0, count);
178           }
179
180         in.close();
181         out.close();
182       }
183     catch (java.io.IOException ex)
184       {
185         System.err.print("jv-convert exception: ");
186         System.err.println(ex);
187         System.exit(-1);
188       }
189   }
190 }