From 10d453e5d7ff18ae3076c617f72751bd7fd947f4 Mon Sep 17 00:00:00 2001 From: Tom Willemsen Date: Thu, 27 Dec 2012 03:08:55 +0100 Subject: [PATCH] Update linkwave * Add parsing of command line arguments, if the first argument starts with "http://" assume it's a new bookmark and save it to the database. * Add adding bookmarks to the database. * linkwave.scm (string-no-null): Only try to remove the final null byte if the string is longer than 0 characters and only if the last character _is_ a null character. --- linkwave.scm | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/linkwave.scm b/linkwave.scm index 7e26a05..f36c5e2 100644 --- a/linkwave.scm +++ b/linkwave.scm @@ -5,13 +5,38 @@ (: string-no-null (string -> string)) (define (string-no-null str) - (substring str 0 (- (string-length str) 1))) + (if (and (> (string-length str) 0) + (char=? (string-ref str (- (string-length str) 1)) #\nul)) + (substring str 0 (- (string-length str) 1)) + str)) (: print-row (string fixnum string string -> void)) (define (print-row url seconds name description) (format #t "~a~% ~a~% ~a~% ~a~%~%" (string-no-null name) (string-no-null description) (string-no-null url) (seconds->string seconds))) +(define (add-bookmark url name description tags) + (execute db "INSERT INTO bookmark VALUES (?, STRFTIME('%s'), ?, ?)" + url name description) + (let ((bookmark-id (last-insert-rowid db))) + (for-each (lambda (tag) + (let ((tag-id (first-result db "SELECT rowid FROM tag WHERE name = ?" tag))) + (unless tag-id + (execute db "INSERT INTO tag VALUES (?)" tag) + (set! tag-id (last-insert-rowid db))) + (execute db "INSERT INTO bookmark_tag VALUES (?, ?)" + bookmark-id tag-id))) + tags))) + (define db (open-database (data-file "linkwave.db"))) -(for-each-row print-row db "select * from bookmark") + +(let ((cla (command-line-arguments))) + (if (null? cla) + (for-each-row print-row db "select * from bookmark") + (cond + ((and (>= (string-length (car cla)) 7) (string= (substring (car cla) 0 7) "http://")) + (add-bookmark (car cla) (cadr cla) (caddr cla) (cdddr cla))) + (else + (format #t "Unrecognized option: ~a~%" (car cla)))))) + (finalize! db #t)