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-2011 Pedro de Oliveira ( falso@rdk.homeip-net )
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
32 #include "configfile.h"
33 #include "functions.h"
35 #define STRINGIFY(x) #x
36 #define STRINGIFY_EXPAND(x) STRINGIFY(x)
38 #define MAX_CONF_ARG_LEN_STR STRINGIFY_EXPAND(MAX_CONF_ARG_LEN)
40 #define MAX_CONF_KEY_LEN 12
41 #define MAX_CONF_KEY_LEN_STR STRINGIFY_EXPAND(MAX_CONF_KEY_LEN)
49 "# cas config file\n",
51 "# font - full path to a ttf font\n",
52 "# font_size - size the font\n",
53 "# source_image - image where the text will be written\n",
54 "# *_line - x,y,[1/0] enabled or disabled\n\n",
55 "font /usr/share/fonts/corefonts/times.ttf\n",
57 "source_image /usr/share/cas/stat.png\n",
58 "first_line 23,17,1\n",
59 "second_line 23,34,1\n",
60 "third_line 23,51,1\n",
61 "fourth_line 23,68,1\n",
62 "fifth_line 23,85,1\n",
63 "sixth_line 23,102,1\n",
64 "seventh_line 23,119,1\n",
65 "template /usr/share/cas/tmp.html\n"
69 path
= get_path("casrc");
73 if ( (config
= fopen(path
, "w")) == NULL
)
76 for (i
= 0; i
< sizeof(def
) / sizeof(char *); i
++)
77 fprintf(config
, "%s", def
[i
]);
81 printf("%s created, please edit it and then rerun cas\n", path
);
87 /* Jacobo221 - [ToDo] There should be a check for corrupt config files! */
88 int readconfig(CONF
*config
)
90 char buffer
[120], option
[15], *path
;
93 char lines
[IMG_TEXTLINES
][13] = {
103 path
= get_path("casrc");
108 if ((conf
= fopen(path
, "r")) == NULL
) {
109 printf("Unable to open %s. Creating it.\n", path
);
111 if (!writeconfig()) {
112 perror("readconfig: unable to create initial config file\n");
119 while (!feof(conf
)) {
120 // Jacobo221 - [ToDo] Only first char per line is comment...
121 if (fgets (buffer
,120,conf
)) {
122 if (buffer
[0] != '#') {
123 /* Only two fileds per line */
124 sscanf(buffer
, "%" MAX_CONF_KEY_LEN_STR
"s %*" MAX_CONF_ARG_LEN_STR
"s", option
);
126 // Jacobo221 - [ToDo] So lines can't be swapped...
127 if (strcmp(option
, "font") == 0) {
128 sscanf(buffer
, "%*" MAX_CONF_KEY_LEN_STR
"s %" MAX_CONF_ARG_LEN_STR
"s", config
->font
);
130 if (strcmp(option
, "font_size") == 0) {
131 sscanf(buffer
, "%*" MAX_CONF_KEY_LEN_STR
"s %10f", &config
->size
);
133 if (strcmp(option
, "source_image") == 0) {
134 sscanf(buffer
, "%*" MAX_CONF_KEY_LEN_STR
"s %" MAX_CONF_ARG_LEN_STR
"s", config
->source
);
136 if (strcmp(option
, "template") == 0) {
137 sscanf(buffer
, "%*" MAX_CONF_KEY_LEN_STR
"s %" MAX_CONF_ARG_LEN_STR
"s", config
->template);
139 if (strcmp(option
, "img_type") == 0) {
140 sscanf(buffer
, "%*" MAX_CONF_KEY_LEN_STR
"s %1d", &config
->img_type
);
143 for (i
= 0; i
< IMG_TEXTLINES
; i
++) {
144 if (strcmp(option
, lines
[i
]) == 0) {
146 "%*" MAX_CONF_KEY_LEN_STR
"s %4d,%4d,%4d",
147 &config
->x
[i
], &config
->y
[i
],
148 &config
->enabled
[i
]);