GnuCash  5.6-150-g038405b370+
csv-tree-export.cpp
1 /*******************************************************************\
2  * csv-tree-export.cpp -- Export Account Tree to a file *
3  * *
4  * Copyright (C) 2012 Robert Fewell *
5  * *
6  * This program is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU General Public License as *
8  * published by the Free Software Foundation; either version 2 of *
9  * the License, or (at your option) any later version. *
10  * *
11  * This program is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License*
17  * along with this program; if not, contact: *
18  * *
19  * Free Software Foundation Voice: +1-617-542-5942 *
20  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
21  * Boston, MA 02110-1301, USA gnu@gnu.org *
22 \********************************************************************/
27 #include <config.h>
28 
29 #include <cstdio>
30 #include <fstream>
31 #include <string>
32 #include <vector>
33 #include <algorithm>
34 
35 #include <gnc-filepath-utils.h>
36 #include "gnc-commodity.h"
37 #include "gnc-ui-util.h"
38 #include "csv-tree-export.h"
39 #include "csv-export-helpers.hpp"
40 
41 /* This static indicates the debugging module that this .o belongs to. */
42 static QofLogModule log_module = GNC_MOD_ASSISTANT;
43 
44 /*******************************************************
45  * csv_tree_export
46  *
47  * write a list of accounts settings to a text file
48  *******************************************************/
49 void
51 {
52  ENTER("");
53  DEBUG("File name is : %s", info->file_name);
54 
55  /* Open File for writing */
56  auto ss{gnc_open_filestream(info->file_name)};
57 
58  /* Header string */
59  StringVec headervec = {
60  _("Type"), _("Full Account Name"), _("Account Name"),
61  _("Account Code"), _("Description"), _("Account Color"),
62  _("Notes"), _("Symbol"), _("Namespace"),
63  _("Hidden"), _("Tax Info"), _("Placeholder")
64  };
65 
66  /* Write header line */
67  info->failed = ss.fail() ||
68  !gnc_csv_add_line (ss, headervec, info->use_quotes, info->separator_str);
69 
70  /* Get list of Accounts */
71  auto root{gnc_book_get_root_account (gnc_get_current_book())};
72  auto accts{gnc_account_get_descendants_sorted (root)};
73  auto str_or_empty = [](const char* a){ return a ? a : ""; };
74  auto bool_to_char = [](bool b){ return b ? "T" : "F"; };
75 
76  /* Go through list of accounts */
77  for (GList *ptr = accts; !info->failed && ptr; ptr = g_list_next (ptr))
78  {
79  auto acc{static_cast<Account*>(ptr->data)};
80  DEBUG("Account being processed is : %s", xaccAccountGetName (acc));
81 
82  StringVec line = {
84  account_get_fullname_str (acc),
85  xaccAccountGetName (acc),
86  str_or_empty (xaccAccountGetCode (acc)),
87  str_or_empty (xaccAccountGetDescription (acc)),
88  str_or_empty (xaccAccountGetColor (acc)),
89  str_or_empty (xaccAccountGetNotes (acc)),
92  bool_to_char (xaccAccountGetHidden (acc)),
93  bool_to_char (xaccAccountGetTaxRelated (acc)),
94  bool_to_char (xaccAccountGetPlaceholder (acc)),
95  };
96  info->failed = !gnc_csv_add_line (ss, line, info->use_quotes, info->separator_str);
97  }
98 
99  g_list_free (accts);
100  LEAVE("");
101 }
102 
103 
104 
GList * gnc_account_get_descendants_sorted(const Account *account)
This function returns a GList containing all the descendants of the specified account, sorted at each level.
Definition: Account.cpp:3002
const char * gnc_commodity_get_mnemonic(const gnc_commodity *cm)
Retrieve the mnemonic for the specified commodity.
utility functions for the GnuCash UI
GNCAccountType xaccAccountGetType(const Account *acc)
Returns the account&#39;s account type.
Definition: Account.cpp:3217
const char * xaccAccountGetCode(const Account *acc)
Get the account&#39;s accounting code.
Definition: Account.cpp:3286
CSV Export Account Tree.
STRUCTS.
#define DEBUG(format, args...)
Print a debugging message.
Definition: qoflog.h:264
const char * xaccAccountTypeEnumAsString(GNCAccountType type)
Conversion routines for the account types to/from strings that are used in persistent storage...
Definition: Account.cpp:4182
const char * gnc_commodity_get_namespace(const gnc_commodity *cm)
Retrieve the namespace for the specified commodity.
#define ENTER(format, args...)
Print a function entry debugging message.
Definition: qoflog.h:272
const char * xaccAccountGetColor(const Account *acc)
Get the account&#39;s color.
Definition: Account.cpp:3300
const char * xaccAccountGetDescription(const Account *acc)
Get the account&#39;s description.
Definition: Account.cpp:3293
void csv_tree_export(CsvExportInfo *info)
The csv_tree_export() will let the user export the account tree to a delimited file.
gboolean xaccAccountGetTaxRelated(const Account *acc)
DOCUMENT ME!
Definition: Account.cpp:3970
gboolean xaccAccountGetHidden(const Account *acc)
Get the "hidden" flag for an account.
Definition: Account.cpp:4125
gnc_commodity * xaccAccountGetCommodity(const Account *acc)
Get the account&#39;s commodity.
Definition: Account.cpp:3351
gboolean xaccAccountGetPlaceholder(const Account *acc)
Get the "placeholder" flag for an account.
Definition: Account.cpp:4054
#define LEAVE(format, args...)
Print a function exit debugging message.
Definition: qoflog.h:282
File path resolution utility functions.
const char * xaccAccountGetName(const Account *acc)
Get the account&#39;s name.
Definition: Account.cpp:3239
Commodity handling public routines.
const char * xaccAccountGetNotes(const Account *acc)
Get the account&#39;s notes.
Definition: Account.cpp:3324