Source language: Translate to:

NB Function: INI_Utility

Questions and information about using VBScript and JavaScript in NeoBook functions

Moderator: Neosoft Support

NB Function: INI_Utility

Postby dpayer » Tue Jul 24, 2012 2:56 pm

INI files are a simple way to store data for your application. This function (can also be used as a subroutine) has the following properties

INI_Utility function
by David Payer

Read/Write values to INI file.

Requests must be in form:
1) file.ini\section\name
2) file.ini\section\name=value
3) file.ini\section\?

1 - this will GET the value of the name in that section and set the variable [%2] with that value.
2 - this will SET the value of that name in that section. It can be overwritten with new values. If the section does not exist, it will be created.
3 - this will GET the entire section and put that value (including the section title) in [%2]

"GET" ting a name that does not exist will dislay an error.
"SET" ting a section\name that does not exist will cause that section\name to exist.

INI File must exist. If no full path given, it is assumed to be in the [pubdir]

Square brackets can only be used in section name. Lines must end in [#13][#10] if using INIs from existing apps.

Two parameters for this function [%1] = Request ; [%2] = Variable for GET request responses


When you submit a GET request, the function gets that value and places it in the variable you have named in the parameter.

When you submit a SET request, the function adds/edits the entry in the INI and writes the change of the INI file to disk.

I have created a sample pub to display functionality:
Image
The sample pub references the INI via NB Function or by SubRoutine.

If developers would like to see my 'internal notes' on developing this function, I have left them inside the subroutine and you can see how I reminded myself of the functionality I was working on in each section. It is embedded in the sample pub file.

Compiled App - easy to view
NB Pub file (requires you to download function file

The function is available here:
Code: Select all
{NeoBook Function}
Version=5.70
Language=NeoBook
Comment=INI_Utility function|by David Payer||Read/Write values to INI file.||Requests must be in form:|1)    file.ini\section\name|2)    file.ini\section\name=value|3)    file.ini\section\?||1 - this will GET the value of the name in that section and set the variable [%2] with that value.|2 - this will SET the value of that name in that section. It can be overwritten with new values. If the section does not exist, it will be created.|3 - this will GET the entire section and put that value (including the section title) in [%2]||"GET" ting a name that does not exist will dislay an error.|"SET" ting a section\name that does not exist will cause that section\name to exist.||INI File must exist. If no full path given, it is assumed to be in the [pubdir]||Square brackets can only be used in section name. Lines must end in [#13][#10] if using INIs from existing apps.
Param=[%1]|Variable|Request in form of    sample.ini\section\name  or  sample.ini\section\name=value  - - - - see comments
Param=[%2]|Variable|NB variable to contain "GET" requests
{End}
SetVar "[INIRequest]" "[%1]"
      StrParse "[INIRequest]" "\" "[INIRequestPart]" "[INIRequestLEN]"
               If "[INIRequestLEN]" "<>" "3"
                 AlertBox "Error" "[INIRequest] |appears to be improperly formatted.||Request must be in the format of:|FileName.ini\Section\Name||INI will be updated with name=value statement|If only name (without = 'value') function returns value from INI."
                 GotoLine "EndINImakerFunction"
               Endif

SearchStr "[#61]" "[INIRequestPart3]" "[FoundEqualSign]" ""
              If "[FoundEqualSign]" "=" "0"
                SetVar "[INIAction]" "GetValue"
              Else
                SetVar "[INIAction]" "SetValue"
              Endif

                 SearchStr ":\" "[INIRequestPart1]" "[FoundFullPath]" ""
                              If "[FoundFullPath]" "=" "0"
                                SetVar "[FullPathINI]" "[pubdir][INIRequestPart1]"
                              Else
                                SetVar "[FullPathINI]" "[INIRequestPart1]"
                                SetVar "[FoundFullPath]" ""
                              Endif
           FileExists "[FullPathINI]" "[iniExists]"
               If "[iniExists]" "=" "False"
                 Alertbox "Error" "Selected INI file not found."
                 GotoLine "EndINImakerFunction"
               Endif
           ExtractFileName "[FullPathINI]" "[selected2INI]"
            StrReplace "[selected2INI]" "." "" "[selected3INI]" ""
            StrReplace "[selected3INI]" " " "" "[selectedINI]" ""

     FileRead "[FullPathINI]" "All" "[[selectedINI]]"

     StrLen "[[selectedINI]]" "[selectedINI_Len]"


     SetVar "[[selectedINI]_Sections]" ""


     StrParse "[[selectedINI]]" "[#93]" "[ThisINI]" "[ThisINI_len]"

         If "[ThisINI_len]" "=" "1"
            AlertBox "Empty" "There are no sections in this INI"
         Else

             Loop "1" "[ThisINI_len]-1" "[WILL_ThisINI]"
             StrParse "[ThisINI[WILL_ThisINI]]" "[#91]" "[SubThis]" ""
             SetVar "[[selectedINI]_Sections]" "[[selectedINI]_Sections],[SubThis2]"
             Endloop
                    StrLen "[[selectedINI]_Sections]" "[[selectedINI]_Sections_LEN]"
                    SubStr "[[selectedINI]_Sections]" "2" "[[selectedINI]_Sections_LEN]" "[[selectedINI]_Sections]"

                        StrParse "[[selectedINI]_Sections]" "," "[ThisSection]" "[ThisSectionLen]"
                        SetVar "[SectionLast]" "[ThisSectionLen]"
                        SetVar "[INIcopy]" "[[selectedINI]]"
                        Loop "1" "[ThisSectionLen]" "[WILL_ThisSection]"
                              SearchStr "[#91][ThisSection[SectionLast]][#93]" "[INIcopy]" "[whereisthissection]" ""
                                 If "[whereisthissection]" ">" "0"
                                 Else
                                 Endif
                              StrLen "[INIcopy]" "[INIcopyLEN]"
                              SubStr "[INIcopy]" "[whereisthissection]" "100000" "[selectedINI[SectionLast]]"
                              StrReplace "[INIcopy]" "[selectedINI[SectionLast]]" "" "[INIcopy]" ""
                              StrLen "[INIcopy]" "[selectedINI_Len]"
                              SetVar "[SectionLast]" "[Sectionlast]-1"
                        Endloop

         Endif

If "[INIAction]" "=" "GETVALUE"

       SearchStr "[INIRequestPart2]" "[[selectedINI]_Sections]" "[SectionFound]" ""
                 If "[SectionFound]" "=" "0"
                  Alertbox "Error" "Section [INIRequestPart2] not found in [INIRequestPart1]"
                  GotoLine "EndINImakerFunction"
                 Endif
               StrParse "[[selectedINI]_Sections]" "," "[ThisRequest_Sections]" "[ThisRequest_Sections_Len]"
               Loop "1" "[ThisRequest_Sections_Len]" "[WILL_ThisRequest]"
                  If "[ThisRequest_Sections[WILL_ThisRequest]]" "=" "[INIRequestPart2]"
                                     If "[INIRequestPart3]" "=" "?"
                                       SetVar "[NameValue]" "[selectedINI[WILL_ThisRequest]]"
                                       GotoLine "EndINImakerFunction"
                                     Endif

                      SearchStr "[INIRequestPart3]=" "[selectedINI[WILL_ThisRequest]]" "[FoundPart3Request]" ""
                        If "[FoundPart3Request]" ">" "0"
                            StrParse "[SelectedINI[WILL_ThisRequest]]" "[#13]" "[This_section_lines]" "[names_len]"
                           Loop "1" "[names_len]" "[WILL_names]"
                                 SearchStr "[INIRequestPart3][#61]" "[This_section_lines[WILL_names]]" "[FoundPart3Request]" ""
                              If "[FoundPart3Request]" ">" "0"
                                 StrReplace "[This_section_lines[WILL_names]]" "[INIRequestPart3]=" "" "[nameValue]" ""
                                 DeleteArray "[INIRequestPart]" "All"
                                 GotoLine "EndINImakerFunction"
                              Endif
                           Endloop

                         Else
                               AlertBox "Name not found" "[INIRequestPart3] was not found in this section."
                               GotoLine "EndINImakerFunction"
                         Endif
                   Endif
                Endloop
   SetVar "[INIAction]" ""
Endif


If "[INIAction]" "=" "SETVALUE"

       SearchStr "=" "[INIRequestPart3]" "[SetFormatError1]" ""
                 If "[SetFormatError1]" "=" "0"
                   AlertBox "Format Error" "Set Value requires 'Name=Value' statement. No equal sign found."
                   GotoLine "EndINImakerFunction"
                 Endif
       SearchStr " =" "[INIRequestPart3]" "[SetFormatError2]" ""
                 If "[SetFormatError2]" ">" "0"
                   AlertBox "Format Error" "Name=Value must not have spaces adjacent to the equal sign"
                   GotoLine "EndINImakerFunction"
                 Endif
       SearchStr "= " "[INIRequestPart3]" "[SetFormatError3]" ""
                 If "[SetFormatError3]" ">" "0"
                   AlertBox "Format Error" "Name=Value must not have spaces adjacent to the equal sign"
                   GotoLine "EndINImakerFunction"
                 Endif
        SearchStr "=" "[INIRequestPart3]" "[SetFormatError4]" ""
                 If "[SetFormatError4]" "=" "1"
                   AlertBox "Format Error" "Set Value requires 'Name=Value' statement. No name found before equal sign"
                   GotoLine "EndINImakerFunction"
                 Endif

      SearchStr "[INIRequestPart2]" "[[selectedINI]_Sections]" "[SetRequestFound]" ""
       If "[SetRequestFound]" ">" "0"


          SetVar "[SetContent]" ""

          StrParse "[INIRequestPart3]" "=" "[NameName]" ""
          SetVar "[NameName]" "[NameName1]="
          StrParse "[[SelectedINI]_Sections]" "," "[ThisSection]" "[ThisSectionLen]"
                   Loop "1" "[ThisSectionLen]" "[WILL_Set_Section]"
                      If "[ThisSection[WILL_Set_Section]]" "=" "[INIRequestPart2]"
                               SearchStr "[NameName]" "[selectedINI[WILL_Set_Section]]" "[FoundNameName]" ""
                               If "[FoundNameName]" ">" "0"

                                 SubStr "[selectedINI[WILL_Set_Section]]" "[FoundNameName]" "100000" "[NameNameSubStr]"
                                 SearchStr "[#13][#10]" "[NameNameSubStr]" "[FoundNameNameEnd]" ""
                                 SubStr "[NameNameSubStr]" "1" "[FoundNameNameEnd]" "[NameNameSubStr]"
                                 StrReplace "[selectedINI[WILL_Set_Section]]" "[NameNameSubStr]" "[INIRequestPart3]" "[selectedINI[WILL_Set_Section]]" ""

                               Else
                                 SetVar "[selectedINI[WILL_Set_Section]]" "[selectedINI[WILL_Set_Section]][#13][#10][INIRequestPart3][#13][#10]"

                               Endif

                       Endif
                   EndLoop
                   SetVar "[SetContent]" ""

                   Loop "1" "[ThisSectionLen]" "[WILL_SectionRebuild]"
                        SetVar "[SetContent]" "[SetContent][selectedINI[WILL_SectionRebuild]][#13][#10]"
                   Endloop

                   StrReplace "[SetContent]" "[#13][#10][#13][#10][#13][#10]" "[#13][#10][#13][#10]" "[SetContent]" ""
                   StrReplace "[SetContent]" "[#10][#10]" "[#10]" "[SetContent]" ""
                   StrReplace "[SetContent]" "[#13][#13]" "[#13]" "[SetContent]" ""

             FileWrite "[fullpathINI]" "All" "[SetContent]"
             StickyNote "-1" "-1" "INI Updated ||Reloading values." "1000"
        Else
           AlertBox "New Section" "Adding Section\Name=Value to INI|[#13][#10][#91][INIRequestPart2][#93][#13][#10][INIRequestPart3][#13][#10]"
           SetVar "[NewSectionValue]" "[#13][#10][#91][INIRequestPart2][#93][#13][#13][#10][INIRequestPart3][#13][#10]"
           SetVar "[SetContent]" "[[selectedINI]][#13][#10][NewSectionValue]"
           AlertBox "Test2" "INI content after addition of new section:|[SetContent]"
           StrReplace "[SetContent]" "[#13][#10][#13][#10][#13][#10]" "[#13][#10][#13][#10]" "[SetContent]" ""
           StrReplace "[SetContent]" "[#10][#10]" "[#10]" "[SetContent]" ""
           StrReplace "[SetContent]" "[#13][#13]" "[#13]" "[SetContent]" ""
             FileWrite "[fullpathINI]" "All" "[SetContent]"
             StickyNote "-1" "-1" "INI Updated ||Reloading values." "1000"
       Endif

  Endif
:EndINImakerFunction
StrReplace "[NameValue]" "[#13][#10][#13][#10]" "[#13][#10]" "[%2]" ""

SetVar "[LastINI_sections]" "[[selectedINI]_Sections]"
SetVar "[[selectedINI]_Sections]" ""
SetVar "[LastINI]" "[selectedINI]"
DeleteArray "[selectedINI]" "All"
SetVar "[selectedINI]" ""
DeleteArray "[INIRequestPart]" "All"
SetVar "[SetContent]" ""
SetVar "[INIAction]" ""

or downloadable here:
http://iowalink.com/SimpleSoftware/INI_Utility/INI_Utility

Comments or suggestions for improvement are welcome.
User avatar
dpayer
 
Posts: 1380
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

Postby Neosoft Support » Wed Jul 25, 2012 10:33 am

That's great. Thanks for sharing.
NeoSoft Support
Neosoft Support
NeoSoft Team
 
Posts: 5593
Joined: Thu Mar 31, 2005 10:48 pm
Location: Oregon, USA

Postby Luiz Alfredo » Thu Jul 26, 2012 3:01 pm

Great Function, and also the Code for Study. Thanks.
L.A.G.M.
Luiz Alfredo
 
Posts: 195
Joined: Thu Apr 19, 2007 6:58 am
Location: Brazil


Return to NeoBook Functions - VBScript & JavaScript

Who is online

Users browsing this forum: No registered users and 1 guest

cron