mirror of
https://github.com/Gnucash/gnucash.git
synced 2025-02-25 18:55:30 -06:00
Remove exceptions and use patterns in parse-price-line.
Also use a more standard numberic parser instead of an ad-hoc string parser. Code written by Christopher Lam.
This commit is contained in:
parent
6bf062c738
commit
34946c7461
@ -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))
|
||||
@ -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
|
||||
@ -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
|
||||
@ -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))))
|
||||
|
@ -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?
|
||||
@ -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))
|
||||
@ -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)))
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user