All pastes #743841 Raw Edit

Unnamed

public lua v1 · immutable
·published 2007-10-20 20:27 UTC
rendered paste body
file = {}	function file.Write(file,dat)	f = io.open(file,"w")	f:write(dat)	f:close()endfunction file.Read(file)	f = io.open(file,"r")	local buf = f:read("*all")	f:close()	return bufendfunction file.Exists(file)	if io.open(file,"r") then		return true	end	return falseendfunction print_r (t, indent, done)  done = done or {}  indent = indent or ""  local nextIndent -- Storage for next indentation value  for key, value in pairs (t) do    if type (value) == "table" and not done [value] then      nextIndent = nextIndent or          (indent .. string.rep(" ",string.len(tostring (key))+2))          -- Shortcut conditional allocation      done [value] = true      print (indent .. "[" .. tostring (key) .. "] => Table {");      print  (nextIndent .. "{");      print_r (value, nextIndent .. string.rep(" ",2), done)      print  (nextIndent .. "}");    else      print  (indent .. "[" .. tostring (key) .. "] => " .. tostring (value).."")    end  endendfunction explode(div,str)	if (div=="") then return false end	local pos,arr = 0,{}	-- for each divider found	for st,sp in function() return string.find(str,div,pos,true) end do		table.insert(arr,string.sub(str,pos,st-1)) -- Attach chars left of current divider		pos = sp + 1 -- Jump past current divider	end	table.insert(arr,string.sub(str,pos)) -- Attach chars right of last divider	return arrend_DBLoader = {}	function DBLoader(dbname)	local obj = {}	for k,v in pairs(_DBLoader) do obj[k]=v end	obj.dbname = dbname	obj.db = {}	return objendfunction _DBLoader:Set(key,value)	key = tostring(key)	if type(value) == "table" then		local kbuf= "table&"		for k,v in pairs(value) do			self:Set("tbl_"..key.."_"..k,v)			kbuf = kbuf.."tbl_"..key.."_"..k..":"		end		kbuf = kbuf:sub(1,-2) --gets rid of trailing :		self.db[key]=kbuf		return	end	if _G["to"..type(value)] == nil then		value = tostring(value)	end	self.db[key]=valueendfunction _DBLoader:Get(key)	if not self.db[key] then return end	if string.find(self.db[key],"table&") then		--we got a table		local expl = explode(":",self.db[key]:gsub("table&",""))		local bufT = {}		for _,v in pairs(expl) do			k=v:gsub("tbl_"..key.."_","") -- get rid of the table key prefix			bufT[k]=self:Get(v)		end		return bufT	end	return self.db[key]endfunction _DBLoader:Save()	local buf=""	for k,v in pairs(self.db) do		buf = buf ..type(v).."-"..k.." "..tostring(v).."\n"	end	file.Write(self.dbname,buf)endfunction getKV(str,term)	if #term > 1 then return end	local chk = false	local k,v = "",""	for i=1,#str do		local ch = str:sub(i,i)		if not chk then			if ch == term then				chk = true			else				k = k .. ch			end		else			v = v .. ch		end	end	return k,vendfunction _DBLoader:Load()	if not file.Exists(self.dbname) then return end	local expl = explode("\n",file.Read(self.dbname))	--print_r(expl)	for k,v in pairs(expl) do		if v == "" then			table.remove(expl,k)		end	end	for _,v in pairs(expl) do		vtype,str = getKV(v,"-")		k,v = getKV(str," ")		self.db[k] = _G["to"..vtype](v)	endend