Skip to content

Commit

Permalink
user setting enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Tammik committed Oct 29, 2014
1 parent 6ac611f commit ad548bc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 25 deletions.
41 changes: 25 additions & 16 deletions RvtVa3c/UserSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
}
}
}
Expand Down
23 changes: 16 additions & 7 deletions RvtVa3c/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,38 +57,47 @@ public static int ColorToInt( Color color )
/// <summary>
/// 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.
/// </summary>
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;
}
Expand Down Expand Up @@ -133,7 +142,7 @@ public static string ElementDescription(
typeName, categoryName, familyName,
symbolName, e.Id.IntegerValue, e.Name );
}

/// <summary>
/// Return a dictionary of all the given
/// element parameter names and values.
Expand Down
4 changes: 2 additions & 2 deletions RvtVa3c/Va3cExportContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ad548bc

Please sign in to comment.