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; }
44 ResourcesStrings = new List<string> ();
48 public static int Main (string[] args)
50 var options = new CmdOptions ();
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 },
63 extra = p.Parse (args);
65 catch (OptionException e) {
66 Console.WriteLine (e.Message);
67 Console.WriteLine ("Try 'txt2sr -help' for more information.");
71 if (options.ShowHelp) {
76 if (extra.Count != 1) {
81 var txtStrings = new List<Tuple<string, string>> ();
82 if (!LoadStrings (txtStrings, options))
85 GenerateFile (extra [0], txtStrings, options);
90 static void ShowHelp (OptionSet p)
92 Console.WriteLine ("Usage: cil-txt2sr [options] output-file");
93 Console.WriteLine ("Generates C# file from reference source resource text file");
95 Console.WriteLine ("Options:");
96 p.WriteOptionDescriptions (Console.Out);
99 static void GenerateFile (string outputFile, List<Tuple<string, string>> txtStrings, CmdOptions options)
101 using (var str = new StreamWriter (outputFile)) {
102 str.WriteLine ("//");
103 str.WriteLine ("// This file was generated by txt2sr tool");
104 str.WriteLine ("//");
107 str.WriteLine ("partial class SR");
109 foreach (var entry in txtStrings) {
110 var value = entry.Item2;
112 if (value.StartsWith ("\"") && value.EndsWith ("\";")) {
113 value = value.Substring (1, value.Length - 3);
118 while (startIndex <= value.Length && (idx = value.IndexOf ("\"", startIndex, StringComparison.Ordinal)) > 0) {
119 startIndex = idx + 1;
121 if (value [idx - 1] == '\\')
124 value = value.Insert (idx, "\\");
128 str.WriteLine ($"\tpublic const string {entry.Item1} = \"{value}\";");
134 static bool LoadStrings (List<Tuple<string, string>> resourcesStrings, CmdOptions options)
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}'");
143 foreach (var l in File.ReadLines (fileName)) {
144 var line = l.Trim ();
145 if (line.Length == 0 || line [0] == '#' || line [0] == ';')
148 var epos = line.IndexOf ('=');
152 var key = line.Substring (0, epos).Trim ();
153 if (key.Contains (" "))
156 var value = line.Substring (epos + 1).Trim ();
159 if (keys.TryGetValue (key, out existing)) {
163 keys.Add (key, value);
164 resourcesStrings.Add (Tuple.Create (key, value));