| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- using System.Drawing;
- public class InIReader
- {
- #region Fields
- private readonly List<string> _contents;
- private readonly string _fileName;
- #endregion
- #region Constructor
- public InIReader(string fileName)
- {
- _fileName = fileName;
- if (!Directory.Exists(Path.GetDirectoryName(fileName)))
- {
- Directory.CreateDirectory(Path.GetDirectoryName(fileName));
- }
- _contents = new List<string>();
- try
- {
- if (File.Exists(_fileName))
- _contents.AddRange(File.ReadAllLines(_fileName));
- }
- catch
- {
- }
- }
- #endregion
- #region Functions
- private string FindValue(string section, string key)
- {
- for (int a = 0; a < _contents.Count; a++)
- if (String.CompareOrdinal(_contents[a], "[" + section + "]") == 0)
- for (int b = a + 1; b < _contents.Count; b++)
- if (String.CompareOrdinal(_contents[b].Split('=')[0], key) == 0)
- return _contents[b].Split('=')[1];
- else if (_contents[b].StartsWith("[") && _contents[b].EndsWith("]"))
- return null;
- return null;
- }
- private int FindIndex(string section, string key)
- {
- for (int a = 0; a < _contents.Count; a++)
- if (String.CompareOrdinal(_contents[a], "[" + section + "]") == 0)
- for (int b = a + 1; b < _contents.Count; b++)
- if (String.CompareOrdinal(_contents[b].Split('=')[0], key) == 0)
- return b;
- else if (_contents[b].StartsWith("[") && _contents[b].EndsWith("]"))
- {
- _contents.Insert(b - 1, key + "=");
- return b - 1;
- }
- else if (_contents.Count - 1 == b)
- {
- _contents.Add(key + "=");
- return _contents.Count - 1;
- }
- if (_contents.Count > 0)
- _contents.Add("");
- _contents.Add("[" + section + "]");
- _contents.Add(key + "=");
- return _contents.Count - 1;
- }
- public void Save()
- {
- try
- {
- File.WriteAllLines(_fileName, _contents);
- }
- catch
- {
- }
- }
- #endregion
- #region Read
- public bool ReadBoolean(string section, string key, bool Default, bool writeWhenNull = true)
- {
- bool result;
- if (!bool.TryParse(FindValue(section, key), out result))
- {
- result = Default;
- if (writeWhenNull) Write(section, key, Default);
- }
- return result;
- }
- public byte ReadByte(string section, string key, byte Default, bool writeWhenNull = true)
- {
- byte result;
- if (!byte.TryParse(FindValue(section, key), out result))
- {
- result = Default;
- if (writeWhenNull) Write(section, key, Default);
- }
- return result;
- }
- public sbyte ReadSByte(string section, string key, sbyte Default, bool writeWhenNull = true)
- {
- sbyte result;
- if (!sbyte.TryParse(FindValue(section, key), out result))
- {
- result = Default;
- if (writeWhenNull) Write(section, key, Default);
- }
- return result;
- }
- public ushort ReadUInt16(string section, string key, ushort Default, bool writeWhenNull = true)
- {
- ushort result;
- if (!ushort.TryParse(FindValue(section, key), out result))
- {
- result = Default;
- if (writeWhenNull) Write(section, key, Default);
- }
- return result;
- }
- public short ReadInt16(string section, string key, short Default, bool writeWhenNull = true)
- {
- short result;
- if (!short.TryParse(FindValue(section, key), out result))
- {
- result = Default;
- if (writeWhenNull) Write(section, key, Default);
- }
- return result;
- }
- public uint ReadUInt32(string section, string key, uint Default, bool writeWhenNull = true)
- {
- uint result;
- if (!uint.TryParse(FindValue(section, key), out result))
- {
- result = Default;
- if (writeWhenNull) Write(section, key, Default);
- }
- return result;
- }
- public int ReadInt32(string section, string key, int Default, bool writeWhenNull = true)
- {
- int result;
- if (!int.TryParse(FindValue(section, key), out result))
- {
- result = Default;
- if (writeWhenNull) Write(section, key, Default);
- }
- return result;
- }
- public ulong ReadUInt64(string section, string key, ulong Default, bool writeWhenNull = true)
- {
- ulong result;
- if (!ulong.TryParse(FindValue(section, key), out result))
- {
- result = Default;
- if (writeWhenNull) Write(section, key, Default);
- }
- return result;
- }
- public long ReadInt64(string section, string key, long Default, bool writeWhenNull = true)
- {
- long result;
- if (!long.TryParse(FindValue(section, key), out result))
- {
- result = Default;
- if (writeWhenNull) Write(section, key, Default);
- }
- return result;
- }
- public float ReadSingle(string section, string key, float Default, bool writeWhenNull = true)
- {
- float result;
- if (!float.TryParse(FindValue(section, key), out result))
- {
- result = Default;
- if (writeWhenNull) Write(section, key, Default);
- }
- return result;
- }
- public double ReadDouble(string section, string key, double Default, bool writeWhenNull = true)
- {
- double result;
- if (!double.TryParse(FindValue(section, key), out result))
- {
- result = Default;
- if (writeWhenNull) Write(section, key, Default);
- }
- return result;
- }
- public decimal ReadDecimal(string section, string key, decimal Default, bool writeWhenNull = true)
- {
- decimal result;
- if (!decimal.TryParse(FindValue(section, key), out result))
- {
- result = Default;
- if (writeWhenNull) Write(section, key, Default);
- }
- return result;
- }
- public string ReadString(string section, string key, string Default, bool writeWhenNull = true)
- {
- string result = FindValue(section, key);
- if (string.IsNullOrEmpty(result))
- {
- result = Default;
- if (writeWhenNull) Write(section, key, Default);
- }
- return result;
- }
- public char ReadChar(string section, string key, char Default, bool writeWhenNull = true)
- {
- char result;
- if (!char.TryParse(FindValue(section, key), out result))
- {
- result = Default;
- if (writeWhenNull) Write(section, key, Default);
- }
- return result;
- }
- public Point ReadPoint(string section, string key, Point Default)
- {
- string temp = FindValue(section, key);
- int tempX, tempY;
- if (temp == null || !int.TryParse(temp.Split(',')[0], out tempX))
- {
- Write(section, key, Default);
- return Default;
- }
- if (!int.TryParse(temp.Split(',')[1], out tempY))
- {
- Write(section, key, Default);
- return Default;
- }
- return new Point(tempX, tempY);
- }
- public Size ReadSize(string section, string key, Size Default)
- {
- string temp = FindValue(section, key);
- int tempX, tempY;
- if (!int.TryParse(temp.Split(',')[0], out tempX))
- {
- Write(section, key, Default);
- return Default;
- }
- if (!int.TryParse(temp.Split(',')[1], out tempY))
- {
- Write(section, key, Default);
- return Default;
- }
- return new Size(tempX, tempY);
- }
- public TimeSpan ReadTimeSpan(string section, string key, TimeSpan Default)
- {
- TimeSpan result;
- if (!TimeSpan.TryParse(FindValue(section, key), out result))
- {
- result = Default;
- Write(section, key, Default);
- }
- return result;
- }
- public float ReadFloat(string section, string key, float Default)
- {
- float result;
- if (!float.TryParse(FindValue(section, key), out result))
- {
- result = Default;
- Write(section, key, Default);
- }
- return result;
- }
- #endregion
- #region Write
- public void Write(string section, string key, bool value)
- {
- _contents[FindIndex(section, key)] = key + "=" + value;
- Save();
- }
- public void Write(string section, string key, byte value)
- {
- _contents[FindIndex(section, key)] = key + "=" + value;
- Save();
- }
- public void Write(string section, string key, sbyte value)
- {
- _contents[FindIndex(section, key)] = key + "=" + value;
- Save();
- }
- public void Write(string section, string key, ushort value)
- {
- _contents[FindIndex(section, key)] = key + "=" + value;
- Save();
- }
- public void Write(string section, string key, short value)
- {
- _contents[FindIndex(section, key)] = key + "=" + value;
- Save();
- }
- public void Write(string section, string key, uint value)
- {
- _contents[FindIndex(section, key)] = key + "=" + value;
- Save();
- }
- public void Write(string section, string key, int value)
- {
- _contents[FindIndex(section, key)] = key + "=" + value;
- Save();
- }
- public void Write(string section, string key, ulong value)
- {
- _contents[FindIndex(section, key)] = key + "=" + value;
- Save();
- }
- public void Write(string section, string key, long value)
- {
- _contents[FindIndex(section, key)] = key + "=" + value;
- Save();
- }
- public void Write(string section, string key, float value)
- {
- _contents[FindIndex(section, key)] = key + "=" + value;
- Save();
- }
- public void Write(string section, string key, double value)
- {
- _contents[FindIndex(section, key)] = key + "=" + value;
- Save();
- }
- public void Write(string section, string key, decimal value)
- {
- _contents[FindIndex(section, key)] = key + "=" + value;
- Save();
- }
- public void Write(string section, string key, string value)
- {
- _contents[FindIndex(section, key)] = key + "=" + value;
- Save();
- }
- public void Write(string section, string key, char value)
- {
- _contents[FindIndex(section, key)] = key + "=" + value;
- Save();
- }
- public void Write(string section, string key, Point value)
- {
- _contents[FindIndex(section, key)] = key + "=" + value.X + "," + value.Y;
- Save();
- }
- public void Write(string section, string key, Size value)
- {
- _contents[FindIndex(section, key)] = key + "=" + value.Width + "," + value.Height;
- Save();
- }
- public void Write(string section, string key, TimeSpan value)
- {
- _contents[FindIndex(section, key)] = key + "=" + value;
- Save();
- }
- #endregion
- }
|