Merge with git+ssh://sgc@fondialog1.hum.uva.nl/fro/git/sgc.git
[sgc.git] / file.c
blob9e242c50e6d800504ffbfd5dba86498a4a8af2f7
1 #include "sgc.h"
2 #include "file.h"
3 #include "signals-editor.h"
5 gchar *base = NULL;
6 gchar *file = NULL;
8 void clear() {
9 gtk_list_store_clear(GTK_LIST_STORE(liststore));
10 treevalid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(liststore), &mainIter);
11 updateWidgets();
14 void setBase(gchar *newbase) {
15 if (base != NULL)
16 g_free(base);
17 base = newbase;
20 void setFile(gchar *newfile) {
21 if (file != NULL)
22 g_free(file);
24 file = newfile;
27 void fileOpen(gchar *filename) {
28 gchar **testList = NULL;
29 gchar *contents;
30 gsize length;
32 clear();
34 if (g_file_get_contents(filename, &contents, &length, NULL) != FALSE) {
35 gchar *processed;
36 int i = 0;
38 processed = checkPinyin(contents);
39 testList = g_strsplit(processed, "\n", MAXTESTS);
40 g_free(processed);
42 while ( testList[i] != NULL) {
43 if (g_str_has_prefix (testList[i], "#")) {
44 /* Voeg commentaar toe ofzo */
45 } else {
46 add_entry(testList[i]);
48 i++;
52 if (testList == NULL) {
53 testList = g_strsplit("xu1\nci2", "\n", MAXTESTS);
54 add_entry("xu1");
55 add_entry("ci2");
58 g_strfreev(testList);
59 gtk_window_set_title(GTK_WINDOW(glade_xml_get_widget(xml, "windowWordlist")), filename);
61 setBase(g_path_get_dirname(filename));
62 setFile(g_strdup(filename));
64 treevalid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(liststore), &mainIter);
65 updateWidgets();
69 void fileSave(gchar *filename) {
70 if (treevalid == TRUE && filename != NULL) {
71 GtkTreeIter iter;
72 gboolean valid;
74 /* Get first row in list store */
75 valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(liststore), &iter);
77 if (valid) {
78 GString *contents = g_string_new("");
79 if (contents != NULL) {
80 while (valid)
82 gchar *string;
83 gtk_tree_model_get(GTK_TREE_MODEL(liststore), &iter, COL_TEXT, &string, -1);
84 contents = g_string_append(contents, string);
85 contents = g_string_append(contents, "\r\n");
87 valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(liststore), &iter);
91 g_file_set_contents(filename, contents->str, -1, NULL);
93 g_string_free(contents, true);