GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Functions
csv-tree-export.h File Reference

CSV Export Account Tree. More...

#include "assistant-csv-export.h"

Go to the source code of this file.

Functions

void csv_tree_export (CsvExportInfo *info)
 

Detailed Description

CSV Export Account Tree.

Author
Copyright (c) 2012 Robert Fewell

Definition in file csv-tree-export.h.

Function Documentation

void csv_tree_export ( CsvExportInfo info)

The csv_tree_export() will let the user export the account tree to a delimited file.

Definition at line 114 of file csv-tree-export.c.

115 {
116  FILE *fh;
117  Account *root;
118  Account *acc;
119  GList *accts, *ptr;
120 
121  ENTER("");
122  DEBUG("File name is : %s", info->file_name);
123 
124  /* Get list of Accounts */
125  root = gnc_book_get_root_account (gnc_get_current_book());
126  accts = gnc_account_get_descendants_sorted (root);
127  info->failed = FALSE;
128 
129  /* Open File for writing */
130  fh = g_fopen (info->file_name, "w");
131  if (fh != NULL)
132  {
133  gchar *header;
134  gchar *part1;
135  gchar *part2;
136  const gchar *currentSel;
137  gchar *end_sep;
138  gchar *mid_sep;
139  int i;
140 
141 
142  /* Set up separators */
143  if (info->use_quotes)
144  {
145  end_sep = "\"";
146  mid_sep = g_strconcat ("\"", info->separator_str, "\"", NULL);
147  }
148  else
149  {
150  end_sep = "";
151  mid_sep = g_strconcat (info->separator_str, NULL);
152  }
153 
154  /* Header string, 'eol = end of line marker' */
155  header = g_strconcat (end_sep, _("type"), mid_sep, _("full_name"), mid_sep, _("name"), mid_sep,
156  _("code"), mid_sep, _("description"), mid_sep, _("color"), mid_sep,
157  _("notes"), mid_sep, _("commoditym"), mid_sep, _("commodityn"), mid_sep,
158  _("hidden"), mid_sep, _("tax"), mid_sep, _("place_holder"), end_sep, EOLSTR, NULL);
159  DEBUG("Header String: %s", header);
160 
161  /* Write header line */
162  if (!write_line_to_file (fh, header))
163  {
164  info->failed = TRUE;
165  g_free (mid_sep);
166  g_free (header);
167  return;
168  }
169  g_free (header);
170 
171  /* Go through list of accounts */
172  for (ptr = accts, i = 0; ptr; ptr = g_list_next (ptr), i++)
173  {
174  gchar *fullname = NULL;
175  gchar *str_temp = NULL;
176  acc = ptr->data;
177  DEBUG("Account being processed is : %s", xaccAccountGetName (acc));
178  /* Type */
179  currentSel = xaccAccountTypeEnumAsString (xaccAccountGetType (acc));
180  part1 = g_strconcat (end_sep, currentSel, mid_sep, NULL);
181  /* Full Name */
182  fullname = gnc_account_get_full_name (acc);
183  str_temp = csv_test_field_string (info, fullname);
184  part2 = g_strconcat (part1, str_temp, mid_sep, NULL);
185  g_free (str_temp);
186  g_free (fullname);
187  g_free (part1);
188  /* Name */
189  currentSel = xaccAccountGetName (acc);
190  str_temp = csv_test_field_string (info, currentSel);
191  part1 = g_strconcat (part2, str_temp, mid_sep, NULL);
192  g_free (str_temp);
193  g_free (part2);
194  /* Code */
195  currentSel = xaccAccountGetCode (acc) ? xaccAccountGetCode (acc) : "";
196  str_temp = csv_test_field_string (info, currentSel);
197  part2 = g_strconcat (part1, str_temp, mid_sep, NULL);
198  g_free (str_temp);
199  g_free (part1);
200  /* Description */
201  currentSel = xaccAccountGetDescription (acc) ? xaccAccountGetDescription (acc) : "";
202  str_temp = csv_test_field_string (info, currentSel);
203  part1 = g_strconcat (part2, str_temp, mid_sep, NULL);
204  g_free (str_temp);
205  g_free (part2);
206  /* Color */
207  currentSel = xaccAccountGetColor (acc) ? xaccAccountGetColor (acc) : "" ;
208  part2 = g_strconcat (part1, currentSel, mid_sep, NULL);
209  g_free (part1);
210  /* Notes */
211  currentSel = xaccAccountGetNotes (acc) ? xaccAccountGetNotes (acc) : "" ;
212  str_temp = csv_test_field_string (info, currentSel);
213  part1 = g_strconcat (part2, str_temp, mid_sep, NULL);
214  g_free (str_temp);
215  g_free (part2);
216  /* Commodity Mnemonic */
218  str_temp = csv_test_field_string (info, currentSel);
219  part2 = g_strconcat (part1, str_temp, mid_sep, NULL);
220  g_free (str_temp);
221  g_free (part1);
222  /* Commodity Namespace */
224  str_temp = csv_test_field_string (info, currentSel);
225  part1 = g_strconcat (part2, str_temp, mid_sep, NULL);
226  g_free (str_temp);
227  g_free (part2);
228  /* Hidden */
229  currentSel = xaccAccountGetHidden (acc) ? "T" : "F" ;
230  part2 = g_strconcat (part1, currentSel, mid_sep, NULL);
231  g_free (part1);
232  /* Tax */
233  currentSel = xaccAccountGetTaxRelated (acc) ? "T" : "F" ;
234  part1 = g_strconcat (part2, currentSel, mid_sep, NULL);
235  g_free (part2);
236  /* Place Holder / end of line marker */
237  currentSel = xaccAccountGetPlaceholder (acc) ? "T" : "F" ;
238  part2 = g_strconcat (part1, currentSel, end_sep, EOLSTR, NULL);
239  g_free (part1);
240 
241  DEBUG("Account String: %s", part2);
242 
243  /* Write to file */
244  if (!write_line_to_file (fh, part2))
245  {
246  info->failed = TRUE;
247  break;
248  }
249  g_free (part2);
250  }
251  g_free (mid_sep);
252  }
253  else
254  info->failed = TRUE;
255  if (fh)
256  fclose (fh);
257 
258  g_list_free (accts);
259  LEAVE("");
260 }
GList * gnc_account_get_descendants_sorted(const Account *account)
Definition: Account.c:2777
const char * gnc_commodity_get_mnemonic(const gnc_commodity *cm)
GNCAccountType xaccAccountGetType(const Account *acc)
Definition: Account.c:3009
const char * xaccAccountGetCode(const Account *acc)
Definition: Account.c:3086
#define DEBUG(format, args...)
Definition: qoflog.h:255
const char * xaccAccountTypeEnumAsString(GNCAccountType type)
Definition: Account.c:4027
const char * gnc_commodity_get_namespace(const gnc_commodity *cm)
#define ENTER(format, args...)
Definition: qoflog.h:261
const char * xaccAccountGetColor(const Account *acc)
Definition: Account.c:3100
gchar * gnc_account_get_full_name(const Account *account)
Definition: Account.c:3038
const char * xaccAccountGetDescription(const Account *acc)
Definition: Account.c:3093
gboolean xaccAccountGetTaxRelated(const Account *acc)
Definition: Account.c:3808
gboolean xaccAccountGetHidden(const Account *acc)
Definition: Account.c:3959
gnc_commodity * xaccAccountGetCommodity(const Account *acc)
Definition: Account.c:3148
gboolean xaccAccountGetPlaceholder(const Account *acc)
Definition: Account.c:3912
#define LEAVE(format, args...)
Definition: qoflog.h:271
const char * xaccAccountGetName(const Account *acc)
Definition: Account.c:3031
const char * xaccAccountGetNotes(const Account *acc)
Definition: Account.c:3121