From b47f04539ef20e409b7516a5886c1b6c459df874 Mon Sep 17 00:00:00 2001
From: Peter Broadbery
Date: Thu, 12 Nov 2015 20:26:18 +0000
Subject: [PATCH] Move account & split.scm to engine-utilities
---
src/engine/engine-utilities.scm | 67 +++++++++++++++++++
src/engine/engine.scm | 13 ++++
src/report/report-system/Makefile.am | 4 +-
src/report/report-system/account.scm | 57 ----------------
src/report/report-system/split.scm | 13 ----
.../report-system/test/test-account.scm | 2 +-
src/report/report-system/test/test-split.scm | 2 +-
src/report/standard-reports/budget.scm | 3 +-
src/report/standard-reports/cash-flow.scm | 3 +-
9 files changed, 85 insertions(+), 79 deletions(-)
diff --git a/src/engine/engine-utilities.scm b/src/engine/engine-utilities.scm
index 6838f66d8e..1310ff9605 100644
--- a/src/engine/engine-utilities.scm
+++ b/src/engine/engine-utilities.scm
@@ -20,6 +20,12 @@
;; Boston, MA 02110-1301, USA gnu@gnu.org
;; Copyright 2000 Rob Browning
+(use-modules (gnucash gnc-module))
+
+(gnc:module-begin-syntax (gnc:module-load "gnucash/engine" 0))
+
+(use-modules (srfi srfi-1)
+ (srfi srfi-13))
(define (gnc:account-map-descendants thunk account)
(let ((descendants (or (gnc-account-get-descendants-sorted account) '())))
@@ -28,3 +34,64 @@
(define (gnc:account-map-children thunk account)
(let ((children (or (gnc-account-get-children-sorted account) '())))
(map thunk children)))
+
+;; account related functions
+;; is account in list of accounts?
+(define (account-same? a1 a2)
+ (or (eq? a1 a2)
+ (string=? (gncAccountGetGUID a1) (gncAccountGetGUID a2))))
+
+(define account-in-list?
+ (lambda (account accounts)
+ (cond
+ ((null? accounts) #f)
+ ((account-same? (car accounts) account) #t)
+ (else (account-in-list? account (cdr accounts))))))
+
+;; Optimized version of accout-in-list if we know
+;; the list in advance.
+(define (account-in-list-pred accounts)
+ (define (my-assoc str alist)
+ (find (lambda (pair) (account-same? str (car pair))) alist))
+ (define (my-hash acc size)
+ (remainder (string-hash (gncAccountGetGUID acc)) size))
+ (let ((hash-table (make-hash-table)))
+ (for-each (lambda (acc) (hashx-set! my-hash my-assoc hash-table acc #t))
+ accounts)
+ (lambda (account)
+ (hashx-ref my-hash my-assoc hash-table account))))
+
+(define account-in-alist
+ (lambda (account alist)
+ (cond
+ ((null? alist) #f)
+ ((account-same? (caar alist) account) (car alist))
+ (else (account-in-alist account (cdr alist))))))
+
+;; helper for sorting of account list
+(define (account-full-name a b)
+ (string (gnc-account-get-full-name a) (gnc-account-get-full-name b)))
+
+;; return maximum depth over accounts and their children, if any
+(define (accounts-get-children-depth accounts)
+ (apply max
+ (map (lambda (acct)
+ (let ((acct-depth (gnc-account-get-current-depth acct)))
+ (+ acct-depth (- (gnc-account-get-tree-depth acct) 1))))
+ accounts)))
+
+;; Splits
+(export split-same?)
+(export split-in-list?)
+
+(define (split-same? s1 s2)
+ (or (eq? s1 s2)
+ (string=? (gncSplitGetGUID s1) (gncSplitGetGUID s2))))
+
+(define split-in-list?
+ (lambda (split splits)
+ (cond
+ ((null? splits) #f)
+ ((split-same? (car splits) split) #t)
+ (else (split-in-list? split (cdr splits))))))
+
diff --git a/src/engine/engine.scm b/src/engine/engine.scm
index c0498865b3..5c52b1441f 100644
--- a/src/engine/engine.scm
+++ b/src/engine/engine.scm
@@ -67,9 +67,22 @@
(export GNC_COMMODITY_NS_MUTUAL)
(export gnc:url->loaded-session)
+
+;; engine-utilities.scm
(export gnc:account-map-descendants)
(export gnc:account-map-children)
+(export account-same?)
+(export account-in-list?)
+(export account-in-list-pred)
+(export account-in-alist)
+(export account-full-name)
+(export account-list-predicate)
+(export accounts-get-children-depth)
+
+(export split-same?)
+(export split-in-list?)
+
(export gnc:split-structure)
(export gnc:make-split-scm)
(export gnc:split-scm?)
diff --git a/src/report/report-system/Makefile.am b/src/report/report-system/Makefile.am
index a6e49a91bd..4ce4cd7bde 100644
--- a/src/report/report-system/Makefile.am
+++ b/src/report/report-system/Makefile.am
@@ -69,11 +69,9 @@ gncscm_DATA = \
gncmodscmdir = ${GNC_SCM_INSTALL_DIR}/gnucash/report/report-system
gncmodscm_DATA = \
- account.scm \
collectors.scm \
list-extras.scm \
- report-collectors.scm \
- split.scm
+ report-collectors.scm
gncscmmoddir = ${GNC_SCM_INSTALL_DIR}/gnucash/report/
gncscmmod_DATA = \
diff --git a/src/report/report-system/account.scm b/src/report/report-system/account.scm
index 787e5f1543..6540329d00 100644
--- a/src/report/report-system/account.scm
+++ b/src/report/report-system/account.scm
@@ -1,62 +1,5 @@
(define-module (gnucash report report-system account))
(use-modules (gnucash gnc-module))
-(use-modules (gnucash gnc-module))
-
-(use-modules (srfi srfi-1)
- (srfi srfi-13))
-
-(gnc:module-begin-syntax (gnc:module-load "gnucash/engine" 0))
-
-(export account-same?)
-(export account-in-list?)
-(export account-in-list-pred)
-(export account-in-alist)
-(export account-full-name)
-(export account-list-predicate)
-(export accounts-get-children-depth)
-
-;; is account in list of accounts?
-(define (account-same? a1 a2)
- (string=? (gncAccountGetGUID a1) (gncAccountGetGUID a2)))
-
-(define account-in-list?
- (lambda (account accounts)
- (cond
- ((null? accounts) #f)
- ((account-same? (car accounts) account) #t)
- (else (account-in-list? account (cdr accounts))))))
-
-;; Optimized version of accout-in-list if we know
-;; the list in advance.
-(define (account-in-list-pred accounts)
- (define (my-assoc str alist)
- (find (lambda (pair) (account-same? str (car pair))) alist))
- (define (my-hash acc size)
- (remainder (string-hash (gncAccountGetGUID acc)) size))
- (let ((hash-table (make-hash-table)))
- (for-each (lambda (acc) (hashx-set! my-hash my-assoc hash-table acc #t))
- accounts)
- (lambda (account)
- (hashx-ref my-hash my-assoc hash-table account))))
-
-(define account-in-alist
- (lambda (account alist)
- (cond
- ((null? alist) #f)
- ((account-same? (caar alist) account) (car alist))
- (else (account-in-alist account (cdr alist))))))
-
-;; helper for sorting of account list
-(define (account-full-name a b)
- (string (gnc-account-get-full-name a) (gnc-account-get-full-name b)))
-
-;; return maximum depth over accounts and their children, if any
-(define (accounts-get-children-depth accounts)
- (apply max
- (map (lambda (acct)
- (let ((acct-depth (gnc-account-get-current-depth acct)))
- (+ acct-depth (- (gnc-account-get-tree-depth acct) 1))))
- accounts)))
diff --git a/src/report/report-system/split.scm b/src/report/report-system/split.scm
index cce15c1d34..07313bc7ab 100644
--- a/src/report/report-system/split.scm
+++ b/src/report/report-system/split.scm
@@ -5,16 +5,3 @@
(use-modules (sw_engine))
-(export split-same?)
-(export split-in-list?)
-
-(define (split-same? s1 s2)
- (string=? (gncSplitGetGUID s1) (gncSplitGetGUID s2)))
-
-(define split-in-list?
- (lambda (split splits)
- (cond
- ((null? splits) #f)
- ((split-same? (car splits) split) #t)
- (else (split-in-list? split (cdr splits))))))
-
diff --git a/src/report/report-system/test/test-account.scm b/src/report/report-system/test/test-account.scm
index fa310d69f7..b30c0d0134 100644
--- a/src/report/report-system/test/test-account.scm
+++ b/src/report/report-system/test/test-account.scm
@@ -1,4 +1,4 @@
-(use-modules (gnucash report report-system account))
+(use-modules (gnucash engine))
(use-modules (gnucash report report-system test test-extras))
(use-modules (sw_engine))
diff --git a/src/report/report-system/test/test-split.scm b/src/report/report-system/test/test-split.scm
index 286864b4ff..b835a57b98 100644
--- a/src/report/report-system/test/test-split.scm
+++ b/src/report/report-system/test/test-split.scm
@@ -3,7 +3,7 @@
(gnc:module-begin-syntax (gnc:module-load "gnucash/app-utils" 0))
-(use-modules (gnucash report report-system split))
+(use-modules (gnucash engine))
(use-modules (gnucash report report-system test test-extras))
(use-modules (gnucash report report-system))
diff --git a/src/report/standard-reports/budget.scm b/src/report/standard-reports/budget.scm
index 00d2d27cb4..c12a07bb0d 100644
--- a/src/report/standard-reports/budget.scm
+++ b/src/report/standard-reports/budget.scm
@@ -31,8 +31,7 @@
(use-modules (gnucash gettext))
(use-modules (gnucash printf))
-(use-modules (gnucash report report-system account))
-(use-modules (gnucash report report-system split))
+(use-modules (gnucash engine))
(gnc:module-load "gnucash/report/report-system" 0)
(gnc:module-load "gnucash/gnome-utils" 0) ;for gnc-build-url
diff --git a/src/report/standard-reports/cash-flow.scm b/src/report/standard-reports/cash-flow.scm
index 6ba919f964..7bd3f0f876 100644
--- a/src/report/standard-reports/cash-flow.scm
+++ b/src/report/standard-reports/cash-flow.scm
@@ -31,8 +31,7 @@
(use-modules (gnucash main)) ;; FIXME: delete after we finish modularizing.
(use-modules (gnucash gnc-module))
(use-modules (gnucash gettext))
-(use-modules (gnucash report report-system account))
-(use-modules (gnucash report report-system split))
+(use-modules (gnucash engine))
(use-modules (gnucash printf))