## [TFindFile] Request for Unicode version

Please post bug reports, feature requests, or any question regarding the DELPHI AREA projects here.

### [TFindFile] Request for Unicode version

I want to request an unicode version of TFindFile.

Little description:
I made an aplication with TTntEdit (from TntWare Delphi Unicode Controls) using Delphi 7. The TTntEdit is used for inputting the directory path. This is the piece of code to connect the TTnTEdit to the TFindFile component:

Code: Select all
// txtFolder1 is the TTntEdit and assume the property Text contains unicode charsFindFile1.Criteria.Files.Location := txtFolder1.Text;FindFile1.Execute;...

The event "FindFile1FileMatch" is not called at all since the Text contain unicode. So I hope you can make the unicode version of TFindFile since I'm stuck at here.

Btw, why the registration activation took such a long time? I thought I was rejected.
avushoi
Active Member

Posts: 8
Joined: March 1st, 2009, 5:07 pm

### Re: [TFindFile] Request for Unicode version

Because the component is already updated for Delphi 2009, only substituting Char to WideChar, String to WideString, and TStringList to TntStringList makes it Unicode ready.

I did the substitutions but didn't test it. Please give me a favor and inform me about the result.
Attachments
FindFileW.pas
Unicode version of TFindFile
Kambiz

Kambiz

Posts: 2430
Joined: March 7th, 2003, 7:10 pm

### Re: [TFindFile] Request for Unicode version

Regarding registration, the delay is because of spammers. Sorry for the inconvenience.
Kambiz

Kambiz

Posts: 2430
Joined: March 7th, 2003, 7:10 pm

### Re: [TFindFile] Request for Unicode version

ok, I've downloaded the code. I will try it and inform you later.

avushoi
Active Member

Posts: 8
Joined: March 1st, 2009, 5:07 pm

### Re: [TFindFile] Request for Unicode version

I guess it is two or three years younger than me.
Kambiz

Kambiz

Posts: 2430
Joined: March 7th, 2003, 7:10 pm

### Re: [TFindFile] Request for Unicode version

ok, I had done my software with TFindFileW. Whew...

However, your FindFileW.pas is stil lack of some changes.
Around line 991:
Code: Select all
  Result := ExpandUNCFileName(Trim(Result));  {$IFDEF COMPILER7_UP} Result := IncludeTrailingPathDelimiter(Result); {$ELSE}  Result := IncludeTrailingBackslash(Result);

The corrections:
IncludeTrailingPathDelimiter -> WideIncludeTrailingPathDelimiter
IncludeTrailingBackslash -> WideIncludeTrailingBackslash

But for "ExpandUNCFileName", I couldn't find the TTnt version, so I change it with "WideExpandFileName" for temporary. I' was thinking to create the unicode version of "ExpandUNCFileName" from delphi source code, but it's too hard, the code involves some windows network API functions. Could you help me with this?

I also replaced all words of "TFindFile" to "TFindFileW". Especially in this line:
Code: Select all
RegisterComponents('Delphi Area', [TFindFile]);

so I can have 2 TFindFile in the pallete.

My modified FindFileW.pas is in attachment.

I have some questions:
* The "Threaded" property of TFindFile, if I set it to true, does it make the finding process more faster?

* I found out that some delphi 7 functions from system & sysutils unit such as: Delete, Copy, StrToIntDef, etc, can accept widestring whereas the delphi 7 help doesn't mention the functions support widestring input. Could you give me explanation about this?

* The unicode text couldn't displayed correctly when I use function ShowMessage, where MessageBoxW could. The unicode char was showed as question mark, i.e. C:\???\. I saw in the delphi 7 documentation, the function showmessage has the overloaded version that accept widestring. I've already tried to type-cast the input, i.e.: Showmessage(WideString(str)), but still no avail. Do you know what's the cause?

Keep up the good work.
Attachments
TFindFileW.zip
avushoi
Active Member

Posts: 8
Joined: March 1st, 2009, 5:07 pm

### Re: [TFindFile] Request for Unicode version

Here you are.

