Jump to content

Need advice for best practice with ini file.


satanico64
 Share

Recommended Posts

hi people ! For those about to rock, we salute you ! :evil:

there is something i want to do:

In all my program, i use several sentences for the users, example:' Not available, try again later', 'Server not responding'...

This sentences, are hardly coded.

I want to use an inifile, to be able to modify the sentences when i need to, without recompile.

And it does'nt matter if my sentences are not updated 'on the fly', at start of my program is enough for me.

I see several approach:

- Each time i need, i can read my inifile: which mean many access to my inifile (that will be copied at start on the local machine of the user at start)

$phrase = IniRead($FichierINICFY, 'Database_messages', 'Message1', "")
MsgBox(16, "Database error",$phrase )

or i can read the whole inifile to an array and use my array each time i need.

 

In fact i don't know if the access to a field in an ini file is cost effective or not..

it does'nt represent many access, but how would you work with such a file of sentences ?

and maybe inifile is not the best solution ..

 

Thanks for reading !

Nicolas.

Link to comment
Share on other sites

I once created a solution to hold the error messages in a separate file. The solution was able to insert variable data into messages, means:
It created message "Could not read file C:\temp\test.txt. @error = 17". from "Could not read file %1. @error = %2".
if you are interested I can post my code.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

You can also create custom environment variables using EnvSet and then parse with _WinAPI_ExpandEnvironmentStrings e.g.

// Ini file
SomeString=This is a custom environment variable $EXAMPLEBLAH%

// Au3 Script

EnvSet('EXAMPLEBLAH', 'Turns into this')
ConsoleWrite(_WinAPI_ExpandEnvironmentStrings(IniRead(....))


 

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

The function is described in this thread. Unfortunately I seen to have removed the ZIP file when cleaning up my attachments. I need to look in my backups ;)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Here goes the ZIP file.
Run MsgTest.au3 to see how it works.

MSG.zip

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I generally use ini files for my 'lightweight' apps, and have always loaded the data into an array at startup, then reference the config array throughout the application.
For applications that tend to be a little larger/have more in depth config/Multiple users I opt for sqlite databases: This is just personal preference however, and not based on any issues experienced with ini files.

As for ini files, I would say the approach taken should depend on how the application will be used. For example, will the app be used constantly, or opened/closed as required, If it's likely to be constantly open, then perhaps periodically updating an array with the variables would be preferably to just at startup.

Anyway, enough of my ramblings, Here's an example of an ini file, how to load it into an array, and reference it .

Error.ini:

[1]
ErrorCode=1
ErrorName=Could not connect
ErrorDesc=Could not connect to the remote host, please check connectivity
[2]
ErrorCode=-1
ErrorName=Invalid Credentials
ErrorDesc=The username or password was not recognised, Please try again
[3]
ErrorCode=3
ErrorName=Unrecognised
ErrorDesc=is an unrecognised character! Please note that ^&*() are not supported


Error.au3:

#include <Array.au3>
GLOBAL $aErrors,$iConnect
$aErrors = _LoadIni(@ScriptDir & "\error.ini")
_ArrayDisplay($aErrors) ;Visual reference of the error array
$iConnect = _Connect("SomeRandomPC") ;Mock function that is destined to fail
if $iConnect <> 1 then _CheckError(@error) ;Check the error code

;load the ini file
func _LoadIni($sIni)
    local $aErrors[1][3], $aSections,$aTempArray
    $aErrors[0][0] = 1
    $aSections = IniReadSectionNames($sIni) ;Load all section names into temporary array
    for $i = 1 to $aSections[0]
        $aTempArray = iniReadSection($sIni,$aSections[$i]) ;Read each section per loop iteration
        reDim $aErrors[$aErrors[0][0] + 1][3] ;Dynamically increase the error array and input ini data to main error array
        $aErrors[$aErrors[0][0]][0] = $aTempArray[1][1]
        $aErrors[$aErrors[0][0]][1] = $aTempArray[2][1]
        $aErrors[$aErrors[0][0]][2] = $aTempArray[3][1]
        $aErrors[0][0] += 1
    Next
    return $aErrors ;Return the newly built array
EndFunc

;Mock function
func _Connect($sPC)
    if $sPC = "MyPC" then return 1
    return SetError(1,0,0) ;Returns @ERROR=1 and $iConnect=0
EndFunc

;error checker
Func _CheckError($sErrCode)
    for $i = 1 to $aErrors[0][0] - 1 ;Loop through error array
        if $sErrCode = $aErrors[$i][0] Then ;Check error code against error code column
            msgbox(0,$aErrors[$i][1],$aErrors[$i][2]) ;Output the name and description for the error
            return 0
        elseif $i = $aErrors[0][0] - 1 ;We've reached the end of the array and not found a matching error
            msgbox(0,"Unhandled error","An unhandled error has ocurred, please contact IT with the following error code: " & $sErrCode) ;Output the unhandled error code
        EndIf
    Next
EndFunc

Cheers

Javi
 

give a man an application, and he'll be frustrated for the day, Teach him how to program applications and he'll be frustrated for a lifetime.

Link to comment
Share on other sites

Glad to be of service :)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...