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

add quotes to a user input

Started by resaloli, 16 July 2013 - 06:59 PM
resaloli #1
Posted 16 July 2013 - 08:59 PM
i have been for alot of hours trying by myself to tranform an user input to qoutes so here is the code that im using:

print("What Password you want?")
local pass = read("*")
local line = "local password = "
fs.copy("/pass", "/installer")
sleep(3);
local file = io.open("/installer", "a")
if file then
	term.setCursorPos(1,1)
	file:write(line)
	file:write(password)
	file:write(code)
	file:close()
	fs.copy("installer1", "startup")
	fs.delete("installer")
	print("Done")
	sleep(2)
	os.reboot()
end

so the user input the desired password and its stored in the variable pass then when the setup is adding the variables "line" and "pass" to the code it comes up with for example: local password = test and i want to transform that to local password = "test"
So basicly i want to what is a variable in one code i want it to be a string in other code.

pls help?
Lyqyd #2
Posted 17 July 2013 - 12:11 AM
Split into new topic.

There are a couple options. You can use escaped quotes:


print("this is a string with \"escaped\" quotes")

Or you can use a different quotation mark and concatenate them without escaping:


print("this is a string that uses a "..[["]].."variety"..'"'.." of quotation marks for demonstration purposes")
resaloli #3
Posted 17 July 2013 - 02:39 AM
Split into new topic. There are a couple options. You can use escaped quotes:
 print("this is a string with \"escaped\" quotes") 
Or you can use a different quotation mark and concatenate them without escaping:
 print("this is a string that uses a "..[["]].."variety"..'"'.." of quotation marks for demonstration purposes") 


i used the contenate method but now i got other problem that is using the mode "a" to edit the file when opening it drops the line on the end of the code and i wanted to know if theres one way to put that on top of the code without to use the mode "w" because it deletes the old code on the file.And add a line between them because i cant use "\n" it gives me the error unespected symbol
Edited on 17 July 2013 - 12:46 AM
Kingdaro #4
Posted 17 July 2013 - 02:45 AM
Read the entire file, put whatever text you want before the read content, then rewrite the entire file.


local filePath = 'a-file-i-want-to-add-stuff-to'

local file = fs.open(filePath, 'r')
local content = file.readAll()
file.close()

local stuff = 'The string I want at the top of the file.'
content = stuff .. '\n' .. content

file = fs.open(filePath, 'w')
file.write(content)
file.close()
immibis #5
Posted 17 July 2013 - 04:54 AM
Or change the lock program so it reads the password from a different file (so e.g. 'startup' is the lock program and 'password' contains the password)
albrat #6
Posted 17 July 2013 - 07:40 AM
You could write a little encryption program, take a password from the setup screen, write the password to a single file like immibis said, then when your "startup" file runs just load the password from the file and decrypt it. / store in a varible for checking purposes.

This way even if someone can read the file… they can't obtain the password.

for the encryption a random number generator from 1 to 9, write it all into a string. Save the string and password to file. First line is the decryption sequence and the second line is the pass, read each line back one char at a time and it reverses your encryption….
theoriginalbit #7
Posted 17 July 2013 - 07:53 AM
everyone is also missing the other obvious one, string formatting.


file:write( string.format("local password = %q", password) )
or

file:write( ("local password = %q"):format(password) )


You could write a little encryption program, take a password from the setup screen, write the password to a single file like immibis said, then when your "startup" file runs just load the password from the file and decrypt it. / store in a varible for checking purposes.

This way even if someone can read the file… they can't obtain the password.

for the encryption a random number generator from 1 to 9, write it all into a string. Save the string and password to file. First line is the decryption sequence and the second line is the pass, read each line back one char at a time and it reverses your encryption….
Hashing is the best way rather than encryption… Using a SHA256 or hashing algorithm like that (i.e. no MD5 or SHA1) with a SALT would be best… This is common practise for passwords as hashed strings are not reversible (without using a lookup table that already contains that password + salt pair), as opposed to an encryption which is designed to be reversed… To check the password entered is correct the process just involves hashing the entered a password with the salt and compare the two hashed strings…
resaloli #8
Posted 17 July 2013 - 11:38 AM
Thx guy for all help, got it working i used:
Read the entire file, put whatever text you want before the read content, then rewrite the entire file.


local filePath = 'a-file-i-want-to-add-stuff-to'

local file = fs.open(filePath, 'r')
local content = file.readAll()
file.close()

local stuff = 'The string I want at the top of the file.'
content = stuff .. '\n' .. content

file = fs.open(filePath, 'w')
file.write(content)
file.close()


there is the final code ready to be used, i used the original computercraft lock code just to start but then i created my own but with admin access so he can terminate the program, added a GUI to chose wich program to start(i will create more XD), heres is the pastebin (pastebin get W0UuR1uX [any name]) then when installed just type "/disk/gui" no quotes, and thats it.

Thx for all suport Best community :D/>
Edited on 17 July 2013 - 06:23 PM
theoriginalbit #9
Posted 17 July 2013 - 11:42 AM
heres is the pastebin MUST BE SAVED AS "install" without quotes

Replace your line

shell.run("delete", "/install")

With

fs.delete( shell.getRunningProgram() )

That will delete the program, no matter what the person names it.
resaloli #10
Posted 17 July 2013 - 08:22 PM
heres is the pastebin MUST BE SAVED AS "install" without quotes

Replace your line

shell.run("delete", "/install")

With

fs.delete( shell.getRunningProgram() )

That will delete the program, no matter what the person names it.

thx for the detail :D/> updated