GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
hello2.c
1 /*
2  * FILE:
3  * hello2.c
4  *
5  * FUNCTION:
6  * the second in a series of cgi-bin programming eamples.
7  */
8 
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 
13 #include "gnc-engine.h"
14 #include "io-gncxml.h"
15 #include "Query.h"
16 
17 int
18 main (int argc, char *argv[])
19 {
20  int fake_argc = 1;
21  char * fake_argv[] = {"hello2", 0};
22  QofBook *book;
23  Account *root;
24  Query *q, *qq;
25  GList *split_list, *sl2, *node;
26  Split *s;
27  char *bufp;
28  int i, ii, rc, sz;
29 
30 
31  /* intitialize the engine */
32  gnc_engine_init (fake_argc, fake_argv);
33 
34  /* contact the database, which is a flat file for this demo */
35  book = qof_book_new ();
36 
37  rc = gnc_book_begin (book, "file:/tmp/demo.gnucash", FALSE);
38  if (!rc)
39  {
40  int err = gnc_book_get_error (book);
41  printf ("HTTP/1.1 500 Server Error\n");
42  printf ("\n");
43  printf ("%d %s\n", err, strerror (err));
44  goto bookerrexit;
45  }
46 
47  rc = gnc_book_load (book);
48  if (!rc)
49  {
50  int err = gnc_book_get_error (book);
51  printf ("HTTP/1.1 500 Server Error\n");
52  printf ("\n");
53  printf ("%d %s\n", err, strerror (err));
54  goto bookerrexit;
55  }
56 
57  /* the root pointer points to our local cache of the data */
58  root = gnc_book_get_root_account (book);
59 
60  /* build a query */
61  q = qof_query_create_for(GNC_ID_SPLIT);
62  xaccQuerySetGroup (q, root);
64 
65  /* Get everything between some random dates */
66  /* In real life, we would use a query as specified by the user */
67  xaccQueryAddDateMatch (q, TRUE, 28, 2, 1982,
68  FALSE, 16, 10, 2010,
69  QOF_QUERY_OR);
70 
71  split_list = qof_query_run (q);
72 
73  /* count number of splits */
74  i = 0;
75  for (node = split_list; node; node = node->next)
76  {
77  s = node->data;
78  i++;
79  }
80 
81  gncxml_write_query_to_buf(q, &bufp, &sz);
82  qq = gncxml_read_query (bufp, sz);
84  xaccQuerySetGroup (qq, root);
85  sl2 = qof_query_run (qq);
86 
87  /* count number of splits */
88  ii = 0;
89  for (node = sl2; node; node = node->next)
90  {
91  s = node->data;
92  ii++;
93  }
94 
95  /* print the HTTP header */
96  printf ("HTTP/1.1 200 OK\n");
97  printf ("Content-Type: text/xml\n");
98  printf ("Content-Length: %d\n", sz);
99  printf ("\n");
100 
101  printf ("%s", bufp);
102 
103  printf (" its %d and %d \n", i, ii);
104 
105  free (bufp);
106  qof_query_destroy (q);
107 
108 
109 bookerrexit:
110  /* close the book */
111  qof_book_destroy (book);
112 
113  /* shut down the engine */
115 
116  return 0;
117 }
void gnc_engine_shutdown(void)
Definition: gnc-engine.c:160
QofBook * qof_book_new(void)
api for Version 1 XML-based file format
void qof_query_set_max_results(QofQuery *q, int n)
void qof_query_destroy(QofQuery *q)
void gnc_engine_init(int argc, char **argv)
Definition: gnc-engine.c:139
All type declarations for the whole Gnucash engine.
GList * qof_query_run(QofQuery *query)
Definition: SplitP.h:71
void qof_book_destroy(QofBook *book)