Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » C

Searching a small file database

 
Jump to:  
 
drhowarddrfine
PostPosted: Tue Jun 24, 2008 4:35 pm    Post subject: Searching a small file database
       
I don't want to use a db manager, like mysql, for such a small
database but I'm finding this trickier than I thought and hope someone
can provide some guidance.

I have a restaurant menu with prices and other info in a small file.
It's set up in a YAML-ish style, if you're familiar with that format.
I'm just looking for some ideas on the best way to retrieve data based
on a "keyword". What complicates things for me is that some of these
keywords might be inside a nested block, such as "prices:".

-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-
Quote:
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?

Originally I did this in XML but felt parsing the tags wasn't any
easier and my format, above, is more readable. I can't just store a
struct in the file because I want it to be editable with any text
editor.

Thanks,
Doc
 

 
Eric Sosman
PostPosted: Tue Jun 24, 2008 4:35 pm    Post subject: Re: Searching a small file database
       
drhowarddrfine wrote:
Quote:
I don't want to use a db manager, like mysql, for such a small
database but I'm finding this trickier than I thought and hope someone
can provide some guidance.

I have a restaurant menu with prices and other info in a small file.
It's set up in a YAML-ish style, if you're familiar with that format.
[...]

I'm not, but Google is. It seems that libraries for dealing
with YAML in several programming languages, including C, have
already been written. Use one of them.

--
Eric.Sosman@sun.com
 

 
Eric Sosman
PostPosted: Tue Jun 24, 2008 4:35 pm    Post subject: Re: Searching a small file database
       
drhowarddrfine wrote:
Quote:
On Jun 24, 11:42 am, Eric Sosman <Eric.Sos...@sun.com> wrote:
drhowarddrfine wrote:
I don't want to use a db manager, like mysql, for such a small
database but I'm finding this trickier than I thought and hope someone
can provide some guidance.
I have a restaurant menu with prices and other info in a small file.
It's set up in a YAML-ish style, if you're familiar with that format.
[...]
I'm not, but Google is. It seems that libraries for dealing
with YAML in several programming languages, including C, have
already been written. Use one of them.

But it's not YAML. I don't know that I want to take the time to learn
all of YAML or its libraries. Perhaps because I'm in a bit of a rush
to get this part done. I feel a need to learn how to do this, too,
and not have some library do this for me.

Then your problem seems to be more about algorithms than
about the language you implement them in. Seriously: Your
questions to this point would be the same whether you were
implementing in C or Java or Lisp or assembly -- which is
a pretty good indication that they've been addressed to the
wrong forum. (Fora, actually, because I notice you've multi-
posted to at least two newsgroups. Consider your wrist slapped.)

I don't know where to refer you, other than to the YAML
documentation and/or to studying the YAML sources. Yes, I
heard your "it's not YAML," but you said it was "YAML-ish"
so you ought to be able to get some ideas, at least.

Also, you'll need to develop a more precise description of
your format than "YAML-ish but not YAML" before you can even
begin to parse and search it. Remember Douglas Adams' line
about a beverage "almost, but not quite exactly, unlike tea?"
That's how your description reads at the moment, and computers
lack sufficient imagination to fill in the details on their own.

--
Eric.Sosman@sun.com
 

 
drhowarddrfine
PostPosted: Tue Jun 24, 2008 4:47 pm    Post subject: Re: Searching a small file database
       
On Jun 24, 11:42 am, Eric Sosman <Eric.Sos...@sun.com> wrote:
Quote:
drhowarddrfine wrote:
I don't want to use a db manager, like mysql, for such a small
database but I'm finding this trickier than I thought and hope someone
can provide some guidance.

I have a restaurant menu with prices and other info in a small file.
It's set up in a YAML-ish style, if you're familiar with that format.
[...]

     I'm not, but Google is.  It seems that libraries for dealing
with YAML in several programming languages, including C, have
already been written.  Use one of them.

--
Eric.Sos...@sun.com

But it's not YAML. I don't know that I want to take the time to learn
all of YAML or its libraries. Perhaps because I'm in a bit of a rush
to get this part done. I feel a need to learn how to do this, too,
and not have some library do this for me.
 

 
Bartc
PostPosted: Tue Jun 24, 2008 4:50 pm    Post subject: Re: Searching a small file database
       
"drhowarddrfine" <robbelics@gmail.com> wrote in message
news:63ec1f2d-fe44-4621-b68c-ef748465b41f@2g2000hsn.googlegroups.com...

Quote:
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
 

 
drhowarddrfine
PostPosted: Tue Jun 24, 2008 6:51 pm    Post subject: Re: Searching a small file database
       
Quote:
     Then your problem seems to be more about algorithms than
about the language you implement them in.
--
Eric.Sos...@sun.com

You're right, now that you mention it.
 

 
soscpd
PostPosted: Tue Jun 24, 2008 7:45 pm    Post subject: Re: Searching a small file database
       
Hello Doc, List

Sqlite can be one of your (best, IMHO) options.

LINK

Regards
Rafael
 

 
drhowarddrfine
PostPosted: Tue Jun 24, 2008 8:14 pm    Post subject: Re: Searching a small file database
       
@Bartc,
Your comment about "how large can a restaurant menu be" is my thought
exactly. Just scanned your code and, if nothing else, it shows my
initial thoughts were along the same line and I have a little more
confidence in proceeding with my own code.

On Jun 24, 2:45 pm, soscpd <sos...@gmail.com> wrote:
Quote:
Hello Doc, List

Sqlite can be one of your (best, IMHO) options.

LINK

Regards
Rafael

I considered sqlite but for such a small amount of data, I just don't
feel this need to install any other programs.
 

 
Martien Verbruggen
PostPosted: Tue Jun 24, 2008 8:19 pm    Post subject: Re: Searching a small file database
       
On Tue, 24 Jun 2008 14:02:08 -0400,
Eric Sosman <Eric.Sosman@sun.com> wrote:
Quote:
Remember Douglas Adams' line
about a beverage "almost, but not quite exactly, unlike tea?"

Almost, but not quite, entirely unlike tea.

The placement of that comma makes a difference.

Martien
--
| Yes; Windows is great for running &
Martien Verbruggen | developing viruses, for instance. It's also
| very popular, but then again, so is the
| common cold. -- Dave Hinz
 

 
Michael Snoyman
PostPosted: Wed Jun 25, 2008 3:06 am    Post subject: Re: Searching a small file database
       
On Tue, 24 Jun 2008 13:14:33 -0700, drhowarddrfine wrote:

Quote:
I considered sqlite but for such a small amount of data, I just don't
feel this need to install any other programs.

Actually, with the sqlite amalgamation you only need add two files to
your source tree (sqlite3.c and sqlite3.h). I use sqlite all the time and
can simply pass off binaries with sqlite included.
 

Page 1 of 1 .:.

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

dzwonki na komórke ubezpieczenie emerytalne Tani Numer Shazza moja miłość - Big Cyc wupe