5 // Marek Safar <marek.safar@gmail.com>
7 // Copyright (C) 2016 Xamarin Inc (http://www.xamarin.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
31 using System.Collections.Generic;
38 public bool ShowHelp { get; set; }
39 public bool Verbose { get; set; }
40 public List<string> ResourcesStrings { get; }
41 public bool IgnoreSemicolon { get; set; }
45 ResourcesStrings = new List<string> ();
49 public static int Main (string[] args)
51 var options = new CmdOptions ();
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 },
66 extra = p.Parse (args);
68 catch (OptionException e) {
69 Console.WriteLine (e.Message);
70 Console.WriteLine ("Try 'txt2sr -help' for more information.");
74 if (options.ShowHelp) {
79 if (extra.Count != 1) {
84 var txtStrings = new List<Tuple<string, string>> ();
85 if (!LoadStrings (txtStrings, options))
88 GenerateFile (extra [0], txtStrings, options);
93 static void ShowHelp (OptionSet p)
95 Console.WriteLine ("Usage: cil-txt2sr [options] output-file");
96 Console.WriteLine ("Generates C# file from reference source resource text file");
98 Console.WriteLine ("Options:");
99 p.WriteOptionDescriptions (Console.Out);
102 static void GenerateFile (string outputFile, List<Tuple<string, string>> txtStrings, CmdOptions options)
104 using (var str = new StreamWriter (outputFile)) {
105 str.WriteLine ("//");
106 str.WriteLine ("// This file was generated by txt2sr tool");
107 str.WriteLine ("//");
110 str.WriteLine ("partial class SR");
112 foreach (var entry in txtStrings) {
113 var value = entry.Item2;
115 if (value.StartsWith ("\"") && value.EndsWith ("\";")) {
116 value = value.Substring (1, value.Length - 3);
121 while (startIndex <= value.Length && (idx = value.IndexOf ("\"", startIndex, StringComparison.Ordinal)) > 0) {
122 startIndex = idx + 1;
124 if (value [idx - 1] == '\\')
127 value = value.Insert (idx, "\\");
131 str.WriteLine ($"\tpublic const string {entry.Item1} = \"{value}\";");
137 static bool LoadStrings (List<Tuple<string, string>> resourcesStrings, CmdOptions options)
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}'");
146 foreach (var l in File.ReadLines (fileName)) {
147 var line = l.Trim ();
148 if (line.Length == 0 || line [0] == '#')
152 if (line [0] == ';') {
153 if (!options.IgnoreSemicolon)
159 var epos = line.IndexOf ('=');
163 var key = line.Substring (start, epos - start).Trim ();
164 if (key.Contains (" "))
167 var value = line.Substring (epos + 1).Trim ();
170 if (keys.TryGetValue (key, out existing)) {
174 keys.Add (key, value);
175 resourcesStrings.Add (Tuple.Create (key, value));