Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2005-12

ExtractFileName function has a bug

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Graeme Geldenhuys

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 -

Reply by email

Peter Vreman
to Graeme Geldenhuys
Show quoted text 8 lines
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!

Fixed

Reply by email