• Threaded search is a bit slower.
• Some functions like Delete and Copy which work on both arrays and strings works on widestrings too.
Some functions like StrToInt only accepts AnsiStrings and if you pass a WideString to them, the compiler implicitly converts it to AnsiString. But the result is the same because for example Latin digits are Latin digits anyway.
If any other function accepts Unicode strings, in the help mentioned that "This function works for multi-byte character systems (MBCS)".
• You have to choose a font that supports Unicode characters. Characters missing in a font will be displayed as question mark.
Attachments
FindFileW.zip
Kambiz

Kambiz

Posts: 2430
Joined: March 7th, 2003, 7:10 pm

### Re: [TFindFile] Request for Unicode version

Thx, I will inspect your new source code.

You have to choose a font that supports Unicode characters. Characters missing in a font will be displayed as question mark.

How to choose a font?
Why font in ShowMessage is different with the font in MessageBox?

I create these 2 functions to calculate file size from a file path given input. Between them, which is more faster and best to use? Do you know other best alternative for this function?
Code: Select all
function GetSizeOfFile(const FileName: string): Int64;var  Handle: THandle;  FindData: TWin32FindData;  Size: TULargeInteger;begin  Result := 0;  Handle := FindFirstFile(PChar(FileName), FindData);  if Handle <> INVALID_HANDLE_VALUE then  begin    Windows.FindClose(Handle);    Size.LowPart := FindData.nFileSizeLow;    Size.HighPart := FindData.nFileSizeHigh;    Result := Size.QuadPart;  end  else    RaiseLastOSError;end;function GetSizeOfFile2(const FileName: string): Int64;var  Handle: THandle;  Size: TULargeInteger;begin  Result := 0;  Handle := CreateFile(PChar(FileName), GENERIC_READ, FILE_SHARE_READ, nil,    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);  if Handle <> INVALID_HANDLE_VALUE then begin    Size.LowPart := GetFileSize(Handle, @Size.HighPart);    Result := Size.QuadPart;    CloseHandle(Handle);  end else    RaiseLastOSError;end;

Is it right to use RaiseLastOSError instead of returning error code?
If there are some things wrong in my code, don't hesitate to tell me.

Thx.
avushoi
Active Member

Posts: 8
Joined: March 1st, 2009, 5:07 pm

### Re: [TFindFile] Request for Unicode version

MessageBox is a Windows API but ShowMessage is a Delphi function that displays a dialog with one TLabel and TButton on it. None of the label and the button are Unicode enabled and font is the default Delphi font which is 'Ms Sans Serif'. I personally write my own ShowMessage function.

The second function seems to be faster but you don't know until you run a test.

Choosing between raising an exception or returning an error code highly depends on your coding logic. Sometime an exception is more convenient and sometimes an error code. But in case of getting file size, I prefer to return -1 on error because if GetFileSize fails too, it returns -1.
Kambiz

Kambiz

Posts: 2430
Joined: March 7th, 2003, 7:10 pm

### Re: [TFindFile] Request for Unicode version

Thx for the help.

I've made a little modifications in your code. I removed the function "WideExpandFileName" since it's already exist in TTnt code. Then I changed the name "GetUniversalName" to "WideGetUniversalName".

I've some questions regarding the code:
What are the function "InitFastContentSearch" and "WildcardMatches" used for? Could you explain the "Character Map" technique?

What is the purpose of these lines:
Code: Select all
{$IFNDEF COMPILER6_UP}uses Forms;{$ENDIF}

If the TFindFile is updated, would you update the TFindFileW too?
Attachments
FindFileW.pas
avushoi
Active Member

Posts: 8
Joined: March 1st, 2009, 5:07 pm

### Re: [TFindFile] Request for Unicode version

Thank you so much for your help. I will keep FindFileW updated.

WildcardMatches is used for comparing file paths against filters (Criteria.Files.Filters property).

For case insensitive comparison, instead of converting each character to lowercase (or uppercase), I use an array for mapping characters to their lowercase.

Of course there are six kind of such arrays. These are for case sensitive, case insenstive, and whole word searches on Ansi and Unicode contents.

