In an attempt to get just a filename:
procedure TSomeClass.GetFileName(AData : string) : string;
var
p : pchar;
begin
p := pchar(AData);
while not (p^ in ['/', '&', '#', ':']) do
inc(p);
result := p;
end;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There is no memory leak there. Your pointer
ppoints to a block of memory that is owned by the stringADataand so you do not need to freep. The string class manages all allocation and deallocation for you.What can happen though is that if the string does not contain at least one of those 4 characters your loop will run off the end and eventually raise an access violation. You should consider terminating the loop when it reaches a null terminator.
It’s just simpler to avoid pointers altogether:
The replicates the logic of your code, and removes the risk of access violations. However, your code was returning the portion of the string starting from, and including, the first instance of
/,&,#or:. Is that really what you wanted?