[sgen] Fix logging of major heap size with concurrent sweep
[mono.git] / mcs / tools / txt2sr / txt2sr.cs
1 //
2 // txt2sr.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 Mono.Options;
33
34 public class Program
35 {
36         class CmdOptions
37         {
38                 public bool ShowHelp { get; set; }
39                 public bool Verbose { get; set; }
40                 public List<string> ResourcesStrings { get; }
41
42                 public CmdOptions ()
43                 {
44                         ResourcesStrings = new List<string> ();
45                 }
46         }
47
48         public static int Main (string[] args)
49         {
50                 var options = new CmdOptions ();
51
52                 var p = new OptionSet () {
53                         { "t|txt=", "File with string resource in key=value format",
54                                 v => options.ResourcesStrings.Add (v) },
55                         { "h|help",  "Display available options", 
56                                 v => options.ShowHelp = v != null },
57                         { "v|verbose",  "Use verbose output", 
58                                 v => options.Verbose = v != null },                     
59                 };
60
61                 List<string> extra;
62                 try {
63                         extra = p.Parse (args);
64                 }
65                 catch (OptionException e) {
66                         Console.WriteLine (e.Message);
67                         Console.WriteLine ("Try 'txt2sr -help' for more information.");
68                         return 1;
69                 }
70
71                 if (options.ShowHelp) {
72                         ShowHelp (p);
73                         return 0;
74                 }
75
76                 if (extra.Count != 1) {
77                         ShowHelp (p);
78                         return 2;
79                 }
80
81                 var txtStrings = new List<Tuple<string, string>> ();
82                 if (!LoadStrings (txtStrings, options))
83                         return 3;
84
85                 GenerateFile (extra [0], txtStrings, options);
86
87                 return 0;
88         }
89
90         static void ShowHelp (OptionSet p)
91         {
92                 Console.WriteLine ("Usage: cil-txt2sr [options] output-file");
93                 Console.WriteLine ("Generates C# file from reference source resource text file");
94                 Console.WriteLine ();
95                 Console.WriteLine ("Options:");
96                 p.WriteOptionDescriptions (Console.Out);
97         }
98
99         static void GenerateFile (string outputFile, List<Tuple<string, string>> txtStrings, CmdOptions options)
100         {
101                 using (var str = new StreamWriter (outputFile)) {
102                         str.WriteLine ("//");
103                         str.WriteLine ("// This file was generated by txt2sr tool");
104                         str.WriteLine ("//");
105                         str.WriteLine ();
106
107                         str.WriteLine ("partial class SR");
108                         str.WriteLine ("{");
109                         foreach (var entry in txtStrings) {
110                                 var value = entry.Item2;
111
112                                 if (value.StartsWith ("\"") && value.EndsWith ("\";")) {
113                                         value = value.Substring (1, value.Length - 3);
114                                 }
115
116                                 int idx;
117                                 int startIndex = 1;
118                                 while (startIndex <= value.Length && (idx = value.IndexOf ("\"", startIndex, StringComparison.Ordinal)) > 0) {
119                                         startIndex = idx + 1;
120
121                                         if (value [idx - 1] == '\\')
122                                                 continue;
123                                         
124                                         value = value.Insert (idx, "\\");
125                                         ++startIndex;
126                                 }
127
128                                 str.WriteLine ($"\tpublic const string {entry.Item1} = \"{value}\";");
129                         }
130                         str.WriteLine ("}");
131                 }
132         }
133
134         static bool LoadStrings (List<Tuple<string, string>> resourcesStrings, CmdOptions options)
135         {
136                 var keys = new Dictionary<string, string> ();
137                 foreach (var fileName in options.ResourcesStrings) {
138                         if (!File.Exists (fileName)) {
139                                 Console.Error.WriteLine ($"Error reading resource file '{fileName}'");
140                                 return false;
141                         }
142
143                         foreach (var l in File.ReadLines (fileName)) {
144                                 var line = l.Trim ();
145                                 if (line.Length == 0 || line [0] == '#' || line [0] == ';')
146                                         continue;
147
148                                 var epos = line.IndexOf ('=');
149                                 if (epos < 0)
150                                         continue;
151
152                                 var key = line.Substring (0, epos).Trim ();
153                                 if (key.Contains (" "))
154                                         continue;
155
156                                 var value = line.Substring (epos + 1).Trim ();
157
158                                 string existing;
159                                 if (keys.TryGetValue (key, out existing)) {                                     
160                                         continue;
161                                 }
162
163                                 keys.Add (key, value);
164                                 resourcesStrings.Add (Tuple.Create (key, value));                               
165                         }
166                 }
167
168                 return true;
169         }
170 }