Simple database: Difference between revisions
Content added Content deleted
Line 2,330: | Line 2,330: | ||
item5,2014-06-03 19:30:23,cat3</pre> |
item5,2014-06-03 19:30:23,cat3</pre> |
||
=={{header|Phix}}== |
|||
<lang Phix>-- |
|||
-- 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</lang> |
|||
Sample session |
|||
<pre> |
|||
C:\Program Files (x86)\Phix>p demo\rosetta\simple_db |
|||
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 |
|||
>sort |
|||
fred,none,2016/07/25 12:30pm |
|||
cliff,none,2016/07/25 12:31pm |
|||
barney,none,2016/07/25 12:32pm |
|||
one,two,2016/07/25 12:33pm |
|||
>add three four |
|||
>sort |
|||
fred,none,2016/07/25 12:30pm |
|||
cliff,none,2016/07/25 12:31pm |
|||
barney,none,2016/07/25 12:32pm |
|||
one,two,2016/07/25 12:33pm |
|||
three,four,2016/07/25 12:39pm |
|||
>last |
|||
three,four,2016/07/25 12:39pm |
|||
>last one |
|||
<no entries for that category> |
|||
>last two |
|||
one,two,2016/07/25 12:33pm |
|||
> |
|||
</pre> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |