100 lines
2.7 KiB
Plaintext
100 lines
2.7 KiB
Plaintext
--
|
|
-- demo\rosetta\Simple_db.exw
|
|
-- ==========================
|
|
--
|
|
include timedate.e
|
|
|
|
constant filename = getenv(iff(platform()=WINDOWS?"APPDATA":"HOME"))&"/simple_db.csv"
|
|
|
|
procedure add(sequence cmd)
|
|
if length(cmd)=0
|
|
or length(cmd)>2 then
|
|
printf(1,"usage: add name [cat]\n")
|
|
else
|
|
string name = cmd[1]
|
|
string cat = iff(length(cmd)=2?cmd[2]:"none")
|
|
string datestr = format_timedate(date(),"YYYY/MM/DD h:mmpm")
|
|
integer fn = open(filename,"a")
|
|
printf(fn,"%s,%s,%s\n",{name,cat,datestr})
|
|
close(fn)
|
|
end if
|
|
end procedure
|
|
|
|
procedure last(sequence cmd)
|
|
integer fn = open(filename,"r")
|
|
if fn=-1 then
|
|
puts(1,"file not found\n")
|
|
return
|
|
end if
|
|
integer lc = length(cmd)
|
|
string last = iff(lc?"<no entries for that category>\n":"<empty>\n")
|
|
while 1 do
|
|
object line = gets(fn)
|
|
if atom(line) then exit end if
|
|
if lc=0 or split(line,',')[2]=cmd[1] then
|
|
last = line
|
|
end if
|
|
end while
|
|
puts(1,last)
|
|
close(fn)
|
|
end procedure
|
|
|
|
sequence dates
|
|
|
|
function by_date(integer d1, integer d2)
|
|
return compare(dates[d1],dates[d2])
|
|
end function
|
|
constant r_by_date = routine_id("by_date")
|
|
|
|
procedure sort_by_date()
|
|
-- (simple_db.csv should be edited manually to prove the date sort works)
|
|
integer fn = open(filename,"r")
|
|
if fn=-1 then
|
|
puts(1,"file not found\n")
|
|
return
|
|
end if
|
|
sequence lines = {}
|
|
dates = {}
|
|
while 1 do
|
|
object line = gets(fn)
|
|
if atom(line) then exit end if
|
|
lines = append(lines,line)
|
|
dates = append(dates,split(line,',')[3])
|
|
end while
|
|
close(fn)
|
|
sequence tags = custom_sort(r_by_date,tagset(length(lines)))
|
|
for i=1 to length(tags) do
|
|
puts(1,lines[tags[i]])
|
|
end for
|
|
end procedure
|
|
|
|
procedure process(sequence cmd)
|
|
switch cmd[1] do
|
|
case "add": add(cmd[2..$])
|
|
case "last": last(cmd[2..$])
|
|
case "sort": sort_by_date()
|
|
default: printf(1,"unknown command: %s\n",{cmd[1]})
|
|
end switch
|
|
end procedure
|
|
|
|
constant helptext = """
|
|
p demo\rosetta\Simple_db -- interactive mode, commands as below
|
|
p demo\rosetta\Simple_db add name [cat] -- add entry
|
|
p demo\rosetta\Simple_db last [cat] -- show last entry [in specified category]
|
|
p demo\rosetta\Simple_db sort -- show full list sorted by date
|
|
"""
|
|
sequence cl = command_line()
|
|
if length(cl)<3 then
|
|
-- interactive mode
|
|
puts(1,helptext)
|
|
while 1 do
|
|
puts(1,">")
|
|
object line = trim(gets(0))
|
|
if atom(line) or length(line)=0 then exit end if
|
|
puts(1,"\n")
|
|
process(split(line))
|
|
end while
|
|
else
|
|
process(cl[3..$])
|
|
end if
|