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 2, or (at your option)
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, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 * This program creates a .resources file from a set of key/value pairs given
25 using System
; /* String, Console, Exception */
26 using System
.IO
; /* Stream, BufferedStream, StreamReader */
27 using System
.Text
; /* StringBuilder, UTF8Encoding */
28 using System
.Resources
; /* ResourceWriter */
30 namespace GNU
.Gettext
{
31 public class WriteResource
{
32 private StreamReader reader
;
33 // Read a NUL-terminated UTF-8 encoded string.
34 private String
ReadString () {
35 StringBuilder b
= new StringBuilder();
37 int c
= reader
.Read();
40 if (c
== 0) // End of String?
46 // Read all msgid/msgstr pairs, register them in the ResourceWriter,
47 // and write the binary contents to the output stream.
48 private void ReadAllInput (ResourceWriter rw
) {
50 String msgid
= ReadString();
53 String msgstr
= ReadString();
56 rw
.AddResource(msgid
, msgstr
);
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.
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
);
73 // Use the ResourceReader to check against pnet-0.6.0 ResourceWriter
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
);
82 tmpoutput
.WriteTo(output
);
87 MemoryStream tmpoutput
= new MemoryStream();
88 ResourceWriter rw
= new ResourceWriter(tmpoutput
);
90 // Use the ResourceReader to check against pnet-0.6.0 ResourceWriter
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
);
98 BufferedStream output
= new BufferedStream(new FileStream(filename
, FileMode
.Create
, FileAccess
.Write
));
99 tmpoutput
.WriteTo(output
);
103 ResourceWriter rw
= new ResourceWriter(filename
);
109 public static int Main (String
[] args
) {
111 new WriteResource(args
[0]);
112 } catch (Exception e
) {
113 Console
.Error
.WriteLine(e
);
114 Console
.Error
.WriteLine(e
.StackTrace
);