Implement mono_gc_alloc_fixed on Boehm to be uncollectable. This matches SGen behavio...
[mono.git] / mcs / tools / resx2sr / resx2sr.cs
1 //
2 // resx2sr.cs
3 //
4 // Authors:
5 //      Marek Safar  <marek.safar@gmail.com>
6 //
7 // Copyright (C) 2016 Xamarin Inc (http://www.xamarin.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.IO;
31 using System.Collections.Generic;
32 using System.Resources;
33 using System.ComponentModel.Design;
34 using Mono.Options;
35
36 public class Program
37 {
38         class CmdOptions
39         {
40                 public bool ShowHelp { get; set; }
41                 public string OutputFile { get; set; }
42         }
43
44         public static int Main (string[] args)
45         {
46                 var options = new CmdOptions ();
47
48                 var p = new OptionSet () {
49                         { "o|out=", "Specifies output file name",
50                                 v => options.OutputFile = v },
51                         { "h|help",  "Display available options", 
52                                 v => options.ShowHelp = v != null },
53                 };
54
55                 List<string> extra;
56                 try {
57                         extra = p.Parse (args);
58                 } catch (OptionException e) {
59                         Console.WriteLine (e.Message);
60                         Console.WriteLine ("Try 'resx2sr -help' for more information.");
61                         return 1;
62                 }
63
64                 if (options.ShowHelp) {
65                         ShowHelp (p);
66                         return 0;
67                 }
68
69                 if (extra.Count != 1) {
70                         ShowHelp (p);
71                         return 2;
72                 }
73
74                 var resxStrings = new List<Tuple<string, string, string>> ();
75                 if (!LoadStrings (resxStrings, extra))
76                         return 3;
77
78                 GenerateFile (resxStrings, options);
79
80                 return 0;
81         }
82
83         static void ShowHelp (OptionSet p)
84         {
85                 Console.WriteLine ("Usage: resx2sr [options] input-files");
86                 Console.WriteLine ("Generates C# file with string constants from resource file");
87                 Console.WriteLine ();
88                 Console.WriteLine ("Options:");
89                 p.WriteOptionDescriptions (Console.Out);
90         }
91
92         static void GenerateFile (List<Tuple<string, string, string>> txtStrings, CmdOptions options)
93         {
94 //              var outputFile = options.OutputFile ?? "SR.cs";
95
96                 using (var str = options.OutputFile == null ? Console.Out : new StreamWriter (options.OutputFile)) {
97                         str.WriteLine ("//");
98                         str.WriteLine ("// This file was generated by resx2sr tool");
99                         str.WriteLine ("//");
100                         str.WriteLine ();
101
102                         str.WriteLine ("partial class SR");
103                         str.WriteLine ("{");
104                         foreach (var entry in txtStrings) {
105                                 str.Write ($"\tpublic const string {entry.Item1} = \"{ToCSharpString (entry.Item2)}\";");
106
107                                 if (!string.IsNullOrEmpty (entry.Item3))
108                                         str.Write (" // {entry.Item3}");
109
110                                 str.WriteLine ();
111
112                         }
113                         str.WriteLine ("}");
114                 }
115         }
116
117         static string ToCSharpString (string str)
118         {
119                 str = str.Replace ("\n", "\\n");
120
121                 return str.Replace ("\\", "\\\\").Replace ("\"", "\\\"");
122         }
123
124         static bool LoadStrings (List<Tuple<string, string, string>> resourcesStrings, List<string> files)
125         {
126                 var keys = new Dictionary<string, string> ();
127                 foreach (var fileName in files) {
128                         if (!File.Exists (fileName)) {
129                                 Console.Error.WriteLine ($"Error reading resource file '{fileName}'");
130                                 return false;
131                         }
132
133                         var rr = new ResXResourceReader (fileName);
134                         rr.UseResXDataNodes = true;
135                         var dict = rr.GetEnumerator ();
136                         while (dict.MoveNext ()) {
137                                 var node = (ResXDataNode)dict.Value;
138
139                                 resourcesStrings.Add (Tuple.Create (node.Name, (string) node.GetValue ((ITypeResolutionService)null), node.Comment));                           
140                         }
141                 }
142
143                 return true;
144         }
145 }