Splitting a massive string to print with
#1
Posted 06 March 2013 - 09:59 AM
In short, I must add the entire log to the page, how do I split up the massive string so it can fit on the pages?
#2
Posted 06 March 2013 - 10:06 AM
#3
Posted 06 March 2013 - 10:12 AM
function printLog(log, fromLn, copies)
if open == true then
error("Log must be ended before printing")
end
--printer setup
local side = nil
local sides = { 1 = "front", 2 = "back", 3 = "left", 4 = "right", 5 = "top", 6 = "bottom" }
local no = 1
for i = 1, 6 do
if no == 1 then
side = sides[no]
elseif no == 2 then
side = sides[no]
elseif no == 3 then
side = sides[no]
elseif no == 4 then
side = sides[no]
elseif no == 5 then
side = sides[no]
elseif no == 6 then
side = sides[no]
else
error("no printer found")
end
end
local p = peripheral.wrap(side)
h = fs.open("log/"..log..".log", r)
local printstr = nil
for i = 1, fromLn do
h.readLine()
end
printstr = h.readAll()
fs.close()
end
its not quite finished as you'll see (the actual printing part is missing)
#4
Posted 06 March 2013 - 10:52 AM
samdeman22, on 06 March 2013 - 10:12 AM, said:
h = fs.open("log/"..log..".log", r)
When opening a file the mode must be in string format like so:
h = fs.open("log/"..log..".log", "r")
Also since you defined the file as "h", you must close it like so:
h.close()
I think storing all the data in a table then printing it will be your best bet
EDIT: "printing" should be indexing (Nub)
#5
Posted 06 March 2013 - 10:57 AM
#6
Posted 06 March 2013 - 12:20 PM
#7
Posted 06 March 2013 - 12:21 PM
#8
Posted 06 March 2013 - 05:52 PM
#9
Posted 06 March 2013 - 06:23 PM
remiX, on 06 March 2013 - 05:52 PM, said:
If it is the same function I am thinking of, it was not written by him, but is actually an example function found in the PIL. The one I'm thinking of is the one that's been floating around the forums for quite some time. I simply don't wish it to be misattributed if that is the case.
#10
Posted 06 March 2013 - 07:13 PM
Lyqyd, on 06 March 2013 - 06:23 PM, said:
remiX, on 06 March 2013 - 05:52 PM, said:
If it is the same function I am thinking of, it was not written by him, but is actually an example function found in the PIL. The one I'm thinking of is the one that's been floating around the forums for quite some time. I simply don't wish it to be misattributed if that is the case.
Oh, sorry if that is the case. He has it licensed in his api and told me that he did make it.
#11
Posted 15 March 2013 - 08:40 AM
--The Log API 1.1
runningProgram = "test"
function openLog(name)
h = fs.open("logs/"..name..".log", "a")
end
function closeLog()
h.close()
end
--function setRunningProgram(name)
-- runningProgram = name
--end
function generateLog() --will create the Dir "log" in the root and create a .log file based on the running program (if if is not already there)
if not fs.exists("log") then
fs.mkDir("log")
end
if runningProgram == nil then
error("program not specified")
end
h = fs.open("logs/"..runningProgram..".log", "a")
h.writeLine("--- The "..runningProgram.." event log ---")
h.writeLine(" ")
h.close()
end
function addEntry(type, str)
local time = os.time()
local day = os.day()
h.writeLine("["..day.."]".."["..time.."]".."["..type.."] | #"..str)
end
function startLog()
open = true
local time = os.time()
local day = os.day()
openLog()
h.writeLine("["..day.."]".."["..time.."] | #STARTING LOG")
end
function endLog()
open = false
local time = os.time()
local day = os.day()
h.writeLine("["..day.."]".."["..time.."] | #ENDING LOG")
end
function setPrinterSide(side)
printside = side
end
function printLog(log, fromLn, copies)
if open == true then
error("Log must be ended before printing")
end
end
#12
Posted 15 March 2013 - 08:44 AM
#13
Posted 15 March 2013 - 08:56 AM
#14
Posted 15 March 2013 - 10:09 AM
#15
Posted 15 March 2013 - 12:11 PM
#16
Posted 17 March 2013 - 02:05 AM
remiX, on 06 March 2013 - 05:52 PM, said:
#17
Posted 17 March 2013 - 02:21 AM
#18
Posted 30 March 2013 - 07:03 AM
-- a function you cant access ;P
local function round(num idp)
local mult = 10 ^ (idp or 0)
return (math.floor(num * mult + 0.5) / mult)
end
function printLog(log, side, fromLn, toLn)
if open == true then
error("Log must be ended before printing")
end
if not fs.exists( "logs/"..log..".log") then
error("Log does not exist")
end
h = fs.open("logs/"..log..".log", "r")
page = {}
while h.readLine() ~= nil do
local ln = h.readLine()
table.insert(page, ln)
end
p = peripheral.wrap(side)
local paper = p.getPaperLevel()
local ink = p.getInkLevel()
local w, h = p.getPageSize()
local doc = {}
for i = fromLn, toLn do
table.insert(doc, page[i])
end
local length = 0
for i = 0, #doc do
length = length + doc[i]
end
local size = w*h
local pages = round(length/size, 0)
if pages > paper then
error("not enough paper")
end
if pages > ink then
error("not enough ink")
end
for i = 0, #doc do
p.write(doc[i])
end
end
This looks good to me but on loading the API I get "bios:338: [string "log"]:59: ' ) ' expected". Why ?
#19
Posted 30 March 2013 - 07:05 AM
#20
Posted 30 March 2013 - 07:13 AM
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











