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