Files
gnucash/src/backend/file/test/test-file-stuff.c

372 lines
7.7 KiB
C
Raw Normal View History

#include "config.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include "gnc-engine-util.h"
#include "sixtp-dom-parsers.h"
#include "sixtp-parsers.h"
#include "test-file-stuff.h"
#include "test-stuff.h"
#include "io-gncxml-gen.h"
#include "sixtp-utils.h"
/***********************************************************************/
static int
files_return(int ret, const char* msg)
{
printf(msg);
return ret;
}
int
files_compare(const gchar* f1, const gchar* f2)
{
gchar buf1[512];
gchar buf2[512];
int fd1, fd2;
int amount_read1, amount_read2;
fd1 = open(f1, O_RDONLY);
fd2 = open(f2, O_RDONLY);
do
{
int memcmp_ret;
amount_read1 = read(fd1, buf1, 512);
amount_read2 = read(fd2, buf2, 512);
if(amount_read1 > amount_read2)
return files_return(1, "read1 > read2");
if(amount_read1 < amount_read2)
return files_return(-1, "read1 < read2");
if((memcmp_ret = memcmp(buf1, buf2, amount_read1)) != 0)
{
return files_return(memcmp_ret, "memcmp return");
}
} while(amount_read1 == 512);
return 0;
}
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(data_for_children);
}
return TRUE;
}
gboolean
check_dom_tree_version(xmlNodePtr node, gchar *verstr)
{
char *verteststr;
g_return_val_if_fail(node, FALSE);
g_return_val_if_fail(verstr, FALSE);
g_return_val_if_fail(node->properties, FALSE);
g_return_val_if_fail(node->properties->xmlAttrPropertyValue, FALSE);
g_return_val_if_fail(node->properties->xmlAttrPropertyValue->content,
FALSE);
verteststr = node->properties->xmlAttrPropertyValue->content;
if(safe_strcmp(verstr, verteststr) == 0)
{
return TRUE;
}
else
{
return FALSE;
}
}
gboolean
equals_node_val_vs_string(xmlNodePtr node, const gchar* str)
{
gchar *cmp1;
g_return_val_if_fail(node, FALSE);
g_return_val_if_fail(str, FALSE);
cmp1 = dom_tree_to_text(node);
if(!cmp1)
{
return FALSE;
}
else if(safe_strcmp(cmp1, str) == 0)
{
g_free(cmp1);
return TRUE;
}
else
{
printf("Differing types: node:`%s' vs string:`%s'\n", cmp1, str);
g_free(cmp1);
return FALSE;
}
}
gboolean
equals_node_val_vs_int(xmlNodePtr node, gint64 val)
{
gchar *text;
gint64 test_val;
g_return_val_if_fail(node, FALSE);
text = dom_tree_to_text(node);
if(!string_to_gint64(text, &test_val))
{
g_free(text);
return FALSE;
}
g_free(text);
return val == test_val;
}
gboolean
equals_node_val_vs_guid(xmlNodePtr node, const GUID *id)
{
GUID *cmpid;
g_return_val_if_fail(node, FALSE);
g_return_val_if_fail(id, FALSE);
cmpid = dom_tree_to_guid(node);
g_return_val_if_fail(cmpid, FALSE);
if(guid_compare(cmpid, id) == 0)
{
g_free(cmpid);
return TRUE;
}
else
{
g_free(cmpid);
return FALSE;
}
}
gboolean
equals_node_val_vs_commodity(xmlNodePtr node, const gnc_commodity *com, GNCBook *book)
{
gnc_commodity *cmpcom;
g_return_val_if_fail(node, FALSE);
g_return_val_if_fail(com, FALSE);
cmpcom = dom_tree_to_commodity_ref_no_engine(node, book);
g_return_val_if_fail(cmpcom, FALSE);
if(gnc_commodity_equiv(com, cmpcom))
{
gnc_commodity_destroy(cmpcom);
return TRUE;
}
else
{
gnc_commodity_destroy(cmpcom);
return TRUE;
}
}
gboolean
equals_node_val_vs_kvp_frame(xmlNodePtr node, const kvp_frame *frm)
{
kvp_frame *cmpfrm;
g_return_val_if_fail(node, FALSE);
g_return_val_if_fail(frm, FALSE);
cmpfrm = dom_tree_to_kvp_frame(node);
g_return_val_if_fail(cmpfrm, FALSE);
if(kvp_frame_compare(frm, cmpfrm) == 0)
{
kvp_frame_delete(cmpfrm);
return TRUE;
}
else
{
gchar *frm1str;
gchar *frm2str;
frm1str = kvp_frame_to_string(frm);
frm2str = kvp_frame_to_string(cmpfrm);
printf("%s vs %s\n", frm1str, frm2str);
g_free(frm1str);
g_free(frm2str);
kvp_frame_delete(cmpfrm);
return FALSE;
}
}
gboolean
equals_node_val_vs_date(xmlNodePtr node, const Timespec tm)
{
Timespec *tm_test = dom_tree_to_timespec(node);
if(tm_test->tv_sec == tm.tv_sec && tm_test->tv_nsec == tm.tv_nsec)
{
return TRUE;
}
else
{
return TRUE;
}
}
/***********************************************************************/
static gboolean
just_dom_tree_end_handler(gpointer data_for_children,
GSList* data_from_children, GSList* sibling_data,
gpointer parent_data, gpointer global_data,
gpointer *result, const gchar *tag)
{
xmlNodePtr tree = (xmlNodePtr)data_for_children;
xmlNodePtr *globaldata = (xmlNodePtr*)global_data;
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);
*globaldata = tree;
return TRUE;
}
static xmlNodePtr
grab_file_doc(const char *filename)
{
sixtp *parser;
xmlNodePtr ret;
gpointer parse_result = NULL;
parser = sixtp_dom_parser_new(just_dom_tree_end_handler, NULL, NULL);
sixtp_parse_file(parser, filename, NULL, &ret, &parse_result);
return ret;
}
static void
test_load_file(const char *filename, gxpf_callback cb,
sixtp *parser, const char *parser_tag,
GNCBook *book)
{
xmlNodePtr node;
sixtp *main_parser;
sixtp *top_parser;
node = grab_file_doc(filename);
if(!node)
{
failure_args("failure of libxml to parse file", __FILE__, __LINE__,
"%s", filename);
return;
}
top_parser = sixtp_new();
main_parser = sixtp_new();
if(!sixtp_add_some_sub_parsers(
top_parser, TRUE,
"gnc-v2", main_parser,
NULL, NULL))
{
return;
}
if(!sixtp_add_some_sub_parsers(
main_parser, TRUE,
parser_tag, parser,
NULL, NULL))
{
return;
}
if(!gnc_xml_parse_file(top_parser, filename, cb, node->childs, book))
{
failure_args("failure to parse file", __FILE__, __LINE__,
"%s", filename);
}
xmlFreeNode(node);
}
void
test_files_in_dir(int argc, char **argv, gxpf_callback cb,
sixtp *parser, const char *parser_tag,
GNCBook *book)
{
int count;
for(count = 1; count < argc; count++)
{
struct stat file_info;
const char *to_open = argv[count];
if(stat(to_open, &file_info) != 0)
{
failure("unable to stat file");
}
else
{
if(!S_ISDIR(file_info.st_mode))
{
2002-10-06 Joshua Sled <jsled@asynchronous.org> * src/gnome/druid-loan.c: The druid now enforces valid account selection for the GNCAccountSel widgets; fixes Bug#90784. * src/gnome/gnc-account-sel.c (gas_destroy): GNCAccountSel now cleans up after itself, especially with removing its engine event observation. Moved from src/gnome-utils/ to src/gnome/ because of circular dep introduced below. * src/dialog-account/dialog-account.c (gnc_ui_new_account_with_types): Added new function to create a NewAccount dialog with only a valid-types filtering list. * src/gnome-utils/gnc-account-sel.c (gnc_account_sel_set_acct_filters): Added support for filtering the accounts available through the GNCAccountSel. (gnc_account_sel_set_new_account_ability): Added support for a conditional "New..." button which allows the creation a new account. If the selector has a filter in place, the filter will apply to the new-account creation as well. * src/gnome-utils/gnc-account-sel.c: Added ifdefs, comments around regions pertaining to unimplemented feature of completion. Fix for Bug#90779. (gnc_account_sel_event_cb): Added support for updating list contents when the account tree changes; the current selection is respected if the account still exists; fixes Bug#94951. * src/gnome/dialog-sxsincelast.c (sxsld_process_to_create_instance): Handle the corner case of not incrementing the state while postponing/ignoring a never-been-created SX. Handle only incrementing the counters of newly-come-due SXes. * src/engine/SchedXaction.c (gnc_sx_create_temporal_state): Fix corner case of creating the temporal state of a never-been-created SX; fixes Bug#94947. git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@7283 57a11ea4-9604-0410-9ed3-97b8803252fd
2002-10-06 22:31:05 +00:00
printf( "testing load of file \"%s\":\n", argv[count] );
test_load_file(to_open, cb, parser, parser_tag, book);
}
}
}
}