kabix
asked on
Convert Delphi function to C++
I have delphi function that i used in delphi project. Now i am started to learn C++ and i was wondering how to translate it to C++ syntax?
function FindWallpaper(Dir: string; files: TStringList): String;
var
sr: TSearchRec;
ext: string;
begin
if files = nil then exit;
try
if FindFirst(dir + '\*.*', faAnyFile, sr) = 0 then
begin
if (sr.Name <> '.') and (sr.Name <> '..') then
begin
if ((sr.Attr and faDirectory) > 0) then
FindWallpaper(dir + '\' + sr.Name, files)
else
begin
ext:= ExtractFileExt(dir + '\' + sr.Name);
if (ext = '.jpg') or (ext = '.jpeg') or (ext = '.bmp') then
files.add(dir + '\' + sr.Name);
end;
end;
while (FindNext(sr) = 0) do
if (sr.Name <> '.') and (sr.Name <> '..') then
begin
if ((sr.Attr and faDirectory) > 0) then
FindWallpaper(dir + '\' + sr.Name, files)
else
begin
ext:= ExtractFileExt(dir + '\' + sr.Name);
if (ext = '.jpg') or (ext = '.jpeg') or (ext = '.bmp') then
files.add(dir + '\' + sr.Name);
end;
end;
end;
finally
findClose(sr);
end;
end;
function FindWallpaper(Dir: string; files: TStringList): String;
var
sr: TSearchRec;
ext: string;
begin
if files = nil then exit;
try
if FindFirst(dir + '\*.*', faAnyFile, sr) = 0 then
begin
if (sr.Name <> '.') and (sr.Name <> '..') then
begin
if ((sr.Attr and faDirectory) > 0) then
FindWallpaper(dir + '\' + sr.Name, files)
else
begin
ext:= ExtractFileExt(dir + '\' + sr.Name);
if (ext = '.jpg') or (ext = '.jpeg') or (ext = '.bmp') then
files.add(dir + '\' + sr.Name);
end;
end;
while (FindNext(sr) = 0) do
if (sr.Name <> '.') and (sr.Name <> '..') then
begin
if ((sr.Attr and faDirectory) > 0) then
FindWallpaper(dir + '\' + sr.Name, files)
else
begin
ext:= ExtractFileExt(dir + '\' + sr.Name);
if (ext = '.jpg') or (ext = '.jpeg') or (ext = '.bmp') then
files.add(dir + '\' + sr.Name);
end;
end;
end;
finally
findClose(sr);
end;
end;
ASKER
How do I call the function within the function because it shows an error when i call FindWallpaper within FindWallapaper?
thanks
thanks
Normally this should not be a problem. Recursion is allowed. In your upper post there is a typo. Is it in your code as well?
Otherwise, what is the error?
Otherwise, what is the error?
String literals in C use double quotes. Is the problem perhaps the concatenating of strings you are doing in the call to FindWallpaper?
dir + "\\" + sr.Name
should be the first argument. Not sure about
dir + '\' + sr.Name
What type is your first argument to FindWallpaper? string or AnsiString?
dir + "\\" + sr.Name
should be the first argument. Not sure about
dir + '\' + sr.Name
What type is your first argument to FindWallpaper? string or AnsiString?
ASKER
Ok... i think i converted to C++ syntax but when i try to compile it shows error (i use borland C++ builder):
[C++ Error] Unit1.cpp(48): E2459 VCL style classes must be constructed using operator new
it highlights the line where is FindWallpaper(name, files); line. How can I fix the error and call the function within a function?
void FindWallpaper(AnsiString Dir, TStringList files)
{
TSearchRec sr;
AnsiString ext, name;
try {
if (FindFirst(Dir + "\\*.*", faAnyFile, sr) == 0)
{
if ((sr.Name != ".") && (sr.Name != ".."))
{
if (((sr.Attr && faDirectory) > 0))
{
name = Dir + "\\" + sr.Name;
FindWallpaper(name, files);
}
else
{
ext = ExtractFileExt( Dir + "\\" + sr.Name );
if((ext == ".jpg") || (ext == ".jpeg") || (ext == ".bmp"))
files.Add(Dir + "\\" + sr.Name);
}
}
while ((FindNext(sr) == 0))
if ((sr.Name != ".") && (sr.Name != ".."))
{
if (((sr.Attr && faDirectory) > 0))
{
name = Dir + "\\" + sr.Name;
FindWallpaper(name, files);
}
else
{
ext = ExtractFileExt(Dir + "\\" + sr.Name);
if ((ext == ".jpg") || (ext == ".jpeg") || (ext == ".bmp"))
files.Add(Dir + "\\" + sr.Name);
}
}
}
}
catch(...) {
FindClose(sr);
}
}
[C++ Error] Unit1.cpp(48): E2459 VCL style classes must be constructed using operator new
it highlights the line where is FindWallpaper(name, files); line. How can I fix the error and call the function within a function?
void FindWallpaper(AnsiString Dir, TStringList files)
{
TSearchRec sr;
AnsiString ext, name;
try {
if (FindFirst(Dir + "\\*.*", faAnyFile, sr) == 0)
{
if ((sr.Name != ".") && (sr.Name != ".."))
{
if (((sr.Attr && faDirectory) > 0))
{
name = Dir + "\\" + sr.Name;
FindWallpaper(name, files);
}
else
{
ext = ExtractFileExt( Dir + "\\" + sr.Name );
if((ext == ".jpg") || (ext == ".jpeg") || (ext == ".bmp"))
files.Add(Dir + "\\" + sr.Name);
}
}
while ((FindNext(sr) == 0))
if ((sr.Name != ".") && (sr.Name != ".."))
{
if (((sr.Attr && faDirectory) > 0))
{
name = Dir + "\\" + sr.Name;
FindWallpaper(name, files);
}
else
{
ext = ExtractFileExt(Dir + "\\" + sr.Name);
if ((ext == ".jpg") || (ext == ".jpeg") || (ext == ".bmp"))
files.Add(Dir + "\\" + sr.Name);
}
}
}
}
catch(...) {
FindClose(sr);
}
}
Waht is the exact error?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
A small addition to my conversion hints:
&& is the boolean and operator and
& is the bitwise and operator.
And a comment on andrewjb
TStringList files will put the value on the stack unlike DELPHI which does everything by reference. It should work as well but you won't see anything in the list after the function has finished since only the local copy was filled with file names.
&& is the boolean and operator and
& is the bitwise and operator.
And a comment on andrewjb
TStringList files will put the value on the stack unlike DELPHI which does everything by reference. It should work as well but you won't see anything in the list after the function has finished since only the local copy was filled with file names.
SteH:
You can't use just 'TStringList files', because Borland C++ Builder doesn't let you create VCL-derived components (like TStringList) on the stack. You HAVE to create them on the heap - hence the error message.
PS... I tested the one I posted and it worked fine.
You can't use just 'TStringList files', because Borland C++ Builder doesn't let you create VCL-derived components (like TStringList) on the stack. You HAVE to create them on the heap - hence the error message.
PS... I tested the one I posted and it worked fine.
For C++ Builder you only have to change the Delphi grammar to the C++ one and perhaps look if all cases of variables and object functions are in C++ style.
begin
end
becomes
{
}
and <-> &&
or <-> ||
:= <-> =
sr: TSearchRec <-> TSearchRec sr;
and string should be replaced with AnsiString.
In the case you change the compiler it gets more complicated. Then you have to replace all calls to VCL objects with either windows API calls or use the proper MFC replacement.