Guest User

Untitled

a guest
Mar 21st, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.44 KB | None | 0 0
  1. #region Copyright(c) 1998-2012, Arnaud Colin Licence GNU GPL version 3
  2. /* Copyright(c) 1998-2012, Arnaud Colin
  3. * All rights reserved.
  4. *
  5. * Licence GNU GPL version 3
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * -> Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * -> Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. */
  17. #endregion
  18.  
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Globalization;
  22. #if NET35
  23. using System.Linq;
  24. #endif
  25. using System.Reflection;
  26. using System.Text;
  27. using System.Diagnostics;
  28. using System.Runtime.CompilerServices;
  29. //using System.Threading.Tasks;
  30.  
  31. namespace Tools
  32. {
  33. /// <summary>
  34. /// This class contains Version
  35. /// </summary>
  36. [Serializable]
  37. public class CVersion : ICloneable, IComparable
  38. {
  39. private DateTime lastModified;
  40. private int[] _Data = new int[4] {0,0,-1,-1};
  41.  
  42. /// <summary>
  43. /// <para/>
  44. /// Static method get Version of the Master Process
  45. /// return a instance of the class
  46. /// <returns>Instance of the class.</returns>
  47. /// </summary>
  48. static public CVersion GetInfo()
  49. {
  50. CVersion ver = new CVersion();
  51. #if !WindowsCE
  52. Process CurrentProcess = System.Diagnostics.Process.GetCurrentProcess();
  53. FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(CurrentProcess.Modules[0].FileName);
  54. System.IO.FileInfo fileInfo = new System.IO.FileInfo(fileVersionInfo.FileName);
  55.  
  56. ver.lastModified = fileInfo.LastWriteTime;
  57. ver.Convert(fileVersionInfo.ProductVersion);
  58. #else
  59. System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
  60. ver.lastModified = DateTime.MinValue;
  61. ver.Convert(Assembly.GetExecutingAssembly().GetName().Version);
  62. #endif
  63. return ver;
  64. }
  65.  
  66.  
  67. /// <summary>
  68. /// <para/>
  69. /// ToString Verbose function
  70. /// <returns>"Version Q3 2012, released 26/07/2012"</returns>
  71. /// </summary>
  72. public string DateVerbose()
  73. {
  74. int quarter = (lastModified.Month / 3) + 1;
  75. return string.Format("Version Q{0} {1}, released {2:d2}/{3:d2}/{4}",
  76. quarter, lastModified.Year, lastModified.Day, lastModified.Month, lastModified.Year);
  77. }
  78.  
  79.  
  80. /// <summary>
  81. /// <para/>
  82. /// Major
  83. /// <returns>int</returns>
  84. /// </summary>
  85. public int Major
  86. {
  87. get
  88. {
  89. return _Data[0];
  90. }
  91. set
  92. {
  93. _Data[0] = value;
  94. }
  95. }
  96.  
  97. /// <summary>
  98. /// <para/>
  99. /// Minor
  100. /// <returns>int</returns>
  101. /// </summary>
  102. public int Minor
  103. {
  104. get
  105. {
  106. return _Data[1];
  107. }
  108. set
  109. {
  110. _Data[1] = value;
  111. }
  112. }
  113.  
  114. /// <summary>
  115. /// <para/>
  116. /// Build
  117. /// <returns>int</returns>
  118. /// </summary>
  119. public int Build
  120. {
  121. get
  122. {
  123. return _Data[2];
  124. }
  125. set
  126. {
  127. _Data[2] = value;
  128. }
  129. }
  130.  
  131. /// <summary>
  132. /// <para/>
  133. /// Revision
  134. /// <returns>int</returns>
  135. /// </summary>
  136. public int Revision
  137. {
  138. get
  139. {
  140. return _Data[3];
  141. }
  142. set
  143. {
  144. _Data[3] = value;
  145. }
  146. }
  147.  
  148. /// <summary>
  149. /// <para/>
  150. /// Last time that the file whas Modified
  151. /// <returns>DateTime</returns>
  152. /// </summary>
  153. public DateTime LastModified
  154. {
  155. get
  156. {
  157. return lastModified;
  158. }
  159. set
  160. {
  161. lastModified = value;
  162. }
  163. }
  164.  
  165. /// <summary>
  166. /// <para/>
  167. /// Construtor
  168. /// </summary>
  169. public CVersion()
  170. {
  171. }
  172.  
  173. #if WindowsCE
  174. /// <summary>
  175. /// <para>
  176. /// <param name="major"></param>
  177. /// <param name="minor"></param>
  178. /// </para>
  179. /// Construtor
  180. /// </summary>
  181. public CVersion(int major, int minor)
  182. {
  183. this.Major = major;
  184. this.Minor = minor;
  185. this.Build = -1;
  186. this.Revision = -1;
  187. }
  188.  
  189. /// <summary>
  190. /// <para>
  191. /// <param name="major"></param>
  192. /// <param name="minor"></param>
  193. /// <param name="revision"></param>
  194. /// </para>
  195. /// Construtor
  196. /// </summary>
  197. public CVersion(int major, int minor, int build)
  198. {
  199. this.Major = major;
  200. this.Minor = minor;
  201. this.Build = build;
  202. this.Revision = -1;
  203. }
  204. #endif
  205.  
  206. /// <summary>
  207. /// <para>
  208. /// <param name="major"></param>
  209. /// <param name="minor"></param>
  210. /// <param name="build">Optional</param>
  211. /// <param name="revision">Optional</param>
  212. /// </para>
  213. /// <example> CVersion(5, 4, 10, 415) CVersion(5, 4, 10) CVersion(5, 4) </example>
  214. /// Construtor
  215. /// </summary>
  216. #if !WindowsCE
  217. public CVersion(int major, int minor, int build = -1, int revision = -1 )
  218. #else
  219. public CVersion(int major, int minor, int build, int revision)
  220. #endif
  221. {
  222. this.Major = major;
  223. this.Minor = minor;
  224. this.Build = build;
  225. this.Revision = revision;
  226. }
  227.  
  228. /// <summary>
  229. /// <para>
  230. /// <param name="version"></param>
  231. /// </para>
  232. /// <example> CVersion("5.4.10.415") </example>
  233. /// Construtor
  234. /// </summary>
  235. #if NET45
  236. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  237. #endif
  238. public CVersion(string version)
  239. {
  240. Convert(version);
  241. }
  242.  
  243. /// <summary>
  244. /// <para>
  245. /// <param name="v1">System.Version</param>
  246. /// </para>
  247. /// Construtor
  248. /// </summary>
  249. #if NET45
  250. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  251. #endif
  252. public CVersion(System.Version v1)
  253. {
  254. Convert(v1);
  255. }
  256.  
  257. /// <summary>
  258. /// <para>
  259. /// <param name="v1">System.Version</param>
  260. /// </para>
  261. /// Convert System.Version in FransBonhomme.CVersion
  262. /// <returns>Void</returns>
  263. /// </summary>
  264. public void Convert(System.Version v1)
  265. {
  266. this.Major = v1.Major;
  267. this.Minor = v1.Minor;
  268. this.Build = v1.Build;
  269. this.Revision = v1.Revision;
  270. }
  271.  
  272.  
  273. /// <summary>
  274. /// <para>
  275. /// <param name="obj">object</param>
  276. /// </para>
  277. /// Check if obj is Equal at CVersion
  278. /// <returns>bool</returns>
  279. /// </summary>
  280. #if NET45
  281. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  282. #endif
  283. public override bool Equals(object obj)
  284. {
  285. return (CompareTo(obj) == 0);
  286. }
  287.  
  288.  
  289. /// <summary>
  290. /// Create a new instance with same data
  291. /// <returns>CVersion</returns>
  292. /// </summary>
  293. public object Clone()
  294. {
  295. CVersion version1 = new CVersion();
  296. this._Data.CopyTo(version1._Data, 0);
  297. version1.lastModified = this.lastModified;
  298. return version1;
  299. }
  300.  
  301.  
  302. /// <summary>
  303. /// <para>
  304. /// <param name="obj">object</param>
  305. /// </para>
  306. /// Compare obj with CVersion
  307. /// <example> new CVersion(4, 0, 10).CompareTo( new CVersion(1, 2) ) = 1 </example>///
  308. /// <returns>int</returns>
  309. /// </summary>
  310. public int CompareTo(object obj)
  311. {
  312. CVersion version = null;
  313.  
  314. if (obj == null)
  315. {
  316. return 1;
  317. }
  318. if (!(obj is CVersion) && !(obj is System.Version) && !(obj is string))
  319. {
  320. throw new ArgumentException("Arg_TypeNotRecognized");
  321. }
  322. if (obj is System.Version)
  323. {
  324. version = new CVersion((System.Version)obj);
  325. }
  326. if (obj is CVersion)
  327. {
  328. version = (CVersion)obj;
  329. }
  330. if (obj is string)
  331. {
  332. version = new CVersion((string)obj);
  333. }
  334.  
  335. for( int i =0; i< _Data.Length; i++)
  336. {
  337. if (this._Data[i] != version._Data[i])
  338. {
  339. if (this._Data[i] > version._Data[i])
  340. {
  341. return 1;
  342. }
  343. return -1;
  344. }
  345. }
  346.  
  347. return 0;
  348. }
  349.  
  350.  
  351. /// <summary>
  352. /// GetHashCode
  353. /// <returns>int</returns>
  354. /// </summary>
  355. public override int GetHashCode()
  356. {
  357. int num1 = 0;
  358. num1 |= ((this.Major & 15) << 0x1c);
  359. num1 |= ((this.Minor & 0xff) << 20);
  360. num1 |= ((this.Build & 0xff) << 12);
  361. return (num1 | this.Revision & 0xfff);
  362. }
  363.  
  364.  
  365. /// <summary>
  366. /// <para>
  367. /// <param name="v1">CVersion</param>
  368. /// <param name="v2">CVersion</param>
  369. /// </para>
  370. /// Compare v1 and v2
  371. /// <example> new CVersion(4, 0, 10) == new CVersion(1, 2) = false </example>///
  372. /// <returns>bool</returns>
  373. /// </summary>
  374. public static bool operator ==(CVersion v1, CVersion v2)
  375. {
  376. return v1.Equals(v2);
  377. }
  378.  
  379.  
  380. /// <summary>
  381. /// <para>
  382. /// <param name="v1">CVersion</param>
  383. /// <param name="v2">CVersion</param>
  384. /// </para>
  385. /// Compare if v1 superior at v2
  386. /// <example> new CVersion(4, 0, 10) > new CVersion(1, 2) = true </example>///
  387. /// <returns>int</returns>
  388. /// </summary>
  389. public static bool operator >(CVersion v1, CVersion v2)
  390. {
  391. return (v2 < v1);
  392. }
  393.  
  394. /// <summary>
  395. /// <para>
  396. /// <param name="v1">CVersion</param>
  397. /// <param name="v2">CVersion</param>
  398. /// </para>
  399. /// Compare if v1 superior and equal at v2
  400. /// <example> new CVersion(4, 0, 10) >= new CVersion(1, 2) = true </example>///
  401. /// <returns>int</returns>
  402. /// </summary>
  403. public static bool operator >=(CVersion v1, CVersion v2)
  404. {
  405. return (v2 <= v1);
  406. }
  407.  
  408. /// <summary>
  409. /// <para>
  410. /// <param name="v1">CVersion</param>
  411. /// <param name="v2">CVersion</param>
  412. /// </para>
  413. /// Compare v1 diffent at v2
  414. /// <example> new CVersion(4, 0, 10) != new CVersion(1, 2) = true </example>///
  415. /// <returns>int</returns>
  416. /// </summary>
  417. public static bool operator !=(CVersion v1, CVersion v2)
  418. {
  419. return (v1 != v2);
  420. }
  421.  
  422. /// <summary>
  423. /// <para>
  424. /// <param name="v1">CVersion</param>
  425. /// <param name="v2">CVersion</param>
  426. /// </para>
  427. /// Compare if v1 inferior at v2
  428. /// <example>new CVersion(4, 0, 10) new CVersion(1, 2) = false </example>///
  429. /// <returns>int</returns>
  430. /// </summary>
  431. public static bool operator <(CVersion v1, CVersion v2)
  432. {
  433. if (v1 == null)
  434. {
  435. throw new ArgumentNullException("v1");
  436. }
  437. return (v1.CompareTo(v2) < 0);
  438. }
  439.  
  440. /// <summary>
  441. /// <para>
  442. /// <param name="v1">CVersion</param>
  443. /// <param name="v2">CVersion</param>
  444. /// </para>
  445. /// Compare if v1 inferior and equal at v2
  446. /// <example> new CVersion(4, 0, 10) new CVersion(1, 2) = false </example>///
  447. /// <returns>int</returns>
  448. /// </summary>
  449. public static bool operator <=(CVersion v1, CVersion v2)
  450. {
  451. if (v1 == null)
  452. {
  453. throw new ArgumentNullException("v1");
  454. }
  455. return (v1.CompareTo(v2) <= 0);
  456. }
  457.  
  458. /// <summary>
  459. /// Get the numbre of field
  460. /// <example> new CVersion(4, 0, 10).FieldCount = 3 </example>///
  461. /// <returns>int</returns>
  462. /// </summary>
  463. public int FieldCount
  464. {
  465. get
  466. {
  467. int i;
  468. for ( i = 3; i >= 0; i--)
  469. if (this._Data[i] != -1)
  470. return i + 1;
  471.  
  472. return 0;
  473. }
  474. }
  475.  
  476. /// <summary>
  477. /// Get the string convertion
  478. /// <example> new CVersion(4, 0, 10).ToString() = "4.0.10" </example>///
  479. /// <returns>int</returns>
  480. /// </summary>
  481. #if NET45
  482. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  483. #endif
  484. public override string ToString()
  485. {
  486. return this.ToString(FieldCount);
  487. }
  488.  
  489. /// <summary>
  490. /// <para>
  491. /// <param name="fieldCount">int</param>
  492. /// </para>
  493. /// Get the string convertion, with the number of digit you want
  494. /// <example> new CVersion(4, 0, 10).ToString(2) = "4.0" </example>///
  495. /// <returns>int</returns>
  496. /// </summary>
  497. public string ToString(int fieldCount)
  498. {
  499.  
  500. if(fieldCount<0 || fieldCount>4)
  501. throw new ArgumentException(string.Format("ArgumentOutOfRange_Bounds_Lower_Upper {0},{1}", "0", "4"), "fieldCount");
  502.  
  503. //object[] objArray1;
  504. List<string> strArray = new List<string>();
  505.  
  506. for (int i = 0; i < fieldCount; i++)
  507. {
  508. if (this._Data[i] < 0)
  509. strArray.Add("0");
  510. else
  511. strArray.Add(this._Data[i].ToString());
  512. }
  513.  
  514. if (strArray.Count > 0)
  515. #if NET35
  516. return strArray.Aggregate((i, j) => i + "." + j);
  517. #else
  518. return NET20.Aggregate(strArray, null, '.' );
  519. #endif
  520. else
  521. return string.Empty;
  522.  
  523. }
  524.  
  525.  
  526. /// <summary>
  527. /// <para>
  528. /// <param name="version">string</param>
  529. /// </para>
  530. /// Convert string to digit
  531. /// <example> new CVersion().Convert("4.0.10") </example>///
  532. /// </summary>
  533. public void Convert(string version)
  534. {
  535. // this.Build = -1;
  536. //this.Revision = -1;
  537. if (version == null)
  538. {
  539. throw new ArgumentNullException("version");
  540. }
  541. char[] chArray1 = new char[1] { '.' };
  542. string[] textArray1 = version.Split(chArray1);
  543. int num1 = textArray1.Length;
  544. if ((num1 < 2) || (num1 > 4))
  545. {
  546. throw new ArithmeticException(version + " is not a valid Version string", new Exception("Arg_Convert_VersionString"));
  547. }
  548.  
  549. for (int i = 0; i < _Data.Length; i++)
  550. {
  551. if (num1 > i )
  552. {
  553. #if !WindowsCE
  554. if (!int.TryParse(textArray1[i], NumberStyles.Integer, CultureInfo.InvariantCulture, out this._Data[i]))
  555. {
  556. throw new ArithmeticException(version + " is not a valid Version string", new Exception("ArgumentNotNumeric_Version"));
  557. }
  558. #else
  559. try
  560. {
  561. this._Data[i] = int.Parse(textArray1[i], NumberStyles.Integer, CultureInfo.InvariantCulture);
  562. }
  563. catch
  564. {
  565. throw new ArithmeticException(version + " is not a valid Version string", new Exception("ArgumentNotNumeric_Version"));
  566. }
  567. #endif
  568.  
  569. if (this._Data[i] < 0)
  570. {
  571. throw new ArgumentOutOfRangeException(version + " isnot a valid Version string", "ArgumentOutOfRange_Version");
  572. }
  573. }
  574. else
  575. {
  576. this._Data[i] = -1;
  577. }
  578. }
  579.  
  580. }
  581.  
  582. }
  583. }
Add Comment
Please, Sign In to add comment