Exemple #1
0
gboolean
write_book_parts(FILE *out, QofBook *book)
{
    xmlNodePtr domnode;

    domnode = guid_to_dom_tree(book_id_string, qof_book_get_guid(book));
    xmlElemDump(out, NULL, domnode);
    xmlFreeNode (domnode);

    if (ferror(out) || fprintf(out, "\n") < 0)
        return FALSE;

    if (qof_instance_get_slots (QOF_INSTANCE (book)))
    {
        xmlNodePtr kvpnode = kvp_frame_to_dom_tree(book_slots_string,
                             qof_instance_get_slots (QOF_INSTANCE (book)));
        if (kvpnode)
        {
            xmlElemDump(out, NULL, kvpnode);
            xmlFreeNode(kvpnode);

            if (ferror(out) || fprintf(out, "\n") < 0)
                return FALSE;
        }
    }

    return TRUE;
}
static
gboolean
sx_defer_inst_handler( xmlNodePtr node, gpointer sx_pdata )
{
    struct sx_pdata *pdata = sx_pdata;
    SchedXaction *sx = pdata->sx;
    SXTmpStateData *tsd;

    g_return_val_if_fail( node, FALSE );

    tsd = g_new0( SXTmpStateData, 1 );
    g_assert( sx_defer_dom_handlers != NULL );
    if ( !dom_tree_generic_parse( node,
                                  sx_defer_dom_handlers,
                                  tsd ) )
    {
        xmlElemDump(stdout, NULL, node);
        g_free( tsd );
        tsd = NULL;
        return FALSE;
    }

    /* We assume they were serialized in sorted order, here. */
    sx->deferredList = g_list_append( sx->deferredList, tsd );
    return TRUE;
}
Exemple #3
0
Transaction *
dom_tree_to_transaction( xmlNodePtr node, QofBook *book )
{
    Transaction *trn;
    gboolean successful;
    struct trans_pdata pdata;

    g_return_val_if_fail(node, NULL);
    g_return_val_if_fail(book, NULL);

    trn = xaccMallocTransaction(book);
    g_return_val_if_fail(trn, NULL);
    xaccTransBeginEdit(trn);

    pdata.trans = trn;
    pdata.book = book;

    successful = dom_tree_generic_parse(node, trn_dom_handlers, &pdata);

    xaccTransCommitEdit(trn);

    if ( !successful )
    {
        xmlElemDump(stdout, NULL, node);
        xaccTransBeginEdit(trn);
        xaccTransDestroy(trn);
        xaccTransCommitEdit(trn);
        trn = NULL;
    }

    return trn;
}
void xml_parser_element_dump(FILE * f, WsXmlDocH doc, WsXmlNodeH node)
{

	xmlNodePtr n = (xmlNodePtr) node;
	xmlDocPtr d = (xmlDocPtr) doc;
	xmlElemDump(f, d, n);
}
/**
 * xslDbgCatToFile:
 * @node : Is valid
 * @file : Is valid
 *
 * Send the results of cat command in @node to @file
 */
