2 * Copyright (C) 2003 Free Software Foundation, Inc.
3 * Written by Bruno Haible <bruno@clisp.org>, 2003.
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.
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.
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/>.
20 * This program creates a .resources file from a set of key/value pairs given
24 using System; /* String, Console, Exception */
25 using System.IO; /* Stream, BufferedStream, StreamReader */
26 using System.Text; /* StringBuilder, UTF8Encoding */
27 using System.Resources; /* ResourceWriter */
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();
36 int c = reader.Read();
39 if (c == 0) // End of String?
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) {
49 String msgid = ReadString();
52 String msgstr = ReadString();
55 rw.AddResource(msgid, msgstr);
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);
72 // Use the ResourceReader to check against pnet-0.6.0 ResourceWriter
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);
81 tmpoutput.WriteTo(output);
86 MemoryStream tmpoutput = new MemoryStream();
87 ResourceWriter rw = new ResourceWriter(tmpoutput);
89 // Use the ResourceReader to check against pnet-0.6.0 ResourceWriter
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);
97 BufferedStream output = new BufferedStream(new FileStream(filename, FileMode.Create, FileAccess.Write));
98 tmpoutput.WriteTo(output);
102 ResourceWriter rw = new ResourceWriter(filename);
108 public static int Main (String[] args) {
110 new WriteResource(args[0]);
111 } catch (Exception e) {
112 Console.Error.WriteLine(e);
113 Console.Error.WriteLine(e.StackTrace);