From ad548bce74be759d1ccde5438675ee146f9f1cdb Mon Sep 17 00:00:00 2001 From: Jeremy Tammik Date: Wed, 29 Oct 2014 15:35:57 +0100 Subject: [PATCH] user setting enhancements --- RvtVa3c/UserSettings.cs | 41 ++++++++++++++++++++++-------------- RvtVa3c/Util.cs | 23 ++++++++++++++------ RvtVa3c/Va3cExportContext.cs | 4 ++-- 3 files changed, 43 insertions(+), 25 deletions(-) diff --git a/RvtVa3c/UserSettings.cs b/RvtVa3c/UserSettings.cs index d94faa0..533a62e 100644 --- a/RvtVa3c/UserSettings.cs +++ b/RvtVa3c/UserSettings.cs @@ -11,8 +11,17 @@ class UserSettings const string _JsonIndent = "JsonIndent"; const string _error_msg_format - = "Invalid settings in '{0}'; " - + "please add '{1}' = {2} or {3}"; + = "Invalid settings in '{0}':\r\n\r\n{1}" + + "\r\n\r\nPlease add {2} = {3} or {4}."; + + static bool SyntaxError( string path, string s ) + { + Util.ErrorMsg( string.Format( + _error_msg_format, path, s, _JsonIndent, + Boolean.TrueString, Boolean.FalseString ) ); + + return false; + } public static bool JsonIndented { @@ -33,35 +42,35 @@ public static bool JsonIndented path ) ); } - string s = File.ReadAllText( path ); + string s1 = File.ReadAllText( path ); - int i = s.IndexOf( _JsonIndent ); + int i = s1.IndexOf( _JsonIndent ); if( 0 > i ) { - Util.ErrorMsg( string.Format( - _error_msg_format, path, _JsonIndent, - Boolean.TrueString, Boolean.FalseString ) ); - - return false; + return SyntaxError( path, s1 ); } - s = s.Substring( i + _JsonIndent.Length ); + string s = s1.Substring( i + + _JsonIndent.Length ); i = s.IndexOf( '=' ); if( 0 > i ) { - Util.ErrorMsg( string.Format( - _error_msg_format, path, _JsonIndent, - Boolean.TrueString, Boolean.FalseString ) ); - - return false; + return SyntaxError( path, s1 ); } s = s.Substring( i + 1 ).Trim(); - return Util.GetTrueOrFalse( s ); + bool rc; + + if( !Util.GetTrueOrFalse( s, out rc ) ) + { + return SyntaxError( path, s1 ); + } + + return rc; } } } diff --git a/RvtVa3c/Util.cs b/RvtVa3c/Util.cs index c260997..28c1023 100644 --- a/RvtVa3c/Util.cs +++ b/RvtVa3c/Util.cs @@ -57,38 +57,47 @@ public static int ColorToInt( Color color ) /// /// Extract a true or false value from the given /// string, accepting yes/no, Y/N, true/false, T/F - /// and 1/0. Default to false if no valid value is - /// given. + /// and 1/0. We are extremely tolerant, i.e., any + /// value starting with one of the characters y, n, + /// t or f is also accepted. Return false if no + /// valid Boolean value can be extracted. /// - public static bool GetTrueOrFalse( string s ) + public static bool GetTrueOrFalse( + string s, + out bool val ) { + val = false; + if( s.Equals( Boolean.TrueString, StringComparison.OrdinalIgnoreCase ) ) { + val = true; return true; } if( s.Equals( Boolean.FalseString, StringComparison.OrdinalIgnoreCase ) ) { - return false; + return true; } if( s.Equals( "1" ) ) { + val = true; return true; } if( s.Equals( "0" ) ) { - return false; + return true; } s = s.ToLower(); if( 't' == s[0] || 'y' == s[0] ) { + val = true; return true; } if( 'f' == s[0] || 'n' == s[0] ) { - return false; + return true; } return false; } @@ -133,7 +142,7 @@ public static string ElementDescription( typeName, categoryName, familyName, symbolName, e.Id.IntegerValue, e.Name ); } - + /// /// Return a dictionary of all the given /// element parameter names and values. diff --git a/RvtVa3c/Va3cExportContext.cs b/RvtVa3c/Va3cExportContext.cs index e64796c..8f4329d 100644 --- a/RvtVa3c/Va3cExportContext.cs +++ b/RvtVa3c/Va3cExportContext.cs @@ -419,13 +419,13 @@ JsonSerializerSettings settings settings.NullValueHandling = NullValueHandling.Ignore; - Formatting formatting + Formatting formatting = UserSettings.JsonIndented ? Formatting.Indented : Formatting.None; File.WriteAllText( _filename, - JsonConvert.SerializeObject( + JsonConvert.SerializeObject( _container, formatting, settings ) ); #if USE_DYNAMIC_JSON