Advertisement
NanoBob

NanoDatabase v3.0

Dec 24th, 2014
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.90 KB | None | 0 0
  1. local defaultFolder="databases/"
  2. local defaultFolderName=string.gsub(defaultFolder,"%/","")
  3. local defaultFileName="database.txt"
  4. local readOnly=false
  5. local debugMode=false
  6.  
  7. if fs.isDir(defaultFolderName)==false then
  8.     fs.makeDir(defaultFolderName)
  9. end
  10.  
  11. function createDataFile(fileName)
  12.     databaseFileName=defaultFolder..fileName or defaultFileName
  13.     if fs.exists(databaseFileName) then
  14.         debugMessage("A file with that name already exists.")
  15.         return false,"A file with that name already exists."
  16.     else
  17.         file=fs.open(databaseFileName,"w")
  18.         if file==nil then
  19.             debugMessage("Unable to create the file")
  20.             return false,"Unable to create the file."
  21.         else
  22.              file.write("Beginning of file.\n\n<Empty Entry>\n\nEnd of file")
  23.              file.close()
  24.              return databaseFileName
  25.         end
  26.     end
  27. end
  28.  
  29. function setData(fileName,key,value)
  30.     if fs.exists(defaultFolder..fileName) then
  31.         file=fs.open(defaultFolder..fileName,"r")
  32.         fullFile=file.readAll()
  33.         file.close()
  34.         start,stop=string.find(fullFile,"<<"..key..">")
  35.         if start==nil then
  36.             start,stop=string.find(fullFile,"<Empty Entry>")   
  37.             newFile=string.gsub(fullFile,"<Empty Entry>","<<"..key..">=<"..value..">>".."\n<Empty Entry>")
  38.             file=fs.open(defaultFolder..fileName,"w")
  39.             file.write(newFile)
  40.             file.close()
  41.             return true
  42.         else
  43.             file.close()
  44.             local oldData=getData(fileName,key)
  45.             newFile=string.gsub(fullFile,"<<"..key..">=<"..oldData..">>","<<"..key..">=<"..value..">>")
  46.             file=fs.open(defaultFolder..fileName,"w")
  47.             file.write(newFile)
  48.             file.close()
  49.             return true
  50.         end
  51.     else
  52.         debugMessage("A file with the name "..fileName.." does not yet exist (failed to setData).")
  53.         return false,"A file with the name "..fileName.." does not yet exist (failed to setData)."
  54.     end
  55. end
  56.  
  57. function getData(fileName,key)
  58.     if fs.exists(defaultFolder..fileName) then
  59.         file=fs.open(defaultFolder..fileName,"r")
  60.         fullFile=file.readAll()
  61.         file.close()
  62.         start,stop=string.find(fullFile,"<<"..key..">")
  63.         if start==nil then
  64.             debugMessage("No data found under that key.")
  65.             return false,"No data found under that key."
  66.         else
  67.             local line=getLine(fullFile,key)
  68.             local __,stop=string.find(line,"%>=<")
  69.             local start=stop+1
  70.             local tempLine=string.sub(line,start,string.len(line))
  71.             local stop,__=string.find(tempLine,"%>>")
  72.             local data=string.sub(tempLine,0,stop-1)
  73.             return data
  74.         end
  75.     else
  76.         debugMessage("A file with the name "..fileName.." does not yet exist (failed to getData).")
  77.         return false,"A file with the name "..fileName.." does not yet exist (failed to getData)."
  78.     end
  79. end
  80.  
  81. function getLine(fullString,key)
  82.     local start,__=string.find(fullString,"<<"..key..">=<")
  83.     local temp=string.sub(fullString,start,string.len(fullString))
  84.     local __,stop=string.find(temp,"%>>")
  85.     local line=string.sub(temp,0,stop)
  86.     return line
  87. end
  88.  
  89. function debugMessage(message)
  90.     if debugMode then
  91.         print("*DATABASE* "..message)
  92.     end
  93. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement