Delphi.int.ru — Портал программистов

Вход Регистрация | Забыли пароль?

Просмотр кода

Идентификатор: b57d0dcf Описание: Код загружен: 28 июня 2011, 07:08 (Gooddy)

  1. function ExpandFileNameCase(const FileName: string;
  2. out MatchFound: TFilenameCaseMatch): string;
  3. var
  4. SR: TSearchRec;
  5. FullPath, Name: string;
  6. Temp: Integer;
  7. FoundOne: Boolean;
  8. {$IFDEF LINUX}
  9. Scans: Byte;
  10. FirstLetter, TestLetter: string;
  11. {$ENDIF}
  12. begin
  13. Result := ExpandFileName(FileName);
  14. FullPath := ExtractFilePath(Result);
  15. Name := ExtractFileName(Result);
  16. MatchFound := mkNone;
  17.  
  18. // if FullPath is not the root directory (portable)
  19. if not SameFileName(FullPath, IncludeTrailingPathDelimiter(ExtractFileDrive(FullPath))) then
  20. begin // Does the path need case-sensitive work?
  21. Temp := FindFirst(FullPath, faAnyFile, SR);
  22. FindClose(SR); // close search before going recursive
  23. if Temp <> 0 then
  24. begin
  25. FullPath := ExcludeTrailingPathDelimiter(FullPath);
  26. FullPath := ExpandFileNameCase(FullPath, MatchFound);
  27. if MatchFound = mkNone then
  28. Exit; // if we can't find the path, we certainly can't find the file!
  29. FullPath := IncludeTrailingPathDelimiter(FullPath);
  30. end;
  31. end;
  32.  
  33. // Path is validated / adjusted. Now for the file itself
  34. try
  35. if FindFirst(FullPath + Name, faAnyFile, SR)= 0 then // exact match on filename
  36. begin
  37. if not (MatchFound in [mkSingleMatch, mkAmbiguous]) then // path might have been inexact
  38. MatchFound := mkExactMatch;
  39. Result := FullPath + SR.Name;
  40. Exit;
  41. end;
  42. finally
  43. FindClose(SR);
  44. end;
  45.  
  46. FoundOne := False; // Windows should never get to here except for file-not-found
  47.  
  48. {$IFDEF LINUX}
  49.  
  50. { Scan the directory.
  51.   To minimize the number of filenames tested, scan the directory
  52.   using upper/lowercase first letter + wildcard.
  53.   This results in two scans of the directory (particularly on Linux) but
  54.   vastly reduces the number of times we have to perform an expensive
  55.   locale-charset case-insensitive string compare. }
  56.  
  57. // First, scan for lowercase first letter
  58. FirstLetter := AnsiLowerCase(Name[1]);
  59. for Scans := 0 to 1 do
  60. begin
  61. Temp := FindFirst(FullPath + FirstLetter + '*', faAnyFile, SR);
  62. while Temp = 0 do
  63. begin
  64. if AnsiSameText(SR.Name, Name) then
  65. begin
  66. if FoundOne then
  67. begin // this is the second match
  68. MatchFound := mkAmbiguous;
  69. Exit;
  70. end
  71. else
  72. begin
  73. FoundOne := True;
  74. Result := FullPath + SR.Name;
  75. end;
  76. end;
  77. Temp := FindNext(SR);
  78. end;
  79. FindClose(SR);
  80. TestLetter := AnsiUpperCase(Name[1]);
  81. if TestLetter = FirstLetter then Break;
  82. FirstLetter := TestLetter;
  83. end;
  84. {$ENDIF}
  85.  
  86. if MatchFound <> mkAmbiguous then
  87. begin
  88. if FoundOne then
  89. MatchFound := mkSingleMatch
  90. else
  91. MatchFound := mkNone;
  92. end;
  93. end;

Ссылка на данный код:

На главную страницу сервиса обмена кодом »