[fpc-devel] ExtractFileName function has a bug

Graeme Geldenhuys graemeg at opensoft.homeip.net
Fri Dec 23 09:24:20 CET 2005


Hi,

Below in a program showing the problem with the ExtractFileName 
function.  It work different compared to Delphi (Test 3 fails). The FPC 
version only extracts the first 255 chars from the filename.  By 
difinition, the function should extract the complete filename part. 
Whether the filename ends up being invalid for the current OS is not its 
purpose!

I included a local version which fixes the problem.

------------------------------------------------------
program Project1;

{$mode objfpc}{$H+}

uses
   Classes, SysUtils;

{ Fixed version of ExtractFileName function }
function lExtractFileName(const FileName: string): string;
var
   i: longint;
begin
   I := LastDelimiter(PathDelim + DriveDelim, FileName);
   Result := Copy(FileName, I + 1, MaxInt);
end;

var
   name, s: string;
   i: integer;

const
   ext = '.txt';

begin
   name := '';
   for i := 1 to 251 do
     name := name + 'a';   // complete name of 255 chars

   s := ExtractFileName(name + ext);
   if Length(s) <> 255 then
     Writeln('Failed on 1');

   s := lExtractFileName(name + ext);
   if Length(s) <> 255 then
     Writeln('Failed on 2');

   name := name + 'a';     // complete name on 256 chars
   s := ExtractFileName(name + ext);
   if Length(s) <> 256 then
     Writeln('Failed on 3');

   s := lExtractFileName(name + ext);
   if Length(s) <> 256 then
     Writeln('Failed on 4');

end.
------------------------------------------------------

Regards,
   - Graeme -




More information about the fpc-devel mailing list