InIReader.cs 11 KB

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