ParseSettings & SaveSettings
Author: Latexi95
        Added: 21. kesäkuuta 2011 kello 15.40
        Edited: 21. kesäkuuta 2011 kello 15.40
        Category: Tiedostot
    
Description
Tarkoitettu asetustiedostojen avaamiseen ja muokkaamiseen. Tarkoitettu samaan kuin MaGetzUbin, mutta tämä on omasta mielestäni hieman kätevämpi.
Asetukstiedostojen tulee olla tätä muotoa:
   
   mun_asetuksen_avain = asetuksen arvo
   #tämä on kommentti
   #kommentit tuhoutuvat kun tallennetaan... :C
   hassu avain = hassun avaimen arvo #tuokin kelpaa, mutta avaimen keskellä olevien välilyöntien määrällä on vaikutusta
   # "hassu avain" ei ole sama kuin "hassu    avain"
   
   # "TÄmä on avaimessa aivan sama kuin "täMÄ", joten kirjain koolla ei ole merkitystä avaimessa
ParseSettigs(filePath$)
-Lataa asetustiedoston osotteesta filePath$.
GetSetting(key$)
-Palauttaa key$ - availella löydetyn asetuksen arvon.
SetSetting(key$,value$)
-Muokkaa asetuksen arvoa jos sellainen löydetään key$ - avaimella. Jos aiempaa asetusta ei löydy luodaan uusi.
HasSetting(key$)
-Plavauttaa "true" mikäli asetus on olemassa ja "false" mikäli asetusta ei ole vielä olemassa.
DeleteSetting(key$)
-Poistaa asetuksen jos sellainen löytyy avaimella key$ ja palauttaa "true", jos asetusta ei löydy, palauttaa "false"
ClearSettings()
-Poistaa kaikki asetukset
SaveSetting(filePath$)
-Tallentaa asetukset filePath$:ssa olevaan tiedostoon.
    Code
Select all1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117  | Type Settings
    Field key$
    Field value$
EndType
Function ParseSettings(filePath$)
    ClearSettings()
    file = OpenToRead(filePath$)
    If file = 0 Then Return False
    lineNum = 1
    While Not EOF(file)
        l$ = ReadLine(file)
        notComment$ = Trim(GetWord(l$,1,"#"))
        If notComment <> "" Then
            If CountWords(notComment$,"=") < 2 Then
                MakeError "Syntax error in file "+filePath$+" at line "+lineNum
            EndIf
            If CountWords(notComment$,"=") = 2 Then
                newSetting.Settings = New(Settings)
                newSetting\key$ = Lower(Trim(GetWord(notComment$,1,"=")))
                newSetting\value$ = Trim(GetWord(notComment$,2,"="))
            Else
                place = InStr(notComment$,"=")
                newSetting.Settings = New(Settings)
                newSetting\key$ = Lower(Trim(Left(notComment$,place-1)))
                newSetting\value$ = Trim(Mid(notComment,place+1))
            EndIf
        EndIf
        lineNum + 1
    Wend
    CloseFile file
EndFunction
Function SaveSettings(filePath$)
    f = OpenToWrite(filePath$)
    For setting.Settings = Each Settings
        WriteLine f,setting\key$ + " = " + setting\value
    Next setting
    CloseFile f     
EndFunction
Function DeleteSetting(key$)
    key$ = Lower(key$)
    For setting.Settings = Each Settings
        If setting\key$ = key Then
            Delete setting
            Return True
        EndIf
    Next setting
    Return False
EndFunction
Function GetSetting(key$)
    key$ = Lower(key$)
    For setting.Settings = Each Settings
        If setting\key$ = key Then Return setting\value$
    Next setting
    Return ""
EndFunction
Function HasSetting(key$)
    key$ = Lower(key$)
    For setting.Settings = Each Settings
        If setting\key$ = key Then Return True
    Next setting
    Return False
EndFunction
Function SetSetting(key$,value$)
    key$ = Lower(key$)
    For setting.Settings = Each Settings
        If setting\key$ = key Then
            setting\value = value
            Return True
        EndIf
    Next setting
    newSetting.Settings = New(Settings)
    newSetting\key = key
    newSetting\value = value
    Return False
EndFunction
    
Function ClearSettings()
    For setting.Settings = Each Settings
        Delete setting
    Next setting
EndFunction
//Esimerkki
Const SCREEN_WIDTH_KEY = "screen-width"
Const SCREEN_HEIGHT_KEY = "screen-height"
Print "setting settings..."
SetSetting(SCREEN_WIDTH_KEY,"500")
SetSetting(SCREEN_HEIGHT_KEY,"400")
Print "saving..."
SaveSettings("setting.ini")
Print "Press any key to load settings"
WaitKey
Print "loading..."
ParseSettings("setting.ini")
If Not (HasSetting(SCREEN_WIDTH_KEY) And HasSetting(SCREEN_HEIGHT_KEY)) Then MakeError "Oletko käynnyt räpelöimässä tiedostoa?"
SCREEN Int(GetSetting(SCREEN_WIDTH_KEY)),Int(GetSetting(SCREEN_HEIGHT_KEY))
Print "Screen-width:" + GetSetting(SCREEN_WIDTH_KEY)
Print "Screen-height:" + GetSetting(SCREEN_HEIGHT_KEY)
Print "finish"
WaitKey
 | 
Comments
#33 Sent by: Latexi95, 9. huhtikuuta 2012 kello 9.01
Tiedostopäätteellä ei ole väliä, kunhan sen sisältämä teksti on ylempänä kuvattujen sääntöjen mukainen. Ja arvon täytyy olla merkkijono.
Leave a comment
            You must be logged in to comment.
        
    
        
#32 Sent by: Konstaduck, 19. helmikuuta 2012 kello 12.05
Voiko tällä avata esimerkiksi .TXT muotoisia tiedostoja? Entä voiko arvo olla merkkijono?