Update voor Wordlist menu
[sgc.git] / file.c
blobd0fe5fbd4055a4a35604b6a63f4faec048979cab
1 #include "sgc.h"
3 gchar * checkpinyin(gchar *pinyin) {
4 gchar **result;
5 g_ascii_strdown(pinyin, -1);
6 g_strcanon(pinyin, "01234abcdefghijklmnopqrstuvwxyz\n", '-');
7 result = g_strsplit(pinyin, "-", MAXTESTS * 2);
8 g_free(pinyin);
9 pinyin = g_strjoinv(NULL, result);
10 return pinyin;
13 void clear() {
14 gtk_list_store_clear(GTK_LIST_STORE(liststore));
15 treevalid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(liststore), &mainIter);
16 updateWidgets();
19 void fileOpen(gchar *filename) {
20 gchar **testList = NULL;
21 gchar *contents;
22 GError *error = NULL;
23 gsize length;
25 clear();
27 if (g_file_get_contents(filename, &contents, &length, &error) != FALSE) {
28 int i = 0;
29 gchar *processed = checkpinyin(contents);
31 testList = g_strsplit(processed, "\n", MAXTESTS);
32 while ( testList[i] != NULL ) {
33 add_entry(testList[i]);
34 i++;
36 } else {
37 g_error_free(error);
40 if (testList == NULL) {
41 testList = g_strsplit("xu1\nci2", "\n", MAXTESTS);
42 add_entry("xu1");
43 add_entry("ci2");
46 treevalid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(liststore), &mainIter);
47 updateWidgets();
49 g_strfreev(testList);
52 void fileSave(gchar *filename) {
53 if (treevalid == TRUE && filename != NULL) {
54 GtkTreeIter iter;
55 gboolean valid;
57 /* Get first row in list store */
58 valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(liststore), &iter);
60 if (valid) {
61 GString *contents = g_string_new("");
62 if (contents != NULL) {
63 while (valid)
65 gchar *string;
66 gtk_tree_model_get(GTK_TREE_MODEL(liststore), &iter, COL_TEXT, &string, -1);
67 contents = g_string_append(contents, string);
68 contents = g_string_append(contents, "\r\n");
70 valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(liststore), &iter);
73 GError *error = NULL;
75 if (g_file_set_contents(filename, contents->str, -1, &error) == FALSE) {
76 g_error_free(error);
78 g_string_free(contents, true);