void
xslDbgCatToFile(xmlNodePtr node, FILE * file)
{
    if (!node || !file)
      return;

    /* assume that HTML usage is enabled */
    if (node->doc->type == XML_HTML_DOCUMENT_NODE) {
        if (node->type == XML_HTML_DOCUMENT_NODE)
            htmlDocDump(file, (htmlDocPtr) node);
        else
            htmlNodeDumpFile(file, node->doc, node);
    } else if (node->type == XML_DOCUMENT_NODE) {
        /* turn off encoding for the moment and just dump UTF-8 
         * which will be converted by xsldbgGeneralErrorFunc */
        xmlDocPtr doc = (xmlDocPtr) node;
        const xmlChar *encoding = doc->encoding;

        if (encoding) {
            xsldbgGenericErrorFunc(i18n("Information: Temporarily setting document's encoding to UTF-8. Previously was %1.\n").arg(xsldbgText(encoding)));
        }
        doc->encoding = (xmlChar *) "UTF-8";
        xmlDocDump(file, (xmlDocPtr) node);
        doc->encoding = encoding;
    } else {
	xmlElemDump(file, node->doc, node);
    }
}
Exemple #6
0
int main()
{
  xmlDoc *doc = NULL;
  xmlNode *root = NULL, *node;
  const char **next;
  int a;

  doc = xmlNewDoc("1.0");
  root = xmlNewNode(NULL, "CharacterRemarks");
  xmlDocSetRootElement(doc, root);

  for(next = names, a = 0; *next != NULL; next++, a++) {
    node = xmlNewNode(NULL, "Character");
    (void)xmlNewProp(node, "name", *next);
    xmlAddChild(node, xmlNewText(remarks[a]));
    xmlAddChild(root, node);
  }

  xmlElemDump(stdout, doc, root);

  xmlFreeDoc(doc);
  xmlCleanupParser();

  return EXIT_SUCCESS;
}
static void
test_dom_tree_to_timespec(void)
{
    int i;
    for (i = 0; i < 20; i++)
    {
        Timespec *test_spec1;
        Timespec test_spec2;
        xmlNodePtr test_node;

        test_spec1 = get_random_timespec();

        test_node = timespec_to_dom_tree("test-spec", test_spec1);

        test_spec2 = dom_tree_to_timespec(test_node);

        if (!dom_tree_valid_timespec(&test_spec2, (const xmlChar*)"test-spec"))
        {
            failure_args("dom_tree_to_timespec",
                         __FILE__, __LINE__, "NULL return");
            printf("Node looks like:\n");
            xmlElemDump(stdout, NULL, test_node);
            printf("\n");
        }

        else if (timespec_cmp(test_spec1, &test_spec2) == 0)
        {
            success("dom_tree_to_timespec");
        }
        else
        {
            failure("dom_tree_to_timespec");
            printf("Node looks like:\n");
            xmlElemDump(stdout, NULL, test_node);
            printf("\n");
            printf("Secs are %" G_GUINT64_FORMAT " vs %" G_GUINT64_FORMAT " :: ",
                   test_spec1->tv_sec,
                   test_spec2.tv_sec);
            printf("NSecs are %ld vs %ld\n",
                   test_spec1->tv_nsec,
                   test_spec2.tv_nsec);
        }

        g_free(test_spec1);
        xmlFreeNode(test_node);
    }
}
Exemple #8
0
/*
 * call-seq:
 *    node.debug_dump -> (true|nil)
 * 
 * Dump this node to stdout, including any debugging
 * information.
 */
