Skip to content

Commit

Permalink
Remove exceptions and use patterns in parse-price-line.
Browse files Browse the repository at this point in the history
Also use a more standard numberic parser instead of an ad-hoc
string parser.

Code written by Christopher Lam.
  • Loading branch information
jwhite66 authored and jralls committed Sep 10, 2023
1 parent 6bf062c commit 34946c7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
43 changes: 18 additions & 25 deletions gnucash/import-export/qif-imp/qif-file.scm
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
(use-modules (srfi srfi-1))
(use-modules (srfi srfi-13))
(use-modules (ice-9 rdelim))
(use-modules (ice-9 match))
(use-modules (gnucash qif-import qif-objects))
(use-modules (gnucash qif-import qif-utils))
(use-modules (gnucash qif-import qif-parse))
Expand Down Expand Up @@ -832,7 +833,7 @@
(start-sub 1)
(check-and-parse-field
qif-price:share-price qif-price:set-share-price! gnc-numeric-equal
qif-parse:check-number-format '(decimal comma)
qif-parse:check-number-format '(decimal comma rational)
qif-parse:parse-number/format (qif-file:prices self)
qif-parse:print-number
'guess-on-ambiguity add-error 'share-price
Expand Down Expand Up @@ -883,7 +884,7 @@
(start-sub 0.1)
(check-and-parse-field
qif-xtn:share-price qif-xtn:set-share-price! gnc-numeric-equal
qif-parse:check-number-format '(decimal comma)
qif-parse:check-number-format '(decimal comma rational)
qif-parse:parse-number/format (qif-file:xtns self)
qif-parse:print-number
'guess-on-ambiguity add-error 'share-price
Expand Down Expand Up @@ -1147,26 +1148,18 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(define (qif-file:parse-price-line line)
(let ((current-xtn (make-qif-price)))
(if (not (false-if-exception
(let* ((symbol (string-trim-both (string-trim-both (car (string-split line #\,)) char-set:punctuation)))
(price (cadr (string-split line #\,)))
(fracprice (string-split price #\space))
(date (string-trim-both (string-trim-both (caddr (string-split line #\,)) char-set:punctuation))))

(qif-price:set-symbol! current-xtn symbol)
(qif-price:set-date! current-xtn date)

;; If we have a fractional portion, convert it to float
(if (> (length fracprice) 1)
(let ((total (+ (string->number (car fracprice))
(string->number (cadr fracprice)))))
(set! price (format #f "~f" total))))

(qif-price:set-share-price! current-xtn price)

;; A blank entry will not necessarily throw an exception, but is invalid
(if (or (string-null? symbol) (or (string-null? price) (string-null? date)))
(set! current-xtn #f)))))
(set! current-xtn #f))
current-xtn))
(define (remove-punctuation s) (string-trim-both s char-set:punctuation))
(and (string? line)
(match (string-split line #\,)
(((= remove-punctuation symbol) (= gnc-numeric-from-string value) (= remove-punctuation date))
(cond
((string-null? symbol) #f)
((not value) #f)
((string-null? date) #f)
(else
(let ((xtn (make-qif-price)))
(qif-price:set-symbol! xtn symbol)
(qif-price:set-date! xtn date)
(qif-price:set-share-price! xtn (number->string value))
xtn))))
(_ #f))))
8 changes: 7 additions & 1 deletion gnucash/import-export/qif-imp/qif-parse.scm
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,11 @@
(and regexp-enabled?
(make-regexp "^ *[$]?[+-]?[$]?[0-9]+[+-]?$|^ *[$]?[+-]?[$]?[0-9]?[0-9]?[0-9]?([\\.'][0-9][0-9][0-9])*(,[0-9]*)?[+-]? *$|^ *[$]?[+-]?[$]?[0-9]+,[0-9]*[+-]? *$")))

;; eg 1/2 or 32/15 or 4
(define rational-regexp
(and regexp-enabled?
(make-regexp "^-?[0-9]+(/[0-9]+|)$")))

;; eg 456 or 123
(define integer-regexp
(and regexp-enabled?
Expand All @@ -419,6 +424,7 @@
(define numtypes-alist
(list (cons 'decimal decimal-radix-regexp)
(cons 'comma comma-radix-regexp)
(cons 'rational rational-regexp)
(cons 'integer integer-regexp)))
(filter (lambda (fmt) (regexp-exec (assq-ref numtypes-alist fmt) value-string))
possible-formats))
Expand All @@ -439,7 +445,7 @@
((comma) (gnc:string-replace-char
(gnc:string-delete-chars filtered-string ".")
#\, #\.))
((integer) filtered-string)))
((integer rational) filtered-string)))
(num (or (string->number (string-append "#e" read-string)) 0)))
(if has-minus? (- num) num)))

Expand Down

0 comments on commit 34946c7

Please sign in to comment.