Reviewed the code, fixed a small bug with option name detection

This commit is contained in:
Gabriele Bartolini
2010-12-16 22:13:30 +01:00
parent 56c65acd99
commit c687d0d670

View File

@@ -10,35 +10,36 @@
void void
parse_config(const char *config_file, char *cluster_name, int *node, char *conninfo) parse_config(const char *config_file, char *cluster_name, int *node, char *conninfo)
{ {
char *s, buff[256]; char *s, buff[MAXLEN];
char name[MAXLEN];
char value[MAXLEN];
FILE *fp = fopen (config_file, "r"); FILE *fp = fopen (config_file, "r");
if (fp == NULL) if (fp == NULL)
return; return;
/* Read next line */ /* Read next line */
while ((s = fgets (buff, sizeof buff, fp)) != NULL) while ((s = fgets (buff, sizeof buff, fp)) != NULL)
{ {
char name[MAXLEN]; /* Skip blank lines and comments */
char value[MAXLEN]; if (buff[0] == '\n' || buff[0] == '#')
continue;
/* Skip blank lines and comments */ /* Parse name/value pair from line */
if (buff[0] == '\n' || buff[0] == '#')
continue;
/* Parse name/value pair from line */
parse_line(buff, name, value); parse_line(buff, name, value);
/* Copy into correct entry in parameters struct */ /* Copy into correct entry in parameters struct */
if (strcmp(name, "cluster") == 0) if (strncmp(name, "cluster", 7) == 0)
strncpy (cluster_name, value, MAXLEN); strncpy (cluster_name, value, MAXLEN);
else if (strcmp(name, "node") == 0) else if (strncmp(name, "node", 4) == 0)
*node = atoi(value); *node = atoi(value);
else if (strcmp(name, "conninfo") == 0) else if (strncmp(name, "conninfo", 8) == 0)
strncpy (conninfo, value, MAXLEN); strncpy (conninfo, value, MAXLEN);
else else
printf ("WARNING: %s/%s: Unknown name/value pair!\n", name, value); printf ("WARNING: %s/%s: Unknown name/value pair!\n", name, value);
} }
/* Close file */ /* Close file */
fclose (fp); fclose (fp);
@@ -52,12 +53,12 @@ trim (char *s)
/* Trim and delimit right side */ /* Trim and delimit right side */
while ( (isspace (*s2)) && (s2 >= s1) ) while ( (isspace (*s2)) && (s2 >= s1) )
s2--; --s2;
*(s2+1) = '\0'; *(s2+1) = '\0';
/* Trim left side */ /* Trim left side */
while ( (isspace (*s1)) && (s1 < s2) ) while ( (isspace (*s1)) && (s1 < s2) )
s1++; ++s1;
/* Copy finished string */ /* Copy finished string */
strcpy (s, s1); strcpy (s, s1);
@@ -67,14 +68,13 @@ trim (char *s)
void void
parse_line(char *buff, char *name, char *value) parse_line(char *buff, char *name, char *value)
{ {
int i; int i = 0;
int j; int j = 0;
/* /*
* first we find the name of the parameter * first we find the name of the parameter
*/ */
j = 0; for ( ; i < MAXLEN; ++i)
for (i = 0; i < MAXLEN; i++)
{ {
if (buff[i] != '=') if (buff[i] != '=')
name[j++] = buff[i]; name[j++] = buff[i];
@@ -83,12 +83,11 @@ parse_line(char *buff, char *name, char *value)
} }
name[j] = '\0'; name[j] = '\0';
i++;
/* /*
* Now the value * Now the value
*/ */
j = 0; j = 0;
for ( ; i < MAXLEN; i++) for ( ++i ; i < MAXLEN; i++)
if (buff[i] == '\'') if (buff[i] == '\'')
continue; continue;
else if (buff[i] != '\n') else if (buff[i] != '\n')
@@ -96,5 +95,5 @@ parse_line(char *buff, char *name, char *value)
else else
break; break;
value[j] = '\0'; value[j] = '\0';
trim(value); trim(value);
} }