Advertisement
AyrA

Readable Config file creator

Jul 29th, 2016
1,125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.76 KB | None | 0 0
  1. //Because of https://www.reddit.com/r/programming/comments/4v6chu/why_json_doesnt_support_comments_douglas_crockford/d5w343r
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Collections;
  6.  
  7. namespace BetterConfig
  8. {
  9.     /// <summary>
  10.     /// Provides loading and creation of human readable config files.
  11.     /// </summary>
  12.     public static class Config
  13.     {
  14.         /// <summary>
  15.         /// Creates a configuration file
  16.         /// </summary>
  17.         /// <param name="Data">Binary data</param>
  18.         /// <param name="Token0">First token</param>
  19.         /// <param name="Token1">Second token</param>
  20.         /// <returns>Configuration content</returns>
  21.         public static string Save(byte[] Data, string Token0, string Token1)
  22.         {
  23.             StringBuilder OMG = new StringBuilder();
  24.             BitArray BA = new BitArray(Data);
  25.  
  26.             if (!CheckTokens(Token0, Token1))
  27.             {
  28.                 throw new ArgumentException("Tokens are ambiguous");
  29.             }
  30.             foreach (bool b in BA)
  31.             {
  32.                 OMG.Append(b ? Token1 : Token0);
  33.             }
  34.  
  35.             return OMG.ToString();
  36.         }
  37.  
  38.         /// <summary>
  39.         /// Loads a configuration file
  40.         /// </summary>
  41.         /// <param name="Data">Configuration data</param>
  42.         /// <param name="Token0">First token</param>
  43.         /// <param name="Token1">Second token</param>
  44.         /// <returns>Binary Configuration</returns>
  45.         public static byte[] Load(string Data, string Token0, string Token1)
  46.         {
  47.             return Load(Data, Token0, Token1, false);
  48.         }
  49.  
  50.         /// <summary>
  51.         /// Loads a configuration file
  52.         /// </summary>
  53.         /// <param name="Data">Configuration data</param>
  54.         /// <param name="Token0">First token</param>
  55.         /// <param name="Token1">Second token</param>
  56.         /// <param name="FilterInvalid">Ignores invalid tokens in the file</param>
  57.         /// <returns>Binary Configuration</returns>
  58.         public static byte[] Load(string Data, string Token0, string Token1, bool FilterInvalid)
  59.         {
  60.             List<bool> ConfigValues = new List<bool>();
  61.             bool[] Values = new bool[] { false, true };
  62.             byte[] Result;
  63.             int Pos = 0;
  64.             if (!CheckTokens(Token0, Token1))
  65.             {
  66.                 throw new ArgumentException("Tokens are ambiguous");
  67.             }
  68.  
  69.             //swap tokens if needed so we always check for the longer token first.
  70.             //Also swap values then
  71.             if (Token1.Length > Token0.Length)
  72.             {
  73.                 var temp = Token0;
  74.                 Token0 = Token1;
  75.                 Token1 = temp;
  76.                 Values[0] = true;
  77.                 Values[1] = false;
  78.             }
  79.  
  80.             while (Pos < Data.Length)
  81.             {
  82.                 if (Data.IndexOf(Token0, Pos) == Pos)
  83.                 {
  84.                     ConfigValues.Add(Values[0]);
  85.                     Pos += Token0.Length;
  86.                 }
  87.                 else if (Data.IndexOf(Token1, Pos) == Pos)
  88.                 {
  89.                     ConfigValues.Add(Values[1]);
  90.                     Pos += Token1.Length;
  91.                 }
  92.                 else if (FilterInvalid)
  93.                 {
  94.                     ++Pos;
  95.                 }
  96.                 else
  97.                 {
  98.                     throw new ArgumentException("Data is not a valid configuration");
  99.                 }
  100.             }
  101.  
  102.             if (ConfigValues.Count % 8 != 0)
  103.             {
  104.                 throw new ArgumentException("Data is not a valid configuration");
  105.             }
  106.  
  107.             Result = new byte[ConfigValues.Count / 8];
  108.             (new BitArray(ConfigValues.ToArray())).CopyTo(Result, 0);
  109.             return Result;
  110.         }
  111.  
  112.         /// <summary>
  113.         /// Makes sure, tokens are unambiguous
  114.         /// </summary>
  115.         /// <param name="Token0">First token</param>
  116.         /// <param name="Token1">Second token</param>
  117.         /// <returns>true, if valid tokens</returns>
  118.         public static bool CheckTokens(string Token0, string Token1)
  119.         {
  120.             //for tokens to be valid there are multiple conditions that all must be met:
  121.             //- Neither token must be null or an empty string
  122.             //- Tokens must not be identical
  123.             //- A token must not be only made up of the other token multiple times
  124.             if (string.IsNullOrEmpty(Token0) ||
  125.                 string.IsNullOrEmpty(Token1) ||
  126.                 Token0 == Token1 ||
  127.                 Token0.Replace(Token1,"").Length==0 ||
  128.                 Token1.Replace(Token0, "").Length == 0)
  129.             {
  130.                 return false;
  131.             }
  132.  
  133.             return true;
  134.         }
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement