lmimetypes.pp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. { Mime types helper
  2. Copyright (C) 2006-2008 Micha Nelissen
  3. This library is Free software; you can redistribute it and/or modify it
  4. under the terms of the GNU Library General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or (at your
  6. option) any later version.
  7. This program is diStributed in the hope that it will be useful, but WITHOUT
  8. ANY WARRANTY; withOut even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  10. for more details.
  11. You should have received a Copy of the GNU Library General Public License
  12. along with This library; if not, Write to the Free Software Foundation,
  13. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  14. This license has been modified. See file LICENSE.ADDON for more information.
  15. Should you find these sources without a LICENSE File, please contact
  16. me at [email protected]
  17. }
  18. unit lMimeTypes;
  19. {$mode objfpc}{$h+}
  20. interface
  21. uses
  22. classes, sysutils, strutils;
  23. type
  24. TStringObject = class(TObject)
  25. Str: string;
  26. end;
  27. procedure InitMimeList(const aFileName: string);
  28. var
  29. MimeList: TStringList = nil;
  30. implementation
  31. var
  32. MimeFileName: string;
  33. procedure InitMimeList(const aFileName: string);
  34. var
  35. MimeFile: Text;
  36. lPos, lNextPos: integer;
  37. lLine, lName: string;
  38. lStrObj: TStringObject;
  39. lBuffer: array[1..32*1024] of byte;
  40. begin
  41. if not Assigned(MimeList) then begin
  42. MimeFileName := aFileName;
  43. MimeList := TStringList.Create;
  44. if FileExists(MimeFileName) then
  45. begin
  46. Assign(MimeFile, MimeFileName);
  47. Reset(MimeFile);
  48. SetTextBuf(MimeFile, lBuffer);
  49. while not Eof(MimeFile) do
  50. begin
  51. ReadLn(MimeFile, lLine);
  52. if (Length(lLine) = 0) or (lLine[1] = '#') then
  53. continue;
  54. lPos := Pos(#9, lLine);
  55. if lPos = 0 then
  56. continue;
  57. lName := Copy(lLine, 1, lPos-1);
  58. while (lPos <= Length(lLine)) and (lLine[lPos] in [#9,' ']) do
  59. Inc(lPos);
  60. if lPos > Length(lLine) then
  61. continue;
  62. repeat
  63. lNextPos := PosEx(' ', lLine, lPos);
  64. if lNextPos = 0 then
  65. lNextPos := Length(lLine)+1;
  66. lStrObj := TStringObject.Create;
  67. lStrObj.Str := lName;
  68. MimeList.AddObject('.'+Copy(lLine, lPos, lNextPos-lPos), lStrObj);
  69. lPos := lNextPos+1;
  70. until lPos > Length(lLine);
  71. end;
  72. close(MimeFile);
  73. end;
  74. MimeList.Sorted := true;
  75. end;
  76. end;
  77. procedure FreeMimeList;
  78. var
  79. I: integer;
  80. begin
  81. if Assigned(MimeList) then begin
  82. for I := 0 to MimeList.Count - 1 do
  83. MimeList.Objects[I].Free;
  84. FreeAndNil(MimeList);
  85. end;
  86. end;
  87. finalization
  88. FreeMimeList;
  89. end.