Changed tone scripts for better neutral and second tones
[sgc.git] / file.c
blobc0bc2ccdce30b2a8337ad1aba215e7814ae09b4e
1 #include "sgc.h"
3 gchar *base = NULL;
4 gchar *file = NULL;
6 gchar * checkpinyin(gchar *pinyin) {
7 gchar **result;
8 g_ascii_strdown(pinyin, -1);
9 g_strcanon(pinyin, "012345abcdefghijklmnopqrstuvwxyz\n", '-');
10 result = g_strsplit(pinyin, "-", MAXTESTS * 2);
11 g_free(pinyin);
12 pinyin = g_strjoinv(NULL, result);
13 return pinyin;
16 void clear() {
17 gtk_list_store_clear(GTK_LIST_STORE(liststore));
18 treevalid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(liststore), &mainIter);
19 updateWidgets();
22 void setBase(gchar *newbase) {
23 if (base != NULL)
24 g_free(base);
26 base = newbase;
29 void setFile(gchar *newfile) {
30 if (file != NULL)
31 g_free(file);
33 file = newfile;
36 void fileOpen(gchar *filename) {
37 gchar **testList = NULL;
38 gchar *contents;
39 gsize length;
41 clear();
43 if (g_file_get_contents(filename, &contents, &length, NULL) != FALSE) {
44 int i = 0;
45 gchar *processed = checkpinyin(contents);
47 testList = g_strsplit(processed, "\n", MAXTESTS);
48 while ( testList[i] != NULL) {
49 add_entry(testList[i]);
50 i++;
54 if (testList == NULL) {
55 testList = g_strsplit("xu1\nci2", "\n", MAXTESTS);
56 add_entry("xu1");
57 add_entry("ci2");
60 g_strfreev(testList);
61 gtk_window_set_title(GTK_WINDOW(glade_xml_get_widget(xml, "windowWordlist")), filename);
63 setBase(g_path_get_dirname(filename));
64 setFile(g_strdup(filename));
66 treevalid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(liststore), &mainIter);
67 updateWidgets();
71 void fileSave(gchar *filename) {
72 if (treevalid == TRUE && filename != NULL) {
73 GtkTreeIter iter;
74 gboolean valid;
76 /* Get first row in list store */
77 valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(liststore), &iter);
79 if (valid) {
80 GString *contents = g_string_new("");
81 if (contents != NULL) {
82 while (valid)
84 gchar *string;
85 gtk_tree_model_get(GTK_TREE_MODEL(liststore), &iter, COL_TEXT, &string, -1);
86 contents = g_string_append(contents, string);
87 contents = g_string_append(contents, "\r\n");
89 valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(liststore), &iter);
93 g_file_set_contents(filename, contents->str, -1, NULL);
95 g_string_free(contents, true);