Advertisement
ladyDia

Config file stuff

Jan 23rd, 2025 (edited)
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.94 KB | None | 0 0
  1. local fileLocation = "config.txt"
  2. local rawFile = io.open(fileLocation,"rb")
  3. local currentLine = ""
  4. local configValues = {}
  5. local tempTable = {}
  6. local stringToData = {
  7.     ["true"] = true,
  8.     ["false"] = false,
  9.     ["nil"] = nil
  10. }
  11.  
  12. local function stripString(inputString, splitChar)
  13.     --[[
  14.         Takes in a string, removes all characters that match the
  15.         splitChar, and outputs a table of strings from where the
  16.         splitChar was.
  17.     --]]   
  18.     local outputTable = {}
  19.     local tempString = ""
  20.     if not type(inputString) == "string" then  
  21.         print("stripString: input not string")
  22.         goto skip
  23.     end
  24.     for index = 1, #inputString, 1 do
  25.         if string.sub(inputString,index,index) == splitChar then
  26.             table.insert(outputTable,tempString)
  27.             tempString = ""
  28.             goto continue
  29.         end
  30.         tempString = tempString .. string.sub(inputString,index,index)
  31.         ::continue::
  32.     end
  33.     table.insert(outputTable,tempString)
  34.     ::skip::
  35.     return outputTable
  36. end
  37.  
  38. local function cullString(inputString, cullChar)
  39.     --[[
  40.         Takes in a string and removes all instances of a character
  41.         from it
  42.     --]]
  43.     local tempString = ""
  44.     local tempChar = ""
  45.     for index = 1, string.len(inputString), 1 do
  46.         tempChar = string.sub(inputString,index,index)
  47.         if tempChar ~= cullChar then
  48.             tempString = tempString .. tempChar
  49.         end
  50.     end
  51.     return tempString
  52. end
  53.  
  54. local function loadConfig()
  55.     --[[
  56.         Wipes the stored config and reloads it
  57.     --]]
  58.     configValues = {}
  59.     rawFile = io.open(fileLocation,"rb")
  60.     while currentLine ~= nil do
  61.         currentLine = rawFile:read("*line")
  62.         tempTable = stripString(currentLine, "=")
  63.         if #tempTable = 2 then
  64.             if stringToData[tempTable[2]] == nil then
  65.                 configValues[cullString(tempTable[1]," ")] = tempTable[2]
  66.             else
  67.                 configValues[cullString(tempTable[1]," ")] = stringToData[tempTable[2]]
  68.             end
  69.         end
  70.     end
  71.     io.close(rawFile)
  72. end
  73.  
  74.  
  75.  
  76.  
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement