Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Organini - simple module for INI files

  1. #1
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Lightbulb Organini - simple module for INI files

    Hi,

    we all know the classic INI module in ThinBASIC. It follows patterns from other languages, and is quite straightforward:
    Uses "Ini"
    
    ' -- Declare new variable
    $myFile = APP_SourcePath + "settings2.ini" 
    
    INI_SetKey($myFile, "Screen", "Width", 1920)
    INI_SetKey($myFile, "Screen", "Height", 1080)
    INI_SetKey($myFile, "Screen", "Depth", 32)
    
    INI_SetKey($myFile, "User", "Name", "Petr")
    INI_SetKey($myFile, "User", "Password", "1234")
    
    ' -- Display the data    
    Long xRes = INI_GetKey($myFile, "Screen", "Width", "")
    Long yRes = INI_GetKey($myFile, "Screen", "Height", "")
    
    MsgBox 0, xRes + "x" + yRes
    
    Code looks okay, but writing it makes you break your fingers - repeating the functions, then specifying file for each call, then explicit reading...

    What about having settings done more naturally? I tried to reinvent the syntax approach and came up with the concept of "Organini" module (from Organic INI). The code then looks like this:
    Uses "Organini"
    
    ' -- Hook it to any file you need
    Dim config As Organini
    config = New Organini(APP_SourcePath + "settings.ini")
    
    ' -- Write data this way - first is section, then key
    config.Screen.Width  = 1920
    config.Screen.Height = 1080
    config.Screen.Depth  = 32
    
    config.User.Name     = "Petr"
    config.User.Password = "1234"                         
    
    ' -- Read the data back easily - this reads always up to date data from file!
    MsgBox 0, config.Screen.Width + "x" + config.Screen.Height
    
    What do you think about it ? It has its issues - like upper casing the sections and keys in file, but otherwise it is one of the many magical things current ThinBASIC SDK allows module authors to do.


    Petr
    Attached Files Attached Files
    Last edited by Petr Schreiber; 03-05-2015 at 23:53.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  2. #2
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10
    Petr's magic as usual.
    Very clever, very nice.
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  3. #3
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10
    Petr,

    maybe the optional default value is missing in "reading" an INI value.
    Something like:
    config.Screen.Width(1920)
    
    where "(1920)" is the default value in case "Screen.Width" section/parameter is not found in INI file

    But its usage/presence should be optionally parsed and not mandatory.

    I think after that I could add as standard module if you like.
    Only doubt is ... name of the module always remind me "orgasmic ini" but maybe it is my current attitude

    Ciao
    Eros
    Last edited by ErosOlmi; 28-04-2015 at 12:47.
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  4. #4
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,527
    Rep Power
    170
    i don't get it: how does it know ".Screen.Width" has to be some function that extends a config-udt-variable (is it one at all?) and how does it accept "= assignsomething".

    Not possible in plain basic- right?

    Else i would be very nosy how i can have dynamic function-names & udt-subelements being accepted by the "varname.[noMatterWhatfollowsHere]"

    ...
    And, for the UCase-thing:
    Why not simply:
    Dim myIni As t_someInifile
    
    myIni.Init(sFilename)
    Printl myIni.GetKey(sSection,sKeyname, sDefaultvalue)
    myIni.SetKey(sSection, sKeyname, sKeyvalue)
    
    Last edited by ReneMiner; 28-04-2015 at 14:44.
    I think there are missing some Forum-sections as beta-testing and support

  5. #5
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    ...yeah, and why not rename module for assembly language OrgASM, right?

    Eros, if you can/want, add this class to INI file module in default ThinBASIC installation - I attached full source code in the first post of this thread . It supports default values now too, thanks for the suggestion!:
    Uses "Organini"
    
    ' -- Hook it to any file you need
    Dim config As Organini
    config = New Organini(APP_SourcePath + "settings.ini")
    
    ' -- Write data this way - first is section, then key
    config.Screen.Width  = 1920
    config.Screen.Height = 1080
    
    ' -- Read the data back easily - this reads always up to date data from file!                
    ' -- Default value can be specified in brackets
    MsgBox 0, config.Screen.Width + "x" + config.Screen.Height + "@" + config.Screen.BitDepth(32)
    
    Also - one more wish. Do you think, you could add my StringBuilder to default installation too? It can be downloaded here:
    https://github.com/petrSchreiber/thi..._StringBuilder

    ...and it is also documented there. I think you can just put link to the GitHub to help file.


    Petr
    Last edited by Petr Schreiber; 28-04-2015 at 19:40.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  6. #6
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    ...maybe world really isn't prepared for OrganINI

    I attach here more conventional approach as Rene suggested. Maybe both can coexist inside Ini module.

    Uses "IniFile"
    
    ' -- Hook it to any file you need
    Dim config As IniFile
    config = New IniFile(APP_SourcePath + "settings.ini")
    
    ' -- Write data this way - first is section, then key
    config.SetKey("Screen", "Width", "1920")
    config.SetKey("Screen", "Height", "1080")
    
    ' -- Read the data back
    MsgBox 0, config.GetKey("Screen", "Width") + "x" + config.GetKey("Screen", "Height") + "@" + config.GetKey("Screen", "BitDepth", "32")
    

    Petr
    Attached Files Attached Files
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  7. #7
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,527
    Rep Power
    170
    Quote Originally Posted by Petr Schreiber View Post
    ...maybe world really isn't prepared for OrganINI
    ...
    Indeed i like better if modules-syntax is closer to thinBasic-script-syntax.

    Still nosy about the first approach- is there some way to add arbitrary subelements to a variable without having to Type them?

    Is it done by thinBasic_AddUDT-function? Is it an udt at all?
    How is it dynamic so one still can add additionally subelements after accessing it once?

    Is it some sort of parsing the code? How can i allow the user to append and instantly pass something like ".Screen.Width" and how does it know what to do with it? Is that possible in thinBasic at all?

    & not to critizise but "IniFile" is misleading module-name since not combination of Ini & File-modules,

    but perhaps it's a "cIni" similar as "cTimer" - class ?

    every app should have an .ini in these days...

    What about Ini_GetSectionsList & Ini_GetSectionKeyList ?
    Would that be possible = complete overhaul of classic ini-module... - who uses this doesn't need for two functions also have to use classic ini.

    your example above, assume Uses "Console" ...
    PrintL config.SetKey("Screen", "Width", "1920")
    PrintL config.SetKey("Screen", "Height", "1080")
    
    could print out/return the keyvalues here to enable instant assignement if needed when creating default-ini-data for something.
    Last edited by ReneMiner; 29-04-2015 at 10:15.
    I think there are missing some Forum-sections as beta-testing and support

  8. #8
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732
    Hi Rene,

    answers to your questions regarding "how it works" are available when you download the source code of the module from the first post of this thread. No UDT is built, but I take advantage of information ThinBASIC gives me about token parsing.

    I am not big fan of c prefix, and Ini could be shortcut for ... Inter-Network Interface for example, so that is why I decided to add the "File" to it, to identify it further

    I implemented your requests, please see in dedicated thread for IniFile module:
    http://www.thinbasic.com/community/s...1998#post91998


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  9. #9
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,527
    Rep Power
    170
    Very nice. Complete modern-times replacement for classic Ini-module.

    (this the answer to the other thread - but i did not want to get to far off topic there since it's still clean from topic-foreign-discussions)

    Something like that i'd wish too for TBass-module, channel-section

    ( + for color-types, fonts, UI-controls + windows, textures, entities, sprites, strings [,heap-memory],...
    so basically anything where objects are represented by a handle, Ptr or ID currently)

    attached a testing-sample, a raw sketch what i imagine for an overhauled TBass-module, but written in thinBasic, have an eye on tSound-Unit mostly.
    I should have better named it tSoundChannel probably...
    (testPerc.tBasic is to test the unit, it random plays 1 out of 3 percussion-sounds)

    It's just making the channel-dword functional here, so instead of TBass_ChannelPlay(mychannel, %TBass_True) its just mychannel.Play() etc.
    I'm not sure if module-classes are able to have static members as thinBasic-types can have, but imo static members are good place for class-specific common values, settings, flags etc.
    Attached Files Attached Files
    Last edited by ReneMiner; 03-05-2015 at 09:38.
    I think there are missing some Forum-sections as beta-testing and support

  10. #10
    thinBasic MVPs
    Join Date
    Oct 2012
    Location
    Germany
    Age
    54
    Posts
    1,527
    Rep Power
    170
    Now you can probably see the reason why i would like to be able to Extends <standard_numerical_types>,
    for more see this post:
    http://www.thinbasic.com/community/s...?12554-Type-As
    I think there are missing some Forum-sections as beta-testing and support

Page 1 of 2 12 LastLast

Similar Threads

  1. MyClass & MyClassCOM - simple demo of creating module class
    By Petr Schreiber in forum Module Classes
    Replies: 3
    Last Post: 21-05-2013, 00:09
  2. Astronomical Unit (module) simple example for testing :)
    By largo_winch in forum thinBasic vaporware
    Replies: 1
    Last Post: 24-12-2011, 11:21
  3. why bundle exe files (module incl.) are so big?
    By largo_winch in forum Modules specific issues
    Replies: 2
    Last Post: 16-07-2011, 11:10
  4. My simple editor
    By Aurel in forum Alternative IDEs
    Replies: 38
    Last Post: 10-01-2011, 19:51
  5. GCC module example: very very simple expression evaluator
    By ErosOlmi in forum Module SDK (GCC C /C++)
    Replies: 4
    Last Post: 15-09-2007, 21:30

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •