This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | Next revision Both sides next revision | ||
pcb:preferences_subsystem [2020/06/28 18:15] cparker |
pcb:preferences_subsystem [2020/06/28 19:23] cparker |
||
---|---|---|---|
Line 81: | Line 81: | ||
=== Functions === | === Functions === | ||
+ | There are a number of key functions here: load_preferences, save_preferences, read_pref_file, write_pref_file, apply_preferences, collect_preferences, readers, and writers. Note that the file IO is being deliberately kept separate so that the actual file format is independent. Also, this allows data from a single file to be scanned more than once against different preference registries. We saw some pseudo-code for the load_preferences and save_preferences above (although it should have used apply_preferences and collect_preferences instead of spelling it out). So let's think about some of the others, again in pseudo-code (python...). | ||
+ | <code python> | ||
+ | def read_pref_file(filename): | ||
+ | with open(filename, r) as f: | ||
+ | f.seek(-1) # seek to the end | ||
+ | flen = f.tell() | ||
+ | r.rewind() | ||
+ | file_data = f.read(flen) | ||
+ | | ||
+ | # make sure to add a null at the end so file_data[flen] is a null char. | ||
+ | |||
+ | # find all the new lines | ||
+ | lines = [0] | ||
+ | for i in range(flen): | ||
+ | c = data[i] | ||
+ | if c == "\n": | ||
+ | data[i] = '\0' | ||
+ | if i + 1 < flen: # more data in file | ||
+ | lines.append(i+1) | ||
+ | | ||
+ | keys = [] | ||
+ | for l in lines: | ||
+ | # ignore lines that start with "#" | ||
+ | if data[l] == "#": continue | ||
+ | # ignore leading whitespace | ||
+ | i = l | ||
+ | while i < flen: | ||
+ | if data[l] in [" ", "\t"]: | ||
+ | i++ | ||
+ | | ||
+ | else: | ||
+ | keys.append[l] | ||
+ | break | ||
+ | | ||
+ | if len(keys) == 0: return | ||
+ | | ||
+ | values = [] | ||
+ | for k in keys: | ||
+ | i = k | ||
+ | while i =< flen: | ||
+ | c = data[i] | ||
+ | if (c == '\0') or (i == flen-1): | ||
+ | # no value for this key | ||
+ | # point it at the key? At a null? | ||
+ | values.append(i) | ||
+ | break | ||
+ | | ||
+ | elif c in [" ", "\t"]: | ||
+ | data[i] = '\0' | ||
+ | if i + 1 < flen: | ||
+ | values.append(i+1) | ||
+ | break | ||
+ | | ||
+ | i++ | ||
+ | | ||
+ | </code> | ||
=== File Format === | === File Format === |