summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c111
1 files changed, 110 insertions, 1 deletions
diff --git a/main.c b/main.c
index 1679cb4..8ffced8 100644
--- a/main.c
+++ b/main.c
@@ -17,17 +17,20 @@
* along with WordExtract. If not, see <http://www.gnu.org/licenses/>.
*/
+#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "main.h"
#include "mainwin.h"
#include "dict.h"
+#include "engparser.h"
Language lang;
SaveOpt save_user_words = {2, 40};
char optpath[PATH_LENGTH] = {0};
char dictfile[PATH_LENGTH] = {0};
+char conffile[PATH_LENGTH] = {0};
static void fill_vars();
@@ -64,10 +67,116 @@ int main(int argc, char *argv[])
return 0;
}
-void fill_vars()
+static void fill_vars()
{
strcpy(optpath, getenv("HOME"));
strcat(optpath, OPT_FOLDER);
strcat(dictfile, optpath);
strcat(dictfile, DICT_FILE);
+ strcat(conffile, optpath);
+ strcat(conffile, CONF_FILE);
+ read_config();
}
+
+void create_config(char *path)
+{
+ mode_t mode_0755 = S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
+ struct stat st;
+ FILE *conf;
+ char file[PATH_LENGTH] = {0};
+
+ if (stat(path, &st)) {
+ fprintf(stderr, "%s: Creating directory\n", path);
+ if (mkdir(path, mode_0755)) {
+ perror(path);
+ exit(1);
+ }
+ }
+ strcat(file, path);
+ strcat(file, CONF_FILE);
+ fprintf(stderr, "%s: Creating configuration file\n", file);
+ if (!(conf = fopen(file, "w"))) {
+ perror(file);
+ exit(1);
+ }
+ fclose(conf);
+}
+
+void read_config()
+{
+ FILE *conf;
+
+ if (!(conf = fopen(conffile, "r"))) {
+ perror(conffile);
+ create_config(optpath);
+ write_config();
+ if (!(conf = fopen(dictfile, "r"))) {
+ perror(conffile);
+ exit(1);
+ }
+ } else {
+ char option[OPTION_LENGTH] = {0};
+ char svalue[OPTION_LENGTH] = {0};
+ while (fscanf(conf, "%s\t\t%s\n", option, svalue) != EOF) {
+ if (!strcmp(option, "columns")) {
+ save_user_words.columns = atoi(svalue);
+ } else if (!strcmp(option, "field")) {
+ save_user_words.col_width = atoi(svalue);
+ } else if (!strcmp(option, "language")) {
+ lang = atoi(svalue);
+ } else if (!strcmp(option, "exclude_with_capital")) {
+ excl_w_capital = atoi(svalue);
+ } else if (!strcmp(option, "lower_first_capital")) {
+ lower_first_capital = atoi(svalue);
+ } else if (!strcmp(option, "exclude_quoted")) {
+ quote.excl_symbolled = atoi(svalue);
+ } else if (!strcmp(option, "exclude_with_first_quote")) {
+ quote.excl_w_starting = atoi(svalue);
+ } else if (!strcmp(option, "exclude_with_middle_quote")) {
+ quote.excl_w_middle = atoi(svalue);
+ } else if (!strcmp(option, "exclude_part_after_quote")) {
+ quote.excl_word_after_symb = atoi(svalue);
+ } else if (!strcmp(option, "exclude_with_end_quote")) {
+ quote.excl_w_ending = atoi(svalue);
+ } else if (!strcmp(option, "exclude_hyphened")) {
+ hyphen.excl_symbolled = atoi(svalue);
+ } else if (!strcmp(option, "exclude_with_first_hyphen")) {
+ hyphen.excl_w_starting = atoi(svalue);
+ } else if (!strcmp(option, "exclude_with_middle_hyphen")) {
+ hyphen.excl_w_middle = atoi(svalue);
+ } else if (!strcmp(option, "exclude_part_after_hyphen")) {
+ hyphen.excl_word_after_symb = atoi(svalue);
+ } else if (!strcmp(option, "exclude_with_end_hyphen")) {
+ hyphen.excl_w_ending = atoi(svalue);
+ }
+ }
+ fclose(conf);
+ }
+}
+
+void write_config()
+{
+ FILE *conf;
+
+ if (!(conf = fopen(conffile, "w")))
+ perror(conffile);
+ else {
+ fprintf(conf, "%s\t\t%d\n", "columns", save_user_words.columns);
+ fprintf(conf, "%s\t\t%d\n", "field", save_user_words.col_width);
+ fprintf(conf, "%s\t\t%d\n", "language", lang);
+ fprintf(conf, "%s\t\t%d\n", "exclude_with_capital", excl_w_capital);
+ fprintf(conf, "%s\t\t%d\n", "lower_first_capital", lower_first_capital);
+ fprintf(conf, "%s\t\t%d\n", "exclude_quoted", quote.excl_symbolled);
+ fprintf(conf, "%s\t\t%d\n", "exclude_with_first_quote", quote.excl_w_starting);
+ fprintf(conf, "%s\t\t%d\n", "exclude_with_middle_quote", quote.excl_w_middle);
+ fprintf(conf, "%s\t\t%d\n", "exclude_part_after_quote", quote.excl_word_after_symb);
+ fprintf(conf, "%s\t\t%d\n", "exclude_with_end_quote", quote.excl_w_ending);
+ fprintf(conf, "%s\t\t%d\n", "exclude_hyphened", hyphen.excl_symbolled);
+ fprintf(conf, "%s\t\t%d\n", "exclude_with_first_hyphen", hyphen.excl_w_starting);
+ fprintf(conf, "%s\t\t%d\n", "exclude_with_middle_hyphen", hyphen.excl_w_middle);
+ fprintf(conf, "%s\t\t%d\n", "exclude_part_after_hyphen", hyphen.excl_word_after_symb);
+ fprintf(conf, "%s\t\t%d\n", "exclude_with_end_hyphen", hyphen.excl_w_ending);
+ fclose(conf);
+ }
+}
+