From f31e34b17e24733bf98d84536ffa0c936719db9d Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Thu, 27 Jan 2011 20:47:23 +0000 Subject: Default to Environment.NewLine for line breaks in ProtoGen, but allow it to be configured. --- src/ProtoGen/Generator.cs | 7 +++---- src/ProtoGen/GeneratorOptions.cs | 29 ++++++++++++++++++++++++----- src/ProtoGen/ProgramPreprocess.cs | 2 +- src/ProtocolBuffers/TextFormat.cs | 7 ++++--- src/ProtocolBuffers/TextGenerator.cs | 15 ++++++++++++--- 5 files changed, 44 insertions(+), 16 deletions(-) diff --git a/src/ProtoGen/Generator.cs b/src/ProtoGen/Generator.cs index f588c141..fd73591f 100644 --- a/src/ProtoGen/Generator.cs +++ b/src/ProtoGen/Generator.cs @@ -45,7 +45,7 @@ namespace Google.ProtocolBuffers.ProtoGen { /// public sealed class Generator { - readonly GeneratorOptions options; + private readonly GeneratorOptions options; private Generator(GeneratorOptions options) { options.Validate(); @@ -59,8 +59,7 @@ namespace Google.ProtocolBuffers.ProtoGen { return new Generator(options); } - public void Generate() { - + public void Generate() { List descriptorProtos = new List(); foreach (string inputFile in options.InputFiles) { ExtensionRegistry extensionRegistry = ExtensionRegistry.CreateInstance(); @@ -94,7 +93,7 @@ namespace Google.ProtocolBuffers.ProtoGen { private void Generate(FileDescriptor descriptor) { UmbrellaClassGenerator ucg = new UmbrellaClassGenerator(descriptor); using (TextWriter textWriter = File.CreateText(GetOutputFile(descriptor))) { - TextGenerator writer = new TextGenerator(textWriter); + TextGenerator writer = new TextGenerator(textWriter, options.LineBreak); ucg.Generate(writer); } } diff --git a/src/ProtoGen/GeneratorOptions.cs b/src/ProtoGen/GeneratorOptions.cs index 8781c5ec..8b3d6c5b 100644 --- a/src/ProtoGen/GeneratorOptions.cs +++ b/src/ProtoGen/GeneratorOptions.cs @@ -49,9 +49,20 @@ namespace Google.ProtocolBuffers.ProtoGen { /// the generator. /// public sealed class GeneratorOptions { + + private static Dictionary LineBreaks = new Dictionary(StringComparer.InvariantCultureIgnoreCase) { + { "Windows", "\r\n" }, + { "Unix", "\n" }, + { "Default", Environment.NewLine } + }; + public IList InputFiles { get; set; } - /// + public GeneratorOptions() { + LineBreak = Environment.NewLine; + } + + /// /// Attempts to validate the options, but doesn't throw an exception if they're invalid. /// Instead, when this method returns false, the output variable will contain a collection /// of reasons for the validation failure. @@ -128,6 +139,8 @@ namespace Google.ProtocolBuffers.ProtoGen { set { fileOptions = value; } } + public string LineBreak { get; set; } + private void ParseArguments(IList tmpReasons) { bool doHelp = Arguments.Count == 0; @@ -159,12 +172,17 @@ namespace Google.ProtocolBuffers.ProtoGen { if (TryCoerceType(value, fld, out obj, tmpReasons)) { builder[fld] = obj; } - } - else if (!File.Exists(argument)) { + } else if (name == "lineBreak") { + string tmp; + if (LineBreaks.TryGetValue(value, out tmp)) { + LineBreak = tmp; + } else { + tmpReasons.Add("Invalid value for 'lineBreak': " + value + "."); + } + } else if (!File.Exists(argument)) { doHelp = true; tmpReasons.Add("Unknown argument '" + name + "'."); - } - else { + } else { InputFiles.Add(argument); } } @@ -178,6 +196,7 @@ namespace Google.ProtocolBuffers.ProtoGen { foreach (KeyValuePair field in fields) { tmpReasons.Add(String.Format("-{0}=[{1}]", field.Key, field.Value.FieldType)); } + tmpReasons.Add("-lineBreak=[" + string.Join("|", new List(LineBreaks.Keys).ToArray()) + "]"); tmpReasons.Add("followed by one or more file paths."); } else { diff --git a/src/ProtoGen/ProgramPreprocess.cs b/src/ProtoGen/ProgramPreprocess.cs index f6032a54..e35eb830 100644 --- a/src/ProtoGen/ProgramPreprocess.cs +++ b/src/ProtoGen/ProgramPreprocess.cs @@ -55,7 +55,7 @@ namespace Google.ProtocolBuffers.ProtoGen { } Console.WriteLine(); Console.WriteLine(); - Console.WriteLine("PRTOGEN.exe: The following options are used to specify defaults for code generation."); + Console.WriteLine("PROTOGEN.exe: The following options are used to specify defaults for code generation."); Console.WriteLine(); Program.Main(new string[0]); return 0; diff --git a/src/ProtocolBuffers/TextFormat.cs b/src/ProtocolBuffers/TextFormat.cs index 81b3447c..27bef8f4 100644 --- a/src/ProtocolBuffers/TextFormat.cs +++ b/src/ProtocolBuffers/TextFormat.cs @@ -43,7 +43,8 @@ using System.Collections; namespace Google.ProtocolBuffers { /// /// Provides ASCII text formatting support for messages. - /// TODO(jonskeet): Parsing support. + /// TODO(jonskeet): Support for alternative line endings. + /// (Easy to print, via TextGenerator. Not sure about parsing.) /// public static class TextFormat { @@ -52,7 +53,7 @@ namespace Google.ProtocolBuffers { /// the parameter output. /// public static void Print(IMessage message, TextWriter output) { - TextGenerator generator = new TextGenerator(output); + TextGenerator generator = new TextGenerator(output, "\n"); Print(message, generator); } @@ -60,7 +61,7 @@ namespace Google.ProtocolBuffers { /// Outputs a textual representation of to . /// public static void Print(UnknownFieldSet fields, TextWriter output) { - TextGenerator generator = new TextGenerator(output); + TextGenerator generator = new TextGenerator(output, "\n"); PrintUnknownFields(fields, generator); } diff --git a/src/ProtocolBuffers/TextGenerator.cs b/src/ProtocolBuffers/TextGenerator.cs index 544fd86e..6b8fa048 100644 --- a/src/ProtocolBuffers/TextGenerator.cs +++ b/src/ProtocolBuffers/TextGenerator.cs @@ -43,6 +43,13 @@ namespace Google.ProtocolBuffers { /// public sealed class TextGenerator { + /// + /// The string to use at the end of each line. We assume that "Print" is only called using \n + /// to indicate a line break; that's what we use to detect when we need to indent etc, and + /// *just* the \n is replaced with the contents of lineBreak. + /// + private readonly string lineBreak; + /// /// Writer to write formatted text to. /// @@ -62,8 +69,9 @@ namespace Google.ProtocolBuffers { /// Creates a generator writing to the given writer. The writer /// is not closed by this class. /// - public TextGenerator(TextWriter writer) { + public TextGenerator(TextWriter writer, string lineBreak) { this.writer = writer; + this.lineBreak = lineBreak; } /// @@ -107,8 +115,9 @@ namespace Google.ProtocolBuffers { for (int i = 0; i < text.Length; i++) { if (text[i] == '\n') { - // TODO(jonskeet): Use Environment.NewLine? - Write(text.Substring(pos, i - pos + 1)); + // Strip off the \n from what we write + Write(text.Substring(pos, i - pos)); + Write(lineBreak); pos = i + 1; atStartOfLine = true; } -- cgit v1.2.3