Home | Reviews | GUIpedia | Forum | Fun500
ksr | rconflibI'm planning to release each of the Reflow modules as they are written. Here is the first - a quick and dirty set of configuration file routines. Use and abuse as you wish. For FreeBASIC only. Some example code: [code] dim as configfile myconfig errorval = myconfig.copen("config.ini") '{check error value} myconfig.cseek("sectionname") print myconfig.cget("keyname") [/code] [code] config.ini: [sectionname] keyname=value etc. [/code] ***** [code]'rconflib - Reflow configuration file library 'Public domain 'Kyle McInnes 'kyle at mcinnes dot uk dot net '16/03/09 #define c_error 1 #define c_nofile 2 #define c_fileerror 3 'note: cget() returns a string, errors are returned as either "!error:key" or "!error:section" type configfile filename as string section as string declare function cnew (filename as string) as byte declare function copen (filename as string) as byte declare function cclose as byte declare function cseek (section as string) as byte declare function cget (key as string) as string declare function cset (key as string, value as string) as byte declare function cadd (section as string) as byte declare function cenumkeys (keylist() as string) as byte declare function cenumsections (sectionlist() as string) as byte end type function configfile.cnew (filename as string) as byte 'creates a new file 'return values: 0 ok ' c_fileerror file access error dim as ubyte ff ff = freefile if open(filename for output as ff) = 0 then close ff this.filename = filename else close ff return c_fileerror end if end function function configfile.copen (filename as string) as byte 'initialises file type 'return values: 0 ok ' c_nofile file not found ' c_fileerror file access error dim as ubyte ff ff = freefile 'check file exists if not fileexists(filename) then return c_nofile 'file exists, attempt to open it if open(filename for input as ff) = 0 then close ff this.filename = filename else close ff return c_fileerror end if end function function configfile.cclose as byte 'resets file type this.filename = "" this.section = "" end function function configfile.cseek (section as string) as byte 'moves to the specified section 'return values: 0 ok ' c_fileerror file access error ' c_error section not found dim as ubyte ff dim as string cline 'open file ff = freefile if open(this.filename for input as ff) > 0 then close ff return c_fileerror end if 'search for [sections] do until eof(ff) input #ff, cline cline = trim(cline) if (left(cline, 1) = "[") and (right(cline, 1) = "]") then 'section found if section = mid(cline, 2, len(cline) - 2) then this.section = section close ff return 0 end if end if loop 'section not found close ff return c_error end function function configfile.cget (key as string) as string 'returns the specified key in the current section 'return values: "!error:key" key not found ' "!error:section" section not found ' "!error:file" file access error dim as ubyte ff dim as string cline dim as integer eqpos 'open file ff = freefile if open(this.filename for input as ff) > 0 then close ff return "error!file" end if 'search for section do until eof(ff) line input #ff, cline cline = trim(cline) if (left(cline, 1) = "[") and (right(cline, 1) = "]") then 'section found if section = mid(cline, 2, len(cline) - 2) then 'search for key do line input #ff, cline cline = trim(cline) if left(cline, len(key)) = key then 'key found eqpos = instr(cline, "=") return trim(mid(cline, eqpos + 1)) end if loop until (left(cline, 1) = "[") or eof(ff) 'key not found close ff return "!error:key" end if end if loop 'section not found close ff return "!error:section" end function function configfile.cset (key as string, value as string) as byte 'sets the specified key to the given value 'return values: 0 ok ' c_fileerror file access error ' c_error section not found dim as ubyte ff dim as string filedata() 'open file ff = freefile if open(this.filename for input as ff) > 0 then close ff return c_fileerror end if 'read do redim preserve filedata(ubound(filedata) + 1) line input #ff, filedata(ubound(filedata)) loop until eof(ff) close ff 'search for section for i as integer = 1 to ubound(filedata) filedata(i) = trim(filedata(i)) 'section found if filedata(i) = "[" + section + "]" then 'search for key if it exists do until (left(filedata(i), 1) = "[") or (i = ubound(filedata)) i += 1 if left(filedata(i), len(key)) = key then 'key found - modify filedata(i) = key + "=" + value 'write if open(this.filename for output as ff) > 0 then close ff return c_fileerror end if for j as integer = 1 to ubound(filedata) print #ff, filedata(j) next close ff return 0 end if loop 'key not found - make room for it redim preserve filedata(ubound(filedata) + 1) for j as integer = ubound(filedata) to i+1 step -1 filedata(j) = filedata(j-1) next filedata(i+1) = key + "=" + value 'write if open(this.filename for output as ff) > 0 then close ff return c_fileerror end if for j as integer = 1 to ubound(filedata) print #ff, filedata(j) next close ff return 0 exit for end if next 'section not found return c_error end function function configfile.cadd (section as string) as byte 'creates a new section with the given name 'return values: 0 ok ' c_fileerror file access error dim as ubyte ff dim as string filedata() 'open file ff = freefile if open(this.filename for input as ff) > 0 then close ff return c_fileerror end if 'read do redim preserve filedata(ubound(filedata) + 1) line input #ff, filedata(ubound(filedata)) loop until eof(ff) close ff 'add section at end redim preserve filedata(ubound(filedata) + 1) filedata(ubound(filedata)) = "[" + section + "]" 'write if open(this.filename for output as ff) > 0 then close ff return c_fileerror end if for j as integer = 1 to ubound(filedata) print #ff, filedata(j) next close ff return 0 end function function configfile.cenumkeys (keylist() as string) as byte 'fills the passed array with all the keys in the current section 'return values: 0 ok ' c_fileerror file access error ' c_error section not found dim as ubyte ff dim as string cline redim preserve keylist(0) 'open file ff = freefile if open(this.filename for input as ff) > 0 then close ff return c_fileerror end if 'search for section do until eof(ff) line input #ff, cline cline = trim(cline) if (left(cline, 1) = "[") and (right(cline, 1) = "]") then 'section found if section = mid(cline, 2, len(cline) - 2) then 'list keys do line input #ff, cline cline = trim(cline) redim preserve keylist(ubound(keylist) + 1) keylist(ubound(keylist)) = left(cline, instr(cline, "=") - 1) loop until (left(cline, 1) = "[") or eof(ff) close ff return 0 end if end if loop 'section not found close ff return c_error end function function configfile.cenumsections (sectionlist() as string) as byte 'fills the passed array with all the sections in the file 'return values: 0 ok ' c_fileerror file access error dim as ubyte ff dim as string cline redim preserve sectionlist(0) 'open file ff = freefile if open(this.filename for input as ff) > 0 then close ff return c_fileerror end if 'search for sections do until eof(ff) line input #ff, cline cline = trim(cline) if (left(cline, 1) = "[") and (right(cline, 1) = "]") then 'section found redim preserve sectionlist(ubound(sectionlist) + 1) sectionlist(ubound(sectionlist)) = left(right(cline, len(cline) - 1), len(cline) - 2) end if loop close ff end function[/code] | 2009-03-18 | 3:34 PM |
2021 Brandon Cornell