2 * monoresgen: convert between the resource formats (.txt, .resources, .resx).
4 * Copyright (c) 2002 Ximian, Inc
6 * Author: Paolo Molaro (lupus@ximian.com)
11 * * escape/unescape in the .txt reader/writer to be able to roundtrip values with newlines
12 * (unlike the MS ResGen utility)
13 * * add .po format to help traslators on unixy systems
18 using System.Collections;
19 using System.Resources;
20 using System.Reflection;
29 * We load the ResX format stuff on demand, since the classes are in
30 * System.Windows.Forms (!!!) and we can't depend on that assembly in mono, yet.
32 static void LoadResX () {
36 swf = Assembly.LoadWithPartialName ("System.Windows.Forms");
37 resxr = swf.GetType ("System.Resources.ResXResourceReader");
38 resxw = swf.GetType ("System.Resources.ResXResourceWriter");
39 } catch (Exception e) {
40 throw new Exception ("Cannot load support for ResX format: " + e.Message);
44 static void Usage () {
45 Console.WriteLine ("Mono Resource Generator version 0.1");
46 Console.WriteLine ("Usage:");
47 Console.WriteLine ("\tmonoresgen source.ext [dest.ext]");
49 Console.WriteLine ("Convert a resource file from one format to another.");
50 Console.WriteLine ("The currently supported formats are: '.txt' '.resources' '.resx'.");
51 Console.WriteLine ("If the destination file is not specified, source.resources will be used.");
54 static IResourceReader GetReader (Stream stream, string name) {
55 string format = Path.GetExtension (name);
56 switch (format.ToLower ()) {
59 return new TxtResourceReader (stream);
61 return new ResourceReader (stream);
64 return (IResourceReader)Activator.CreateInstance (resxr, new object[] {stream});
66 throw new Exception ("Unknown format in file " + name);
70 static IResourceWriter GetWriter (Stream stream, string name) {
71 string format = Path.GetExtension (name);
72 switch (format.ToLower ()) {
75 return new TxtResourceWriter (stream);
77 return new ResourceWriter (stream);
80 return (IResourceWriter)Activator.CreateInstance (resxw, new object[] {stream});
82 throw new Exception ("Unknown format in file " + name);
86 static int Main (string[] args) {
87 FileStream source, dest;
89 IResourceReader reader;
90 IResourceWriter writer;
92 if (args.Length == 1) {
94 dname = Path.ChangeExtension (sname, "resources");
95 } else if (args.Length != 2) {
104 source = new FileStream (sname, FileMode.Open, FileAccess.Read);
105 dest = new FileStream (dname, FileMode.OpenOrCreate, FileAccess.Write);
107 reader = GetReader (source, sname);
108 writer = GetWriter (dest, dname);
110 foreach (DictionaryEntry e in reader) {
111 object val = e.Value;
113 writer.AddResource ((string)e.Key, (string)e.Value);
115 writer.AddResource ((string)e.Key, e.Value);
120 } catch (Exception e) {
121 Console.WriteLine ("Error: {0}", e.Message);
128 class TxtResourceWriter : IResourceWriter {
131 public TxtResourceWriter (Stream stream) {
132 s = new StreamWriter (stream);
135 public void AddResource (string name, byte[] value) {
136 throw new Exception ("Binary data not valid in a text resource file");
139 public void AddResource (string name, object value) {
140 if (value is string) {
141 AddResource (name, (string)value);
144 throw new Exception ("Objects not valid in a text resource file");
147 /* FIXME: handle newlines */
148 public void AddResource (string name, string value) {
149 s.WriteLine ("{0}={1}", name, value);
152 public void Close () {
156 public void Dispose () {}
158 public void Generate () {}
161 class TxtResourceReader : IResourceReader {
165 public TxtResourceReader (Stream stream) {
166 data = new Hashtable ();
171 public virtual void Close () {
174 public IDictionaryEnumerator GetEnumerator() {
175 return data.GetEnumerator ();
179 StreamReader reader = new StreamReader (s);
180 string line, key, val;
181 int epos, line_num = 0;
182 while ((line = reader.ReadLine ()) != null) {
185 if (line.Length == 0 || line [0] == '#' ||
188 epos = line.IndexOf ('=');
190 throw new Exception ("Invalid format at line " + line_num);
191 key = line.Substring (0, epos);
192 val = line.Substring (epos + 1);
196 throw new Exception ("Key is empty at line " + line_num);
201 IEnumerator IEnumerable.GetEnumerator () {
202 return ((IResourceReader) this).GetEnumerator();
205 void IDisposable.Dispose () {}