diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/hallo.nim | 7 | ||||
-rw-r--r-- | examples/htmlrefs.nim | 58 | ||||
-rw-r--r-- | examples/htmltitle.nim | 37 | ||||
-rw-r--r-- | examples/readme.txt | 5 |
4 files changed, 107 insertions, 0 deletions
diff --git a/examples/hallo.nim b/examples/hallo.nim new file mode 100644 index 000000000..73ef2cb95 --- /dev/null +++ b/examples/hallo.nim @@ -0,0 +1,7 @@ +# Hallo world program + +import strutils + +echo($(parseFloat("0.5")* toFloat(5000) / toFloat(parseInt("5000")))) + +echo("Hallo world!") diff --git a/examples/htmlrefs.nim b/examples/htmlrefs.nim new file mode 100644 index 000000000..cf1b3be28 --- /dev/null +++ b/examples/htmlrefs.nim @@ -0,0 +1,58 @@ +# Example program to show the new parsexml module +# This program reads an HTML file and writes all its used links to stdout. +# Errors and whitespace are ignored. +# (c) 2009 Andreas Rumpf + +import os, streams, parsexml, strutils + +proc `=?=` (a, b: string): bool = + # little trick: define our own comparator that ignores case + return cmpIgnoreCase(a, b) == 0 + +if paramCount() < 1: + quit("Usage: htmlrefs filename[.html]") + +var links = 0 # count the number of links +var filename = appendFileExt(ParamStr(1), "html") +var s = newFileStream(filename, fmRead) +if s == nil: quit("cannot open the file" & filename) +var x: TXmlParser +open(x, s, filename) +next(x) # get first event +block mainLoop: + while true: + case x.kind + of xmlElementOpen: + # the <a href = "xyz"> tag we are interested in always has an attribute, + # thus we search for ``xmlElementOpen`` and not for ``xmlElementStart`` + if x.elementName =?= "a": + x.next() + if x.kind == xmlAttribute: + if x.attrKey =?= "href": + var link = x.attrValue + inc(links) + # skip until we have an ``xmlElementClose`` event + while true: + x.next() + case x.kind + of xmlEof: break mainLoop + of xmlElementClose: break + else: nil + x.next() # skip ``xmlElementClose`` + # now we have the description for the ``a`` element + var desc = "" + while x.kind == xmlCharData: + desc.add(x.charData) + x.next() + Echo(desc & ": " & link) + else: + x.next() + of xmlEof: break # end of file reached + of xmlError: + Echo(errorMsg(x)) + x.next() + else: x.next() # skip other events + +echo($links & " link(s) found!") +x.close() + diff --git a/examples/htmltitle.nim b/examples/htmltitle.nim new file mode 100644 index 000000000..6cdfd90eb --- /dev/null +++ b/examples/htmltitle.nim @@ -0,0 +1,37 @@ +# Example program to show the new parsexml module +# This program reads an HTML file and writes its title to stdout. +# Errors and whitespace are ignored. +# (c) 2009 Andreas Rumpf + +import os, streams, parsexml, strutils + +if paramCount() < 1: + quit("Usage: htmltitle filename[.html]") + +var filename = appendFileExt(ParamStr(1), "html") +var s = newFileStream(filename, fmRead) +if s == nil: quit("cannot open the file" & filename) +var x: TXmlParser +open(x, s, filename) +while true: + x.next() + case x.kind + of xmlElementStart: + if cmpIgnoreCase(x.elementName, "title") == 0: + var title = "" + x.next() # skip "<title>" + while x.kind == xmlCharData: + title.add(x.charData) + x.next() + if x.kind == xmlElementEnd and cmpIgnoreCase(x.elementName, "title") == 0: + Echo("Title: " & title) + quit(0) # Success! + else: + echo(x.errorMsgExpected("/title")) + + of xmlEof: break # end of file reached + else: nil # ignore other events + +quit("Could not determine title!") +x.close() + diff --git a/examples/readme.txt b/examples/readme.txt new file mode 100644 index 000000000..e6e47c1d1 --- /dev/null +++ b/examples/readme.txt @@ -0,0 +1,5 @@ +In this directory you will find several examples for how to use the Nimrod +library. + +Copyright (c) 2004-2009 Andreas Rumpf. +All rights reserved. |