-- TIME_STAMP 2019-02-26 10:19:46 v 0.2 do local oDict = { ------------------------------------------------------------------------------------------------ lasterr = 0, -- error value from last called function lastcall = '', -- the name of the last called function callline = 0, -- the script line from the function call dict = {}, -- the dictionary itself ------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------ new = function(self, _dict) -- creates a new dictionary object local _obj = {} -- optionally from a passed dictionary setmetatable(_obj, self) self.__index = self if _dict == nil then self.lastcall = 'oDict:new( )' else self.lastcall = 'oDict:new(DICT)' end if debug then self.callline = debug.getinfo(2).currentline end self.dict = _dict or {} return _obj end, ------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------ keys = function(self, _bCount) -- gets all keys as table local tmp = _bCount or ' ' self.lastcall = 'oDict:keys('..tostring(tmp)..')' if debug then self.callline = debug.getinfo(2).currentline end self.lasterr = 0 local tkeys = {} for k in pairs(self.dict) do table.insert(tkeys, k) end if _bCount == true then return #tkeys end return tkeys end, ------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------ exists = function(self, _key) -- checks if an key already exists local tkeys = self:keys() self.lastcall = 'oDict:exists('..tostring(_key)..')' self.lasterr = 0 if debug then self.callline = debug.getinfo(2).currentline end for i in pairs(tkeys) do if tkeys[i] == _key then return true end end return false end, ------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------ count = function(self) -- gets the count of key-value pairs local n = self:keys(true) self.lastcall = 'oDict:count( )' self.lasterr = 0 if debug then self.callline = debug.getinfo(2).currentline end return n end, ------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------ del = function(self, _key) -- deletes one entry by its key if not self:exists(_key) then if debug then self.callline = debug.getinfo(2).currentline end self.lastcall = 'oDict:del('..tostring(_key)..')' self.lasterr = 2 return self end if debug then self.callline = debug.getinfo(2).currentline end self.lastcall = 'oDict:del('..tostring(_key)..')' self.lasterr = 0 self.dict[_key] = nil return self end, ------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------ delall = function(self) -- deletes all entries if debug then self.callline = debug.getinfo(2).currentline end self.lastcall = 'oDict:delall( )' self.lasterr = 0 self.dict = {} return self end, ------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------ add = function(self, _key, _val) -- adds one key-value pair if self:exists(_key) then if debug then self.callline = debug.getinfo(2).currentline end self.lastcall = 'oDict:add('..tostring(_key)..', '..self:_valtype(_val)..')' self.lasterr = 1 return self end if debug then self.callline = debug.getinfo(2).currentline end self.lastcall = 'oDict:add('..tostring(_key)..', '..self:_valtype(_val)..')' self.lasterr = 0 self.dict[_key] = _val return self end, ------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------ set = function(self, _key, _val) -- sets a new value for a key if not self:exists(_key) then if debug then self.callline = debug.getinfo(2).currentline end self.lastcall = 'oDict:set('..tostring(_key)..', '..self:_valtype(_val)..')' self.lasterr = 2 return self end if debug then self.callline = debug.getinfo(2).currentline end self.lastcall = 'oDict:set('..tostring(_key)..', '..self:_valtype(_val)..')' self.lasterr = 0 self.dict[_key] = _val return self end, ------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------ val = function(self, _key) -- gets the value from a key if not self:exists(_key) then if debug then self.callline = debug.getinfo(2).currentline end self.lastcall = 'oDict:val('..tostring(_key)..')' self.lasterr = 2 return nil end if debug then self.callline = debug.getinfo(2).currentline end self.lastcall = 'oDict:val('..tostring(_key)..')' self.lasterr = 0 return self.dict[_key] end, ------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------ _valtype = function(self, _val) -- internal function sType = type(_val) if sType == 'string' then return _val elseif sType == 'number' or sType == 'boolean' then return tostring(_val) elseif sType == nil then return '' else return sType end end, ------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------ err = function(self, _bPrint) -- gets or prints the last error with text if _bPrint == nil then _bPrint = true end local sErrText, pre, sDebug = '', '! ', '' if debug then sDebug = string.format('@@_Debug_line\t\t%d\t', self.callline) end if self.lasterr == 1 then sErrText = '\n! [1] Trying to add a existing key.' elseif self.lasterr == 2 then sErrText = '\n! [2] Trying to access a nonexistent key.' else sErrText = '\n+ [0] None error occurred.' pre = '+ ' end if debug then pre = '' end sErrText = pre..sDebug..self.lastcall..sErrText if _bPrint then print(sErrText) return self else return sErrText end end, ------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------ doc = function(self) -- documentation of all functions return [[ + ===== FUNCTION LIST ===== < oDict:new( [_dict] ) > Creates a new dictionary object, optionally from an existing dictionary. > Returns the dictionary object. < oDict:add( _key, _val ) > Adds one key-value pair to the dictionary. If the key already exists, oDict.lasterr will set. > With oDict:err() you get the error as text. > Returns the dictionary object. < oDict:set( _key, _val ) > Changes the value of a key. If the key not exists, oDict.lasterr will set. > Returns the dictionary object. < oDict:val( _key ) > Returns the value of a key. If the key not exists, oDict.lasterr will set. < oDict:del( _key ) > Deletes an key-value pair. If the key not exists, oDict.lasterr will set. > Returns the dictionary object. < oDict:delall( ) > Deletes all pairs from the dictionary. > Returns the dictionary object. < oDict:exists( _key ) > Checks, if the key in the dictionary exists. > Returns true/false. < oDict:count( ) > Returns the count of key-value pairs. < oDict:keys( ) > Returns a table with all keys. < oDict:err( [_bPrint] ) > Prints the last error (the error value from the last called function) as text to console and > returns the dictionary object. Optionally (_bPrint=false) it will return the text. < oDict:doc( ) > Returns this documentation. + =========================== ]] end ------------------------------------------------------------------------------------------------ } ---------------------------------------------------------------------------------------------------- return oDict ---------------------------------------------------------------------------------------------------- end