/* conffile.c function for conffile * Copyright (c) 2004 by SmartLu All Rights Reserved * Distributed under the terms of the GNU General Public License (GPL) * See the GNU Library General Public License for more details. */ #include "srecite.h" #include #include static void print_conf();/* print S_conf for load srecite.conf*/ gchar *get_value(gchar *str) { gchar *ptr1,*ptr2; str [strlen(str)-1] = '\0'; ptr1 = strchr(str, ':') + 1; while (*ptr1 == ' ' || *ptr1 == '\t') ptr1++; ptr2 = ptr1; // add '\0' to end of ptr1 while (*ptr2 && *ptr2 != ' ' && *ptr2 != '\t') ptr2++; *ptr2 = '\0'; return ptr1; } /* get key word to sting value */ void get_cfg(FILE *fp,gchar *key,gchar *value) { gchar line[255]; while(!feof(fp)) { fgets(line,256,fp); if (line[0] == '\n' || line[0] == '#') continue; else if (!strncmp(line,key,strlen(key))) { strcpy (value,get_value(line)); return; } } } void get_cfg_string(FILE *fp,gchar *key, gchar * result) { get_cfg(fp,key,result); } gint get_cfg_int(FILE *fp,gchar *key) { gchar temp[5]; get_cfg(fp,key,temp); return atoi(temp); } gint get_cfg_bool(FILE *fp,gchar *key) { gchar temp[5]; get_cfg(fp,key,temp); if (!strcmp("TRUE",temp)) return TRUE; else return FALSE; } /* load srecite.conf */ void load_conf() { FILE *fp=NULL; gchar strPath[1024]; strcpy(strPath, home_path); strcat(strPath, "srecite.conf"); if ((fp=fopen(strPath,"r")) == NULL) { g_print("Can't open srecite.conf (%s) !\n",strPath); g_print("Use default conf and write a srecite.conf after quit(root)\n\n"); S_conf.style = 0; S_conf.random = 0; S_conf.EN_word = TRUE; S_conf.TK_word = TRUE; S_conf.CH_word = TRUE; S_conf.time_interval = 6000; S_conf.win_x = 400; S_conf.win_y = 0; S_conf.test_counter = 10; strcpy(S_conf.font,"sans "); strcpy(S_conf.fontsize,"18"); S_conf.pronounce = 0; S_conf.show_format = 0; //exit(0); } else { //get_cfg_string(fp,"FILENAME",S_conf.filename); g_print("Open (%s)\n",strPath); S_conf.style = get_cfg_int(fp,"STYLE"); S_conf.random = get_cfg_int(fp,"RANDOM"); S_conf.EN_word = get_cfg_bool(fp,"EN_WORD"); S_conf.TK_word = get_cfg_bool(fp,"TK_WORD"); S_conf.CH_word = get_cfg_bool(fp,"CH_WORD"); S_conf.time_interval = get_cfg_int(fp,"TIME_INTERVAL"); S_conf.win_x = get_cfg_int(fp,"WIN_X"); S_conf.win_y = get_cfg_int(fp,"WIN_Y"); S_conf.test_counter = get_cfg_int(fp,"TEST_COUNTER"); get_cfg_string(fp,"FONT",S_conf.font); get_cfg_string(fp,"FONTSIZE",S_conf.fontsize); S_conf.pronounce = get_cfg_int(fp,"PRONOUNCE"); S_conf.show_format = get_cfg_int(fp,"SHOW_FORMAT"); fclose (fp); } print_conf(); } /* print S_conf for load srecite.conf*/ static void print_conf() { //g_print("%s = %s\n","FILENAME",S_conf.filename); g_print("-------------- screcite.conf ----------------\n"); g_print("-%s = %d\n","STYLE",S_conf.style); g_print("-%s = %d\n","RANDOM",S_conf.random); g_print("-%s = %d\t\t","EN",S_conf.EN_word); g_print("-%s = %d\n","CH",S_conf.CH_word); g_print("-%s = %d\n","TIME_INTERVAL",S_conf.time_interval); g_print("-%s = %d\t\t","WIN_X",S_conf.win_x); g_print("-%s = %d\n","WIN_Y",S_conf.win_y); g_print("-%s = %d\n","TEST_COUNTER",S_conf.test_counter); g_print("-%s = %s\t\t","FONT",S_conf.font); g_print("-%s = %s\n","FONTSIZE",S_conf.fontsize); g_print("-%s = %d\n","PRONOUNCE",S_conf.pronounce); g_print("-%s = %d\n","SHOW_FORMAT",S_conf.show_format); g_print("-------------- screcite.conf ----------------\n"); } /* change show format switch,0=3line,1=1line */ void show_format_switch() { S_conf.show_format ++; S_conf.show_format = S_conf.show_format % 3; g_print("click line format! S_conf.show_format = %d\n",S_conf.show_format); } /* change show pronuciation switch,0=off,1=on */ void pronounce_switch() { // show_stop(); if(GTK_CHECK_MENU_ITEM(pron_menu_item)->active) { S_conf.pronounce = 1; g_print("pronounce switch ON! S_conf.pronounce = %d\n",S_conf.pronounce); } else { S_conf.pronounce = 0; g_print("pronounce switch OFF! S_conf.pronounce = %d\n",S_conf.pronounce); } // show_play(); } /* change show word style,0=normal,1=EN first */ void change_style() { // show_stop(); if(GTK_CHECK_MENU_ITEM(style_menu_item)->active) { S_conf.style = 1; g_print("style switch ON! S_conf.style = %d\n",S_conf.style); } else { S_conf.style = 0; g_print("style switch OFF! S_conf.style = %d\n",S_conf.style); } // show_play(); } /* int to sting */ gchar *save_cfg_boolean(gint num) { gchar *t="TRUE",*f="FALSE"; if (num==1) return t; else return f; } /* save srecite.conf */ void save_conf() { FILE *fp=NULL; gchar strPath[1024]; strcpy(strPath, home_path); if (access (strPath, 0)) { mkdir (strPath, S_IRWXU); g_print("mkdir (%s) !!\n",strPath); } strcat(strPath, "srecite.conf"); if ((fp=fopen(strPath,"w")) == NULL) { g_print("Can't write srecite.conf (%s) !\n",strPath); return; } //fprintf(fp,"FILENAME: %s\n",S_conf.filename); fprintf(fp,"STYLE: %d\n",S_conf.style); fprintf(fp,"RANDOM: %d\n",S_conf.random); fprintf(fp,"EN_WORD: %s\n",save_cfg_boolean(S_conf.EN_word)); fprintf(fp,"TK_WORD: %s\n",save_cfg_boolean(S_conf.TK_word)); fprintf(fp,"CH_WORD: %s\n",save_cfg_boolean(S_conf.CH_word)); fprintf(fp,"TIME_INTERVAL: %d\n",S_conf.time_interval); fprintf(fp,"WIN_X: %d\n",S_conf.win_x); fprintf(fp,"WIN_Y: %d\n",S_conf.win_y); fprintf(fp,"TEST_COUNTER: %d\n",S_conf.test_counter); fprintf(fp,"FONT: %s\n",S_conf.font); fprintf(fp,"FONTSIZE: %s\n",S_conf.fontsize); fprintf(fp,"PRONOUNCE: %d\n",S_conf.pronounce); fprintf(fp,"SHOW_FORMAT: %d\n",S_conf.show_format); //fprintf(fp,"BACK_COLOR: %s\n",S_conf.back_color); //fprintf(fp,"TEXT_COLOR: %s\n",S_conf.text_color); fclose (fp); }