eglib: checking for locale_charset function
[mono.git] / mcs / tools / resgen / monoresgen.cs
1 /*
2  * resgen: convert between the resource formats (.txt, .resources, .resx).
3  *
4  * Copyright (c) 2002 Ximian, Inc
5  *
6  * Authors:
7  *      Paolo Molaro (lupus@ximian.com)
8  *      Gonzalo Paniagua Javier (gonzalo@ximian.com)
9  */
10
11 using System;
12 using System.Globalization;
13 using System.Text;
14 using System.IO;
15 using System.Collections;
16 using System.Resources;
17 using System.Reflection;
18 using System.Xml;
19
20 class ResGen {
21
22         static Assembly swf;
23         static Type resxr;
24         static Type resxw;
25
26         /*
27          * We load the ResX format stuff on demand, since the classes are in 
28          * System.Windows.Forms (!!!) and we can't depend on that assembly in mono, yet.
29          */
30         static void LoadResX () {
31                 if (swf != null)
32                         return;
33                 try {
34                         swf = Assembly.Load (Consts.AssemblySystem_Windows_Forms);
35                         resxr = swf.GetType ("System.Resources.ResXResourceReader");
36                         resxw = swf.GetType ("System.Resources.ResXResourceWriter");
37                 } catch (Exception e) {
38                         throw new Exception ("Cannot load support for ResX format: " + e.Message);
39                 }
40         }
41
42         static void Usage () {
43
44                 string Usage = @"Mono Resource Generator version " + Consts.MonoVersion +
45                     @"
46 Usage:
47                 resgen source.ext [dest.ext]
48                 resgen [options] /compile source.ext[,dest.resources] [...]";
49                 Usage += @"
50
51 Convert a resource file from one format to another.
52 The currently supported formats are: '.txt' '.resources' '.resx' '.po'.
53 If the destination file is not specified, source.resources will be used.";
54
55                 Usage += @"
56
57 Options:
58 -compile, /compile
59         takes a list of .resX or .txt files to convert to .resources files
60         in one bulk operation, replacing .ext with .resources for the 
61         output file name (if not set).
62 -usesourcepath, /useSourcePath
63         to resolve relative file paths, use the directory of the resource 
64         file as current directory.";
65                 Usage += @"
66 ";
67                 Console.WriteLine( Usage );
68         }
69         
70         static IResourceReader GetReader (Stream stream, string name, bool useSourcePath) {
71                 string format = Path.GetExtension (name);
72                 switch (format.ToLower (System.Globalization.CultureInfo.InvariantCulture)) {
73                 case ".po":
74                         return new PoResourceReader (stream);
75                 case ".txt":
76                 case ".text":
77                         return new TxtResourceReader (stream);
78                 case ".resources":
79                         return new ResourceReader (stream);
80                 case ".resx":
81                         LoadResX ();
82                         IResourceReader reader = (IResourceReader) Activator.CreateInstance (
83                                 resxr, new object[] {stream});
84                         if (useSourcePath) { // only possible on 2.0 profile, or higher
85                                 PropertyInfo p = reader.GetType ().GetProperty ("BasePath",
86                                         BindingFlags.Public | BindingFlags.Instance);
87                                 if (p != null && p.CanWrite) {
88                                         p.SetValue (reader, Path.GetDirectoryName (name), null);
89                                 }
90                         }
91                         return reader;
92                 default:
93                         throw new Exception ("Unknown format in file " + name);
94                 }
95         }
96         
97         static IResourceWriter GetWriter (Stream stream, string name) {
98                 string format = Path.GetExtension (name);
99                 switch (format.ToLower ()) {
100                 case ".po":
101                         return new PoResourceWriter (stream);
102                 case ".txt":
103                 case ".text":
104                         return new TxtResourceWriter (stream);
105                 case ".resources":
106                         return new ResourceWriter (stream);
107                 case ".resx":
108                         LoadResX ();
109                         return (IResourceWriter)Activator.CreateInstance (resxw, new object[] {stream});
110                 default:
111                         throw new Exception ("Unknown format in file " + name);
112                 }
113         }
114         
115         static int CompileResourceFile (string sname, string dname, bool useSourcePath) {
116                 FileStream source = null;
117                 FileStream dest = null;
118                 IResourceReader reader = null;
119                 IResourceWriter writer = null;
120
121                 try {
122                         source = new FileStream (sname, FileMode.Open, FileAccess.Read);
123                         reader = GetReader (source, sname, useSourcePath);
124
125                         dest = new FileStream (dname, FileMode.Create, FileAccess.Write);
126                         writer = GetWriter (dest, dname);
127
128                         int rescount = 0;
129                         foreach (DictionaryEntry e in reader) {
130                                 rescount++;
131                                 object val = e.Value;
132                                 if (val is string)
133                                         writer.AddResource ((string)e.Key, (string)e.Value);
134                                 else
135                                         writer.AddResource ((string)e.Key, e.Value);
136                         }
137                         Console.WriteLine( "Read in {0} resources from '{1}'", rescount, sname );
138
139                         reader.Close ();
140                         writer.Close ();
141                         Console.WriteLine("Writing resource file...  Done.");
142                 } catch (Exception e) {
143                         Console.WriteLine ("Error: {0}", e.Message);
144                         Exception inner = e.InnerException;
145
146                         // under 2.0 ResXResourceReader can wrap an exception into an XmlException
147                         // and this hides some helpful message from the original exception
148                         XmlException xex = (inner as XmlException);
149                         if (xex != null) {
150                                 // message is identical to the inner exception (from MWF ResXResourceReader)
151                                 Console.WriteLine ("Position: Line {0}, Column {1}.", xex.LineNumber, xex.LinePosition);
152                                 inner = inner.InnerException;
153                         }
154
155                         if (inner is TargetInvocationException && inner.InnerException != null)
156                                 inner = inner.InnerException;
157                         if (inner != null)
158                                 Console.WriteLine ("Inner exception: {0}", inner.Message);
159
160                         if (reader != null)
161                                 reader.Dispose ();
162                         if (source != null)
163                                 source.Close ();
164                         if (writer != null)
165                                 writer.Dispose ();
166                         if (dest != null)
167                                 dest.Close ();
168
169                         // since we're not first reading all entries in source, we may get a
170                         // read failure after we're started writing to the destination file
171                         // and leave behind a broken resources file, so remove it here
172                         try {
173                                 File.Delete (dname);
174                         } catch {
175                         }
176                         return 1;
177                 }
178                 return 0;
179         }
180         
181         static int Main (string[] args) {
182                 bool compileMultiple = false;
183                 bool useSourcePath = false;
184                 ArrayList inputFiles = new ArrayList ();
185
186                 for (int i = 0; i < args.Length; i++) {
187                         switch (args [i].ToLower ()) {
188                         case "-h":
189                         case "/h":
190                         case "-?":
191                         case "/?":
192                                 Usage ();
193                                 return 1;
194                         case "/compile":
195                         case "-compile":
196                                 if (inputFiles.Count > 0) {
197                                         // the /compile option should be specified before any files
198                                         Usage ();
199                                         return 1;
200                                 }
201                                 compileMultiple = true;
202                                 break;
203
204                         case "/usesourcepath":
205                         case "-usesourcepath":
206                                 if (compileMultiple) {
207                                         // the /usesourcepath option should not appear after the
208                                         // /compile switch on the command-line
209                                         Console.WriteLine ("ResGen : error RG0000: Invalid "
210                                                 + "command line syntax.  Switch: \"/compile\"  Bad value: "
211                                                 + args [i] + ".  Use ResGen /? for usage information.");
212                                         return 1;
213                                 }
214                                 useSourcePath = true;
215                                 break;
216
217                         default:
218                                 if (!IsFileArgument (args [i])) {
219                                         Usage ();
220                                         return 1;
221                                 }
222
223                                 ResourceInfo resInf = new ResourceInfo ();
224                                 if (compileMultiple) {
225                                         string [] pair = args [i].Split (',');
226                                         switch (pair.Length) {
227                                         case 1:
228                                                 resInf.InputFile = Path.GetFullPath (pair [0]);
229                                                 resInf.OutputFile = Path.ChangeExtension (resInf.InputFile,
230                                                         "resources");
231                                                 break;
232                                         case 2:
233                                                 if (pair [1].Length == 0) {
234                                                         Console.WriteLine (@"error: You must specify an input & outfile file name like this:");
235                                                         Console.WriteLine ("inFile.txt,outFile.resources.");
236                                                         Console.WriteLine ("You passed in '{0}'.", args [i]);
237                                                         return 1;
238                                                 }
239                                                 resInf.InputFile = Path.GetFullPath (pair [0]);
240                                                 resInf.OutputFile = Path.GetFullPath (pair [1]);
241                                                 break;
242                                         default:
243                                                 Usage ();
244                                                 return 1;
245                                         }
246                                 } else {
247                                         if ((i + 1) < args.Length) {
248                                                 resInf.InputFile = Path.GetFullPath (args [i]);
249                                                 // move to next arg, since we assume that one holds
250                                                 // the name of the output file
251                                                 i++;
252                                                 resInf.OutputFile = Path.GetFullPath (args [i]);
253                                         } else {
254                                                 resInf.InputFile = Path.GetFullPath (args [i]);
255                                                 resInf.OutputFile = Path.ChangeExtension (resInf.InputFile,
256                                                         "resources");
257                                         }
258                                 }
259                                 inputFiles.Add (resInf);
260                                 break;
261                         }
262                 }
263
264                 if (inputFiles.Count == 0) {
265                         Usage ();
266                         return 1;
267                 }
268
269                 foreach (ResourceInfo res in inputFiles) {
270                         int ret = CompileResourceFile (res.InputFile, res.OutputFile, useSourcePath);
271                         if (ret != 0 )
272                                 return ret;
273                 }
274                 return 0;
275         }
276
277         private static bool RunningOnUnix {
278                 get {
279                         // check for Unix platforms - see FAQ for more details
280                         // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
281                         int platform = (int) Environment.OSVersion.Platform;
282                         return ((platform == 4) || (platform == 128) || (platform == 6));
283                 }
284         }
285
286         private static bool IsFileArgument (string arg)
287         {
288                 if ((arg [0] != '-') && (arg [0] != '/'))
289                         return true;
290
291                 // cope with absolute filenames for resx files on unix, as
292                 // they also match the option pattern
293                 //
294                 // `/home/test.resx' is considered as a resx file, however
295                 // '/test.resx' is considered as error
296                 return (RunningOnUnix && arg.Length > 2 && arg.IndexOf ('/', 2) != -1);
297         }
298 }
299
300 class TxtResourceWriter : IResourceWriter {
301         StreamWriter s;
302         
303         public TxtResourceWriter (Stream stream) {
304                 s = new StreamWriter (stream);
305         }
306         
307         public void AddResource (string name, byte[] value) {
308                 throw new Exception ("Binary data not valid in a text resource file");
309         }
310         
311         public void AddResource (string name, object value) {
312                 if (value is string) {
313                         AddResource (name, (string)value);
314                         return;
315                 }
316                 throw new Exception ("Objects not valid in a text resource file");
317         }
318         
319         public void AddResource (string name, string value) {
320                 s.WriteLine ("{0}={1}", name, Escape (value));
321         }
322
323         // \n -> \\n ...
324         static string Escape (string value)
325         {
326                 StringBuilder b = new StringBuilder ();
327                 for (int i = 0; i < value.Length; i++) {
328                         switch (value [i]) {
329                         case '\n':
330                                 b.Append ("\\n");
331                                 break;
332                         case '\r':
333                                 b.Append ("\\r");
334                                 break;
335                         case '\t':
336                                 b.Append ("\\t");
337                                 break;
338                         case '\\':
339                                 b.Append ("\\\\");
340                                 break;
341                         default:
342                                 b.Append (value [i]);
343                                 break;
344                         }
345                 }
346                 return b.ToString ();
347         }
348         
349         public void Close () {
350                 s.Close ();
351         }
352         
353         public void Dispose () {}
354         
355         public void Generate () {}
356 }
357
358 class TxtResourceReader : IResourceReader {
359         Hashtable data;
360         Stream s;
361         
362         public TxtResourceReader (Stream stream) {
363                 data = new Hashtable ();
364                 s = stream;
365                 Load ();
366         }
367         
368         public virtual void Close () {
369         }
370         
371         public IDictionaryEnumerator GetEnumerator() {
372                 return data.GetEnumerator ();
373         }
374         
375         void Load () {
376                 StreamReader reader = new StreamReader (s);
377                 string line, key, val;
378                 int epos, line_num = 0;
379                 while ((line = reader.ReadLine ()) != null) {
380                         line_num++;
381                         line = line.Trim ();
382                         if (line.Length == 0 || line [0] == '#' ||
383                             line [0] == ';')
384                                 continue;
385                         epos = line.IndexOf ('=');
386                         if (epos < 0) 
387                                 throw new Exception ("Invalid format at line " + line_num);
388                         key = line.Substring (0, epos);
389                         val = line.Substring (epos + 1);
390                         key = key.Trim ();
391                         val = val.Trim ();
392                         if (key.Length == 0) 
393                                 throw new Exception ("Key is empty at line " + line_num);
394
395                         val = Unescape (val);
396                         if (val == null)
397                                 throw new Exception (String.Format ("Unsupported escape character in value of key '{0}'.", key));
398
399
400                         data.Add (key, val);
401                 }
402         }
403
404         // \\n -> \n ...
405         static string Unescape (string value)
406         {
407                 StringBuilder b = new StringBuilder ();
408
409                 for (int i = 0; i < value.Length; i++) {
410                         if (value [i] == '\\') {
411                                 if (i == value.Length - 1)
412                                         return null;
413
414                                 i++;
415                                 switch (value [i]) {
416                                 case 'n':
417                                         b.Append ('\n');
418                                         break;
419                                 case 'r':
420                                         b.Append ('\r');
421                                         break;
422                                 case 't':
423                                         b.Append ('\t');
424                                         break;
425                                 case 'u':
426                                         int ch = int.Parse (value.Substring (++i, 4), NumberStyles.HexNumber);
427                                         b.Append (char.ConvertFromUtf32 (ch));
428                                         i += 3;
429                                         break;
430                                 case '\\':
431                                         b.Append ('\\');
432                                         break;
433                                 default:
434                                         return null;
435                                 }
436
437                         } else {
438                                 b.Append (value [i]);
439                         }
440                 }
441
442                 return b.ToString ();
443         }
444         
445         IEnumerator IEnumerable.GetEnumerator () {
446                 return ((IResourceReader) this).GetEnumerator();
447         }
448
449         void IDisposable.Dispose () {}
450 }
451
452 class PoResourceReader : IResourceReader {
453         Hashtable data;
454         Stream s;
455         int line_num;
456         
457         public PoResourceReader (Stream stream)
458         {
459                 data = new Hashtable ();
460                 s = stream;
461                 Load ();
462         }
463         
464         public virtual void Close ()
465         {
466                 s.Close ();
467         }
468         
469         public IDictionaryEnumerator GetEnumerator()
470         {
471                 return data.GetEnumerator ();
472         }
473         
474         string GetValue (string line)
475         {
476                 int begin = line.IndexOf ('"');
477                 if (begin == -1)
478                         throw new FormatException (String.Format ("No begin quote at line {0}: {1}", line_num, line));
479
480                 int end = line.LastIndexOf ('"');
481                 if (end == -1)
482                         throw new FormatException (String.Format ("No closing quote at line {0}: {1}", line_num, line));
483
484                 return line.Substring (begin + 1, end - begin - 1);
485         }
486         
487         void Load ()
488         {
489                 StreamReader reader = new StreamReader (s);
490                 string line;
491                 string msgid = null;
492                 string msgstr = null;
493                 bool ignoreNext = false;
494
495                 while ((line = reader.ReadLine ()) != null) {
496                         line_num++;
497                         line = line.Trim ();
498                         if (line.Length == 0)
499                                 continue;
500                                 
501                         if (line [0] == '#') {
502                                 if (line.Length == 1 || line [1] != ',')
503                                         continue;
504
505                                 if (line.IndexOf ("fuzzy") != -1) {
506                                         ignoreNext = true;
507                                         if (msgid != null) {
508                                                 if (msgstr == null)
509                                                         throw new FormatException ("Error. Line: " + line_num);
510                                                 data.Add (msgid, msgstr);
511                                                 msgid = null;
512                                                 msgstr = null;
513                                         }
514                                 }
515                                 continue;
516                         }
517                         
518                         if (line.StartsWith ("msgid ")) {
519                                 if (msgid == null && msgstr != null)
520                                         throw new FormatException ("Found 2 consecutive msgid. Line: " + line_num);
521
522                                 if (msgstr != null) {
523                                         if (!ignoreNext)
524                                                 data.Add (msgid, msgstr);
525
526                                         ignoreNext = false;
527                                         msgid = null;
528                                         msgstr = null;
529                                 }
530
531                                 msgid = GetValue (line);
532                                 continue;
533                         }
534
535                         if (line.StartsWith ("msgstr ")) {
536                                 if (msgid == null)
537                                         throw new FormatException ("msgstr with no msgid. Line: " + line_num);
538
539                                 msgstr = GetValue (line);
540                                 continue;
541                         }
542
543                         if (line [0] == '"') {
544                                 if (msgid == null || msgstr == null)
545                                         throw new FormatException ("Invalid format. Line: " + line_num);
546
547                                 msgstr += GetValue (line);
548                                 continue;
549                         }
550
551                         throw new FormatException ("Unexpected data. Line: " + line_num);
552                 }
553
554                 if (msgid != null) {
555                         if (msgstr == null)
556                                 throw new FormatException ("Expecting msgstr. Line: " + line_num);
557
558                         if (!ignoreNext)
559                                 data.Add (msgid, msgstr);
560                 }
561         }
562         
563         IEnumerator IEnumerable.GetEnumerator ()
564         {
565                 return GetEnumerator();
566         }
567
568         void IDisposable.Dispose ()
569         {
570                 if (data != null)
571                         data = null;
572
573                 if (s != null) {
574                         s.Close ();
575                         s = null;
576                 }
577         }
578 }
579
580 class PoResourceWriter : IResourceWriter
581 {
582         TextWriter s;
583         bool headerWritten;
584         
585         public PoResourceWriter (Stream stream)
586         {
587                 s = new StreamWriter (stream);
588         }
589         
590         public void AddResource (string name, byte [] value)
591         {
592                 throw new InvalidOperationException ("Binary data not valid in a po resource file");
593         }
594         
595         public void AddResource (string name, object value)
596         {
597                 if (value is string) {
598                         AddResource (name, (string) value);
599                         return;
600                 }
601                 throw new InvalidOperationException ("Objects not valid in a po resource file");
602         }
603
604         StringBuilder ebuilder = new StringBuilder ();
605         
606         public string Escape (string ns)
607         {
608                 ebuilder.Length = 0;
609
610                 foreach (char c in ns){
611                         switch (c){
612                         case '"':
613                         case '\\':
614                                 ebuilder.Append ('\\');
615                                 ebuilder.Append (c);
616                                 break;
617                         case '\a':
618                                 ebuilder.Append ("\\a");
619                                 break;
620                         case '\n':
621                                 ebuilder.Append ("\\n");
622                                 break;
623                         case '\r':
624                                 ebuilder.Append ("\\r");
625                                 break;
626                         default:
627                                 ebuilder.Append (c);
628                                 break;
629                         }
630                 }
631                 return ebuilder.ToString ();
632         }
633         
634         public void AddResource (string name, string value)
635         {
636                 if (!headerWritten) {
637                         headerWritten = true;
638                         WriteHeader ();
639                 }
640                 
641                 s.WriteLine ("msgid \"{0}\"", Escape (name));
642                 s.WriteLine ("msgstr \"{0}\"", Escape (value));
643                 s.WriteLine ("");
644         }
645         
646         void WriteHeader ()
647         {
648                 s.WriteLine ("msgid \"\"");
649                 s.WriteLine ("msgstr \"\"");
650                 s.WriteLine ("\"MIME-Version: 1.0\\n\"");
651                 s.WriteLine ("\"Content-Type: text/plain; charset=UTF-8\\n\"");
652                 s.WriteLine ("\"Content-Transfer-Encoding: 8bit\\n\"");
653                 s.WriteLine ("\"X-Generator: Mono resgen 0.1\\n\"");
654                 s.WriteLine ("#\"Project-Id-Version: FILLME\\n\"");
655                 s.WriteLine ("#\"POT-Creation-Date: yyyy-MM-dd HH:MM+zzzz\\n\"");
656                 s.WriteLine ("#\"PO-Revision-Date: yyyy-MM-dd HH:MM+zzzz\\n\"");
657                 s.WriteLine ("#\"Last-Translator: FILLME\\n\"");
658                 s.WriteLine ("#\"Language-Team: FILLME\\n\"");
659                 s.WriteLine ("#\"Report-Msgid-Bugs-To: \\n\"");
660                 s.WriteLine ();
661         }
662
663         public void Close ()
664         {
665                 s.Close ();
666         }
667         
668         public void Dispose () { }
669         
670         public void Generate () {}
671 }
672
673 class ResourceInfo
674 {
675         public string InputFile;
676         public string OutputFile;
677 }