VALUE
ruby_xml_node_debug_dump(VALUE self) {
  xmlNodePtr xnode;
  Data_Get_Struct(self, xmlNode, xnode);

  if (xnode->doc == NULL)
    return(Qnil);

  xmlElemDump(stdout, xnode->doc, xnode);
  return(Qtrue);
}
void
write_dom_node_to_file(xmlNodePtr node, int fd)
{
    FILE *out;

    out = fdopen(fd, "w");

    xmlElemDump(out, NULL, node);

    fclose(out);
}
gboolean
print_dom_tree(gpointer data_for_children, GSList* data_from_children,
               GSList* sibling_data, gpointer parent_data,
               gpointer global_data, gpointer *result, const gchar *tag)
{
    if (parent_data == NULL)
    {
        xmlElemDump((FILE*)global_data, NULL, (xmlNodePtr)data_for_children);
        xmlFreeNode(static_cast<xmlNodePtr>(data_for_children));
    }
    return TRUE;
}
static gboolean
gnc_commodity_end_handler(gpointer data_for_children,
                          GSList* data_from_children, GSList* sibling_data,
                          gpointer parent_data, gpointer global_data,
                          gpointer *result, const gchar *tag)
{
    gnc_commodity *com, *old_com;
    xmlNodePtr achild;
    xmlNodePtr tree = (xmlNodePtr)data_for_children;
    gxpf_data *gdata = (gxpf_data*)global_data;
    QofBook *book = static_cast<decltype(book)>(gdata->bookdata);

    if (parent_data)
    {
        return TRUE;
    }

    /* OK.  For some messed up reason this is getting called again with a
       NULL tag.  So we ignore those cases */
    if (!tag)
    {
        return TRUE;
    }

    g_return_val_if_fail(tree, FALSE);

    com = gnc_commodity_new(book, NULL, NULL, NULL, NULL, 0);
    old_com = gnc_commodity_find_currency(book, tree);
    if (old_com)
        gnc_commodity_copy(com, old_com);

    for (achild = tree->xmlChildrenNode; achild; achild = achild->next)
    {
        set_commodity_value(achild, com);
    }

    if (!valid_commodity(com))
    {
        PWARN("Invalid commodity parsed");
        xmlElemDump(stdout, NULL, tree);
        printf("\n");
        fflush(stdout);
        gnc_commodity_destroy(com);
        return FALSE;
    }

    gdata->cb(tag, gdata->parsedata, com);

    xmlFreeNode(tree);

    return TRUE;
}
static void
xml_add_billterm (QofInstance *term_p, gpointer out_p)
{
    xmlNodePtr node;
    GncBillTerm *term = (GncBillTerm *) term_p;
    FILE *out = out_p;

    if (ferror(out))
        return;

    node = billterm_dom_tree_create (term);
    xmlElemDump(out, NULL, node);
    xmlFreeNode (node);
    if (ferror(out) || fprintf(out, "\n") < 0)
        return;
}
static void
xml_add_taxtable (QofInstance * table_p, gpointer out_p)
{
    xmlNodePtr node;
    GncTaxTable *table = (GncTaxTable *) table_p;
    FILE *out = out_p;

    if (ferror(out))
        return;

    node = taxtable_dom_tree_create (table);
    xmlElemDump(out, NULL, node);
    xmlFreeNode (node);
    if (ferror(out) || fprintf(out, "\n") < 0)
        return;
}
Exemple #14
0
static void
xml_add_order (QofInstance * order_p, gpointer out_p)
{
    xmlNodePtr node;
    GncOrder *order = (GncOrder *) order_p;
    FILE *out = out_p;

    if (ferror(out))
        return;
    if (!order_should_be_saved (order))
        return;

    node = order_dom_tree_create (order);
    xmlElemDump(out, NULL, node);
    xmlFreeNode (node);
    if (ferror(out) || fprintf(out, "\n") < 0)
        return;
}
Exemple #15
0
static void
xml_add_job (QofInstance * job_p, gpointer out_p)
{
    xmlNodePtr node;
    GncJob *job = (GncJob *) job_p;
    FILE *out = out_p;

    if (ferror(out))
        return;
    if (!job_should_be_saved (job))
        return;

    node = job_dom_tree_create (job);
    xmlElemDump(out, NULL, node);
    xmlFreeNode (node);
    if (ferror(out) || fprintf(out, "\n") < 0)
        return;
}
static void
xml_add_vendor (QofInstance* vendor_p, gpointer out_p)
{
    xmlNodePtr node;
    GncVendor* vendor = (GncVendor*) vendor_p;
    FILE* out = static_cast<decltype (out)> (out_p);

    if (ferror (out))
        return;
    if (!vendor_should_be_saved (vendor))
        return;

    node = vendor_dom_tree_create (vendor);
    xmlElemDump (out, NULL, node);
    xmlFreeNode (node);
    if (ferror (out) || fprintf (out, "\n") < 0)
        return;
}
static void
xml_add_employee (QofInstance * employee_p, gpointer out_p)
{
    xmlNodePtr node;
    GncEmployee *employee = (GncEmployee *) employee_p;
    FILE *out = out_p;

    if (ferror(out))
        return;
    if (!employee_should_be_saved (employee))
        return;

    node = employee_dom_tree_create (employee);
    xmlElemDump(out, NULL, node);
    xmlFreeNode (node);
    if (ferror(out) || fprintf(out, "\n") < 0)
        return;
}
Exemple #18
0
static void
xml_add_invoice (QofInstance* invoice_p, gpointer out_p)
{
    xmlNodePtr node;
    GncInvoice* invoice = (GncInvoice*) invoice_p;
    FILE* out = static_cast<decltype (out)> (out_p);

    if (ferror (out))
        return;
    if (!invoice_should_be_saved (invoice))
        return;

    node = invoice_dom_tree_create (invoice);
    xmlElemDump (out, NULL, node);
    xmlFreeNode (node);
    if (ferror (out) || fprintf (out, "\n") < 0)
        return;
}
Exemple #19
0
static void
xml_add_customer (QofInstance * cust_p, gpointer out_p)
{
    xmlNodePtr node;
    GncCustomer *cust = (GncCustomer *) cust_p;
    FILE *out = out_p;

    if (ferror(out))
        return;
    if (!customer_should_be_saved (cust))
        return;

    node = customer_dom_tree_create (cust);
    xmlElemDump(out, NULL, node);
    xmlFreeNode (node);
    if (ferror(out) || fprintf(out, "\n") < 0)
        return;
}
Exemple #20
0
/**
 * function get_math_xhtml(xhtml)
 *
 * return the xml math tags from memory written in 'xhtml'
 */
