[Pinfo-devel] r71 - pinfo/branches/cxx/src
Nathanael Nerode
neroden-guest at costa.debian.org
Mon Aug 29 05:25:46 UTC 2005
Author: neroden-guest
Date: 2005-08-29 05:25:44 +0000 (Mon, 29 Aug 2005)
New Revision: 71
Modified:
pinfo/branches/cxx/src/parse_config.cxx
Log:
Convert low-hanging fruit in parse_config.cxx.
Modified: pinfo/branches/cxx/src/parse_config.cxx
===================================================================
--- pinfo/branches/cxx/src/parse_config.cxx 2005-08-29 05:08:42 UTC (rev 70)
+++ pinfo/branches/cxx/src/parse_config.cxx 2005-08-29 05:25:44 UTC (rev 71)
@@ -20,6 +20,8 @@
***************************************************************************/
#include "common_includes.h"
+#include <string>
+using std::string;
RCSID("$Id$")
@@ -101,73 +103,60 @@
int
parse_config(void)
{
- char config_file_name[256], *home = 0;
- char line[256];
+ string config_file_name;
+ string home;
FILE *f;
- int line_number = 0;
- if (rcfile != NULL)
- {
+
+ if (rcfile != NULL) { /* User specified config file */
f = fopen(rcfile, "r");
- if (f == NULL)
- {
+ if (f == NULL) {
fprintf(stderr, _("Can't open config file!\n"));
exit(1);
}
- }
- else
- {
- if (rcfile == NULL)
- if (getenv("HOME"))
- home = strdup(getenv("HOME"));
- else
- home = 0;
- if (home)
- {
- strcpy(config_file_name, home);
- strcat(config_file_name, "/.pinforc");
- if (!(f = fopen(config_file_name, "r")))
- {
- strcpy(config_file_name, CONFIGDIR);
- if (!(f = fopen(config_file_name, "r")))
- {
- free(home); /* home is nonzero; see if (home) above */
+ } else { /* rcfile == NULL */
+ char* rawhome = getenv("HOME");
+ if (rawhome != NULL)
+ home = rawhome;
+ if (home != "") {
+ config_file_name = home;
+ config_file_name += "/.pinforc";
+ f = fopen(config_file_name.c_str(), "r");
+ if (f == NULL) {
+ config_file_name = CONFIGDIR;
+ f = fopen(config_file_name.c_str(), "r");
+ if (f == NULL) {
return 0; /* no config file available */
}
}
- }
- else
- {
- strcpy(config_file_name, CONFIGDIR);
- if (!(f = fopen(config_file_name, "r")))
- {
- /* free(home); home is unallocated; see if (home) above */
- return 0;
+ } else { /* home == "" */
+ config_file_name = CONFIGDIR;
+ f = fopen(config_file_name.c_str(), "r");
+ if (f == NULL) {
+ return 0; /* no config file available */
}
}
}
- while (!feof(f))
- {
- if (!(fgets(line, 255, f)))
- {
+
+ int line_number = 0;
+ while (!feof(f)) {
+ char line[256];
+ if (!(fgets(line, 255, f))) {
fclose(f);
- if (home)
- free(home);
return 0;
}
- if (parse_line(line))
- {
+ if (parse_line(line)) {
+ /* Line parse failure */
line_number++;
fclose(f);
fprintf(stderr, _("Parse error in config file on line %d\n"), line_number);
exit(1);
- }
- else
+ } else {
+ /* Line parsed successfully */
line_number++;
+ }
}
fclose(f);
- if (home)
- free(home);
return 0;
}
More information about the Pinfo-devel
mailing list