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

[API]Consistant Variables

Started by Mads, 27 March 2012 - 11:38 AM
Mads #1
Posted 27 March 2012 - 01:38 PM
Consistant Variables
This is a new API, by me. It allows you to create variables, that can be used even after the program is exited, or the computer is rebooted. It saves the variables to the disk, and loads them when calling the variable.



NEWS!!!: Now supports tables! though not tableception :o/>/>
To create a table variable, use this code(With your own table ofc:D):
Spoiler

var.create('test', 'table')
var.setValue('test', {1, 'Hello', 'World!', true, false, nil})

Functions
Spoiler

These are the current functions, and I'm planning to add more in future updates

var.create(name, type) --Creates a variable
var.delete(name) --Deletes a variable
var.exists(name) --Checks if a variable exists
var.getType(name) --Returns the type of the variable
var.getValue(name) --Returns the value of the variable
var.setType(name, type) --Sets the type of the variable(deletes the value)
var.setValue(name, value) --Sets the type of the variable(If the type is correct)


Examples
Spoiler

Creating a variable, setting the value and printing the type and value
Spoiler

var.create('test', 'number') --Creating the variable test
var.setValue('test', 13) --Setting the value of test to 13
local sType = var.getType('test')
local value = var.getValue('test')
print(sType..' '..value)

----------------------------------------
Output:
number 13

Changing the type of a variable, and printing it(Using the variable from before)
Spoiler

var.setType('test', 'string')
var.setValue('test', 'I am a string')
local sType = var.getType('test')
local value = var.getValue('test')
print(sType..' '..value)

----------------------------------------
Output:
string I am a string


Removing a variable(Using the variable from before)
Spoiler

var.delete('test')



Code
Spoiler

--Copyright (C) 2012 Mads Clausen, Tomoyo Apps http://www.tomoyo.uphero.com
function exists(name)
if fs.exists('vars/'..name..'.txt') then
  return true
end
end
function getType(name)
if exists(name) then
  local file = io.open('vars/'..name..'.txt', 'r')
  local line = file:read('*l')
  file:close()
  return line
else
  print('var.getType(): Variable does not exist')
  error()
end
end
function getValue(name)
if exists(name) then
  if getType(name) ~= 'table' then
   local file = io.open('vars/'..name..'.txt', 'r')
   local line = file:read('*l')
   line = file:read('*l')
   file:close()
   if getType(name) == 'number' then
	return tonumber(line)
   elseif getType(name) == 'string' then
	return line
   elseif getType(name) == 'boolean' then
	if line == 'true' then
	 return true
	elseif line == 'false' then
	 return false
	end
   elseif getType(name) == 'nil' then
	return nil
   end
  else
   local file = io.open('vars/'..name..'.txt', 'r')
   local line = file:read('*l')
   local lines = {}
   local returnTable = {}
   local i = 1
   line = file:read('*l')
   while line ~= nil do
	lines[i] = line
	line = file:read('*l')
	i = i + 1
   end
   for _i = 1, #lines do
	local pos = string.find(lines[_i], '_')
	local _type = string.sub(lines[_i], 1, pos - 1)
	local value = string.sub(lines[_i], pos + 1)
	if _type == 'number' then
	 returnTable[_i] = tonumber(value)
	elseif _type == 'string' then
	 returnTable[_i] = value
	elseif _type == 'boolean' then
	 if value == 'true' then
	  returnTable[_i] = true
	 elseif value == 'false' thenW
	  returnTable[_i] = false
	 end
	elseif _type == 'nil' then
	 returnTable[_i] = nil
	elseif _type == 'table' then
	 print('var.getValue(): You cannot have a table inside a table')
	end
   end
   return returnTable
  end
else
  print('var.getValue(): Variable does not exist')
  error()
end
end
function setType(name, _type)
if string.lower(_type) ~= 'number' and string.lower(_type) ~= 'string' and string.lower(_type) ~= 'boolean' and string.lower(_type) ~= 'nil' and string.lower(_type) ~= 'table' and exists(name) then
  print('var.setType(): Unsupported type, or the variable already exists')
  error()
else
  local file = io.open('vars/'..name..'.txt', 'w')
  file:write(_type)
  file:close()
end
end
function setValue(name, value)
if exists(name) then
  if tostring(type(value)) ~= getType(name) then
   print('var.setValue(): Wrong type: '..type(value))
   error()
  end
  if getType(name) ~= 'table' then
   local file = io.open('vars/'..name..'.txt', 'r')
   local firstLine = file:read('*l')
   file:close()
   local file = io.open('vars/'..name..'.txt', 'w')
   file:write(firstLine..'n'..tostring(value))
   file:close()
  else
   local file = io.open('vars/'..name..'.txt', 'r')
   local firstLine = file:read('*l')
   file:close()
   local file = io.open('vars/'..name..'.txt', 'w')
   file:write(firstLine..'n')
   for i = 1, #value do
	file:write(type(value[i])..'_'..tostring(value[i])..' n')
   end
   file:close()
  end
else
  print('var.setValue(): Variable does not exist')
  error()
end
end
function create(name, _type)
if not fs.exists('vars') then fs.makeDir('vars') end
if string.lower(_type) ~= 'number' and string.lower(_type) ~= 'string' and string.lower(_type) ~= 'boolean' and string.lower(_type) ~= 'nil' and string.lower(_type) ~= 'table' and exists(name) then
  print('var.create(): Unsupported type, or the variable already exists')
  error()
else
  local file = io.open('vars/'..name..'.txt', 'w')
  file:write(_type)
  file:close()
end
end
function delete(name)
if exists(name) then
  fs.delete('vars/'..name..'.txt')
else
  print('var.delete(): Variable does not exist')
  error()
end
end

Please leave some feedback, so I know what to improve. Enjoy!
kamnxt #2
Posted 27 March 2012 - 03:28 PM
Nice API, but shouldn't it be in the "API's and Utilities" section?
Mads #3
Posted 27 March 2012 - 04:15 PM
Nice API, but shouldn't it be in the "API's and Utilities" section?
Thanks!
It is in the "API's and Utilities" section, but not many people go there :o/>/>
kamnxt #4
Posted 27 March 2012 - 05:20 PM
Maybe you could use the type(x) function so it would automatically set the type?
Like this:

x = "A variable."
var.create("MyVar", type(x)) --creates a string
var.setValue("MyVar",x)
y = 5
var.create("ANumber",type(y)) --creates a number
var.setValue("ANumber",y)
Btw, you could also change the directory from "/vars" to "/.vars" (hide it).
Wolvan #5
Posted 27 March 2012 - 05:24 PM
And that saves the var even if the PC is shutdown
Mads #6
Posted 29 March 2012 - 09:15 AM
And that saves the var even if the PC is shutdown
Yes
Wolvan #7
Posted 29 March 2012 - 01:54 PM
And that saves the var even if the PC is shutdown
Yes
Thank you! That's epic
Mads #8
Posted 29 March 2012 - 06:07 PM
And that saves the var even if the PC is shutdown
Yes
Thank you! That's epic
Thanks! No problem
Wolvan #9
Posted 29 March 2012 - 06:48 PM
Am I allowed to use this in some of my programs I write and which I write for others
Mads #10
Posted 30 March 2012 - 07:47 AM
Am I allowed to use this in some of my programs I write and which I write for others
Yes, but include the license please xD