Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Convert Delphi function to C++

Posted on 2003-11-11
9
Medium Priority
?
609 Views
Last Modified: 2007-12-19
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;
0
Comment
Question by:kabix
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
9 Comments
 
LVL 13

Expert Comment

by:SteH
ID: 9729679
Depending whether you stay with Borland or not.
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.

0
 
LVL 4

Author Comment

by:kabix
ID: 9732072
How do I call the function within the function because it shows an error when i call FindWallpaper within FindWallapaper?

thanks
0
 
LVL 13

Expert Comment

by:SteH
ID: 9732127
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?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:SteH
ID: 9732148
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?
0
 
LVL 4

Author Comment

by:kabix
ID: 9732299
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);
  }
}
0
 
LVL 13

Expert Comment

by:SteH
ID: 9732451
Waht is the exact error?
0
 
LVL 12

Accepted Solution

by:
andrewjb earned 100 total points
ID: 9732575
A couple of mistakes:

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);
  }
}


Which, I think were:
TStringList *, not TStringList;
use name-> not name.
and & instead of && in your attribute tests.


To use it, do something like:
  TStringList *lResults = new TStringList;

  FindWallpaper( "c:\\windows" , lResults );

  Memo1->Lines->Assign( lResults );
  delete lResults;
 

0
 
LVL 13

Expert Comment

by:SteH
ID: 9732628
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.
0
 
LVL 12

Expert Comment

by:andrewjb
ID: 9732652
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.

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question