CatchPreferences(*MemoryAddress)

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Julian
Enthusiast
Enthusiast
Posts: 276
Joined: Tue May 24, 2011 1:36 pm

CatchPreferences(*MemoryAddress)

Post by Julian »

Allow Preferences to be embedded, for example a language file which can be easily edited away from the source code. This would allow for reading in of multi-line strings without having to use escape characters in source code or xml and to allow for a single file release.

See http://www.purebasic.fr/english/viewtop ... =3&t=65077 for the trigger that made me think about this request.

Code: Select all

Result = CatchPreferences(?Languages)
If Result
  PreferenceGroup("English")
  BigString$ = ReadPreferenceString("BigString")
  SmallString$ = ReadPreferenceString("SmallString")
EndIf
End

DataSection
  Languages: 
    IncludeBinary "Languages.txt"
EndDataSection
Languages.txt

Code: Select all

[English]
BigString=Hello World
This is a multi line test
SmallString=Hello World
[Welsh]
BigString=OI Butt
Phlem Phlem spit spit
SmallString=OI Butt with unicode characters ‰
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: CatchPreferences(*MemoryAddress)

Post by Danilo »

Does it make any sense?

Preferences files (Human-readable configuration files for applications) are there for
the specific task to easily change some application settings - from the outside of the application.

Embedded preferences files are not easily changeable and extensionable within all operating systems.

You want easily exchange-able include files that hold language strings, and that should be already
do-able with current iterations of PureBasic.

So, instead of requesting a very special feature for your own purposes, a small change in
your own design/development configuration could probably already fit your requirements.

What are your specific requirements that totally require a fundamental change of the PureBasic programming language?
User_Russian
Addict
Addict
Posts: 1518
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: CatchPreferences(*MemoryAddress)

Post by User_Russian »

Julian
Enthusiast
Enthusiast
Posts: 276
Joined: Tue May 24, 2011 1:36 pm

Re: CatchPreferences(*MemoryAddress)

Post by Julian »

Sorry User_Russian, my search didn't see yours, I just noticed another request here too, guess my original search didn't work that well.

I guess its not going to happen any time soon then as yours was from 2012 :(

http://www.purebasic.fr/english/viewtopic.php?p=419443
Danilo wrote:What are your specific requirements that totally require a fundamental change of the PureBasic programming language?
Bit sensationalist?

I don't think that adding another command to the preference lib requires a fundamental change of the purebasic programming language, do you?

We have a perfectly usable Preference library created for us to use, however we want to use it for things outside the authors original scope because its a great general purpose way of getting data into the system. There are many ways to get a preference file to a program other than reading it off a file system (embed/web) but we want to use the same library as if the file is on the file system so we a) touch the disk b) reinvent the wheel because the base doesnt have one small feature.

Yeah, I could write a program to present a language file to a user for them to edit with a fancy UI, when they save it, it spits out xml, binary that xml onto the end of the exe and read that in ... or I could use a text file and the Preference library which would be able to read the languages in from the exe or from a text file if I wanted to touch the file system (ie. have an installed exe rather than a quick use/deploy situation from a usb stick).

/shrug guess I'll code a whole new prefs lib when I get the time
User avatar
kenmo
Addict
Addict
Posts: 2033
Joined: Tue Dec 23, 2003 3:54 am

Re: CatchPreferences(*MemoryAddress)

Post by kenmo »

Some comments:

1. The easiest workaround is to write the memory to a temp file and load it (but yes, this uses the filesystem)

Code: Select all

Procedure.i CatchPreferences(*Address, Size.i)
  Protected Result.i = #False
  If (*Address And Size >= 0)
    Protected File.s = GetTemporaryDirectory() + Str(Random(9999)) + ".ini"
    Protected FN.i = CreateFile(#PB_Any, File)
    If (FN)
      WriteData(FN, *Address, Size)
      CloseFile(FN)
      Result = OpenPreferences(File)
    EndIf
  EndIf
  ProcedureReturn (Result)
EndProcedure

If CatchPreferences(?Languages, ?Languages_End - ?Languages)
  PreferenceGroup("English")
  Debug ReadPreferenceString("BigString", "")
  Debug ReadPreferenceString("SmallString", "")
  ClosePreferences()
EndIf
End

DataSection
  Languages:
    IncludeBinary "Languages.txt"
  Languages_End:  ; need the end address of the file,
                  ; because prefs are just a text format,
                  ; (no EOF indicator or header with filesize)
                  ; so we would read past the end into next memory!
EndDataSection

2. I did write a whole new prefs lib :) and a "Catch" function could easily be added
http://www.purebasic.fr/english/viewtop ... 12&t=62236

BUT...


3. I don't think this solves your original goal. The PureBasic preferences library does NOT read multi-line unescaped strings. (Run the code above with your Languages.txt, it only prints "Hello World".

I don't know if the preferences/INI format is a good idea for storing multi-line unescaped text. Wikipedia says:
Some implementations also offer varying support for an escape character, typically with the backslash (\). Some support "line continuation", where a backslash followed immediately by EOL (end-of-line) causes the line break to be ignored, and the "logical line" to be continued on the next actual line from the INI file.
https://en.wikipedia.org/wiki/INI_file# ... characters




EDIT: Just want to add, I DO use this method in real programs! I embed a default language file in my code, write it to an INI on disk (which the user can edit, if they wish), parse it with the PB prefs commands, and use "\n" or "|" to represent linebreaks which I convert to #LF$.
Post Reply