Upstream tarball 20080721
[amule.git] / src / utils / cas / configfile.c
blob0c399f3de048f0591a253b5c0b9722ea8d0f4bfa
1 /*
2 * Name: Config file functions
4 * Purpose: Read info from casrc ou create one if it doesnt exist
6 * Author: Pedro de Oliveira <falso@rdk.homeip.net>
8 * Copyright (c) 2004-2008 Pedro de Oliveira
9 *
10 * This file is part of aMule.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the
24 * Free Software Foundation, Inc.,
25 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
32 #include "configfile.h"
33 #include "functions.h"
35 int writeconfig(void)
37 FILE *config;
38 char *path;
39 unsigned int i;
40 char *def[] = {
41 "# cas config file\n",
42 "#\n",
43 "# font - full path to a ttf font\n",
44 "# font_size - size the font\n",
45 "# source_image - image where the text will be writen\n",
46 "# *_line - x,y,[1/0] enabled or disabled\n\n",
47 "font /usr/share/fonts/corefonts/times.ttf\n",
48 "font_size 10.5\n",
49 "source_image /usr/share/pixmaps/stat.png\n",
50 "first_line 23,17,1\n",
51 "second_line 23,34,1\n",
52 "third_line 23,51,1\n",
53 "fourth_line 23,68,1\n",
54 "fifth_line 23,85,1\n",
55 "sixth_line 23,102,1\n",
56 "seventh_line 23,119,1\n",
57 "template /usr/share/pixmaps/tmp.html\n",
58 "img_type 0\n"
61 path = get_path("casrc");
62 if (path == NULL)
63 return 0;
65 if ( (config = fopen(path, "w")) == NULL)
66 return 0;
68 for (i = 0; i < sizeof(def) / sizeof(char *); i++)
69 fprintf(config, "%s", def[i]);
71 fclose(config);
73 printf("%s created, please edit it and then rerun cas\n", path);
74 free(path);
76 return 1;
79 /* Jacobo221 - [ToDo] There should be a check for corrupt config files! */
80 int readconfig(CONF *config)
82 char buffer[120], option[15], *path;
83 FILE *conf;
84 int i = 0;
85 char lines[IMG_TEXTLINES][13] = {
86 "first_line",
87 "second_line",
88 "third_line",
89 "fourth_line",
90 "fifth_line",
91 "sixth_line",
92 "seventh_line"
95 path = get_path("casrc");
96 if (path == NULL) {
97 return 0;
100 if ((conf = fopen(path, "r")) == NULL) {
101 printf("Unable to open %s. Creating it.\n", path);
102 free(path);
103 if (!writeconfig()) {
104 perror("readconfig: unable to create initial config file\n");
106 return 0;
108 free(path);
110 buffer[0] = 0;
111 while (!feof(conf)) {
112 // Jacobo221 - [ToDo] Only first char per line is comment...
113 if (fgets (buffer,120,conf)) {
114 if (buffer[0] != '#') {
115 /* Only two fileds per line */
116 sscanf(buffer, "%s %*s", option);
117 fflush (stdout);
118 // Jacobo221 - [ToDo] So lines can't be swapped...
119 if (strcmp(option, "font") == 0) {
120 sscanf(buffer, "%*s %s", config->font);
122 if (strcmp(option, "font_size") == 0) {
123 sscanf(buffer, "%*s %f", &config->size);
125 if (strcmp(option, "source_image") == 0) {
126 sscanf(buffer, "%*s %s", config->source);
128 if (strcmp(option, "template") == 0) {
129 sscanf(buffer, "%*s %s", config->template);
131 if (strcmp(option, "img_type") == 0) {
132 sscanf(buffer, "%*s %d", &config->img_type);
135 for (i = 0; i <= IMG_TEXTLINES; i++) {
136 if (strcmp(option, lines[i]) == 0) {
137 sscanf(buffer,
138 "%*s %d,%d,%d",
139 &config->x[i], &config->y[i],
140 &config->enabled[i]);
147 fclose(conf);
149 return 1;