1 /* GNU gettext for C# 2 * Copyright (C) 2003-2004, 2018 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 <https://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. ReadString()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. ReadAllInput(ResourceWriter rw)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. WriteResource(String 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 tmpoutput.WriteTo(output); 72 rw.Close(); 73 output.Close(); 74 } else { 75 ResourceWriter rw = new ResourceWriter(filename); 76 ReadAllInput(rw); 77 rw.Close(); 78 } 79 } Main(String[] args)80 public static int Main (String[] args) { 81 try { 82 new WriteResource(args[0]); 83 } catch (Exception e) { 84 Console.Error.WriteLine(e); 85 Console.Error.WriteLine(e.StackTrace); 86 return 1; 87 } 88 return 0; 89 } 90 } 91 } 92