"drhowarddrfine" <ro*******@gmail.comwrote in message
news:63**********************************@2g2000hs n.googlegroups.com...
I have a restaurant menu with prices and other info in a small file.
-navigation
link: link1
link: link2
-menu
chicken:
-roasted
-fried
steak:
-strip
prices:
small: 15.00
medium: 17.00
large: 19.00
-sirloin
prices:
small: 15.00
fish:
-location
In the above example, indentation matters. I am not bound by this
format so better ideas are welcome, but searching for menu->steak-
>>strip->prices->large returning "19.00" seems logical to me.
What complicates things is when I want to do something like
"next_record()". It must tell me there are no more prices in "strip"
and not mistakenly search into the "prices" records of "sirloin".
Is my whole concern my answer, too? That I am trying to write a small
db manager and it's not a simple solution? Or is there simply a
better idea I'm missing?
Are you sure you really want to use C? This looks an ideal job for a rapid
development language. I had a go at this using a toy language, and the
result, which possibly might be helpful, follows below (this code is not
case-sensitive and ! is like a // comment).
If you are going to use C, this code at least shows that you don't need
complex data structures like trees or linked lists; a linear set of records
is stored (or you could use 3 parallel arrays), but the indent level has to
be stored (as 1+). (Because, how large will a restaurant menu ever be?)
This uses a modified form of your menu: no "-" or ":" characters (whatever
they meant), and each indentation is a definite number of "\t" characters
(relying on multiples of 3 spaces might be iffy).
The next_record() function here (as findnext()), will also return any (index
to) records at the same or higher level than current.
Hope this helps.
---------------------------------
type rmenuitem = record(var level,name,value)
var menuitems
var currindex
PROC START=
if not readmenu("menu") then stop fi
forall m in menuitems do
println m
od
!index:=finditem("menu->chicken->fried")
index:=finditem("menu->steak->strip->prices")
if index then
println "Found: ",menuitems[currindex]
while findnext() do
println "Next:",menuitems[currindex]
od
else
println "Not found"
fi
END
FUNCTION FINDITEM(searchstring)=
!return index of item (also in currindex), or 0 if not found
keywords:=splitstring(searchstring,"->")
!println "Keywords=",keywords
currindex:=0
forall i,k in keywords do !i is search level
do
++currindex
if currindex>menuitems.upb then return 0 fi
if menuitems[currindex].level<i then return 0 fi
if menuitems[currindex].name=k then exit fi !ie. break
od
od
return 1
END
FUNCTION FINDNEXT=
!return index of next item after currindex, at same level, otherwise return
0
if currindex=0 or currindex>=menuitems.upb then return 0 fi
currlevel:=menuitems[currindex].level
++currindex
if menuitems[currindex].level<currlevel then return 0 fi
return currindex
END
FUNCTION READMENU(file)=
f:=openfile(file)
if f=0 then return 0 fi
menuitems:=()
nitems:=0
currindex:=0
while not eof(f) do
readln #f,x:"l"
if x="" then loop fi
indent:=0
while left(x)="\t" do
++indent
x:=right(x,-1)
od
sreread()
read name:"n"
read value:"l"
menuitems[++nitems]:=rmenuitem(indent+1,name,value)
od
closefile(f)
return nitems
END
--
Bartc