IniReader.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. using System.Drawing;
  2. public class InIReader
  3. {
  4. #region Fields
  5. private readonly List<string> _contents;
  6. private readonly string _fileName;
  7. #endregion
  8. #region Constructor
  9. public InIReader(string fileName)
  10. {
  11. _fileName = fileName;
  12. if (!Directory.Exists(Path.GetDirectoryName(fileName)))
  13. {
  14. Directory.CreateDirectory(Path.GetDirectoryName(fileName));
  15. }
  16. _contents = new List<string>();
  17. try
  18. {
  19. if (File.Exists(_fileName))
  20. _contents.AddRange(File.ReadAllLines(_fileName));
  21. }
  22. catch
  23. {
  24. }
  25. }
  26. #endregion
  27. #region Functions
  28. private string FindValue(string section, string key)
  29. {
  30. for (int a = 0; a < _contents.Count; a++)
  31. if (String.CompareOrdinal(_contents[a], "[" + section + "]") == 0)
  32. for (int b = a + 1; b < _contents.Count; b++)
  33. if (String.CompareOrdinal(_contents[b].Split('=')[0], key) == 0)
  34. return _contents[b].Split('=')[1];
  35. else if (_contents[b].StartsWith("[") && _contents[b].EndsWith("]"))
  36. return null;
  37. return null;
  38. }
  39. private int FindIndex(string section, string key)
  40. {
  41. for (int a = 0; a < _contents.Count; a++)
  42. if (String.CompareOrdinal(_contents[a], "[" + section + "]") == 0)
  43. for (int b = a + 1; b < _contents.Count; b++)
  44. if (String.CompareOrdinal(_contents[b].Split('=')[0], key) == 0)
  45. return b;
  46. else if (_contents[b].StartsWith("[") && _contents[b].EndsWith("]"))
  47. {
  48. _contents.Insert(b - 1, key + "=");
  49. return b - 1;
  50. }
  51. else if (_contents.Count - 1 == b)
  52. {
  53. _contents.Add(key + "=");
  54. return _contents.Count - 1;
  55. }
  56. if (_contents.Count > 0)
  57. _contents.Add("");
  58. _contents.Add("[" + section + "]");
  59. _contents.Add(key + "=");
  60. return _contents.Count - 1;
  61. }
  62. public void Save()
  63. {
  64. try
  65. {
  66. File.WriteAllLines(_fileName, _contents);
  67. }
  68. catch
  69. {
  70. }
  71. }
  72. #endregion
  73. #region Read
  74. public bool ReadBoolean(string section, string key, bool Default, bool writeWhenNull = true)
  75. {
  76. bool result;
  77. if (!bool.TryParse(FindValue(section, key), out result))
  78. {
  79. result = Default;
  80. if (writeWhenNull) Write(section, key, Default);
  81. }
  82. return result;
  83. }
  84. public byte ReadByte(string section, string key, byte Default, bool writeWhenNull = true)
  85. {
  86. byte result;
  87. if (!byte.TryParse(FindValue(section, key), out result))
  88. {
  89. result = Default;
  90. if (writeWhenNull) Write(section, key, Default);
  91. }
  92. return result;
  93. }
  94. public sbyte ReadSByte(string section, string key, sbyte Default, bool writeWhenNull = true)
  95. {
  96. sbyte result;
  97. if (!sbyte.TryParse(FindValue(section, key), out result))
  98. {
  99. result = Default;
  100. if (writeWhenNull) Write(section, key, Default);
  101. }
  102. return result;
  103. }
  104. public ushort ReadUInt16(string section, string key, ushort Default, bool writeWhenNull = true)
  105. {
  106. ushort result;
  107. if (!ushort.TryParse(FindValue(section, key), out result))
  108. {
  109. result = Default;
  110. if (writeWhenNull) Write(section, key, Default);
  111. }
  112. return result;
  113. }
  114. public short ReadInt16(string section, string key, short Default, bool writeWhenNull = true)
  115. {
  116. short result;
  117. if (!short.TryParse(FindValue(section, key), out result))
  118. {
  119. result = Default;
  120. if (writeWhenNull) Write(section, key, Default);
  121. }
  122. return result;
  123. }
  124. public uint ReadUInt32(string section, string key, uint Default, bool writeWhenNull = true)
  125. {
  126. uint result;
  127. if (!uint.TryParse(FindValue(section, key), out result))
  128. {
  129. result = Default;
  130. if (writeWhenNull) Write(section, key, Default);
  131. }
  132. return result;
  133. }
  134. public int ReadInt32(string section, string key, int Default, bool writeWhenNull = true)
  135. {
  136. int result;
  137. if (!int.TryParse(FindValue(section, key), out result))
  138. {
  139. result = Default;
  140. if (writeWhenNull) Write(section, key, Default);
  141. }
  142. return result;
  143. }
  144. public ulong ReadUInt64(string section, string key, ulong Default, bool writeWhenNull = true)
  145. {
  146. ulong result;
  147. if (!ulong.TryParse(FindValue(section, key), out result))
  148. {
  149. result = Default;
  150. if (writeWhenNull) Write(section, key, Default);
  151. }
  152. return result;
  153. }
  154. public long ReadInt64(string section, string key, long Default, bool writeWhenNull = true)
  155. {
  156. long result;
  157. if (!long.TryParse(FindValue(section, key), out result))
  158. {
  159. result = Default;
  160. if (writeWhenNull) Write(section, key, Default);
  161. }
  162. return result;
  163. }
  164. public float ReadSingle(string section, string key, float Default, bool writeWhenNull = true)
  165. {
  166. float result;
  167. if (!float.TryParse(FindValue(section, key), out result))
  168. {
  169. result = Default;
  170. if (writeWhenNull) Write(section, key, Default);
  171. }
  172. return result;
  173. }
  174. public double ReadDouble(string section, string key, double Default, bool writeWhenNull = true)
  175. {
  176. double result;
  177. if (!double.TryParse(FindValue(section, key), out result))
  178. {
  179. result = Default;
  180. if (writeWhenNull) Write(section, key, Default);
  181. }
  182. return result;
  183. }
  184. public decimal ReadDecimal(string section, string key, decimal Default, bool writeWhenNull = true)
  185. {
  186. decimal result;
  187. if (!decimal.TryParse(FindValue(section, key), out result))
  188. {
  189. result = Default;
  190. if (writeWhenNull) Write(section, key, Default);
  191. }
  192. return result;
  193. }
  194. public string ReadString(string section, string key, string Default, bool writeWhenNull = true)
  195. {
  196. string result = FindValue(section, key);
  197. if (string.IsNullOrEmpty(result))
  198. {
  199. result = Default;
  200. if (writeWhenNull) Write(section, key, Default);
  201. }
  202. return result;
  203. }
  204. public char ReadChar(string section, string key, char Default, bool writeWhenNull = true)
  205. {
  206. char result;
  207. if (!char.TryParse(FindValue(section, key), out result))
  208. {
  209. result = Default;
  210. if (writeWhenNull) Write(section, key, Default);
  211. }
  212. return result;
  213. }
  214. public Point ReadPoint(string section, string key, Point Default)
  215. {
  216. string temp = FindValue(section, key);
  217. int tempX, tempY;
  218. if (temp == null || !int.TryParse(temp.Split(',')[0], out tempX))
  219. {
  220. Write(section, key, Default);
  221. return Default;
  222. }
  223. if (!int.TryParse(temp.Split(',')[1], out tempY))
  224. {
  225. Write(section, key, Default);
  226. return Default;
  227. }
  228. return new Point(tempX, tempY);
  229. }
  230. public Size ReadSize(string section, string key, Size Default)
  231. {
  232. string temp = FindValue(section, key);
  233. int tempX, tempY;
  234. if (!int.TryParse(temp.Split(',')[0], out tempX))
  235. {
  236. Write(section, key, Default);
  237. return Default;
  238. }
  239. if (!int.TryParse(temp.Split(',')[1], out tempY))
  240. {
  241. Write(section, key, Default);
  242. return Default;
  243. }
  244. return new Size(tempX, tempY);
  245. }
  246. public TimeSpan ReadTimeSpan(string section, string key, TimeSpan Default)
  247. {
  248. TimeSpan result;
  249. if (!TimeSpan.TryParse(FindValue(section, key), out result))
  250. {
  251. result = Default;
  252. Write(section, key, Default);
  253. }
  254. return result;
  255. }
  256. public float ReadFloat(string section, string key, float Default)
  257. {
  258. float result;
  259. if (!float.TryParse(FindValue(section, key), out result))
  260. {
  261. result = Default;
  262. Write(section, key, Default);
  263. }
  264. return result;
  265. }
  266. #endregion
  267. #region Write
  268. public void Write(string section, string key, bool value)
  269. {
  270. _contents[FindIndex(section, key)] = key + "=" + value;
  271. Save();
  272. }
  273. public void Write(string section, string key, byte value)
  274. {
  275. _contents[FindIndex(section, key)] = key + "=" + value;
  276. Save();
  277. }
  278. public void Write(string section, string key, sbyte value)
  279. {
  280. _contents[FindIndex(section, key)] = key + "=" + value;
  281. Save();
  282. }
  283. public void Write(string section, string key, ushort value)
  284. {
  285. _contents[FindIndex(section, key)] = key + "=" + value;
  286. Save();
  287. }
  288. public void Write(string section, string key, short value)
  289. {
  290. _contents[FindIndex(section, key)] = key + "=" + value;
  291. Save();
  292. }
  293. public void Write(string section, string key, uint value)
  294. {
  295. _contents[FindIndex(section, key)] = key + "=" + value;
  296. Save();
  297. }
  298. public void Write(string section, string key, int value)
  299. {
  300. _contents[FindIndex(section, key)] = key + "=" + value;
  301. Save();
  302. }
  303. public void Write(string section, string key, ulong value)
  304. {
  305. _contents[FindIndex(section, key)] = key + "=" + value;
  306. Save();
  307. }
  308. public void Write(string section, string key, long value)
  309. {
  310. _contents[FindIndex(section, key)] = key + "=" + value;
  311. Save();
  312. }
  313. public void Write(string section, string key, float value)
  314. {
  315. _contents[FindIndex(section, key)] = key + "=" + value;
  316. Save();
  317. }
  318. public void Write(string section, string key, double value)
  319. {
  320. _contents[FindIndex(section, key)] = key + "=" + value;
  321. Save();
  322. }
  323. public void Write(string section, string key, decimal value)
  324. {
  325. _contents[FindIndex(section, key)] = key + "=" + value;
  326. Save();
  327. }
  328. public void Write(string section, string key, string value)
  329. {
  330. _contents[FindIndex(section, key)] = key + "=" + value;
  331. Save();
  332. }
  333. public void Write(string section, string key, char value)
  334. {
  335. _contents[FindIndex(section, key)] = key + "=" + value;
  336. Save();
  337. }
  338. public void Write(string section, string key, Point value)
  339. {
  340. _contents[FindIndex(section, key)] = key + "=" + value.X + "," + value.Y;
  341. Save();
  342. }
  343. public void Write(string section, string key, Size value)
  344. {
  345. _contents[FindIndex(section, key)] = key + "=" + value.Width + "," + value.Height;
  346. Save();
  347. }
  348. public void Write(string section, string key, TimeSpan value)
  349. {
  350. _contents[FindIndex(section, key)] = key + "=" + value;
  351. Save();
  352. }
  353. #endregion
  354. }