GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
test-save-in-lang.c
1 #include "config.h"
2 #include <glib.h>
3 #include <glib/gstdio.h>
4 #include <unistd.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <dirent.h>
8 #include <errno.h>
9 #include <stdlib.h>
10 #include <string.h>
11 
12 #include "test-stuff.h"
13 #include "test-engine-stuff.h"
14 #include "test-file-stuff.h"
15 
16 #include "gnc-engine.h"
17 #include "TransLog.h"
18 
19 #include "io-gncxml-v2.h"
20 
21 const char *possible_envs[] =
22 {
23  "C", "af", "ar", "az", "be", "bg", "bg_BG.cp1251", "br", "ca", "cs",
24  "cy", "cz", "da", "de", "de_AT", "el", "en", "en_AU", "en_CA", "en_GB",
25  "eo", "es", "es_DO", "es_ES", "es_GT", "es_HN", "es_MX", "es_PA",
26  "es_PE", "es_SV", "et", "et_EE", "eu", "fi", "fi_FI", "fr", "ga",
27  "gd", "gl", "gr", "gv", "he", "hr", "hu", "id", "is", "it", "ja",
28  "ja_JP", "ja_JP.SJIS", "ko", "ko_KR", "ko_KR.eucKR", "kw", "l10n",
29  "locale.alias", "lt", "nl", "nn", "no", "no@nynorsk", "no_NO", "no_NY",
30  "pl", "pl_PL", "pt", "pt_BR", "pt_PT", "ro", "ru", "ru_RU", "rudos",
31  "rukoi8", "ruwin", "sk", "sl", "sl_SI", "sp", "sr", "sr_YU", "sv", "ta",
32  "tr", "uk", "vi", "vi_VN.VISCII", "wa", "zh", "zh_CN", "zh_CN.EUC",
33  "zh_CN.GB2312", "zh_TW", "zh_TW.Big5",
34  NULL
35 };
36 
37 const char *possible_vars[] =
38 {
39  "LANG", "LC_CTYPE", "LC_COLLATE", "LC_TIME", "LC_NUMERIC",
40  "LC_MONETARY", "LC_MESSAGES",
41  NULL
42 };
43 
44 const char *diff_command = "cmp %s %s";
45 const char *test_dir = "test-files/xml2";
46 const char *base_env = "C";
47 
48 static char*
49 gen_new_file_name(const char *filename, const char *env)
50 {
51  char *ret;
52 
53  ret = g_new(char, strlen(filename) + strlen(env) + 2);
54  strcpy(ret, filename);
55  strcat(ret, "-");
56  strcat(ret, env);
57 
58  return ret;
59 }
60 
61 static int
62 run_command_get_return(const char *command)
63 {
64  return system(command);
65 }
66 
67 static char *
68 test_file(const char *filename)
69 {
70  int i;
71 
72  for (i = 0; possible_envs[i] != NULL; i++)
73  {
74  QofBackendError err;
75  QofSession *session;
76  char *cmd;
77  char *new_file = gen_new_file_name(filename, possible_envs[i]);
78  QofSession *new_session;
79 
80  session = qof_session_new();
81 
82  qof_session_begin(session, filename, TRUE, FALSE, FALSE);
83  err = qof_session_pop_error (session);
84  if (err)
85  {
86  qof_session_destroy(session);
87  return g_strdup_printf("qof_session_begin errorid %d", err);
88  }
89 
90  qof_session_load(session, NULL);
91  err = qof_session_pop_error (session);
92  if (err)
93  {
94  qof_session_destroy(session);
95  return g_strdup_printf("qof_session_load errorid %d", err);
96  }
97 
98  if (!g_setenv("LANG", possible_envs[i], TRUE))
99  return g_strdup("setenv for LANG");
100 
101  new_session = qof_session_new();
102 
103  qof_session_begin(new_session, new_file, FALSE, FALSE, FALSE);
104  err = qof_session_pop_error (new_session);
105  if (err)
106  {
107  g_free(new_file);
108  qof_session_destroy(session);
109  qof_session_destroy(new_session);
110  return g_strdup_printf("qof_session_begin 2 with LANG=%s",
111  possible_envs[i]);
112  }
113 
114  qof_session_save(new_session, NULL);
115 
116  cmd = g_strdup_printf(diff_command, filename, new_file);
117 
118  if (run_command_get_return(cmd) != 0)
119  {
120  g_free(cmd);
121  g_free(new_file);
122  qof_session_destroy(session);
123  qof_session_destroy(new_session);
124  return g_strdup_printf("run_command_get_return with LANG=%s",
125  possible_envs[i]);
126  }
127 
128  g_free(new_file);
129  g_free(cmd);
130  qof_session_destroy(session);
131  qof_session_destroy(new_session);
132  }
133 
134  return NULL;
135 }
136 
137 int
138 main(int argc, char **argv)
139 {
140  GDir *adir;
141 
142  gnc_engine_init(argc, argv);
143  xaccLogDisable();
144 
145  if ((adir = g_dir_open(test_dir, 0, NULL)) == NULL)
146  {
147  failure_args("g_dir_open", __FILE__, __LINE__,
148  "couldn't open dir %s", test_dir);
149  }
150  else
151  {
152  const gchar *next_file;
153 
154  while ((next_file = g_dir_read_name(adir)) != NULL)
155  {
156  struct stat file_info;
157  char* filename;
158 
159  filename = g_build_filename(test_dir, next_file, (gchar*) NULL);
160 
161  if (g_stat(filename, &file_info) != 0)
162  {
163  failure_args("stat", __FILE__, __LINE__,
164  "couldn't stat file %s: %s", filename,
165  strerror(errno));
166  g_free(filename);
167  break;
168  }
169 
170  if (!g_setenv("LANG", base_env, TRUE))
171  {
172  failure_args("setenv", __FILE__, __LINE__,
173  "setenv of LANG failed");
174  g_free(filename);
175  break;
176  }
177 
178  if (!S_ISDIR(file_info.st_mode))
179  {
180  char *msg = test_file(filename);
181 
182  if (msg != NULL)
183  {
184  failure_args("test_file", __FILE__, __LINE__,
185  "failure testing file %s with msg %s",
186  filename, msg);
187  }
188  g_free(msg);
189  }
190 
191  g_free(filename);
192  }
193  g_dir_close(adir);
194  }
195 
196  print_test_results();
197  exit(get_rv());
198 }
void qof_session_save(QofSession *session, QofPercentageFunc percentage_func)
QofBackendError
The errors that can be reported to the GUI & other front-end users.
Definition: qofbackend.h:59
void xaccLogDisable(void)
Definition: TransLog.c:93
api for GnuCash version 2 XML-based file format
QofBackendError qof_session_pop_error(QofSession *session)
void gnc_engine_init(int argc, char **argv)
Definition: gnc-engine.c:139
All type declarations for the whole Gnucash engine.
API for the transaction logger.
void qof_session_begin(QofSession *session, const char *book_id, gboolean ignore_lock, gboolean create, gboolean force)