xmlPrefs - alternative zu Preferences

Hier könnt Ihr gute, von Euch geschriebene Codes posten. Sie müssen auf jeden Fall funktionieren und sollten möglichst effizient, elegant und beispielhaft oder einfach nur cool sein.
Benutzeravatar
ts-soft
Beiträge: 22292
Registriert: 08.09.2004 00:57
Computerausstattung: Mainboard: MSI 970A-G43
CPU: AMD FX-6300 Six-Core Processor
GraKa: GeForce GTX 750 Ti, 2 GB
Memory: 16 GB DDR3-1600 - Dual Channel
Wohnort: Berlin

xmlPrefs - alternative zu Preferences

Beitrag von ts-soft »

Dieses Includefile enthält alternative Befehle für alle Befehle der Preference-Lib.
Es wird xml genutzt. Ähnlichen Tipp gab es bereits hier: http://www.purebasic.fr/german/viewtopi ... =8&t=20847

Vorteile gegenüber Preferences: eigentlich keine :mrgreen: , abgesehen von der Optik und der
OOP-Schreibweise.

Erforderliche PB Version: 4.41 und höher.
Plattform: Alle

Die > 500 Zeilen Source-Datei werde ich nicht hier posten, sondern die könnt Ihr hier downloaden.

Damit Ihr eine Vorstellung habt, das Interface:

Code: Alles auswählen

Interface xmlPrefsObject
; the result of "Create", "Catch" and "Open" is the xmlObject!
;
; you can add the groupname to a keyword with a slash between:
; Keyword.s = "Group/Key"
; this will not change the actuel group!
; to move outside of any groups, an empty name ("") can be used
;
  Create.i(BaseName.s)      ; creates a new empty xmlPrefs
  Catch.i(*adress, length)  ; load a xmlPrefs from memory
  Open.i(FileName.s)        ; load a xmlPrefs from file
  Save.i(FileName.s = "")   ; save the xmlPrefs to file
  Close()                   ; free the objectmemory
  ;
  Comment.i(Text.s)         ; writes a comment in actual group
  Group.i(Name.s)           ; create or change group
  ;
  ExamineGroups.i() ; starts the enumeration of all the groups
  NextGroup.i()     ; retrieves information about the next group found in the enumeration
  GroupName.s()     ; returns the name of the current group being enumerated
  ;
  ExamineKeys.i()   ; starts the enumeration of all the keys found in the current group
  NextKey.i()       ; retrieves information about the next key found in the enumeration
  KeyName.s()       ; returns the name of the current key being enumerated
  KeyValue.s()      ; returns the value, in string form, of the current key being enumerated
  ;
  DelGroup.i(Name.s)        ; delete a group with all keys
  DelKey.i(Keyword.s)       ; delete a key
  ;
  ReadD.d(Keyword.s, DefaultValue.d = 0)  ; read a double
  ReadF.f(Keyword.s, DefaultValue.f = 0)  ; read a float
  ReadI.i(Keyword.s, DefaultValue.i = 0)  ; read a integer
  ReadL.l(Keyword.s, DefaultValue.l = 0)  ; read a long
  ReadQ.q(Keyword.s, DefaultValue.q = 0)  ; read a quad
  ReadS.s(Keyword.s, DefaultValue.s = "") ; read a string
  ;  
  WriteD.i(Keyword.s, value.d) ; write a double
  WriteF.i(Keyword.s, value.f) ; write a float
  WriteI.i(Keyword.s, value.i) ; write a integer
  WriteL.i(Keyword.s, value.l) ; write a long
  WriteQ.i(Keyword.s, value.q) ; write a quad
  WriteS.i(Keyword.s, value.s) ; write a string
EndInterface
Beispielcode:

Code: Alles auswählen

EnableExplicit

XIncludeFile "xmlPrefs_Include.pbi"

Define.xmlPrefsObject prefs = xmlPrefs_CreateObject()

prefs\Create("testing")
prefs\WriteS("group1/1", "100")
prefs\WriteS("group1/2", "200")
prefs\WriteS("group1/3", "300")
prefs\WriteS("group1/4", "400")