Code: Select all
// These arrays map each character to itself for case sensitive searchAnsiCharMap: array [AnsiChar] of AnsiChar;       WideCharMap: array [WideChar] of WideChar;// These arrays map each character to its lowercase for case insensitive searchAnsiLowerCharMap: array [AnsiChar] of AnsiChar;  WideLowerCharMap: array [WideChar] of WideChar;// These arrays map alphanumeric characters to false and other characters to true, used for whole word searchAnsiIsDelimiter: array[AnsiChar] of Boolean;WideIsDelimiter: array[WideChar] of Boolean;

When a case sensitive search is requested, I pass AnsiLowerCharMap/WideLowerCharMap to the search function. Otherwise, I pass AnsiCharMap/WideCharMap to the search function. So, it doesn't matter which kind of search you choose, the search speed is the same.

The InitFastContentSearch procedure initializes the above arrays with the appropriate values once during application lifetime.

I hope I could describe the technique.
Kambiz

Kambiz

Posts: 2430
Joined: March 7th, 2003, 7:10 pm

### Re: [TFindFile] Request for Unicode version

Code: Select all
{$IFNDEF COMPILER6_UP}uses Forms;{$ENDIF}

In Delphi 5 and earlier, AllocateWnd and DeallocateWnd are declared in Forms unit. However, in the newer versions of Delphi these declarations has been moved to Classes unit.
Kambiz

Kambiz

Posts: 2430
Joined: March 7th, 2003, 7:10 pm

### Re: [TFindFile] Request for Unicode version

What's the purpose & benefit of using the character mapping? If I don't use character mapping, what's the common technique people use?

Some questions again:
* Do you know another good unicode delphi component unit besides TntUnicodeControls? Do you use TntUnicodeControls too in your application?

I forget to tell you that I'm using TntUnicodeControls v2.3.0 (old freeware). The latest version (now shareware) might already have defined "WideExpandUNCFileName".

I'm thinking that isn't it better if FindFileW depends on a free-and-still-updated unicode library? Or maybe better if the "WideXXX" functions are written into FindFileW.pas to lessen dependency to 3rd party unit file or make a unit file to hold the unicode functions and included by FindFileW.pas.

* Do you ever use delphi 2009? Is Delphi 2009 better than Delphi 7? If so, do you think that I should switch to Delphi 2009?

It's me that should thank to you.
You're very kind to lend your little time to help me.
Btw, I like this smily:
avushoi
Active Member

Posts: 8
Joined: March 1st, 2009, 5:07 pm

### Re: [TFindFile] Request for Unicode version

By using character mapping we do case insensitive character comparison as:

Code: Select all
if LowerCharMap[C1] = LowerCharMap[C2] then  // do something

without it we have to use something like:

Code: Select all
if UpCase(C1) = UpCase[C2] then  // do something

The first method is more efficient.

Delphi 2009 is the best Delphi ever but I stuck to Delphi 7 and TntUnicodeControls v2.3.0 because it is cheaper to use what already we have.

There is also another Unicode library which is just a single unit written by Mike Lischke. Because it's free and simple, I am going to use it for FindFileW.
Kambiz

Kambiz

Posts: 2430
Joined: March 7th, 2003, 7:10 pm

### Re: [TFindFile] Request for Unicode version

Once again, thx for your replies.

Delphi 2009 is the best Delphi ever but I stuck to Delphi 7 and TntUnicodeControls v2.3.0 because it is cheaper to use what already we have.

Do you plan to replace the delphi 7 with delphi 2009 in the future?

There is also another Unicode library which is just a single unit written by Mike Lischke. Because it's free and simple, I am going to use it for FindFileW.

Good decision.

Are you going to include the FindFileW with FindFile together or separated?
Which page that I should check for the later FindFileW release?

I have a question below that not related to programming, you don't need to answer it if you don't want to.
Are you really iranian? Because you looks like mexican in your avatar.
avushoi
Active Member

Posts: 8
Joined: March 1st, 2009, 5:07 pm

Next