Recent

Author Topic: Strange filename from OpenDialog  (Read 7897 times)

dkjMusic

  • Full Member
  • ***
  • Posts: 146
Strange filename from OpenDialog
« on: December 29, 2010, 07:10:23 pm »
I'm using the following Lazarus code (on Linux):

     with OpenDialog1 do begin
          InitialDir := LastDir;
          DefaultExt := 'rev';
          Filter := '*.rev';
     end;
     ProjectFileName := '';
     if OpenDialog1.Execute then ProjectFileName := OpenDialog1.Filename;
     Showmessage(ProjectFileName);
     if ProjectFileName = '' then exit;

The Showmessage displays the filename selected in the dialog correctly, e.g., home/Dennis/Dropbox/...,
but a breakpoint after the last statement shows ProjectFileName (a global String) as
   0xb7f8f7a8'home/Dennis/Dropbox/...'
and the hex-stuff in the filename causes problems in the code that follows.

Why is the 0xb7f8f7a8 appearing and how do I get rid of it?
Windows 7 Ultimate/32 bit;
Lazarus 0.9.31/Rev 33300;
FPC 2.7.1/Rev 19464;
GDB 7.0.50;
(via CodeTyphon 2.30)

Laksen

  • Hero Member
  • *****
  • Posts: 794
    • J-Software
Re: Strange filename from OpenDialog
« Reply #1 on: December 29, 2010, 07:43:16 pm »
I don't know what it could be without more information. What's the code that follows?

dkjMusic

  • Full Member
  • ***
  • Posts: 146
Re: Strange filename from OpenDialog
« Reply #2 on: December 29, 2010, 08:01:58 pm »
I don't know what it could be without more information. What's the code that follows?

Ok. Here's the whole shebang:

procedure TForm1.Menu_File_Open_ItemClick(Sender: TObject);
var
   ProjectFile: TIniFile;
   ChecklistFile: String;
   Ans: Integer;
begin
     with OpenDialog1 do begin
          InitialDir := LastDir;
          DefaultExt := 'rev';
          Filter := '*.rev';
     end;
     ProjectFileName := '';
     if OpenDialog1.Execute then ProjectFileName := OpenDialog1.Filename;
     Showmessage(ProjectFileName);
     if ProjectFileName = '' then exit;
     ChecklistFile :='';
     LastDir := ExtractFileDir(ProjectFileName);
     ProjectFile := TIniFile.Create(ProjectFileName);
     try
        ProjectFile.ReadString('Compound','ID', CompoundID);
        ProjectFile.ReadString('Compound','Name', CompoundName);
        ProjectFile.ReadString('Checklist','File', ChecklistFile);
        ChecklistSynEdit.Lines.LoadfromFile(ChecklistFile);
        ChecklistSynEdit.Visible := True;
        ProjectIsOpen := True;
        ProjectHasChanged := False;
        UpdateMenus();
     finally
        ProjectFile.Free;
     end;
end;

The problem arises that with the hex-ified filename, TIniFile.Create does not open the specified file, which already exists, then ProjectFile.ReadString('Checklist','File', ChecklistFile) reads in a blank ChecklistFile (which is a file name), and then ChecklistSynEdit.Lines.LoadfromFile(ChecklistFile) bombs with '' for ChecklistFile.
Windows 7 Ultimate/32 bit;
Lazarus 0.9.31/Rev 33300;
FPC 2.7.1/Rev 19464;
GDB 7.0.50;
(via CodeTyphon 2.30)

BlueIcaro

  • Hero Member
  • *****
  • Posts: 819
    • Blog personal
Re: Strange filename from OpenDialog
« Reply #3 on: December 29, 2010, 09:41:42 pm »
Quote
hex-ified filename

I don't know this.

TInifile, only use files in ascii format

BTW. What  version of Lazarus do you use?

/BlueIcaro

dkjMusic

  • Full Member
  • ***
  • Posts: 146
Re: Strange filename from OpenDialog
« Reply #4 on: December 29, 2010, 10:11:19 pm »
Quote
hex-ified filename

I don't know this.

TInifile, only use files in ascii format

BTW. What  version of Lazarus do you use?

/BlueIcaro

 :)"hex-ified filename" was a term I made up to try to describe what I see when I hover over the variable ProjectFileName after execution is paused by a breakpoint, i.e.,
0xb7f8f7a8'home/Dennis/Dropbox/...', that is, the correct file name as text preceded by some hexadecimal gobbledygook that the statement
  ProjectFile := TIniFile.Create(ProjectFileName)
can't deal with correctly, that is, open the existing ini file named 'home/Dennis/Dropbox/...'  .

I'm using Lazarus v0.9.28.2 in Ubuntu 10.10.

Btw, I tried TIniFile.Create(UTF8ToSys(OpenDialog1.Filename)) but got the same behavior.

If worse comes to worse, I'll just load the ini file into a StringList and parse it myself.
Windows 7 Ultimate/32 bit;
Lazarus 0.9.31/Rev 33300;
FPC 2.7.1/Rev 19464;
GDB 7.0.50;
(via CodeTyphon 2.30)

Bart

  • Hero Member
  • *****
  • Posts: 5564
    • Bart en Mariska's Webstek
Re: Strange filename from OpenDialog
« Reply #5 on: December 29, 2010, 11:41:23 pm »
Maybe only a debugger issue? (showing the leading bytes of the AnsiString?)
If you write the filename to a textfile, then do a hexdump of it, do you also see the "gobbledygook"?
Is Length(OpenDialog.Filename) what you expect it to be (or UTF8Length() if your filename has any character not in the lower ASCII range)?

Bart

Laksen

  • Hero Member
  • *****
  • Posts: 794
    • J-Software
Re: Strange filename from OpenDialog
« Reply #6 on: December 30, 2010, 01:06:27 am »
The problem is not the hex string in front of. It could be the address of the string. TIniFile internally uses TStringList

The problem is that you call ReadString wrong :) The third parameter is the default value to return if the value is not found. It's a function

Use ChecklistFile := ProjectFile.ReadString('Checklist','File', '');

dkjMusic

  • Full Member
  • ***
  • Posts: 146
Re: Strange filename from OpenDialog
« Reply #7 on: December 30, 2010, 01:25:13 pm »
The problem is not the hex string in front of. It could be the address of the string. TIniFile internally uses TStringList

The problem is that you call ReadString wrong :) The third parameter is the default value to return if the value is not found. It's a function

Use ChecklistFile := ProjectFile.ReadString('Checklist','File', '');

You're exactly right. I woke up last night and realised that was my problem. Maybe I should sleep on things before posting. ;) I had copied/pasted/edited the ProjectFile.WriteString's that I had used to create the file. Nothing like chasing the wrong rabbit, eh?
Windows 7 Ultimate/32 bit;
Lazarus 0.9.31/Rev 33300;
FPC 2.7.1/Rev 19464;
GDB 7.0.50;
(via CodeTyphon 2.30)

 

TinyPortal © 2005-2018