Τρίτη 29 Νοεμβρίου 2022

Simple Database (RosettaCode task)


Write the code on a UTF-8 text file as tool.gsb

Assign gsb files to open with M2000.exe (the M2000 environment)

Open the folder where exist tool.gsb in a cmd window

then follow the > lines in the out session

The format of file is xml, Utf8 with a BOM.

The M2000 environment not used here, all the output redirect to console. (Although the environment is a Window program, we can attach the console).

first time the program make a tool.bat

>tool.gsb


So now we can call the tool bat.

>tool

Commands:

tool add name "anyname" tag "tagtext" date "YYYY.MM.DD.HH:MM"

tool latest

tool latest-per-tag

tool all-entries


>tool add name "BOB" tag "EAT A LOT" date "2022.10.21.04:00"

>tool add name "JOHN" tag "DRINK WATER" "date 2022.10.15.12:00"

>tool add name "PAUL" tag "EAT A LOT" date "2022.10.13.22:00"

>tool add name "SUZAN" tag "DRINK WATER" date "2022.10.13.12:00"

>tool add name "PHILIP" tag "DRINK WATER" date "2022.10.13.10:00"

>tool add name "MONDY" tag "EAT A LOT" date "2022.10.10.12:00"

>tool add name "MARY" tag "PIZZA FRIENDLY" date "2022.10.10.12:00"

>tool add name "DONALD" tag "PIZZA FRIENDLY" date "2022.10.10.02:00"

>tool add name "GEORGE" date "2022.11.29.16:55"


This is the last print from last tool addd:

<?xml version="1.0" encoding="utf-8-sig"?>

<MyFile>

    <Row name="BOB" tag="EAT A LOT" date="2022.10.21.04:00"></Row>

    <Row name="PAUL" tag="EAT A LOT" date="2022.10.13.22:00"></Row>

    <Row name="SUZAN" tag="DRINK WATER" date="2022.10.13.12:00"></Row>

    <Row name="PHILIP" tag="DRINK WATER" date="2022.10.13.10:00"></Row>

    <Row name="MONDY" tag="EAT A LOT" date="2022.10.10.12:00"></Row>

    <Row name="MARY" tag="PIZZA FRIENDLY" date="2022.10.10.12:00"></Row>

    <Row name="DONALD" tag="PIZZA FRIENDLY" date="2022.10.10.02:00"></Row>

    <Row name="GEORGE" date="2022.11.29.16:55"></Row>

</MyFile>



>tool latest

The latest entry is:

GEORGE,,2022.11.29.16:55


>tool latest-per-tag

latest entry for each tag:

GEORGE,,2022.11.29.16:55

PHILIP,DRINK WATER,2022.10.13.10:00

MONDY,EAT A LOT,2022.10.10.12:00

DONALD,PIZZA FRIENDLY,2022.10.10.02:00


>tool all-entries

All entries sorted by date:

DONALD,PIZZA FRIENDLY,2022.10.10.02:00

MONDY,EAT A LOT,2022.10.10.12:00

MARY,PIZZA FRIENDLY,2022.10.10.12:00

PHILIP,DRINK WATER,2022.10.13.10:00

SUZAN,DRINK WATER,2022.10.13.12:00

PAUL,EAT A LOT,2022.10.13.22:00

BOB,EAT A LOT,2022.10.21.04:00

GEORGE,,2022.11.29.16:55



V1.1 Added msg box if you load it from M2000 console

