GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
csv-tree-export.c
Go to the documentation of this file.
1 /*******************************************************************\
2  * csv-tree-export.c -- 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 [email protected] *
22 \********************************************************************/
27 #include "config.h"
28 
29 #include <gtk/gtk.h>
30 #include <glib/gi18n.h>
31 #include <glib/gstdio.h>
32 
33 #include "gnc-commodity.h"
34 #include "gnc-ui-util.h"
35 
36 #include "csv-tree-export.h"
37 
38 /* This static indicates the debugging module that this .o belongs to. */
39 static QofLogModule log_module = GNC_MOD_ASSISTANT;
40 
41 /* CSV spec requires CRLF line endings. Tweak the end-of-line string so this
42  * true for each platform */
43 #ifdef G_OS_WIN32
44 # define EOLSTR "\n"
45 #else
46 # define EOLSTR "\r\n"
47 #endif
48 
49 /******************************************************************/
50 
51 /*******************************************************
52  * write_line_to_file
53  *
54  * write a text string to a file pointer, return TRUE if
55  * successfull.
56  *******************************************************/
57 static
58 gboolean write_line_to_file (FILE *fh, char * line)
59 {
60  int len, written;
61  DEBUG("Account String: %s", line);
62 
63  /* Write account line */
64  len = strlen (line);
65  written = fwrite (line, 1, len, fh);
66 
67  if (written != len)
68  return FALSE;
69  else
70  return TRUE;
71 }
72 
73 /*******************************************************
74  * csv_test_field_string
75  *
76  * Test the field string for ," and new lines
77  *******************************************************/
78 static
79 gchar *csv_test_field_string (CsvExportInfo *info, const gchar *string_in)
80 {
81  gboolean need_quote = FALSE;
82  gchar **parts;
83  gchar *string_parts;
84  gchar *string_out;
85 
86  /* Check for " and then "" them */
87  parts = g_strsplit (string_in, "\"", -1);
88  string_parts = g_strjoinv ("\"\"", parts);
89  g_strfreev (parts);
90 
91  /* Check for separator string and \n and " in field,
92  if so quote field if not allready quoted */
93  if (g_strrstr (string_parts, info->separator_str) != NULL)
94  need_quote = TRUE;
95  if (g_strrstr (string_parts, "\n") != NULL)
96  need_quote = TRUE;
97  if (g_strrstr (string_parts, "\"") != NULL)
98  need_quote = TRUE;
99 
100  if (!info->use_quotes && need_quote)
101  string_out = g_strconcat ("\"", string_parts, "\"", NULL);
102  else
103  string_out = g_strdup (string_parts);
104 
105  g_free (string_parts);
106  return string_out;
107 }
108 
109 /*******************************************************
110  * csv_tree_export
111  *
112  * write a list of accounts settings to a text file
113  *******************************************************/
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 }
261 
262 
263 
GList * gnc_account_get_descendants_sorted(const Account *account)
Definition: Account.c:2777
const char * gnc_commodity_get_mnemonic(const gnc_commodity *cm)
utility functions for the GnuCash UI
GNCAccountType xaccAccountGetType(const Account *acc)
Definition: Account.c:3009
const char * xaccAccountGetCode(const Account *acc)
Definition: Account.c:3086
CSV Export Account Tree.
#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
void csv_tree_export(CsvExportInfo *info)
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
Commodity handling public routines.
const gchar * QofLogModule
Definition: qofid.h:89
const char * xaccAccountGetNotes(const Account *acc)
Definition: Account.c:3121