dialect "rtobjectdraw" import "io" as inout type Bookmark = { description -> String address -> String asBookmarkFileEntry } class bookmark(theDescription: String, theAddress: String) -> Bookmark { method description -> String {theDescription} method address -> String {theAddress} method asString -> String {description++" ---> " ++ address} method asBookmarkFileEntry -> String { "
" ++ description ++ "
" } } def gbm: Bookmark = bookmark ("Google","www.google.com") def abm: Bookmark = bookmark ("Amazon", "www.amazon.com") def pbm: Bookmark = bookmark ("Pomona College","www.pomona.edu") def bookmarks: List[[Bookmark]] = list[gbm,abm,pbm] method printBookmarks (marks: List[[Bookmark]]) -> Done { for (marks) do {bm: Bookmark -> print(bm) } } print ">>printing bookmarks" printBookmarks (bookmarks) print ">>done printing bookmarks" method saveBookmarksFile (marks: List[[Bookmark]]) -> Done { try { def myFile: inout.FileStream = inout.open ("Lec32/bookmarks.txt","w") for (marks) do {bm: Bookmark -> myFile.write (bm.asString ++ "\n") } myFile.close } catch {ex: EnvironmentException -> print "can't access bookmarks file"} } saveBookmarksFile (bookmarks) method saveBookmarksAsWebFile (marks: List[[Bookmark]]) -> Done { try { def myFile: inout.FileStream = inout.open ("Lec32/bookmarks.html", "w") myFile.write ("\n\n

Bookmarks

\n

\n") for (marks) do {bm: Bookmark -> myFile.write (bm.asBookmarkFileEntry ++ "\n") } myFile.write ("

\n\n\n") myFile.close } catch {ex:EnvironmentException -> print "can't access bookmarks file"} } saveBookmarksAsWebFile (bookmarks) // open file for reading var myReadFile: inout.FileStream try{ myReadFile := inout.open ("Lec32/bookmarks.txt", "r") print "pathname is {myReadFile.pathname}" } catch {ex:EnvironmentException -> print "can't open file"} print "reading file:" var contents: String := myReadFile.read print (contents) contents := myReadFile.read print("second: "++contents) print "simple file contents:" print(contents) var lineNumber: Number := 1 while {!myReadFile.eof} do { print (lineNumber.asString ++ ": " ++ myReadFile.getline) lineNumber := lineNumber + 1 } myReadFile.close method retrieveBookmarks -> List[[Bookmark]] { def newMarks: List[[Bookmark]] = list.empty try { def bookmarkFile: inout.FileStream = inout.open ("Lec32/bookmarks.html", "r") while {!bookmarkFile.eof} do { def line: String = bookmarkFile.getline if (line.indexOf (" 0) then { newMarks.add (extractBookmark (line)) } } bookmarkFile.close } catch {ex:EnvironmentException -> print "can't open bookmarks file"} newMarks } method extractBookmark (line: String) -> Bookmark { def start: Number = line.indexOf ("://") + 3 def end: Number = line.indexOf (">") startingAt (start + 1) - 1 def address: String = line.substringFrom (start) to (end) def endDescription: Number = line.indexOf ("") - 1 def description: String = line.substringFrom (end + 2) to (endDescription) bookmark (description, address) } def extracted: List[[Bookmark]] = retrieveBookmarks print "Here are the extracted bookmarks:" printBookmarks (extracted) method countLines (fileName: String) -> Number { var numLines: Number := 0 try { def inputFile: inout.FileStream = inout.open (fileName, "r") while {!inputFile.eof} do { def line: String = inputFile.getline numLines := numLines + 1 } inputFile.close } catch {ex:EnvironmentException -> numLines := 0} numLines } print (countLines ("Lec32/bookmarks.txt"))