string - linking 4 pieces of information and saving them -


saving, editing , loading information. information want load add myself. each line of information contain 4 pieces, (string, integer, string, integer). via 4 seperate edit boxes , button add information 'database' (not sure if need database or if can done via tstringlist). everytime button clicked added content typed @ moment in 'database'.

the demand of saved data when type first string list place rest of information belongs in memobox or edit boxes well. suppose have able search. want keep simple possible. there 10 15 lines of information. , if possible if can load them again later time.

here's basic code should on way. there's no error checking, , you'll no doubt want develop , modify further. point there should ideas write code works you.

now have comma-separated fields, made no attempt handle appearance of commas in of values. if problem choose different delimiter, or escape commas. had toyed writing each field on own line (effectively using newline separator), makes reading code more tricky write.

again, main point not final production code, intended give starting point.

function split(const s: string; separator: char): tstringdynarray; var   i, itemindex: integer;   len: integer;   separatorcount: integer;   start: integer; begin   len := length(s);   if len=0 begin     result := nil;     exit;   end;    separatorcount := 0;   := 1 len begin     if s[i]=separator begin       inc(separatorcount);     end;   end;    setlength(result, separatorcount+1);   itemindex := 0;   start := 1;   := 1 len begin     if s[i]=separator begin       result[itemindex] := copy(s, start, i-start);       inc(itemindex);       start := i+1;     end;   end;   result[itemindex] := copy(s, start, len-start+1); end;  type   tvalue = record     i1, i2: integer;     s: string;   end;    tmydict = class(tdictionary<string,tvalue>)   public     procedure savetofile(const filename: string);     procedure loadfromfile(const filename: string);   end;  { tmydict }  procedure tmydict.savetofile(const filename: string); var   strings: tstringlist;   item: tpair<string,tvalue>; begin   strings := tstringlist.create;   try     item in self begin       strings.add(format(         '%s,%s,%d,%d',         [item.key, item.value.s, item.value.i1, item.value.i2]       ));     end;     strings.savetofile(filename);       freeandnil(strings);   end; end;  procedure tmydict.loadfromfile(const filename: string); var   strings: tstringlist;   item: tpair<string,tvalue>;   line: string;   fields: tstringdynarray; begin   strings := tstringlist.create;   try     strings.loadfromfile(filename);     line in strings begin       fields := split(line, ',');       assert(length(fields)=4);       item.key := fields[0];       item.value.s := fields[1];       item.value.i1 := strtoint(fields[2]);       item.value.i2 := strtoint(fields[3]);       add(item.key, item.value);     end;       freeandnil(strings);   end; end; 

note don't attempt search file on disk. load memory, dictionary , things there.

a dictionary great when use same key. if have multiple keys dictionary less convenient, cares performance impact if you've got 15 records?!

disclaimer: i've not run code, i've not tested it, etc. etc.


Comments

Popular posts from this blog

Javascript line number mapping -

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -