Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local defaultFolder="databases/"
- local defaultFolderName=string.gsub(defaultFolder,"%/","")
- local defaultFileName="database.txt"
- local readOnly=false
- local debugMode=false
- if fs.isDir(defaultFolderName)==false then
- fs.makeDir(defaultFolderName)
- end
- function createDataFile(fileName)
- databaseFileName=defaultFolder..fileName or defaultFileName
- if fs.exists(databaseFileName) then
- debugMessage("A file with that name already exists.")
- return false,"A file with that name already exists."
- else
- file=fs.open(databaseFileName,"w")
- if file==nil then
- debugMessage("Unable to create the file")
- return false,"Unable to create the file."
- else
- file.write("Beginning of file.\n\n<Empty Entry>\n\nEnd of file")
- file.close()
- return databaseFileName
- end
- end
- end
- function setData(fileName,key,value)
- if fs.exists(defaultFolder..fileName) then
- file=fs.open(defaultFolder..fileName,"r")
- fullFile=file.readAll()
- file.close()
- start,stop=string.find(fullFile,"<<"..key..">")
- if start==nil then
- start,stop=string.find(fullFile,"<Empty Entry>")
- newFile=string.gsub(fullFile,"<Empty Entry>","<<"..key..">=<"..value..">>".."\n<Empty Entry>")
- file=fs.open(defaultFolder..fileName,"w")
- file.write(newFile)
- file.close()
- return true
- else
- file.close()
- local oldData=getData(fileName,key)
- newFile=string.gsub(fullFile,"<<"..key..">=<"..oldData..">>","<<"..key..">=<"..value..">>")
- file=fs.open(defaultFolder..fileName,"w")
- file.write(newFile)
- file.close()
- return true
- end
- else
- debugMessage("A file with the name "..fileName.." does not yet exist (failed to setData).")
- return false,"A file with the name "..fileName.." does not yet exist (failed to setData)."
- end
- end
- function getData(fileName,key)
- if fs.exists(defaultFolder..fileName) then
- file=fs.open(defaultFolder..fileName,"r")
- fullFile=file.readAll()
- file.close()
- start,stop=string.find(fullFile,"<<"..key..">")
- if start==nil then
- debugMessage("No data found under that key.")
- return false,"No data found under that key."
- else
- local line=getLine(fullFile,key)
- local __,stop=string.find(line,"%>=<")
- local start=stop+1
- local tempLine=string.sub(line,start,string.len(line))
- local stop,__=string.find(tempLine,"%>>")
- local data=string.sub(tempLine,0,stop-1)
- return data
- end
- else
- debugMessage("A file with the name "..fileName.." does not yet exist (failed to getData).")
- return false,"A file with the name "..fileName.." does not yet exist (failed to getData)."
- end
- end
- function getLine(fullString,key)
- local start,__=string.find(fullString,"<<"..key..">=<")
- local temp=string.sub(fullString,start,string.len(fullString))
- local __,stop=string.find(temp,"%>>")
- local line=string.sub(temp,0,stop)
- return line
- end
- function debugMessage(message)
- if debugMode then
- print("*DATABASE* "..message)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement