summaryrefslogtreecommitdiffstats
path: root/gettext-tools/src/msgfmt.cs
blob: 1a97f6412ab14aea0b43e7e97bc2f9a0d25e94db (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
/* GNU gettext for C#
 * Copyright (C) 2003, 2015 Free Software Foundation, Inc.
 * Written by Bruno Haible <bruno@clisp.org>, 2003.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * This program creates a .resources file from a set of key/value pairs given
 * on standard input.
 */

using System; /* String, Console, Exception */
using System.IO; /* Stream, BufferedStream, StreamReader */
using System.Text; /* StringBuilder, UTF8Encoding */
using System.Resources; /* ResourceWriter */

namespace GNU.Gettext {
  public class WriteResource {
    private StreamReader reader;
    // Read a NUL-terminated UTF-8 encoded string.
    private String ReadString () {
      StringBuilder b = new StringBuilder();
      for (;;) {
        int c = reader.Read();
        if (c < 0) // EOF?
          return null;
        if (c == 0) // End of String?
          break;
        b.Append((char)c);
      }
      return b.ToString();
    }
    // Read all msgid/msgstr pairs, register them in the ResourceWriter,
    // and write the binary contents to the output stream.
    private void ReadAllInput (ResourceWriter rw) {
      for (;;) {
        String msgid = ReadString();
        if (msgid == null)
          break;
        String msgstr = ReadString();
        if (msgstr == null)
          break;
        rw.AddResource(msgid, msgstr);
      }
      rw.Generate();
    }
    // Read all msgid/msgstr pairs (each string being NUL-terminated and
    // UTF-8 encoded) and write the .resources file to the given filename.
    WriteResource (String filename) {
      Stream input = new BufferedStream(Console.OpenStandardInput());
      reader = new StreamReader(input, new UTF8Encoding());
      if (filename.Equals("-")) {
        BufferedStream output = new BufferedStream(Console.OpenStandardOutput());
        // A temporary output stream is needed because ResourceWriter.Generate
        // expects to be able to seek in the Stream.
        MemoryStream tmpoutput = new MemoryStream();
        ResourceWriter rw = new ResourceWriter(tmpoutput);
        ReadAllInput(rw);
#if __CSCC__
        // Use the ResourceReader to check against pnet-0.6.0 ResourceWriter
        // bug.
        try {
          ResourceReader rr = new ResourceReader(new MemoryStream(tmpoutput.ToArray()));
          foreach (System.Collections.DictionaryEntry entry in rr);
        } catch (IOException e) {
          throw new Exception("class ResourceWriter is buggy", e);
        }
#endif
        tmpoutput.WriteTo(output);
        rw.Close();
        output.Close();
      } else {
#if __CSCC__
        MemoryStream tmpoutput = new MemoryStream();
        ResourceWriter rw = new ResourceWriter(tmpoutput);
        ReadAllInput(rw);
        // Use the ResourceReader to check against pnet-0.6.0 ResourceWriter
        // bug.
        try {
          ResourceReader rr = new ResourceReader(new MemoryStream(tmpoutput.ToArray()));
          foreach (System.Collections.DictionaryEntry entry in rr);
        } catch (IOException e) {
          throw new Exception("class ResourceWriter is buggy", e);
        }
        BufferedStream output = new BufferedStream(new FileStream(filename, FileMode.Create, FileAccess.Write));
        tmpoutput.WriteTo(output);
        rw.Close();
        output.Close();
#else
        ResourceWriter rw = new ResourceWriter(filename);
        ReadAllInput(rw);
        rw.Close();
#endif
      }
    }
    public static int Main (String[] args) {
      try {
        new WriteResource(args[0]);
      } catch (Exception e) {
        Console.Error.WriteLine(e);
        Console.Error.WriteLine(e.StackTrace);
        return 1;
      }
      return 0;
    }
  }
}