27 #include <sys/types.h>
39 #include <glib/gi18n.h>
40 #include <glib/gstdio.h>
45 #include "io-example-account.h"
46 #include "io-gncxml-gen.h"
48 #include "sixtp-dom-generators.h"
49 #include "sixtp-dom-parsers.h"
50 #include "sixtp-parsers.h"
56 # define g_fopen fopen
61 #define GNC_ACCOUNT_STRING "gnc-account-example"
62 #define GNC_ACCOUNT_SHORT "gnc-act:short-description"
63 #define GNC_ACCOUNT_LONG "gnc-act:long-description"
64 #define GNC_ACCOUNT_TITLE "gnc-act:title"
65 #define GNC_ACCOUNT_EXCLUDEP "gnc-act:exclude-from-select-all"
66 #define GNC_ACCOUNT_SELECTED "gnc-act:start-selected"
71 if (gea->title != NULL)
76 if (gea->filename != NULL)
78 g_free(gea->filename);
81 if (gea->root != NULL)
87 if (gea->short_description != NULL)
89 g_free(gea->short_description);
90 gea->short_description = NULL;
92 if (gea->long_description != NULL)
94 g_free(gea->long_description);
95 gea->long_description = NULL;
97 if (gea->book != NULL)
106 clear_up_account_commodity(
119 g_return_if_fail (tbl != NULL);
121 gcom = gnc_commodity_table_lookup(tbl,
131 PWARN(
"unable to find global commodity for %s adding new",
149 clear_up_account_commodity(table, act,
163 g_warning(
"The example account file should declared a ROOT "
164 "account before declaring any other accounts.");
165 gea->root = gnc_book_get_root_account(gea->book);
172 generic_callback(
const char *tag, gpointer globaldata, gpointer data)
176 if (g_strcmp0(tag,
"gnc:account") == 0)
178 add_account_local(gea, (
Account*)data);
184 squash_extra_whitespace(
char *text)
187 int length = strlen(text);
189 for (spot = 1; spot < length; spot++)
191 if (isspace(text[spot]) && isspace(text[spot - 1]))
193 memmove(text + spot, text + spot + 1, length - spot + 1);
205 grab_clean_string(xmlNodePtr tree)
207 return squash_extra_whitespace(g_strstrip(dom_tree_to_text(tree)));
211 gnc_short_descrip_end_handler(gpointer data_for_children,
212 GSList* data_from_children, GSList* sibling_data,
213 gpointer parent_data, gpointer global_data,
214 gpointer *result,
const gchar *tag)
219 gea->short_description = grab_clean_string((xmlNodePtr)data_for_children);
225 gnc_short_descrip_sixtp_parser_create(
void)
227 return sixtp_dom_parser_new(gnc_short_descrip_end_handler, NULL, NULL);
231 gnc_long_descrip_end_handler(gpointer data_for_children,
232 GSList* data_from_children, GSList* sibling_data,
233 gpointer parent_data, gpointer global_data,
234 gpointer *result,
const gchar *tag)
239 gea->long_description = grab_clean_string((xmlNodePtr)data_for_children);
245 gnc_long_descrip_sixtp_parser_create(
void)
247 return sixtp_dom_parser_new(gnc_long_descrip_end_handler, NULL, NULL);
251 gnc_excludep_end_handler(gpointer data_for_children,
252 GSList* data_from_children, GSList* sibling_data,
253 gpointer parent_data, gpointer global_data,
254 gpointer *result,
const gchar *tag)
260 dom_tree_to_integer ((xmlNodePtr)data_for_children, &val);
261 gea->exclude_from_select_all = (val ? TRUE : FALSE);
267 gnc_excludep_sixtp_parser_create(
void)
269 return sixtp_dom_parser_new(gnc_excludep_end_handler, NULL, NULL);
273 gnc_selected_end_handler(gpointer data_for_children,
274 GSList* data_from_children, GSList* sibling_data,
275 gpointer parent_data, gpointer global_data,
276 gpointer *result,
const gchar *tag)
282 dom_tree_to_integer ((xmlNodePtr)data_for_children, &val);
283 gea->start_selected = (val ? TRUE : FALSE);
289 gnc_selected_sixtp_parser_create(
void)
291 return sixtp_dom_parser_new(gnc_selected_end_handler, NULL, NULL);
295 gnc_title_end_handler(gpointer data_for_children,
296 GSList* data_from_children, GSList* sibling_data,
297 gpointer parent_data, gpointer global_data,
298 gpointer *result,
const gchar *tag)
303 gea->title = grab_clean_string((xmlNodePtr)data_for_children);
309 gnc_titse_sixtp_parser_create(
void)
311 return sixtp_dom_parser_new(gnc_title_end_handler, NULL, NULL);
316 gnc_read_example_account(
const gchar *filename)
322 g_return_val_if_fail (filename != NULL, NULL);
327 gea->filename = g_strdup(filename);
329 top_parser = sixtp_new();
330 main_parser = sixtp_new();
332 if (!sixtp_add_some_sub_parsers(
334 GNC_ACCOUNT_STRING, main_parser,
337 gnc_destroy_example_account(gea);
341 if (!sixtp_add_some_sub_parsers(
343 GNC_ACCOUNT_TITLE, gnc_titse_sixtp_parser_create(),
344 GNC_ACCOUNT_SHORT, gnc_short_descrip_sixtp_parser_create(),
345 GNC_ACCOUNT_LONG, gnc_long_descrip_sixtp_parser_create(),
346 GNC_ACCOUNT_EXCLUDEP, gnc_excludep_sixtp_parser_create(),
347 GNC_ACCOUNT_SELECTED, gnc_selected_sixtp_parser_create(),
348 "gnc:account", gnc_account_sixtp_parser_create(),
351 gnc_destroy_example_account(gea);
355 if (!gnc_xml_parse_file(top_parser, filename,
356 generic_callback, gea, gea->book))
358 sixtp_destroy(top_parser);
360 gnc_destroy_example_account(gea);
368 write_string_part(FILE *out,
const char *tag,
const char *data)
372 node = text_to_dom_tree(tag, data);
374 xmlElemDump(out, NULL, node);
381 write_bool_part(FILE *out,
const char *tag, gboolean data)
385 node = int_to_dom_tree(tag, data);
387 xmlElemDump(out, NULL, node);
399 out = g_fopen(filename,
"w");
405 fprintf(out,
"<?xml version=\"1.0\"?>\n");
406 fprintf(out,
"<" GNC_ACCOUNT_STRING
">\n");
408 write_string_part(out, GNC_ACCOUNT_TITLE, gea->title);
410 write_string_part(out, GNC_ACCOUNT_SHORT, gea->short_description);
412 write_string_part(out, GNC_ACCOUNT_LONG, gea->long_description);
414 write_bool_part(out, GNC_ACCOUNT_EXCLUDEP, gea->exclude_from_select_all);
416 write_account_tree(out, gea->root, &data);
418 fprintf(out,
"</" GNC_ACCOUNT_STRING
">\n\n");
420 write_emacs_trailer(out);
431 slist_destroy_example_account(gpointer data, gpointer user_data)
439 PWARN(
"GncExampleAccount pointer in slist was NULL");
444 gnc_free_example_account_list(GSList *list)
446 g_slist_foreach(list, slist_destroy_example_account, NULL);
451 gnc_load_example_account_list(
const char *dirname)
455 const gchar *direntry;
457 dir = g_dir_open(dirname, 0, NULL);
466 for (direntry = g_dir_read_name(dir); direntry != NULL;
467 direntry = g_dir_read_name(dir))
471 if (!g_str_has_suffix(direntry,
"xea"))
474 filename = g_build_filename(dirname, direntry, (gchar*) NULL);
476 if (!g_file_test(filename, G_FILE_TEST_IS_DIR))
478 gea = gnc_read_example_account(filename);
483 gnc_free_example_account_list(ret);
488 ret = g_slist_append(ret, gea);
gnc_commodity * gnc_commodity_table_insert(gnc_commodity_table *table, gnc_commodity *comm)
Account * gnc_account_get_parent(const Account *acc)
gnc_commodity_table * gnc_commodity_table_get_table(QofBook *book)
void gnc_account_append_child(Account *new_parent, Account *child)
const char * gnc_commodity_get_mnemonic(const gnc_commodity *cm)
GNCAccountType xaccAccountGetType(const Account *acc)
void xaccAccountScrubCommodity(Account *account)
QofBook * qof_book_new(void)
const char * gnc_commodity_get_namespace(const gnc_commodity *cm)
void xaccAccountDestroy(Account *acc)
#define PWARN(format, args...)
convert single-entry accounts to clean double-entry
All type declarations for the whole Gnucash engine.
API for the transaction logger.
void xaccAccountBeginEdit(Account *acc)
gnc_commodity * xaccAccountGetCommodity(const Account *acc)
const char * gnc_commodity_get_unique_name(const gnc_commodity *cm)
void gnc_commodity_destroy(gnc_commodity *cm)
void qof_book_destroy(QofBook *book)
const gchar * QofLogModule
void xaccAccountSetCommodity(Account *acc, gnc_commodity *com)