versioncmp.pas 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. {
  2. Copyright (c) 2022 by Jonas Maebe
  3. Target OS version comparisons
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit versioncmp;
  18. {$i fpcdefs.inc}
  19. interface
  20. type
  21. tversion = object
  22. private
  23. fstr: string;
  24. fnum: cardinal;
  25. public
  26. { initialise with string and numerical representation of the version }
  27. constructor init(const str: string; major: byte; minor: word; patch: byte);
  28. constructor invalidate;
  29. function relationto(const other: tversion): shortint;
  30. function relationto(major: byte; minor: word; patch: byte): shortint;
  31. function isvalid: boolean;
  32. property str: string read fstr;
  33. private
  34. function tonum(major: byte; minor: word; patch: byte): cardinal; inline;
  35. end;
  36. implementation
  37. function tversion.tonum(major: byte; minor: word; patch: byte): cardinal;
  38. begin
  39. result:=(major shl 24) or (minor shl 8) or patch;
  40. end;
  41. constructor tversion.init(const str: string; major: byte; minor: word; patch: byte);
  42. begin
  43. fstr:=str;
  44. fnum:=tonum(major,minor,patch);
  45. end;
  46. constructor tversion.invalidate;
  47. begin
  48. fstr:='';
  49. fnum:=0;
  50. end;
  51. function tversion.relationto(const other: tversion): shortint;
  52. begin
  53. if fnum>other.fnum then
  54. result:=1
  55. else if fnum<other.fnum then
  56. result:=-1
  57. else
  58. result:=0;
  59. end;
  60. function tversion.relationto(major: byte; minor: word; patch: byte): shortint;
  61. var
  62. othernum: cardinal;
  63. begin
  64. othernum:=tonum(major,minor,patch);
  65. if fnum>othernum then
  66. result:=1
  67. else if fnum<othernum then
  68. result:=-1
  69. else
  70. result:=0;
  71. end;
  72. function tversion.isvalid: boolean;
  73. begin
  74. result:=fstr<>'';
  75. end;
  76. end.