OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [tools/] [gnu/] [classpath/] [tools/] [native2ascii/] [Native2ASCII.java] - Blame information for rev 779

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 779 jeremybenn
/* Native2ASCII.java - native2ascii program
2
 Copyright (C) 2003, 2007, 2008 Free Software Foundation, Inc.
3
 
4
 This file is part of GNU Classpath.
5
 
6
 GNU Classpath is free software; you can redistribute it and/or modify
7
 it under the terms of the GNU General Public License as published by
8
 the Free Software Foundation; either version 2, or (at your option)
9
 any later version.
10
 
11
 GNU Classpath is distributed in the hope that it will be useful, but
12
 WITHOUT ANY WARRANTY; without even the implied warranty of
13
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 General Public License for more details.
15
 
16
 You should have received a copy of the GNU General Public License
17
 along with GNU Classpath; see the file COPYING.  If not, write to the
18
 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
 02110-1301 USA.
20
 
21
 Linking this library statically or dynamically with other modules is
22
 making a combined work based on this library.  Thus, the terms and
23
 conditions of the GNU General Public License cover the whole
24
 combination.
25
 
26
 As a special exception, the copyright holders of this library give you
27
 permission to link this library with independent modules to produce an
28
 executable, regardless of the license terms of these independent
29
 modules, and to copy and distribute the resulting executable under
30
 terms of your choice, provided that you also meet, for each linked
31
 independent module, the terms and conditions of the license of that
32
 module.  An independent module is a module which is not derived from
33
 or based on this library.  If you modify this library, you may extend
34
 this exception to your version of the library, but you are not
35
 obligated to do so.  If you do not wish to do so, delete this
36
 exception statement from your version. */
37
 
38
 
39
package gnu.classpath.tools.native2ascii;
40
 
41
import gnu.classpath.tools.common.ClasspathToolParser;
42
import gnu.classpath.tools.getopt.FileArgumentCallback;
43
import gnu.classpath.tools.getopt.Option;
44
import gnu.classpath.tools.getopt.OptionException;
45
import gnu.classpath.tools.getopt.Parser;
46
 
47
import java.io.BufferedReader;
48
import java.io.BufferedWriter;
49
import java.io.FileInputStream;
50
import java.io.FileOutputStream;
51
import java.io.InputStream;
52
import java.io.InputStreamReader;
53
import java.io.OutputStream;
54
import java.io.OutputStreamWriter;
55
import java.io.PrintWriter;
56
 
57
/**
58
 * Native2ASCII main program.
59
 * @author Ito Kazumitsu <kaz@maczuka.gcd.org>
60
 */
61
public class Native2ASCII
62
{
63
  // Input file.
64
  String input;
65
  // Output file.
66
  String output;
67
  // Encoding to use.
68
  String encoding;
69
  // True for reverse operation.
70
  boolean reversed;
71
 
72
  private class HandleFile extends FileArgumentCallback
73
  {
74
    public HandleFile()
75
    {
76
    }
77
 
78
    public void notifyFile(String fileArgument)
79
      throws OptionException
80
    {
81
      if (input == null)
82
        input = fileArgument;
83
      else if (output == null)
84
        output = fileArgument;
85
      else
86
        throw new OptionException(Messages.getString("Native2ASCII.TooManyFiles")); //$NON-NLS-1$
87
    }
88
  }
89
 
90
  private Parser createParser()
91
  {
92
    Parser result = new ClasspathToolParser("native2ascii", true); //$NON-NLS-1$
93
    result.setHeader(Messages.getString("Native2ASCII.Usage")); //$NON-NLS-1$
94
 
95
    result.add(new Option("encoding", Messages.getString("Native2ASCII.EncodingHelp"), Messages.getString("Native2ASCII.EncodingArgName")) //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
96
    {
97
      public void parsed(String argument) throws OptionException
98
      {
99
        if (encoding != null)
100
          throw new OptionException(Messages.getString("Native2ASCII.EncodingSpecified")); //$NON-NLS-1$
101
        encoding = argument;
102
      }
103
    });
104
    result.add(new Option("reverse", Messages.getString("Native2ASCII.ReverseHelp")) //$NON-NLS-1$ //$NON-NLS-2$
105
    {
106
      public void parsed(String argument) throws OptionException
107
      {
108
        reversed = true;
109
      }
110
    });
111
 
112
    // We mistakenly added the extra "d" in "reversed"; now we don't
113
    // want to remove it, for backward compatibility.
114
    result.add(new Option("reversed", Messages.getString("Native2ASCII.ReversedHelpCompat")) //$NON-NLS-1$ //$NON-NLS-2$
115
    {
116
      public void parsed(String argument) throws OptionException
117
      {
118
        reversed = true;
119
      }
120
    });
121
 
122
    return result;
123
  }
124
 
125
  private void run(String[] args)
126
  {
127
    Parser argParser = createParser();
128
    argParser.parse(args, new HandleFile());
129
 
130
    if (encoding == null)
131
      encoding = System.getProperty("file.encoding"); //$NON-NLS-1$
132
    try
133
      {
134
        InputStream is = (input == null ? System.in
135
                                        : new FileInputStream(input));
136
        OutputStream os = (output == null ? (OutputStream) System.out
137
                                          : new FileOutputStream(output));
138
 
139
        BufferedReader rdr = new BufferedReader(new InputStreamReader(is,
140
                                                                      encoding));
141
        PrintWriter wtr = new PrintWriter(
142
                                          new BufferedWriter(
143
                                                             new OutputStreamWriter(
144
                                                                                    os,
145
                                                                                    encoding)));
146
        while (true)
147
          {
148
            String s = rdr.readLine();
149
            if (s == null)
150
              break;
151
            StringBuilder sb = new StringBuilder(s.length() + 80);
152
            for (int i = 0; i < s.length(); i++)
153
              {
154
                char c = s.charAt(i);
155
                if (reversed
156
                    && i + 6 <= s.length()
157
                    && s.charAt(i) == '\\'
158
                    && s.charAt(i + 1) == 'u')
159
                  {
160
                    int num = Integer.parseInt(s.substring(i + 2, i + 6), 16);
161
                    sb.append((char) num);
162
                    i += 5;
163
                  }
164
                else if ((int)c <= 127 || reversed)
165
                  {
166
                    sb.append(c);
167
                  }
168
                else
169
                  {
170
                    sb.append("\\u"); //$NON-NLS-1$
171
                    if ((int)c <= 0xff)
172
                      sb.append("00"); //$NON-NLS-1$
173
                    else if ((int)c <= 0xfff)
174
                      sb.append("0"); //$NON-NLS-1$
175
                    sb.append(Integer.toHexString((int) c));
176
                  }
177
              }
178
            wtr.println(sb.toString());
179
          }
180
        rdr.close();
181
        wtr.flush();
182
        wtr.close();
183
      }
184
    catch (Exception e)
185
      {
186
        e.printStackTrace();
187
      }
188
  }
189
 
190
  public static void main(String[] args)
191
  {
192
    new Native2ASCII().run(args);
193
  }
194
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.