prefs\WriteS("group2/1", "100")
prefs\WriteS("group2/2", "200")
prefs\Group("group2")
prefs\WriteS("3", "300")
prefs\WriteS("4", "400")

prefs\Group("other")
prefs\WriteD("double", 1)
prefs\WriteI("integer", 1)
prefs\WriteS("string", "Feel the ..Pure.. Power")

prefs\Group("")
prefs\WriteI("x", #PB_Ignore)
prefs\WriteI("y", #PB_Ignore)
prefs\writeI("width", 640)
;prefs\WriteI("height", 480)
prefs\WriteS("title", "Testing Window")

prefs\ExamineGroups()
While prefs\NextGroup()
  prefs\Group(prefs\GroupName())
  Debug prefs\GroupName() + ":"
  prefs\ExamineKeys()
  While prefs\NextKey()
    Debug prefs\KeyName() + " = " + prefs\KeyValue()
  Wend
  Debug "================="
Wend
prefs\Group("")
prefs\ExamineKeys()
While prefs\NextKey()
  Debug prefs\KeyName() + " = " + prefs\KeyValue()
Wend

With prefs
  \Group("")
  OpenWindow(0, \ReadI("x"), \ReadI("y"), \ReadI("width"), \ReadI("height", 480), \ReadS("title"))
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndWith

prefs\Save("test.xml")
prefs\Close()

Feedback willkommen
Thomas
PureBasic 5.73 LTS | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Nutella hat nur sehr wenig Vitamine. Deswegen muss man davon relativ viel essen.
Bild
tmjuk
Beiträge: 380
Registriert: 30.06.2006 00:10
Wohnort: Backaryd, Schweden

Re: xmlPrefs - alternative zu Preferences

Beitrag von tmjuk »

:allright: Gefällt mir gut. Kommt gerade recht.

Eine Sache:
(Gut, eigentlich sollte man für sowas selber sorgen, aber..)
Wie wäre es noch mit einer "Sperre" in der Include, welche verhindert, dass man z.B.
Leerzeichen in den Attributnamen vorkommen.
Das Attribut wird dann nämlich nicht geschrieben. (Was ja richtig ist)

Also wenn man z.B. statt

Code: Alles auswählen

prefs\Create("testing")
prefs\WriteS("group1/1", "100")
dieses hier schreiben würde

Code: Alles auswählen

prefs\Create("testing")
prefs\WriteS("group1/mein node", "100")
Torsten
PB 4.51 32 Windows Vista, 32 XP, PB 4.51 32 Ubuntu 10.10
Benutzeravatar
ts-soft
Beiträge: 22292
Registriert: 08.09.2004 00:57
Computerausstattung: Mainboard: MSI 970A-G43
CPU: AMD FX-6300 Six-Core Processor
GraKa: GeForce GTX 750 Ti, 2 GB
Memory: 16 GB DDR3-1600 - Dual Channel
Wohnort: Berlin

Re: xmlPrefs - alternative zu Preferences

Beitrag von ts-soft »

tmjuk hat geschrieben: :allright: Gefällt mir gut. Kommt gerade recht.
:D danke, so hab ich wenigstens einen Testkandidaten!
tmjuk hat geschrieben: Eine Sache:
(Gut, eigentlich sollte man für sowas selber sorgen, aber..)
Wie wäre es noch mit einer "Sperre" in der Include, welche verhindert, dass man z.B.
Leerzeichen in den Attributnamen vorkommen.
Das Attribut wird dann nämlich nicht geschrieben. (Was ja richtig ist)
Checkroutine für BaseName, Groupname und Keyword integriert!
Debugger meckert falsche Zeichen und Spaces an. Erlaubt sind: 0 - 9, a - z, A - Z, / und _

Gruß
Thomas
PureBasic 5.73 LTS | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Nutella hat nur sehr wenig Vitamine. Deswegen muss man davon relativ viel essen.
Bild
Antworten