Repo for the search and displace ingest module that takes odf, docx and pdf and transforms it into .md to be used with search and displace operations
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
2.7 KiB

3 years ago
  1. unit Utils;
  2. interface
  3. uses
  4. Classes, SysUtils, TypInfo, ImagingTypes;
  5. type
  6. // Workaround for generic functions needing FPC 3.1.1+
  7. TEnumUtils<T> = class
  8. public
  9. class function EnumToStr(const EnumValue: T): string;
  10. class function StrToEnum(const Str: string): T;
  11. class function GetEnumPrefix: string;
  12. end;
  13. function FindDeskewExePath: string;
  14. function DetermineConfigFolder: string;
  15. function ColorToString(Color: TColor32): string;
  16. function StringToColorDef(const Str: string; Default: TColor32): TColor32;
  17. implementation
  18. uses
  19. {$IF Defined(DARWIN)}
  20. StrUtils,
  21. {$ENDIF}
  22. LazFileUtils, Forms;
  23. function FindDeskewExePath: string;
  24. var
  25. ExeDir, S: string;
  26. begin
  27. Result := './deskew';
  28. ExeDir := Application.Location;
  29. if DirectoryExists(ExeDir) then
  30. begin
  31. {$IF Defined(MSWINDOWS)}
  32. S := ExeDir + 'deskew64.exe';
  33. if FileExists(S) then
  34. Exit(S);
  35. S := ExeDir + 'deskew.exe';
  36. if FileExists(S) then
  37. Exit(S);
  38. S := ExeDir + 'deskew32.exe';
  39. if FileExists(S) then
  40. Exit(S);
  41. {$ELSEIF Defined(DARWIN)}
  42. S := ExeDir + 'deskew-mac';
  43. if FileExists(S) then
  44. Exit(S);
  45. S := ExeDir + 'deskew';
  46. if FileExists(S) then
  47. Exit(S);
  48. if AnsiContainsText(ExeDir, '.app/Contents/MacOS') then
  49. begin
  50. // Get out af the bundle
  51. S := ExtractFileDir(ExtractFileDir(ExtractFileDir(ExcludeTrailingPathDelimiter(ExeDir)))) + '/deskew-mac';
  52. if FileExists(S) then
  53. Exit(S);
  54. end;
  55. {$ELSEIF Defined(LINUX)}
  56. S := ExeDir + 'deskew';
  57. if FileExists(S) then
  58. Exit(S);
  59. {$ENDIF}
  60. end;
  61. end;
  62. function DetermineConfigFolder: string;
  63. var
  64. ExeDir: string;
  65. begin
  66. Result := GetAppConfigDir(False);
  67. ExeDir := Application.Location;
  68. if DirectoryExists(ExeDir) and DirectoryIsWritable(ExeDir) then
  69. Result := ExeDir;
  70. end;
  71. function ColorToString(Color: TColor32): string;
  72. begin
  73. Result := '#' + HexStr(Color, 8);
  74. end;
  75. function StringToColorDef(const Str: string; Default: TColor32): TColor32;
  76. var
  77. S: string;
  78. begin
  79. S := '$' + Copy(Str, 2);
  80. Result := StrToDWordDef(S, Default);
  81. end;
  82. class function TEnumUtils<T>.EnumToStr(const EnumValue: T): string;
  83. var
  84. S: string;
  85. L: Integer;
  86. begin
  87. S := TypInfo.GetEnumName(TypeInfo(T), Integer(EnumValue));
  88. L := Length(GetEnumPrefix);
  89. Result := Copy(S, L + 1);
  90. end;
  91. class function TEnumUtils<T>.StrToEnum(const Str: string): T;
  92. var
  93. S: string;
  94. I: Integer;
  95. begin
  96. S := GetEnumPrefix + Str;
  97. I := TypInfo.GetEnumValue(TypeInfo(T), S);
  98. if I >= 0 then
  99. Result := T(I)
  100. else
  101. Result := Default(T);
  102. end;
  103. class function TEnumUtils<T>.GetEnumPrefix: string;
  104. var
  105. S: string;
  106. begin
  107. S := TypInfo.GetEnumName(TypeInfo(T), Integer(Default(T)));
  108. Result := Copy(S, 1, 2);
  109. if S[3] in ['a'..'z'] then
  110. Result := Result + S[3];
  111. end;
  112. end.