This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ShadowDisruptor's profile picture

string.find help and fs.readAll()

Started by ShadowDisruptor, 15 October 2014 - 10:42 PM
ShadowDisruptor #1
Posted 16 October 2014 - 12:42 AM
Hello! So I'm working on a config API (It'll be pretty simple to do once I get this bug figured out) I get an error on line 60 and all similar lines due to the configData returning nil (Yes, I am calling the configSet before running the other codes) Another error I found when debugging the line 60 error is that no matter what the code dosen't work. In the code I set configData to "test[Banana]" and ran this code in a separate file:


os.loadAPI("config")
print(config.getString("test"))

What I would get is just a blank printed line.

Thanks for any help I get!

My code:
Spoiler

--[[
Coded by Cameron Lund
Bukkit: ShadowDisruptor
BukkitDev: ShadowDisrupter
Minecraft: footballfan12

License
Fair-use
--]]
configSet = false
function setConfig(config)
configSet = true
if fs.exists(config) then
if not configFile == nil then
configFile.close()
configFile = fs.open((config), "r")
else
configFile = fs.open((config), "r")
end
if not configFileWrite == nil then
configFileWrite.close()
configFileWrite = fs.open((config), "a")
else
configFileWrite = fs.open((config), "a")
end
return true
else
return nil
end
configData = configFile.readAll()
end

function reloadConfig()
if configSet == true then
configData = configFile.readAll()
return true
else
return nil
end
end

function doesExist(node)
if configSet == true then
if not string.find(node, configData) == nil then
return true
else
return false
end
else
return nil
end
end

function getString(node)
if configSet == true then
if string.find(".", node) then

else
if not string.find(node.."[", configData) == nil then
pos2, pos1 = string.find(node.."[",configData)
pos3, pos2 = string.find("]",configData,pos1)
theString = string.sub(configData,pos1,pos2)
return theString
else
return nil
end
end
else
return nil
end
end
(Not sure why copying isn't copying my formatting.. sorry if it's hard to read)
EDIT:
Want the formatting? Check it out on pastebin. http://pastebin.com/0GUjr64m
Edited on 15 October 2014 - 10:46 PM
valithor #2
Posted 16 October 2014 - 01:33 AM
It is never getting to line 30, because you are returning true on line 26 which causes it stop doing anything else in the function. By using return it breaks the function and does not do anything else below it. Because of this configData is nil when you attempt to use it later.
Edited on 15 October 2014 - 11:38 PM
ShadowDisruptor #3
Posted 16 October 2014 - 03:26 AM
It is never getting to line 30, because you are returning true on line 26 which causes it stop doing anything else in the function. By using return it breaks the function and does not do anything else below it. Because of this configData is nil when you attempt to use it later.
Actually, that wasn't the issue. I figured out the issue. The 'return' only calls if it successfully runs that section of code. If it does successfully run that section, I want it to end the function. No reason to keep compiling after it's finished its job.
valithor #4
Posted 16 October 2014 - 05:03 AM
It is never getting to line 30, because you are returning true on line 26 which causes it stop doing anything else in the function. By using return it breaks the function and does not do anything else below it. Because of this configData is nil when you attempt to use it later.
Actually, that wasn't the issue. I figured out the issue. The 'return' only calls if it successfully runs that section of code. If it does successfully run that section, I want it to end the function. No reason to keep compiling after it's finished its job.

Woops reading over it guess i left out a part… Meant to say that on both line 26 and 28 you have returns one following a if and the other following a else statement. Because of this no matter what, there is a return before you actually set the variable to something so that code is unreachable. So it is nil. I understand you dont want to continue compiling, but you made it to where you never reached where you set the variable as i said earlier. I do not see how you could have fixed this issue without either removing a return or moving the place where you set the variable above one of them.

It is fine not to want to make it run more than it has to, but you have to make sure that when you make it stop that you are not making code under it unreachable. You could have moved the variable set above the return true or even instead of making it return make it return by "return configFile.readAll()".

This way you could use something like this at any point in the code. Would make it useful if you want to have multiple config files in the same program for some reason.
configData = setConfig(filenamehere)

function setConfig(config)
		configSet = true
		if fs.exists(config) then
				if not configFile == nil then
						configFile.close()
						configFile = fs.open((config), "r")
				else
						configFile = fs.open((config), "r")
				end
				if not configFileWrite == nil then
						configFileWrite.close()
						configFileWrite = fs.open((config), "a")
				else
						configFileWrite = fs.open((config), "a")
				end
				return true -- program breaks here if the file does exist
		else
				return nil -- program breaks here if the file does not exist
		end
		configData = configFile.readAll()  -- no matter what it will never reach here
end

This was only a answer to half of your question. "I get an error on line 60 and all similar lines due to the configData returning nil (Yes, I am calling the configSet before running the other codes)"
Edited on 17 October 2014 - 02:41 AM