xref: /netbsd-src/external/gpl2/gettext/dist/gettext-tools/src/msgfmt.cs (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /* GNU gettext for C#
2  * Copyright (C) 2003 Free Software Foundation, Inc.
3  * Written by Bruno Haible <bruno@clisp.org>, 2003.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 /*
21  * This program creates a .resources file from a set of key/value pairs given
22  * on standard input.
23  */
24 
25 using System; /* String, Console, Exception */
26 using System.IO; /* Stream, BufferedStream, StreamReader */
27 using System.Text; /* StringBuilder, UTF8Encoding */
28 using System.Resources; /* ResourceWriter */
29 
30 namespace GNU.Gettext {
31   public class WriteResource {
32     private StreamReader reader;
33     // Read a NUL-terminated UTF-8 encoded string.
ReadString()34     private String ReadString () {
35       StringBuilder b = new StringBuilder();
36       for (;;) {
37         int c = reader.Read();
38         if (c < 0) // EOF?
39           return null;
40         if (c == 0) // End of String?
41           break;
42         b.Append((char)c);
43       }
44       return b.ToString();
45     }
46     // Read all msgid/msgstr pairs, register them in the ResourceWriter,
47     // and write the binary contents to the output stream.
ReadAllInput(ResourceWriter rw)48     private void ReadAllInput (ResourceWriter rw) {
49       for (;;) {
50         String msgid = ReadString();
51         if (msgid == null)
52           break;
53         String msgstr = ReadString();
54         if (msgstr == null)
55           break;
56         rw.AddResource(msgid, msgstr);
57       }
58       rw.Generate();
59     }
60     // Read all msgid/msgstr pairs (each string being NUL-terminated and
61     // UTF-8 encoded) and write the .resources file to the given filename.
WriteResource(String filename)62     WriteResource (String filename) {
63       Stream input = new BufferedStream(Console.OpenStandardInput());
64       reader = new StreamReader(input, new UTF8Encoding());
65       if (filename.Equals("-")) {
66         BufferedStream output = new BufferedStream(Console.OpenStandardOutput());
67         // A temporary output stream is needed because ResourceWriter.Generate
68         // expects to be able to seek in the Stream.
69         MemoryStream tmpoutput = new MemoryStream();
70         ResourceWriter rw = new ResourceWriter(tmpoutput);
71         ReadAllInput(rw);
72 #if __CSCC__
73         // Use the ResourceReader to check against pnet-0.6.0 ResourceWriter
74         // bug.
75         try {
76           ResourceReader rr = new ResourceReader(new MemoryStream(tmpoutput.ToArray()));
77           foreach (System.Collections.DictionaryEntry entry in rr);
78         } catch (IOException e) {
79           throw new Exception("class ResourceWriter is buggy", e);
80         }
81 #endif
82         tmpoutput.WriteTo(output);
83         rw.Close();
84         output.Close();
85       } else {
86 #if __CSCC__
87         MemoryStream tmpoutput = new MemoryStream();
88         ResourceWriter rw = new ResourceWriter(tmpoutput);
89         ReadAllInput(rw);
90         // Use the ResourceReader to check against pnet-0.6.0 ResourceWriter
91         // bug.
92         try {
93           ResourceReader rr = new ResourceReader(new MemoryStream(tmpoutput.ToArray()));
94           foreach (System.Collections.DictionaryEntry entry in rr);
95         } catch (IOException e) {
96           throw new Exception("class ResourceWriter is buggy", e);
97         }
98         BufferedStream output = new BufferedStream(new FileStream(filename, FileMode.Create, FileAccess.Write));
99         tmpoutput.WriteTo(output);
100         rw.Close();
101         output.Close();
102 #else
103         ResourceWriter rw = new ResourceWriter(filename);
104         ReadAllInput(rw);
105         rw.Close();
106 #endif
107       }
108     }
Main(String[] args)109     public static int Main (String[] args) {
110       try {
111         new WriteResource(args[0]);
112       } catch (Exception e) {
113         Console.Error.WriteLine(e);
114         Console.Error.WriteLine(e.StackTrace);
115         return 1;
116       }
117       return 0;
118     }
119   }
120 }
121