[-] winclass Dictionary [ ] //* class [ ] // Copyright 1997 by Thru-Put Technologies, All Rights Reserved. [ ] // Written by Richard Weth [ ] // [ ] // This class is used for declaring objects that store [ ] // associations of keys and values.The actual data is stored [ ] // in the class data LIST named ldict .. the actual key is limited [ ] // to a string .. but the value can be anytype within reason. [ ] // [ ] // What that statement means is essentially you can store [ ] // other Dictionaries,Lists (of strings), numbers, and constants. [ ] // Runtime structures such as file handles COULD be stored within this [ ] // class. HOWEVER the value of such a structure would have no meaning [ ] // after the test script ran. If you take that into account no [ ] // problem .. however if you invoke the method StoreToFile and try to [ ] // reuse a dynamic runtime structure in another test run .. your results [ ] // will not be what you want. [ ] // [ ] // There is a persistance mechinism to this object. What that means [ ] // is that you can store/initialize dictionary values to/from a file. [ ] // This is a very desirable atribute for attaching large amounts of [ ] // keyed data to a useable data structure. The methods which act [ ] // the ldict LIST are designed to facilitate an easy friendly [ ] // interface into maintaining, retrieving, changeing, and storeing [ ] // this data in a non-volitile fashion. [ ] //* [ ] [+] LIST ldict = {...} //* ivar [ ] //* LIST to contain dictionary data [ ] //* [+] Void Assn (STRING key, ANYTYPE value) [ ] //* [ ] // Assigns the pparm "value" of ANYTYPE to the pparm "key" [ ] // of STRING. This key now becomes the index or associated [ ] // key to retrieving or altering the data it accesses. You [ ] // might make the anology that this key is the index into an [ ] // array which points to the data stored in said array. But [ ] // unlike a cryptic index keys can be user specified and have [ ] // much more meaning .. this will become clear after you [ ] // use this for awhile. [ ] //* [ ] INTEGER inx = 0 [ ] inx = ListFind(ldict,key) [-] if(inx == 0) [ ] ListAppend(ldict,key) [ ] ListAppend(ldict,value) [-] else [ ] ListInsert(ldict,inx+1,value) [ ] ListDelete(ldict,inx+2) [ ] [ ] [+] Void Delete(STRING key) [ ] //* [ ] // Delete's the pparm "value" of ANYTYPE and the pparm "key" [ ] // of STRING from the dictionary. [ ] //* [ ] INTEGER inx = 0 [ ] inx = ListFind(ldict,key) [+] if(inx == 0) [ ] // well there is nothing to delete so my job is done [ ] inx = 0 // gotta do something [+] else [ ] ListDelete(ldict,inx+1) [ ] ListDelete(ldict,inx) [ ] [+] Void Merge (ANYTYPE addDict) [ ] //* [ ] // Merges the "host" dictionary (the dictionary the call is [ ] // being made from "host.addDict()") with the pparm addDict [ ] //* [ ] LIST OF STRING addKlist = addDict.KeyList() [ ] LIST OF STRING Klist = KeyList() [ ] //LIST OF STRING mlist = {...} [ ] STRING key ="" [ ] INTEGER inx [ ] [-] for each key in addKlist [ ] inx = ListFind(ldict,key) [ ] // we only merge in keys that do not already exist [-] if inx == 0 [ ] Assn(key,addDict.GetValue(key)) [ ] [+] Void OverWrite (ANYTYPE addDict) [ ] //* [ ] // Merges the "host" dictionary (the dictionary the call is [ ] // being made from "host.addDict()") with the pparm addDict [ ] //* [ ] LIST OF STRING addKlist = addDict.KeyList() [ ] LIST OF STRING Klist = KeyList() [ ] //LIST OF STRING mlist = {...} [ ] STRING key ="" [ ] INTEGER inx [ ] [-] for each key in addKlist [ ] inx = ListFind(ldict,key) [ ] // we only merge in keys that do not already exist [ ] Assn(key,addDict.GetValue(key)) [ ] [+] List GetDict() [ ] //* [ ] // returns the ivar ldict [ ] //* [ ] return ldict [-] void StoreToFile(STRING p_file) [ ] //* [ ] // Stores the ldict to specified pparm p_file this [ ] // is so the object can have persistence. Use caution when [ ] // employing the persistance functionality. Certain run [ ] // time structures cannot be archived. [ ] //* [ ] [ ] INTEGER len = ListCount(ldict) [ ] [-] if len != 0 [ ] INTEGER i = 0 [ ] HFILE fh = FileOpen(p_file,FM_WRITE) [ ] [-] for i=1 to len step 2 [ ] FileWriteValue (fh,{ldict[i],ldict[i+1]}) [ ] [+] else [ ] print("Sorry but your dictionary is empty .. I don't store empty dictionaries") [ ] //FileWriteValue (fh,ldict) [ ] FileClose(fh) [+] void InitFromFile(STRING p_file) [ ] //* [ ] // Initializes ldict from specified pparm p_file. This is [ ] // so you can use previously stored "key" value pairs. [ ] // This is ideal for tests requiring large and different [ ] // amounts of data. Instead of writing lots of tests [ ] // with different hard coded values .. the same test [ ] // can use the dictionary and init from a diffrerent file. [ ] //* [ ] [ ] [ ] // here is an annoying issue .. if you load ldict [ ] // directly from FileReadValue .. You "clobber" the [ ] // type of ldict from ANYTYPE to string. Preety damn [ ] // annoying .. so an intermediate temp list must be [ ] // employed .. how irritating! [ ] LIST temp = {...} [ ] [ ] // this ensures an initialization [ ] ldict = {} [ ] INTEGER i = 0 [ ] INTEGER len = 0 [ ] HFILE fh [ ] fh = FileOpen(p_file,FM_READ) [-] while (FileReadValue (fh,temp)) [ ] Assn(temp[1],temp[2]) [ ] FileClose(fh) [ ] [ ] [-] void MergeFromFile(STRING p_file) [ ] //* [ ] // Initializes ldict from specified pparm p_file. This is [ ] // so you can use previously stored "key" value pairs. [ ] // This is ideal for tests requiring large and different [ ] // amounts of data. Instead of writing lots of tests [ ] // with different hard coded values .. the same test [ ] // can use the dictionary and init from a diffrerent file. [ ] //* [ ] [ ] [ ] // here is an annoying issue .. if you load ldict [ ] // directly from FileReadValue .. You "clobber" the [ ] // type of ldict from ANYTYPE to string. Preety damn [ ] // annoying .. so an intermediate temp list must be [ ] // employed .. how irritating! [ ] LIST temp = {...} [ ] [ ] INTEGER i = 0 [ ] INTEGER len = 0 [ ] INTEGER inx = 0 [ ] HFILE fh [ ] fh = FileOpen(p_file,FM_READ) [-] while (FileReadValue (fh,temp)) [ ] inx = ListFind(ldict,temp[1]) [ ] // we only merge in keys that do not already exist [-] if inx == 0 [ ] Assn(temp[1],temp[2]) [ ] FileClose(fh) [ ] [ ] [-] void OverWriteFromFile(STRING p_file) [ ] //* [ ] // Overwrites ldict from specified pparm p_file. This is [ ] // so you can use previously stored "key" value pairs. [ ] // This is ideal for tests requiring large and different [ ] // amounts of data. Instead of writing lots of tests [ ] // with different hard coded values .. the same test [ ] // can use the dictionary and init from a diffrerent file. [ ] //* [ ] [ ] [ ] // here is an annoying issue .. if you load ldict [ ] // directly from FileReadValue .. You "clobber" the [ ] // type of ldict from ANYTYPE to string. Preety damn [ ] // annoying .. so an intermediate temp list must be [ ] // employed .. how irritating! [ ] LIST temp = {...} [ ] [ ] INTEGER i = 0 [ ] INTEGER len = 0 [ ] HFILE fh [ ] fh = FileOpen(p_file,FM_READ) [-] while (FileReadValue (fh,temp)) [ ] Assn(temp[1],temp[2]) [ ] FileClose(fh) [ ] [ ] [-] ANYTYPE GetValue(STRING key) [ ] //* [ ] // Returns the value cast as ANYTYPE referanced off [ ] // the associated pparm "key" [ ] //* [ ] INTEGER inx = 0 [ ] inx = ListFind(ldict,key) [-] if(inx == 0) [ ] return null [-] else [ ] return ldict[inx+1] [ ] [+] LIST Of String KeyList() [ ] //* [ ] // Returns a list of STRING of all keys stored in the [ ] // dictionary. [ ] //* [+] LIST Of STRING key_list ={...} [ ] [ ] INTEGER len = ListCount(ldict) [-] if len != 0 [ ] INTEGER i [-] for i = 1 to len step 2 [ ] ListAppend(key_list,ldict[i]) [ ] [ ] return key_list [ ] // [ ] // [ ] [+] ANYTYPE GetRelValue (STRING sIdent) [ ] ANYTYPE ret_val [ ] STRING key [ ] STRING sRel [ ] sRel = this.GetValue("rel") [ ] key = ("{sIdent}"+"_"+"{sRel}") [ ] ret_val = this.GetValue("{key}") [ ] [-] if ret_val == null [ ] LogWarning("could not locate {sIdent}_{sRel} as a key useing {sIdent} instead") [ ] ret_val = this.GetValue("{sIdent}") [ ] [ ] return ret_val [ ] [-] VOID PrintDict(String hdr optional,Boolean sort optional) [-] if hdr == null [ ] hdr = "Dictionary Contents " [-] if sort == null [ ] sort = TRUE [ ] [ ] LIST OF STRING klist = this.KeyList() [ ] STRING key = "" [-] if sort [ ] ListSort(klist) [ ] ResOpenList(hdr) [-] for each key in klist [ ] print("{key}::: {this.GetValue(key)}") [ ] ResCloseList() [ ]