Recent

Author Topic: SOLVED Help storeing filename  (Read 726 times)

DreamVB

  • New Member
  • *
  • Posts: 21
SOLVED Help storeing filename
« on: July 03, 2022, 07:55:01 pm »
Hi, I am making a small tabbed notepad program and I not sure were to store the filename of an opened file. as the TTabsheet has a property for tag but can only store a integer.
so I was wondering how is the best way of doing it. I thought of using a string list to keep each of the opened filenames in the list but I don't know if this is effective way of doing it.

I did have the idea of maybe turning a string into a pointer I done this is C++ but not sure how to do it in lazarus then store that pointer in the tag and then to get the string I need a way to convert the pointer back to a string.

anyway I hope you understand what I am trying to archive and any help will be great full

Thank you,
« Last Edit: July 03, 2022, 10:47:41 pm by DreamVB »
When your dreams come true

MarkMLl

  • Hero Member
  • *****
  • Posts: 4755
Re: Help storeing filename
« Reply #1 on: July 03, 2022, 10:28:12 pm »
Hi, I am making a small tabbed notepad program and I not sure were to store the filename of an opened file. as the TTabsheet has a property for tag but can only store a integer.
so I was wondering how is the best way of doing it. I thought of using a string list to keep each of the opened filenames in the list but I don't know if this is effective way of doing it.

A stringlist with the same number of entries as the tabsheet would seem ideal. Otherwise a dynamic array of strings.

Quote
I did have the idea of maybe turning a string into a pointer I done this is C++ but not sure how to do it in lazarus then store that pointer in the tag and then to get the string I need a way to convert the pointer back to a string.

That sort of thing is a bad idea in C++ and a worse idea in a well-designed language :-)

Seriously: neither necessary nor wise. At the application level I use an explicit pointer every five years or so.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Bart

  • Hero Member
  • *****
  • Posts: 4725
    • Bart en Mariska's Webstek
Re: Help storeing filename
« Reply #2 on: July 03, 2022, 10:46:19 pm »
I suppose that on the TTabSheet you have a component fort the actual editing of that file.
My notepad replacement has a TSynEdit derived component on each TTabSheet.
In the derived class I added a field for the filename.
e.g.
Code: Pascal  [Select][+][-]
  1. type
  2.   TEditor = class(TSynEdit)
  3.   private
  4.     FFileName: String;
  5.   public
  6.     property FileName: string read FFileName write FFileName;
  7.   end;
I store the filename in the TEditor and display the filename (without path) in the Tab of the TTabSheet.

Bart

DreamVB

  • New Member
  • *
  • Posts: 21
Re: Help storeing filename
« Reply #3 on: July 03, 2022, 10:47:21 pm »
Hi thanks for the info MarkMLl

I did sort of find a way around it, what I did was store the filename in the TabSheets hint property seems to work. thanks again
When your dreams come true

PascalDragon

  • Hero Member
  • *****
  • Posts: 4303
  • Compiler Developer
Re: Help storeing filename
« Reply #4 on: July 04, 2022, 01:40:41 pm »
Quote
I did have the idea of maybe turning a string into a pointer I done this is C++ but not sure how to do it in lazarus then store that pointer in the tag and then to get the string I need a way to convert the pointer back to a string.

That sort of thing is a bad idea in C++ and a worse idea in a well-designed language :-)

Seriously: neither necessary nor wise. At the application level I use an explicit pointer every five years or so.

In fact that is the purpose of the Tag property, that's why it's a PtrInt after all.

@DreamVB: the important point when you store something in the Tag property is that you need to clean up correctly. So if you'd store a string it would be like this:

Code: Pascal  [Select][+][-]
  1. // storing:
  2. // tmp is of type PtrInt
  3. PAnsiString(tmp)^ := filename;
  4. tabsheet.Tag := tmp;
  5.  
  6. // retrieving:
  7. filename := PAnsiString(tabsheet.Tag);
  8.  
  9. // deleting:
  10. tmp := tabsheet.Tag;
  11. tabsheet.Tag := 0;
  12. PAnsiString(tmp)^ := '';

Similar if you use a class instance or a pointer to a record as value for the Tag (if you need to store more than one piece of information).

I did sort of find a way around it, what I did was store the filename in the TabSheets hint property seems to work. thanks again

Which will only work until you need to display a different hint for the tab sheet...

MarkMLl

  • Hero Member
  • *****
  • Posts: 4755
Re: Help storeing filename
« Reply #5 on: July 04, 2022, 01:56:50 pm »
In fact that is the purpose of the Tag property, that's why it's a PtrInt after all.

Didn't used to be: Delphi defined it as a straight integer of indeterminate size. And not even as an unsigned, which would have been reasonable if they anticipated people putting pointers in that field.

In any event, it would be better to put a reference to an object into it, since at that point type can be checked at runtime.

Which I suppose raises the interesting question of what the lowest-overhead object is which can store a single string.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

DreamVB

  • New Member
  • *
  • Posts: 21
Re: SOLVED Help storeing filename
« Reply #6 on: July 04, 2022, 05:50:52 pm »
Thanks PascalDragon I try that
When your dreams come true

PascalDragon

  • Hero Member
  • *****
  • Posts: 4303
  • Compiler Developer
Re: Help storeing filename
« Reply #7 on: July 05, 2022, 02:13:37 pm »
In fact that is the purpose of the Tag property, that's why it's a PtrInt after all.

Didn't used to be: Delphi defined it as a straight integer of indeterminate size. And not even as an unsigned, which would have been reasonable if they anticipated people putting pointers in that field.

In FPC it originally wasn't either, but that was already changed around a decade ago to prepare for Delphi 64-bit compatibility which changed it from LongInt to NativeInt (also for a pointer it's irrelevant whether it's stored as a signed or unsigned ordinal as long as the bit width is correct, cause it needs to be cast anyway).

Also please note that Delphi's documentation explicitly mentions that Tag is often used for pointer values (emphasis mine):

Quote
Tag has no predefined meaning. The Tag property can store any additional integer value for the convenience of developers. Often, Tag stores a pointer. A Tag value can be typecast to the appropriate pointer type. Notice that on 64-bit platforms, all pointer types are 8 bytes in size, while on 32-bit platforms, pointer types are 4 bytes. These pointer sizes correspond to sizes of NativeInt integral values on 64-bit and 32-bit platforms.

Which I suppose raises the interesting question of what the lowest-overhead object is which can store a single string.

Depends: if you know it's less than 4 characters it would be a UInt32 or Int32 (e.g. for ACPI device names) (in MacPas mode FPC supports assigning 4 character strings to 32-bit ordinals ;) ), otherwise it would probably be PChar, followed by ShortString (with the obvious restriction in length), then a dynamic array of Char and then AnsiString.

MarkMLl

  • Hero Member
  • *****
  • Posts: 4755
Re: Help storeing filename
« Reply #8 on: July 05, 2022, 03:05:09 pm »
Which I suppose raises the interesting question of what the lowest-overhead object is which can store a single string.

Depends: if you know it's less than 4 characters it would be a UInt32 or Int32 (e.g. for ACPI device names) (in MacPas mode FPC supports assigning 4 character strings to 32-bit ordinals ;) ), otherwise it would probably be PChar, followed by ShortString (with the obvious restriction in length), then a dynamic array of Char and then AnsiString.

Thanks for that but I did specifically say object, i.e. something whose type/class can be tested at runtime which I presume isn't possible for a dynamic array or an ansistring.

Superficially one would assume that a StringList with a single entry is overkill, but that depends on how much per-instance space is allocated and on whether anything time-consuming is done during creation.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

PascalDragon

  • Hero Member
  • *****
  • Posts: 4303
  • Compiler Developer
Re: Help storeing filename
« Reply #9 on: July 06, 2022, 01:28:48 pm »
Which I suppose raises the interesting question of what the lowest-overhead object is which can store a single string.

Depends: if you know it's less than 4 characters it would be a UInt32 or Int32 (e.g. for ACPI device names) (in MacPas mode FPC supports assigning 4 character strings to 32-bit ordinals ;) ), otherwise it would probably be PChar, followed by ShortString (with the obvious restriction in length), then a dynamic array of Char and then AnsiString.

Thanks for that but I did specifically say object, i.e. something whose type/class can be tested at runtime which I presume isn't possible for a dynamic array or an ansistring.

“Object” is a rather overloaded world in the Pascal world. Aside from an object instance it might mean TP-style object or - as I had interpreted it - simply a collective name for something that represents something.

Please note that you can't reliably detect however the case that the value is not 0, but also not a valid object (e.g. store 1 into the Tag and try to handle it as a TObject-descendant).

Superficially one would assume that a StringList with a single entry is overkill, but that depends on how much per-instance space is allocated and on whether anything time-consuming is done during creation.

If you want to go with a TObject-descendant then a direct descendant of TObject with a String field is the best bet (and some do in fact use the Tag property like this)

MarkMLl

  • Hero Member
  • *****
  • Posts: 4755
Re: Help storeing filename
« Reply #10 on: July 06, 2022, 02:14:25 pm »
“Object” is a rather overloaded world in the Pascal world. Aside from an object instance it might mean TP-style object or - as I had interpreted it - simply a collective name for something that represents something.

My apologies, I generally try to avoid the word instead favouring something like item/entity vs instance.

Quote
Please note that you can't reliably detect however the case that the value is not 0, but also not a valid object (e.g. store 1 into the Tag and try to handle it as a TObject-descendant).

Although the misalignment should be a strong hint. That's actually how classic Smalltalk distinguished between objects (their term :-) and integers: whether the LSB was set; and I think I've noticed hardware support in recent x86 to specifically make that check.

Quote
If you want to go with a TObject-descendant then a direct descendant of TObject with a String field is the best bet (and some do in fact use the Tag property like this)

Thanks, that's what I was thinking (but I was interested in case I was overlooking something).

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

PascalDragon

  • Hero Member
  • *****
  • Posts: 4303
  • Compiler Developer
Re: Help storeing filename
« Reply #11 on: July 07, 2022, 01:54:02 pm »
Quote
Please note that you can't reliably detect however the case that the value is not 0, but also not a valid object (e.g. store 1 into the Tag and try to handle it as a TObject-descendant).

Although the misalignment should be a strong hint. That's actually how classic Smalltalk distinguished between objects (their term :-) and integers: whether the LSB was set; and I think I've noticed hardware support in recent x86 to specifically make that check.

In principle nothing forbids the heap manager to allocate its memory 1-Byte aligned and thus you could have a class instance that starts at an odd address. And even then you'd only detect this every second value. ;)

MarkMLl

  • Hero Member
  • *****
  • Posts: 4755
Re: Help storeing filename
« Reply #12 on: July 07, 2022, 02:07:46 pm »
In principle nothing forbids the heap manager to allocate its memory 1-Byte aligned and thus you could have a class instance that starts at an odd address. And even then you'd only detect this every second value. ;)

Which is why I went no stronger than "hint", and why I remain a fan of tagged or descriptor-based architectures. It will be interesting to see whether CHERI takes off https://www.cl.cam.ac.uk/research/security/ctsrd/cheri/

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018