Save in files instead of eeprom,

Place code snippets and demo code here
Post Reply
User avatar
Oli
Posts: 44
Joined: Tue Feb 09, 2021 10:07 am
Location: Germany, Meissen
Has thanked: 13 times
Been thanked: 44 times
Contact:

Save in files instead of eeprom,

Post by Oli »

I'm still looking for a
file.save Filename $, content $, [line_num]
The line number would be helpful, also with the save, not only when reading
Ret$ = FILE.READ$(filename$, [line_num]

For each separate variable, a separate file, or
I got a moderate alternative here:

Code: [Local Link Removed for Guests]

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' how save and restore variables into a "configuration" file
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' variables to be stored
color$ = "green"
size$ = "big"
orientation$ = "left"
line$ = "first"

''''''''''''''''''''''''''''''''''''''''''''''''''''''
'  SNIPPET 1 
'store all the variables in the file "myconf.txt"
''''''''''''''''''''''''''''''''''''''''''''''''''''''
a$ = ""
'set the variables in a$
WORD.SETPARAM a$, "color$", color$
WORD.SETPARAM a$, "size$", size$
WORD.SETPARAM a$, "orientation$", orientation$
WORD.SETPARAM a$, "line$", line$
wlog a$ ' shows the content just for debug purposes
'save a$ in the config file
file.save "/myconf.txt", a$

'end

''''''''''''''''''''''''''''''''''''''''''''''''''''''
'  SNIPPET 2
'read variable from the config file into a$
''''''''''''''''''''''''''''''''''''''''''''''''''''''
a$ = file.read$( "/myconf.txt")
wlog a$ ' shows the content just for debug purposes
'extract the variables
color$ = WORD.GETPARAM$(a$, "color$")
size$ = WORD.GETPARAM$(a$, "size$")
orientation$ = WORD.GETPARAM$(a$, "orientation$")
line$ = WORD.GETPARAM$(a$, "line$")
wlog color$, size$, orientation$, line$

'end
'''''''''''''''''''''''''''
'  SNIPPET 3
'modify only one variable
'''''''''''''''''''''''''''
'read variable from the config file into a$
a$ = file.read$( "/myconf.txt")
wlog a$ ' shows the content just for debug purposes
WORD.SETPARAM a$, "color$", "blue" ' modify the content of the variable
wlog a$ ' shows the content just for debug purposes
file.save "/myconf.txt", a$

'end

''''''''''''''''''''''''''''''''''''''''''''''''''''''
'  SNIPPET 4
'another "dynamic" approach for reading variables
''''''''''''''''''''''''''''''''''''''''''''''''''''''
' clear the variables 
color$ = ""
size$ = ""
orientation$ = ""
line$ = ""
a$ = file.read$( "/myconf.txt")
for z = 1 to 10 ' max 10 parameters
  b$ = WORD$(a$, z, chr$(10)) 'split all the lines separated by new the line character (\n)
  if b$ = "" then exit for 'if the line is empty, stops
  b$ = replace$(b$, |=|, |="|) + |"| ' put the " around the argument (i.e. from lilo$=abc -> lilo$="abc" )
  command b$ 'execute dynamically the line setting the variable
next z
wlog color$, size$, orientation$, line$
Post Reply