std::vector<std::string> get_math_xhtml(const string& xhtml,
                                        const string& url) {
    const xmlChar *xpath = (const xmlChar*)
            "//m:math/m:semantics/m:annotation-xml[@encoding='MathML-Content']/*";

    vector<string> harvest_exprs;

    xmlDocPtr doc = get_XMLDoc(xhtml.c_str());

    xmlXPathObjectPtr result = get_XMLNodeset(doc, xpath);
    if (result != nullptr) {
        xmlNodeSetPtr nodeset = result->nodesetval;

        for (int i = 0; i < nodeset->nodeNr; ++i) {
            char* buf;
            size_t sz;
            FILE* stream = open_memstream(&buf, &sz);

            xmlChar* id = xmlGetProp(
                nodeset->nodeTab[i]->parent->parent->parent, (xmlChar*)"id");

            fprintf(stream, "<mws:expr url=\"%s#%s\">\n", url.c_str(), id);
            xmlElemDump(stream, doc, nodeset->nodeTab[i]);
            fprintf(stream, "</mws:expr>\n");
            fclose(stream);

            string str = (string)buf;
            if (is_good_xml(str)) {
                harvest_exprs.push_back(str);
            }

            xmlFree(id);
            free(buf);
        }

        xmlXPathFreeObject(result);
    }

    xmlFreeDoc(doc);

    // xmlCleanupParser(); should be called only at the end

    return harvest_exprs;
}
Exemple #21
0
static void
xml_add_entry (QofInstance * entry_p, gpointer out_p)
{
    xmlNodePtr node;
    GncEntry *entry = (GncEntry *) entry_p;
    FILE *out = out_p;

    if (ferror(out))
        return;

    /* Don't save non-attached entries! */
    if (!(gncEntryGetOrder (entry) || gncEntryGetInvoice (entry) ||
            gncEntryGetBill (entry)))
        return;

    node = entry_dom_tree_create (entry);
    xmlElemDump(out, NULL, node);
    xmlFreeNode (node);
    if (ferror(out) || fprintf(out, "\n") < 0)
        return;
}
static void
test_dom_tree_to_text(void)
{
    int i;

    for (i = 0; i < 20; i++)
    {
        gchar *test_string1;
        gchar *test_string2;
        xmlNodePtr test_node;

        test_node = xmlNewNode(NULL, BAD_CAST "test-node");
        test_string1 = get_random_string();

        xmlNodeAddContent(test_node, BAD_CAST test_string1);

        test_string2 = dom_tree_to_text(test_node);

        if (!test_string2)
        {
            failure_args("dom_tree_to_text", __FILE__, __LINE__,
                         "null return from dom_tree_to_text");
            xmlElemDump(stdout, NULL, test_node);
        }
        else if (g_strcmp0(test_string1, test_string2) == 0)
        {
            success_args("dom_tree_to_text", __FILE__, __LINE__, "with string %s",
                         test_string1);
        }
        else
        {
            failure_args("dom_tree_to_text", __FILE__, __LINE__,
                         "with string %s", test_string1);
        }

        xmlFreeNode(test_node);
        g_free(test_string1);
        if (test_string2) g_free(test_string2);
    }
}
Exemple #23
0
static gboolean
gnc_schedXaction_end_handler(gpointer data_for_children,
                             GSList* data_from_children, GSList* sibling_data,
                             gpointer parent_data, gpointer global_data,
                             gpointer *result, const gchar *tag)
{
    SchedXaction *sx;
    gboolean     successful = FALSE;
    xmlNodePtr   tree = (xmlNodePtr)data_for_children;
    gxpf_data    *gdata = (gxpf_data*)global_data;
    struct sx_pdata sx_pdata;

    if ( parent_data )
    {
        return TRUE;
    }

    if ( !tag )
    {
        return TRUE;
    }

    g_return_val_if_fail( tree, FALSE );

    sx = xaccSchedXactionMalloc( gdata->bookdata );

    memset(&sx_pdata, 0, sizeof(sx_pdata));
    sx_pdata.sx = sx;
    sx_pdata.book = gdata->bookdata;

    g_assert( sx_dom_handlers != NULL );

    successful = dom_tree_generic_parse( tree, sx_dom_handlers, &sx_pdata );
    if (!successful)
    {
        g_critical("failed to parse scheduled xaction");
        xmlElemDump( stdout, NULL, tree );
        gnc_sx_begin_edit( sx );
        xaccSchedXactionDestroy( sx );
        goto done;
    }

    if (tree->properties)
    {
        gchar *sx_name = xaccSchedXactionGetName(sx);
        xmlAttr *attr;
        for (attr = tree->properties; attr != NULL; attr = attr->next)
        {
            xmlChar *attr_value = attr->children->content;
            g_debug("sx attribute name[%s] value[%s]", attr->name, attr_value);
            if (strcmp((const char *)attr->name, "version") != 0)
            {
                g_warning("unknown sx attribute [%s]", attr->name);
                continue;
            }

            // if version == 1.0.0: ensure freqspec, no recurrence
            // if version == 2.0.0: ensure recurrence, no freqspec.
            if (strcmp((const char *)attr_value,
                       schedxaction_version_string) == 0)
            {
                if (!sx_pdata.saw_freqspec)
                    g_critical("did not see freqspec in version 1 sx [%s]", sx_name);
                if (sx_pdata.saw_recurrence)
                    g_warning("saw recurrence in supposedly version 1 sx [%s]", sx_name);
            }

            if (strcmp((const char *)attr_value,
                       schedxaction_version2_string) == 0)
            {
                if (sx_pdata.saw_freqspec)
                    g_warning("saw freqspec in version 2 sx [%s]", sx_name);
                if (!sx_pdata.saw_recurrence)
                    g_critical("did not find recurrence in version 2 sx [%s]", sx_name);
            }
        }
    }

    // generic_callback -> book_callback: insert the SX in the book
    gdata->cb( tag, gdata->parsedata, sx );

    /* FIXME: this should be removed somewhere near 1.8 release time. */
    if ( sx->template_acct == NULL )
    {
        Account *ra = NULL;
        const char *id = NULL;
        Account *acct = NULL;
        sixtp_gdv2 *sixdata = gdata->parsedata;
        QofBook *book;

        book = sixdata->book;

        /* We're dealing with a pre-200107<near-end-of-month> rgmerk
           change re: storing template accounts. */
        /* Fix: get account with name of our GncGUID from the template
           accounts.  Make that our template_acct pointer. */
        /* THREAD-UNSAFE */
        id = guid_to_string( xaccSchedXactionGetGUID( sx ) );
        ra = gnc_book_get_template_root(book);
        if ( ra == NULL )
        {
            g_warning( "Error getting template root account from being-parsed Book." );
            xmlFreeNode( tree );
            return FALSE;
        }
        acct = gnc_account_lookup_by_name( ra, id );
        if ( acct == NULL )
        {
            g_warning("no template account with name [%s]", id);
            xmlFreeNode( tree );
            return FALSE;
        }
        g_debug("template account name [%s] for SX with GncGUID [%s]",
                xaccAccountGetName( acct ), id );

        /* FIXME: free existing template account.
         *  HUH????? We only execute this if there isn't
         * currently an existing template account, don't we?
         * <rgmerk>
         */

        sx->template_acct = acct;
    }

done:
    xmlFreeNode( tree );

    return successful;
}
Exemple #24
0
static gboolean
gnc_template_transaction_end_handler(gpointer data_for_children,
                                     GSList* data_from_children,
                                     GSList* sibling_data,
                                     gpointer parent_data,
                                     gpointer global_data,
                                     gpointer *result,
                                     const gchar *tag)
{
    gboolean   successful = FALSE;
    xmlNodePtr tree = data_for_children;
    gxpf_data  *gdata = global_data;
    QofBook    *book = gdata->bookdata;
    GList      *n;
    gnc_template_xaction_data txd;

    txd.book = book;
    txd.accts = NULL;
    txd.transactions = NULL;

    /* the DOM tree will have an account tree [the template
       accounts] and a list of transactions [which will be members
       of the template account].

       we want to parse through the dom trees for each, placing
       the null-parent account in the book's template-group slot,
       the others under it, and the transactions as normal. */

    if ( parent_data )
    {
        return TRUE;
    }

    if ( !tag )
    {
        return TRUE;
    }

    g_return_val_if_fail( tree, FALSE );

    successful = dom_tree_generic_parse( tree, tt_dom_handlers, &txd );

    if ( successful )
    {
        gdata->cb( tag, gdata->parsedata, &txd );
    }
    else
    {
        g_warning("failed to parse template transaction");
        xmlElemDump( stdout, NULL, tree );
    }

    /* cleanup */
    for ( n = txd.accts; n; n = n->next )
    {
        n->data = NULL;
    }
    for ( n = txd.transactions; n; n = n->next )
    {
        n->data = NULL;
    }
    g_list_free( txd.accts );
    g_list_free( txd.transactions );

    xmlFreeNode( tree );

    return successful;
}
static void
test_transaction(void)
{
    int i;

    for (i = 0; i < 50; i++)
    {
        Transaction *ran_trn;
        Account *root;
        xmlNodePtr test_node;
        gnc_commodity *com, *new_com;
        gchar *compare_msg;
        gchar *filename1;
        int fd;

        /* The next line exists for its side effect of creating the
         * account tree. */
        root = get_random_account_tree(book);
        ran_trn = get_random_transaction(book);
        new_com = get_random_commodity( book );
        if (!ran_trn)
        {
            failure_args("transaction_xml", __FILE__, __LINE__,
                         "get_random_transaction returned NULL");
            return;
        }

        {
            /* xaccAccountInsertSplit can reorder the splits. */
            GList * list = g_list_copy(xaccTransGetSplitList (ran_trn));
            GList * node = list;
            for ( ; node; node = node->next)
            {
                Split * s = node->data;
                Account * a = xaccMallocAccount(book);

                xaccAccountBeginEdit (a);
                xaccAccountSetCommodity( a, new_com );
                xaccAccountSetCommoditySCU (a, xaccSplitGetAmount (s).denom);
                xaccAccountInsertSplit (a, s);
                xaccAccountCommitEdit (a);
            }
            g_list_free(list);
        }

        com = xaccTransGetCurrency (ran_trn);

        test_node = gnc_transaction_dom_tree_create(ran_trn);
        if (!test_node)
        {
            failure_args("transaction_xml", __FILE__, __LINE__,
                         "gnc_transaction_dom_tree_create returned NULL");
            really_get_rid_of_transaction(ran_trn);
            continue;
        }

        if ((compare_msg = node_and_transaction_equal(test_node, ran_trn)) !=
                NULL)
        {
            failure_args("transaction_xml", __FILE__, __LINE__,
                         "node and transaction were not equal: %s",
                         compare_msg);
            xmlElemDump(stdout, NULL, test_node);
            printf("\n");
            fflush(stdout);
            xmlFreeNode(test_node);
            really_get_rid_of_transaction(ran_trn);
            continue;
        }
        else
        {
            success_args("transaction_xml", __FILE__, __LINE__, "%d", i );
        }

        filename1 = g_strdup_printf("test_file_XXXXXX");

        fd = g_mkstemp(filename1);

        write_dom_node_to_file(test_node, fd);

        close(fd);

        {
            GList * node = xaccTransGetSplitList (ran_trn);
            for ( ; node; node = node->next)
            {
                Split * s = node->data;
                Account * a1 = xaccSplitGetAccount(s);
                Account * a2 = xaccMallocAccount(book);

                xaccAccountBeginEdit (a2);
                xaccAccountSetCommoditySCU (a2, xaccAccountGetCommoditySCU (a1));
                xaccAccountSetGUID (a2, xaccAccountGetGUID (a1));
                xaccAccountCommitEdit (a2);
            }
        }

        {
            sixtp *parser;
            tran_data data;

            data.trn = ran_trn;
            data.com = com;
            data.value = i;

            g_print(" There will follow a bunch of CRIT scrub errors about the account not having a commodity. There isn't an account in the XML, so of course not. Ignore the errors\n");
            parser = gnc_transaction_sixtp_parser_create();

            if (!gnc_xml_parse_file(parser, filename1, test_add_transaction,
                                    (gpointer)&data, book))
            {
                failure_args("gnc_xml_parse_file returned FALSE",
                             __FILE__, __LINE__, "%d", i);
            }
            else
                really_get_rid_of_transaction (data.new_trn);

            /* no handling of circular data structures.  We'll do that later */
            /* sixtp_destroy(parser); */
        }

        g_unlink(filename1);
        g_free(filename1);
        really_get_rid_of_transaction(ran_trn);
        xmlFreeNode(test_node);
    }
}
static void
test_account (int i, Account* test_act)
{
    xmlNodePtr test_node;
    gchar* filename1;
    gchar* compare_msg;
    int fd;

    test_node = gnc_account_dom_tree_create (test_act, FALSE, TRUE);

    if (!test_node)
    {
        failure_args ("account_xml", __FILE__, __LINE__,
                      "gnc_account_dom_tree_create returned NULL");
        return;
    }

    if ((compare_msg = node_and_account_equal (test_node, test_act)) != NULL)
    {
        failure_args ("account_xml", __FILE__, __LINE__,
                      "node and account were not equal: %s", compare_msg);
        xmlElemDump (stdout, NULL, test_node);
        fprintf (stdout, "\n");
        xmlFreeNode (test_node);
        g_free (compare_msg);
        return;
    }
    else
    {
        success ("account_xml");
    }

    filename1 = g_strdup_printf ("test_file_XXXXXX");

    fd = g_mkstemp (filename1);

    write_dom_node_to_file (test_node, fd);

    close (fd);

    {
        sixtp* parser;
        act_data data;

        data.act = test_act;
        data.value = i;

        parser = gnc_account_sixtp_parser_create ();

        if (!gnc_xml_parse_file (parser, filename1, test_add_account,
                                 &data, sixbook))
        {
            failure_args ("gnc_xml_parse_file returned FALSE",
                          __FILE__, __LINE__, "%d", i);
        }

        /* no handling of circular data structures.  We'll do that later */
        /* sixtp_destroy(parser); */
    }


    g_unlink (filename1);
    g_free (filename1);
    xmlFreeNode (test_node);
}
static void
test_transaction (void)
{
    int i;

    for (i = 0; i < 50; i++)
    {
        Transaction* ran_trn;
        xmlNodePtr test_node;
        gnc_commodity* com, *new_com;
        gchar* filename1;
        int fd;

        /* The next line exists for its side effect of creating the
         * account tree. */
        get_random_account_tree (book);
        ran_trn = get_random_transaction (book);
        new_com = get_random_commodity (book);
        if (!ran_trn)
        {
            failure_args ("transaction_xml", __FILE__, __LINE__,
                          "get_random_transaction returned NULL");
            return;
        }

        {
            /* xaccAccountInsertSplit can reorder the splits. */
            GList* list = g_list_copy (xaccTransGetSplitList (ran_trn));
            GList* node = list;
            for (; node; node = node->next)
            {
                Split* s = static_cast<decltype (s)> (node->data);
                Account* a = xaccMallocAccount (book);

                xaccAccountBeginEdit (a);
                xaccAccountSetCommodity (a, new_com);
                xaccAccountSetCommoditySCU (a, xaccSplitGetAmount (s).denom);
                xaccAccountInsertSplit (a, s);
                xaccAccountCommitEdit (a);
            }
            g_list_free (list);
        }

        com = xaccTransGetCurrency (ran_trn);

        test_node = gnc_transaction_dom_tree_create (ran_trn);
        if (!test_node)
        {
            failure_args ("transaction_xml", __FILE__, __LINE__,
                          "gnc_transaction_dom_tree_create returned NULL");
            really_get_rid_of_transaction (ran_trn);
            continue;
        }
        auto compare_msg = node_and_transaction_equal (test_node, ran_trn);
        if (compare_msg != nullptr)
        {
            failure_args ("transaction_xml", __FILE__, __LINE__,
                          "node and transaction were not equal: %s",
                          compare_msg);
            xmlElemDump (stdout, NULL, test_node);
            printf ("\n");
            fflush (stdout);
            xmlFreeNode (test_node);
            really_get_rid_of_transaction (ran_trn);
            continue;
        }
        else
        {
            success_args ("transaction_xml", __FILE__, __LINE__, "%d", i);
        }

        filename1 = g_strdup_printf ("test_file_XXXXXX");

        fd = g_mkstemp (filename1);

        write_dom_node_to_file (test_node, fd);

        close (fd);

        {
            GList* node = xaccTransGetSplitList (ran_trn);
            for (; node; node = node->next)
            {
                Split* s = static_cast<decltype (s)> (node->data);
                Account* a1 = xaccSplitGetAccount (s);
                Account* a2 = xaccMallocAccount (book);

                xaccAccountBeginEdit (a2);
                xaccAccountSetCommoditySCU (a2, xaccAccountGetCommoditySCU (a1));
                xaccAccountSetGUID (a2, xaccAccountGetGUID (a1));
                xaccAccountCommitEdit (a2);
            }
        }

        {
            sixtp* parser;
            tran_data data;

            const char* msg =
                "[xaccAccountScrubCommodity()] Account \"\" does not have a commodity!";
            const char* logdomain = "gnc.engine.scrub";
            GLogLevelFlags loglevel = static_cast<decltype (loglevel)>
                                      (G_LOG_LEVEL_CRITICAL);
            TestErrorStruct check = { loglevel, const_cast<char*> (logdomain),
                                      const_cast<char*> (msg)
                                    };
            g_log_set_handler (logdomain, loglevel,
                               (GLogFunc)test_checked_handler, &check);
            data.trn = ran_trn;
            data.com = com;
            data.value = i;
            parser = gnc_transaction_sixtp_parser_create ();

            if (!gnc_xml_parse_file (parser, filename1, test_add_transaction,
                                     (gpointer)&data, book))
            {
                failure_args ("gnc_xml_parse_file returned FALSE",
                              __FILE__, __LINE__, "%d", i);
            }
            else
                really_get_rid_of_transaction (data.new_trn);
        }
        /* no handling of circular data structures.  We'll do that later */
        /* sixtp_destroy(parser); */


        g_unlink (filename1);
        g_free (filename1);
        really_get_rid_of_transaction (ran_trn);
        xmlFreeNode (test_node);
    }
}
Exemple #28
0
static void
test_generation (void)
{
    int i;
    for (i = 0; i < 20; i++)
    {
        gnc_commodity* ran_com;
        xmlNodePtr test_node;
        gchar* filename1;
        int fd;

        ran_com = get_random_commodity (book);

        test_node = gnc_commodity_dom_tree_create (ran_com);
        if (!test_node)
        {
            failure_args ("commodity_xml", __FILE__, __LINE__,
                          "gnc_commodity_dom_tree_create returned NULL");
            gnc_commodity_destroy (ran_com);
            continue;
        }
        auto compare_msg = node_and_commodity_equal (test_node, ran_com);
        if (compare_msg != nullptr)
        {
            failure_args ("commodity_xml", __FILE__, __LINE__,
                          "node and commodity were not equal: %s", compare_msg);
            xmlElemDump (stdout, NULL, test_node);
            xmlFreeNode (test_node);
            gnc_commodity_destroy (ran_com);
            continue;
        }
        else
        {
            success_args ("commodity_xml", __FILE__, __LINE__, "%d", i);
        }

        filename1 = g_strdup_printf ("test_file_XXXXXX");

        fd = g_mkstemp (filename1);

        write_dom_node_to_file (test_node, fd);

        close (fd);

        {
            sixtp* parser;
            com_data data;

            data.com = ran_com;
            data.value = i;

            parser = gnc_commodity_sixtp_parser_create ();

            if (!gnc_xml_parse_file (parser, filename1, test_add_commodity,
                                     (gpointer)&data, book))
            {
                failure_args ("gnc_xml_parse_file returned FALSE",
                              __FILE__, __LINE__, "%d", i);
            }

            /* no handling of circular data structures.  We'll do that later */
            /* sixtp_destroy(parser); */
        }

        g_unlink (filename1);
        g_free (filename1);
        gnc_commodity_destroy (ran_com);
        xmlFreeNode (test_node);
    }
}