Using the error reporting framework
This commit is contained in:
parent
e99fa5c400
commit
4ddafea8be
50
src/config.c
50
src/config.c
|
@ -13,8 +13,6 @@
|
||||||
* You should have received a copy of the Creative Commons Attribution-
|
* You should have received a copy of the Creative Commons Attribution-
|
||||||
* NonCommercial-ShareAlike 3.0 along with libSystem; if not, browse to
|
* NonCommercial-ShareAlike 3.0 along with libSystem; if not, browse to
|
||||||
* http://creativecommons.org/licenses/by-nc-sa/3.0/ */
|
* http://creativecommons.org/licenses/by-nc-sa/3.0/ */
|
||||||
/* TODO:
|
|
||||||
* - use the error reporting framework */
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -143,15 +141,14 @@ int config_load(Config * config, char const * filename)
|
||||||
char * str;
|
char * str;
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
if((section = string_new("")) == NULL
|
if((section = string_new("")) == NULL)
|
||||||
|| (fp = fopen(filename, "r")) == NULL)
|
return 1;
|
||||||
|
if((fp = fopen(filename, "r")) == NULL)
|
||||||
{
|
{
|
||||||
free(section);
|
free(section);
|
||||||
return error_set_code(1, "%s%s%s", filename, ": ",
|
return error_set_code(1, "%s: %s", filename, strerror(errno));
|
||||||
strerror(errno));
|
|
||||||
}
|
}
|
||||||
while((c = fgetc(fp)) != EOF)
|
while((c = fgetc(fp)) != EOF)
|
||||||
{
|
|
||||||
if(c == '#')
|
if(c == '#')
|
||||||
while((c = fgetc(fp)) != EOF && c != '\n');
|
while((c = fgetc(fp)) != EOF && c != '\n');
|
||||||
else if(c == '[')
|
else if(c == '[')
|
||||||
|
@ -174,14 +171,10 @@ int config_load(Config * config, char const * filename)
|
||||||
}
|
}
|
||||||
else if(c != '\n')
|
else if(c != '\n')
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
free(section);
|
free(section);
|
||||||
free(variable);
|
free(variable);
|
||||||
if(!feof(fp))
|
if(!feof(fp))
|
||||||
{
|
ret = error_set_code(1, "%s: %s", filename, "Syntax error");
|
||||||
errno = EINVAL;
|
|
||||||
ret = 1;
|
|
||||||
}
|
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -272,8 +265,8 @@ static char * _load_value(FILE * fp)
|
||||||
|
|
||||||
|
|
||||||
/* config_save */
|
/* config_save */
|
||||||
static int _save_section(Hash * h, size_t i, FILE * fp);
|
static int _save_section(Hash * h, size_t i, FILE * fp, char const * filename);
|
||||||
static int _save_variables(Hash * h, FILE * fp);
|
static int _save_variables(Hash * h, FILE * fp, char const * filename);
|
||||||
|
|
||||||
int config_save(Config * config, char const * filename)
|
int config_save(Config * config, char const * filename)
|
||||||
{
|
{
|
||||||
|
@ -285,19 +278,15 @@ int config_save(Config * config, char const * filename)
|
||||||
if((i = array_count(config)) == 0)
|
if((i = array_count(config)) == 0)
|
||||||
return 1;
|
return 1;
|
||||||
if((fp = fopen(filename, "w")) == NULL)
|
if((fp = fopen(filename, "w")) == NULL)
|
||||||
{
|
return error_set_code(1, "%s: %s", filename, strerror(errno));
|
||||||
fprintf(stderr, "%s", "libutils: ");
|
|
||||||
perror(filename);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
for(j = 0; j < i; j++)
|
for(j = 0; j < i; j++)
|
||||||
if((ret = _save_section(config, j, fp)) != 0)
|
if((ret = _save_section(config, j, fp, filename)) != 0)
|
||||||
break;
|
break;
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int _save_section(Hash * h, size_t i, FILE * fp)
|
static int _save_section(Hash * h, size_t i, FILE * fp, char const * filename)
|
||||||
{
|
{
|
||||||
HashEntry * he;
|
HashEntry * he;
|
||||||
|
|
||||||
|
@ -305,17 +294,19 @@ static int _save_section(Hash * h, size_t i, FILE * fp)
|
||||||
if(he->name[0] != '\0')
|
if(he->name[0] != '\0')
|
||||||
{
|
{
|
||||||
if(fprintf(fp, "[%s]\n", he->name) < 0)
|
if(fprintf(fp, "[%s]\n", he->name) < 0)
|
||||||
return 1;
|
return error_set_code(1, "%s: %s", filename,
|
||||||
|
strerror(errno));
|
||||||
}
|
}
|
||||||
else if(i != 0)
|
else if(i != 0 && fwrite("[]\n", sizeof(char), 3, fp) != 3)
|
||||||
if(fwrite("[]\n", sizeof(char), 3, fp) != 3)
|
return error_set_code(1, "%s: %s", filename, strerror(errno));
|
||||||
|
if(_save_variables(he->data, fp, filename) != 0)
|
||||||
return 1;
|
return 1;
|
||||||
if(_save_variables(he->data, fp) != 0)
|
if(fputc('\n', fp) == '\n')
|
||||||
return 1;
|
return 0;
|
||||||
return fputc('\n', fp) == '\n' ? 0 : 1;
|
return error_set_code(1, "%s: %s", filename, strerror(errno));
|
||||||
}
|
}
|
||||||
|
|
||||||
static int _save_variables(Hash * h, FILE * fp)
|
static int _save_variables(Hash * h, FILE * fp, char const * filename)
|
||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
size_t j;
|
size_t j;
|
||||||
|
@ -328,7 +319,8 @@ static int _save_variables(Hash * h, FILE * fp)
|
||||||
if(he->name == NULL || he->data == NULL)
|
if(he->name == NULL || he->data == NULL)
|
||||||
continue;
|
continue;
|
||||||
if(fprintf(fp, "%s=%s\n", he->name, (char*)he->data) < 0)
|
if(fprintf(fp, "%s=%s\n", he->name, (char*)he->data) < 0)
|
||||||
return 1;
|
return error_set_code(1, "%s: %s", filename,
|
||||||
|
strerror(errno));
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user