Advertisement
szymski

Configuration

Aug 12th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7.  
  8. namespace SrajtasmaEngine
  9. {
  10.     public class Configuration
  11.     {
  12.         Dictionary<string, object> keys = new Dictionary<string, object>();
  13.  
  14.         public object this[string key]
  15.         {
  16.             get
  17.             {
  18.                 return keys[key];
  19.             }
  20.             set
  21.             {
  22.                 if (!keys.ContainsKey(key))
  23.                     keys.Add(key, value);
  24.             }
  25.         }
  26.  
  27.         public bool HasKey(string key)
  28.         {
  29.             return keys.ContainsKey(key);
  30.         }
  31.  
  32.         public static Configuration Load(string filename)
  33.         {
  34.             Configuration configuration = new Configuration();
  35.             foreach (string line in File.ReadAllText(filename).Split('\n'))
  36.             {
  37.                 if (line.Replace(" ", "").Replace("\t", "").IndexOf("//") == 0) continue;
  38.                 string[] split = line.Split(new string[] { "=" }, 2, StringSplitOptions.None);
  39.                 if (split.Length != 2) continue;
  40.  
  41.                 object obj = null;
  42.  
  43.                 int i = 0;
  44.                 float f = 0;
  45.  
  46.                 if (int.TryParse(split[1].Replace(" ", "").Replace("\t", ""), out i)) { obj = i; }
  47.                 else if (float.TryParse(split[1].Replace(" ", "").Replace("\t", "").Replace('.', ','), out f)) { obj = f; }
  48.                 else if (split[1].Replace(" ", "").Replace("\t", "") == "true") { obj = true; }
  49.                 else if (split[1].Replace(" ", "").Replace("\t", "") == "false") { obj = false; }
  50.                 else
  51.                 {
  52.                     if (split[1].IndexOf('"') != -1 && split[1].IndexOf('"') != split[1].LastIndexOf('"'))
  53.                     {
  54.                         obj = split[1].Substring(split[1].IndexOf('"') + 1, split[1].LastIndexOf('"') - split[1].IndexOf('"') - 1);
  55.                     }
  56.                 }
  57.  
  58.                 if (obj != null)
  59.                 {
  60.                     configuration.keys.Add(split[0].Replace(" ", "").Replace("\t", ""), obj);
  61.                 }
  62.             }
  63.             return configuration;
  64.         }
  65.  
  66.         public void Save(string filename)
  67.         {
  68.             string lines = "";
  69.             foreach (string s in keys.Keys)
  70.             {
  71.                 if (keys[s] is string)
  72.                     lines += s + " = \"" + keys[s].ToString() + "\"\n";
  73.                 else
  74.                     lines += s + " = " + keys[s].ToString().ToLower() + "\n";
  75.             }
  76.             File.WriteAllText(filename, lines);
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement