Dynamic ReadPreferenceString String Length...

Just starting out? Need help? Post your questions and find answers here.
User avatar
Teddy Rogers
User
User
Posts: 92
Joined: Sun Feb 23, 2014 2:05 am
Location: Australia
Contact:

Dynamic ReadPreferenceString String Length...

Post by Teddy Rogers »

Is there a max string length for ReadPreferenceString? Is it possible to work out the string length before ReadPreferenceString, without having to manually count string length ahead, so that I can dynamically allocate required string size/space?

I can opt to either use a predefined size, though it isn't very dynamic if the text is longer then the constant value. The advantage is if ReadPreferenceString is longer than the constant value there isn't a memory overflow...

Code: Select all

Procedure.s StringTest1(nKey.s, nText.s)
  
  text.s{5} = ReadPreferenceString(nKey.s, nText.s)
  
  ProcedureReturn text.s
EndProcedure

Debug StringTest1("TestKey", "1234567890")
Second option is to allocate memory and hope ReadPreferenceString does not return a string that is greater than the allocated memory and cause an overflow...

Code: Select all

Procedure.s StringTest2(nKey.s, nText.s)
  
  *StringMem = AllocateMemory(StringByteLength(nText.s) + SizeOf(Character))
  
  If *StringMem
    PokeS(*StringMem, ReadPreferenceString(nKey.s, nText.s))
    ;Debug MemoryStringLength(*StringMem)
    text.s = PeekS(*StringMem)
  EndIf
  
  FreeMemory(*StringMem)
  
  ProcedureReturn text.s
EndProcedure

Debug StringTest2("TestKey", "1234567890")
Ted.
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: Dynamic ReadPreferenceString String Length...

Post by kenmo »

Sorry, I don't understand your goal... What is wrong with:

Code: Select all

Debug ReadPreferenceString("TestKey", "")
It's already dynamically sized. The second parameter is the default text if the key is missing, it's not used for sizing your output string.

No need for AllocateMemory() or PeekS() or {FixedLengthStrings}.
User avatar
Teddy Rogers
User
User
Posts: 92
Joined: Sun Feb 23, 2014 2:05 am
Location: Australia
Contact:

Re: Dynamic ReadPreferenceString String Length...

Post by Teddy Rogers »

Apologies for not being clear. If I have a preference file that can be edited by the user with a new custom key value the result from ReadPreferenceString is of an unknown string length. Where I will be using some of the strings they need to be, at some point limited to a maximum string length. I don't want ReadPreferenceString to read any more from the string value than is needed. I can simply opt to allow ReadPreferenceString to get the entire string, whatever that length may be, and choose to use x length from that string.

My question is in two parts. One, is there a dynamic way to define/read a maximum string length? In my perfect programming world ReadPreferenceString would look something like this;

Code: Select all

Result$ = ReadPreferenceString(Key$, DefaultValue$, Length)
The second question was to know if there is a maximum string length ReadPreferenceString reads? I haven't purposely tried or tested to find if there is a limit or if at some point it reaches an overflow and crashes my program, I was hoping someone here would already know this...

Ted.
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: Dynamic ReadPreferenceString String Length...

Post by kenmo »

OK, I thought you misunderstood the second parameter.

Short answer is no, you can't directly limit ReadPreferenceString().
But you can indirectly do this:

Code: Select all

Result$ = Left(ReadPreferenceString(Key$, DefaultValue$), Length)
The maximum length of ReadPreferenceString() is probably the max length of any PB string, which is "unlimited" (limited by how much memory your system will allocate to it).
Maybe someone can confirm this??

If you're worried about your user's files containing enormous, problem-causing strings, maybe you shouldn't use the simple Preference() functions, and parse the file manually, so you can limit it however you want.
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Dynamic ReadPreferenceString String Length...

Post by Demivec »

Teddy Rogers wrote:The second question was to know if there is a maximum string length ReadPreferenceString reads? I haven't purposely tried or tested to find if there is a limit or if at some point it reaches an overflow and crashes my program, I was hoping someone here would already know this...
You could check the size of the preference file. That would tell you the maximum possible size of any string contained in the file without having to parse the contents.
Post Reply