To edit the code from console use Edit "tool.gsb" this open the file for edit and at the exit save it (you can use Shift F12 to skip saving (drop changes). Edit using string open editor, and if the file extension is gsb then we get syntax coloring. You can find txt file using File "txt"

menu  // clear menu internal array
files + "txt"  // fill menu array using names of file .txt from current directory
menu ! // open menu using internal array
if menu<>0 then
// edit text - use Esc to exit - use Shift F12 to drop changes and exit
edit menu$(menu)+".txt"
end if

The editor use the split screen, so you can make a split, say Cls, 12  (from 13th row, see the comma, we use the same color  (as from last CLS) for clearing the low part of screen). We can use SCROLL SPLIT 12 to set the split row without clearing any part of screen. The editor open on a control, above the screen. This editor open when we use Edit.Doc for documents (we can use transparency, so we edit text above a background image). Although Edit and Edit.Doc use the split row, and  full with, there is another statement, Input which can be used to get text from a user defined area on the screen. Also for user forms (Input/Key$/Inkey$ can't be used for those type of layers) we have controls like EditBox and TextBox for input text, and EditBox for multiline text (has a programmable syntax coloring system). There are some interesting modules in Info (Info.gsb, we get it with setup, or you can download from github), like MEditor (A Form to edit M2000 programs, in a form, with two Editbox controls, one for edit code and another one for help, also this use a control to alter the size of boxes as split control), CS a csharp editor (you can compile csharp programs), htmlEditor (for html/css), and see the Wind4 (picj randomly one of 5 types for input for M2000 console, including two for multiline text, Edit.Doc and  an Input variation)






MODULE GLOBAL interpret {
global filename$="base1.xml"
module latest {
PrintConsoleLn("The latest entry is:")
if exist(filename$) else exit
declare xml xmlData
with xml, "xml" as doc$, "beautify" as beautify
doc$=string$(eval$(buffer(filename$)) as UTF8dec)
with xml, "lastchild" set child
with child,"attr" as attr$()
PrintConsoleLn(attr$("name")+","+@tag$()+","+attr$("date"))
declare xml nothing
}
module latestForEachTag {
PrintConsoleLn("latest entry for each tag:")
if exist(filename$) else exit
declare xml xmlData
with xml, "xml" as doc$, "beautify" as beautify
doc$=string$(eval$(buffer("base1.xml")) as UTF8dec)
with xml, "firstchild" as firstchild
child=firstchild
with child,"attr" as attr$()
inventory alfa
do
if not exist(alfa, @tag$()) then
append alfa, @tag$():=child
else
return alfa, @tag$():=child
end if
Method xml, "EndOffChilds", &child as ok
when ok
sort alfa
k=each(alfa)
while k
child=eval(k)
PrintConsoleLn(attr$("name")+","+@tag$()+","+attr$("date"))
end while
declare xml nothing
}
module All {
PrintConsoleLn("All entries sorted by date:")
if exist(filename$) else exit
declare xml xmlData
with xml, "xml" as doc$, "beautify" as beautify
doc$=string$(eval$(buffer("base1.xml")) as UTF8dec)
with xml, "firstchild" as firstchild
child=firstchild
with child,"attr" as attr$()
inventory alfa
i=0
do
// prevent same keys using a unique patch key
append alfa, attr$("date")+str$(i,"000000"):=child
i++
Method xml, "EndOffChilds", &child as ok
when ok
sort alfa
k=each(alfa)
while k
child=eval(k)
PrintConsoleLn(attr$("name")+","+@tag$()+","+attr$("date"))
end while
declare xml nothing
}
module add (line$) {
line$=trim$(line$)
if line$="" then exit
declare xml xmlData
with xml, "xml" as doc$, "beautify" as beautify
bom$=str$(format$("\uef\ubb\ubf"))
//  len(bom$)=1.5 (1.5*2=3 bytes)
k=0
if exist(filename$) then try {k=filelen(filename$)}
if k<10 then
method xml, "PrepareNodeSimple", "xml" as ProcessInstructions
method xml, "PlaceAttributeToNode", ProcessInstructions, "version", "1.0"
method xml, "PlaceAttributeToNode", ProcessInstructions, "encoding", "utf-8-sig"
method xml, "PlaceProcessingInstructions", ProcessInstructions
method xml, "PrepareNode", "MyFile" as Node
method xml, "InsertNode", Node
else
doc$=string$(eval$(buffer(filename$)) as UTF8dec)
end if
a$=""""+line$
def name$, tag$,date$
do
a$=rightpart$(a$, """") : what$=lcase$(trim$(leftpart$(a$, """")))
if what$="" then exit
a$=rightpart$(a$, """") :par$=leftpart$(a$, """")
select case what$
case "name"
name$=par$
case "tag"
tag$=par$
case "date"
date$=par$
end select
always
if name$<>"" and date$<>"" then
method xml, "PrepareNode", "Row", "" as Node1
method xml, "PlaceAttributeToNode", Node1, "name", name$
if tag$<>"" then method xml, "PlaceAttributeToNode", Node1, "tag", tag$
method xml, "PlaceAttributeToNode", Node1, "date", date$
method xml, "AppendChild", Node1
open filename$ for wide output as #f
print #f, bom$;string$(doc$ as UTF8enc);
close #f
beautify=-4
PrintConsoleLn(doc$)
end if
declare xml nothing
}
declare FreeConsole lib "Kernel32.FreeConsole"
declare GetStdHandle lib "Kernel32.GetStdHandle" {long a}
declare AttachConsole lib "Kernel32.AttachConsole" {long a}
declare CloseHandle lib "Kernel32.CloseHandle" {long a}
declare global WriteCons Lib "Kernel32.WriteConsoleW" {long cons, a$, long n, Long p, long u}
long STD_OUTPUT_HANDLE=-11
global retvalue
buffer clear retvalue as long
ret=AttachConsole(-1)
global m=GetStdHandle(STD_OUTPUT_HANDLE)
if ret=0 then
beep
push Ask("Run from cmd line","Problem",,"")
drop
exit
end if
if not islet then
try {
open "tool.bat" for output as #f
print #f, {@}+appdir$+{m2000.exe data {%*}: dir %cd%:load tool
}
close #f
}
PrintConsoleLn("")
dos "tool.bat"

else
read cmd$
cmd$=trim$(cmd$)+" "
select case lcase$(leftpart$(cmd$, " "))
case "add"
add rightpart$(cmd$," ")
case "latest"
latest
case "latest-per-tag"
latestForEachTag
case "all-entries"
all
case else
help()
end select
end if
call void closehandle(m)
call void freeconsole()


Sub PrintConsole(a$)
      Call Void WriteCons(m, a$, Len(a$), retvalue(0), 0)
End Sub
Sub PrintConsoleLn(a$)
a$+={
}
Call Void WriteCons(m, a$, Len(a$), retvalue(0), 0)
End Sub
function tag$()
try {
=attr$("tag")
}
end function
Sub Help()
h$={Commands:
tool add name "anyname" tag "tagtext" date "YYYY.MM.DD.HH:MM"
tool latest
tool latest-per-tag
tool all-entries
}
PrintConsole(h$)
End Sub
}
module interpret1 {
try {interpret}
}
interpret1: end



Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου

You can feel free to write any suggestion, or idea on the subject.