Forum > General

TMemoryStreamUTF8 cannot LoadFromFile

(1/9) > >>

martinrame:
Hi, I need to load a file whose name contains accented characters, such as:

"/mnt/Informes/202205/PEÑALOZA JORGE.pdf"

The fpc program is a CGI, which runs in a FreeBSD system. The files reside in a Windows server mounted on /mnt/Informes on the FreeBSD system.

The filename is returned from a database query, and I store it in a string, such as:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---lFile := lQuery.FieldByName('filename').AsString;
Then, I check if the file exists:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---if FileExists(lFile) thenbegin...end;
If the file exists I do this:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---lPdfStream := TMemoryStreamUTF8.Create;lPdfStream.LoadFromFile(lFile);... 
The file exists, but when I call LoadFromFile I get "Unable to open file ...".

What can I do to open it?

martinrame:
I also tried with a TFileStream (and TFileStreamUTF8), with the same results.

martinrame:
I created a simple program to traverse all files in the directory and got the names with "ñ" where replaced with "??":


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure AddAllFilesInDir(const Dir: string);var  SR: TSearchRec;begin  if FindFirst(IncludeTrailingBackslash(Dir) + '*.*', faAnyFile or faDirectory, SR) = 0 then    try      repeat        if (SR.Attr and faDirectory) = 0 then          ListBox1.Items.Add(SR.Name)        else if (SR.Name <> '.') and (SR.Name <> '..') then          AddAllFilesInDir(IncludeTrailingBackslash(Dir) + SR.Name);  // recursive call!      until FindNext(Sr) <> 0;    finally      FindClose(SR);    end;end;
This is the result:


--- Quote ---..
PERALTA ANDRES.pdf
PE??ALOZA JORGE.pdf
..
--- End quote ---

Gustavo 'Gus' Carreno:
Hey Martin,

Let me start by saying that this is not an answer to your problem, it's more of pointing you in a direction so that you can solve your problem.

With that said, if you look at this page: FindFirst, you'll find that there are 2 overloads for the same function.

One uses a UnicodeString and the other uses a RawByteString. And both return a different type of SearchRec.

Why can't I help you more? Well, that's because I've never dug deep into the magic that is the transformation the type String gets involved in when it's passed around with UnicodeString, WideString, UTF8String and all the others that I'm not aware of.

Because of that the issue could be anywhere since you're passing around a string with UTF content, but some of the LCL objects don't really know how to deal with that.

And good luck trying to identify what type of string it is. And how or where it gets mangled. And if you're planing on cross platform, if you get it running on Windows, then nothing guarantees you it will work on Linux due mainly to the OS, not the FCL or LCL.

So yeah, while it's a pain, and believe me, I'm Portuguese and we have to deal with the same issues, it's something that you'll have to dig around and see what type of string is being returned an then do stuff accordingly.

Unfortunately, you'll have to wait for the people that have more experience with UTF containing strings, cuz I'm pretty crap at it myself, and for that I apologise profusely.

Cheers,
Gus

PascalDragon:

--- Quote from: martinrame on June 13, 2022, 11:02:05 pm ---The fpc program is a CGI, which runs in a FreeBSD system.
--- End quote ---

Make sure that your main program uses either cwstring or fpwidestring. If that's not enough please provide the output of the locale (at least I hope that also exists on FreeBSD).

Navigation

[0] Message Index

[#] Next page

Go to full version