aboutsummaryrefslogtreecommitdiff
path: root/libjava/gnu/gcj/convert/Convert.java
blob: 4cdc1152654291c89385535354c83bf5b2accc0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* Copyright (C) 1999  Cygnus Solutions

   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)
  {
    System.err.print("convert: ");
    System.err.println(message);
    System.err.println("Usage:  convert [--from srcEncoding] [--to dstEncoding]");
    System.err.println("  [inputfile [outputfile]]");
    System.exit(-1);
  }

  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;
	      }
	    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)
      {
	System.err.print("convert exception: ");
	System.err.println(ex);
	System.exit(-1);
      }
  }
}