Imported Upstream version 0.18.3.2
[platform/upstream/gettext.git] / gettext-tools / src / msgfmt.cs
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 3 of the License, or
8  * (at your option) 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, see <http://www.gnu.org/licenses/>.
17  */
18
19 /*
20  * This program creates a .resources file from a set of key/value pairs given
21  * on standard input.
22  */
23
24 using System; /* String, Console, Exception */
25 using System.IO; /* Stream, BufferedStream, StreamReader */
26 using System.Text; /* StringBuilder, UTF8Encoding */
27 using System.Resources; /* ResourceWriter */
28
29 namespace GNU.Gettext {
30   public class WriteResource {
31     private StreamReader reader;
32     // Read a NUL-terminated UTF-8 encoded string.
33     private String ReadString () {
34       StringBuilder b = new StringBuilder();
35       for (;;) {
36         int c = reader.Read();
37         if (c < 0) // EOF?
38           return null;
39         if (c == 0) // End of String?
40           break;
41         b.Append((char)c);
42       }
43       return b.ToString();
44     }
45     // Read all msgid/msgstr pairs, register them in the ResourceWriter,
46     // and write the binary contents to the output stream.
47     private void ReadAllInput (ResourceWriter rw) {
48       for (;;) {
49         String msgid = ReadString();
50         if (msgid == null)
51           break;
52         String msgstr = ReadString();
53         if (msgstr == null)
54           break;
55         rw.AddResource(msgid, msgstr);
56       }
57       rw.Generate();
58     }
59     // Read all msgid/msgstr pairs (each string being NUL-terminated and
60     // UTF-8 encoded) and write the .resources file to the given filename.
61     WriteResource (String filename) {
62       Stream input = new BufferedStream(Console.OpenStandardInput());
63       reader = new StreamReader(input, new UTF8Encoding());
64       if (filename.Equals("-")) {
65         BufferedStream output = new BufferedStream(Console.OpenStandardOutput());
66         // A temporary output stream is needed because ResourceWriter.Generate
67         // expects to be able to seek in the Stream.
68         MemoryStream tmpoutput = new MemoryStream();
69         ResourceWriter rw = new ResourceWriter(tmpoutput);
70         ReadAllInput(rw);
71 #if __CSCC__
72         // Use the ResourceReader to check against pnet-0.6.0 ResourceWriter
73         // bug.
74         try {
75           ResourceReader rr = new ResourceReader(new MemoryStream(tmpoutput.ToArray()));
76           foreach (System.Collections.DictionaryEntry entry in rr);
77         } catch (IOException e) {
78           throw new Exception("class ResourceWriter is buggy", e);
79         }
80 #endif
81         tmpoutput.WriteTo(output);
82         rw.Close();
83         output.Close();
84       } else {
85 #if __CSCC__
86         MemoryStream tmpoutput = new MemoryStream();
87         ResourceWriter rw = new ResourceWriter(tmpoutput);
88         ReadAllInput(rw);
89         // Use the ResourceReader to check against pnet-0.6.0 ResourceWriter
90         // bug.
91         try {
92           ResourceReader rr = new ResourceReader(new MemoryStream(tmpoutput.ToArray()));
93           foreach (System.Collections.DictionaryEntry entry in rr);
94         } catch (IOException e) {
95           throw new Exception("class ResourceWriter is buggy", e);
96         }
97         BufferedStream output = new BufferedStream(new FileStream(filename, FileMode.Create, FileAccess.Write));
98         tmpoutput.WriteTo(output);
99         rw.Close();
100         output.Close();
101 #else
102         ResourceWriter rw = new ResourceWriter(filename);
103         ReadAllInput(rw);
104         rw.Close();
105 #endif
106       }
107     }
108     public static int Main (String[] args) {
109       try {
110         new WriteResource(args[0]);
111       } catch (Exception e) {
112         Console.Error.WriteLine(e);
113         Console.Error.WriteLine(e.StackTrace);
114         return 1;
115       }
116       return 0;
117     }
118   }
119 }