From fdc8a21a48a865ce26fd48a5bbb08ce189adb359 Mon Sep 17 00:00:00 2001 From: Ahmed Badran Date: Sat, 16 Aug 2008 00:57:32 -0700 Subject: [PATCH] Miscellaneous fixes, add support for single word section names Signed-off-by: Ahmed Badran --- src/Makefile.am | 9 ++++-- src/Makefile.in | 89 ++++++++++++++++++++++++++++++++++++++++++------------- src/configfile.cc | 49 ++++++++++++++++++++++-------- src/configfile.h | 4 +-- 4 files changed, 112 insertions(+), 39 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 7f53f60..435fde5 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,3 +1,6 @@ -lib_LTLIBRARIES = libconfigfile.la -libconfigfile_la_SOURCES = configfile.cc -include_HEADERS = configfile.h +lib_LTLIBRARIES = libconfigfile.la +libconfigfile_la_SOURCES = configfile.cc +include_HEADERS = configfile.h +bin_PROGRAMS = configfile_test.exe +configfile_test_exe_SOURCES = configfile_test.cc +configfile_test_exe_LDADD = libconfigfile.la diff --git a/src/Makefile.in b/src/Makefile.in index d81e362..cfe1048 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -15,6 +15,7 @@ @SET_MAKE@ + VPATH = @srcdir@ pkgdatadir = $(datadir)/@PACKAGE@ pkglibdir = $(libdir)/@PACKAGE@ @@ -33,6 +34,7 @@ PRE_UNINSTALL = : POST_UNINSTALL = : build_triplet = @build@ host_triplet = @host@ +bin_PROGRAMS = configfile_test.exe$(EXEEXT) subdir = src DIST_COMMON = $(include_HEADERS) $(srcdir)/Makefile.am \ $(srcdir)/Makefile.in @@ -49,12 +51,18 @@ am__vpath_adj = case $$p in \ *) f=$$p;; \ esac; am__strip_dir = `echo $$p | sed -e 's|^.*/||'`; -am__installdirs = "$(DESTDIR)$(libdir)" "$(DESTDIR)$(includedir)" +am__installdirs = "$(DESTDIR)$(libdir)" "$(DESTDIR)$(bindir)" \ + "$(DESTDIR)$(includedir)" libLTLIBRARIES_INSTALL = $(INSTALL) LTLIBRARIES = $(lib_LTLIBRARIES) libconfigfile_la_LIBADD = am_libconfigfile_la_OBJECTS = configfile.lo libconfigfile_la_OBJECTS = $(am_libconfigfile_la_OBJECTS) +binPROGRAMS_INSTALL = $(INSTALL_PROGRAM) +PROGRAMS = $(bin_PROGRAMS) +am_configfile_test_exe_OBJECTS = configfile_test.$(OBJEXT) +configfile_test_exe_OBJECTS = $(am_configfile_test_exe_OBJECTS) +configfile_test_exe_DEPENDENCIES = libconfigfile.la DEFAULT_INCLUDES = -I. -I$(top_builddir)@am__isrc@ depcomp = $(SHELL) $(top_srcdir)/depcomp am__depfiles_maybe = depfiles @@ -67,8 +75,9 @@ CXXLD = $(CXX) CXXLINK = $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \ --mode=link $(CXXLD) $(AM_CXXFLAGS) $(CXXFLAGS) $(AM_LDFLAGS) \ $(LDFLAGS) -o $@ -SOURCES = $(libconfigfile_la_SOURCES) -DIST_SOURCES = $(libconfigfile_la_SOURCES) +SOURCES = $(libconfigfile_la_SOURCES) $(configfile_test_exe_SOURCES) +DIST_SOURCES = $(libconfigfile_la_SOURCES) \ + $(configfile_test_exe_SOURCES) includeHEADERS_INSTALL = $(INSTALL_HEADER) HEADERS = $(include_HEADERS) ETAGS = etags @@ -183,6 +192,8 @@ top_srcdir = @top_srcdir@ lib_LTLIBRARIES = libconfigfile.la libconfigfile_la_SOURCES = configfile.cc include_HEADERS = configfile.h +configfile_test_exe_SOURCES = configfile_test.cc +configfile_test_exe_LDADD = libconfigfile.la all: all-am .SUFFIXES: @@ -245,6 +256,37 @@ clean-libLTLIBRARIES: done libconfigfile.la: $(libconfigfile_la_OBJECTS) $(libconfigfile_la_DEPENDENCIES) $(CXXLINK) -rpath $(libdir) $(libconfigfile_la_OBJECTS) $(libconfigfile_la_LIBADD) $(LIBS) +install-binPROGRAMS: $(bin_PROGRAMS) + @$(NORMAL_INSTALL) + test -z "$(bindir)" || $(MKDIR_P) "$(DESTDIR)$(bindir)" + @list='$(bin_PROGRAMS)'; for p in $$list; do \ + p1=`echo $$p|sed 's/$(EXEEXT)$$//'`; \ + if test -f $$p \ + || test -f $$p1 \ + ; then \ + f=`echo "$$p1" | sed 's,^.*/,,;$(transform);s/$$/$(EXEEXT)/'`; \ + echo " $(INSTALL_PROGRAM_ENV) $(LIBTOOL) --mode=install $(binPROGRAMS_INSTALL) '$$p' '$(DESTDIR)$(bindir)/$$f'"; \ + $(INSTALL_PROGRAM_ENV) $(LIBTOOL) --mode=install $(binPROGRAMS_INSTALL) "$$p" "$(DESTDIR)$(bindir)/$$f" || exit 1; \ + else :; fi; \ + done + +uninstall-binPROGRAMS: + @$(NORMAL_UNINSTALL) + @list='$(bin_PROGRAMS)'; for p in $$list; do \ + f=`echo "$$p" | sed 's,^.*/,,;s/$(EXEEXT)$$//;$(transform);s/$$/$(EXEEXT)/'`; \ + echo " rm -f '$(DESTDIR)$(bindir)/$$f'"; \ + rm -f "$(DESTDIR)$(bindir)/$$f"; \ + done + +clean-binPROGRAMS: + @list='$(bin_PROGRAMS)'; for p in $$list; do \ + f=`echo $$p|sed 's/$(EXEEXT)$$//'`; \ + echo " rm -f $$p $$f"; \ + rm -f $$p $$f ; \ + done +configfile_test.exe$(EXEEXT): $(configfile_test_exe_OBJECTS) $(configfile_test_exe_DEPENDENCIES) + @rm -f configfile_test.exe$(EXEEXT) + $(CXXLINK) $(configfile_test_exe_OBJECTS) $(configfile_test_exe_LDADD) $(LIBS) mostlyclean-compile: -rm -f *.$(OBJEXT) @@ -253,6 +295,7 @@ distclean-compile: -rm -f *.tab.c @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/configfile.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/configfile_test.Po@am__quote@ .cc.o: @am__fastdepCXX_TRUE@ $(CXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< @@ -374,9 +417,11 @@ distdir: $(DISTFILES) done check-am: all-am check: check-am -all-am: Makefile $(LTLIBRARIES) $(HEADERS) +all-am: Makefile $(LTLIBRARIES) $(PROGRAMS) $(HEADERS) +install-binPROGRAMS: install-libLTLIBRARIES + installdirs: - for dir in "$(DESTDIR)$(libdir)" "$(DESTDIR)$(includedir)"; do \ + for dir in "$(DESTDIR)$(libdir)" "$(DESTDIR)$(bindir)" "$(DESTDIR)$(includedir)"; do \ test -z "$$dir" || $(MKDIR_P) "$$dir"; \ done install: install-am @@ -405,8 +450,8 @@ maintainer-clean-generic: @echo "it deletes files that may require special tools to rebuild." clean: clean-am -clean-am: clean-generic clean-libLTLIBRARIES clean-libtool \ - mostlyclean-am +clean-am: clean-binPROGRAMS clean-generic clean-libLTLIBRARIES \ + clean-libtool mostlyclean-am distclean: distclean-am -rm -rf ./$(DEPDIR) @@ -428,7 +473,7 @@ install-data-am: install-includeHEADERS install-dvi: install-dvi-am -install-exec-am: install-libLTLIBRARIES +install-exec-am: install-binPROGRAMS install-libLTLIBRARIES install-html: install-html-am @@ -460,24 +505,26 @@ ps: ps-am ps-am: -uninstall-am: uninstall-includeHEADERS uninstall-libLTLIBRARIES +uninstall-am: uninstall-binPROGRAMS uninstall-includeHEADERS \ + uninstall-libLTLIBRARIES .MAKE: install-am install-strip -.PHONY: CTAGS GTAGS all all-am check check-am clean clean-generic \ - clean-libLTLIBRARIES clean-libtool ctags distclean \ - distclean-compile distclean-generic distclean-libtool \ - distclean-tags distdir dvi dvi-am html html-am info info-am \ - install install-am install-data install-data-am install-dvi \ - install-dvi-am install-exec install-exec-am install-html \ - install-html-am install-includeHEADERS install-info \ - install-info-am install-libLTLIBRARIES install-man install-pdf \ - install-pdf-am install-ps install-ps-am install-strip \ - installcheck installcheck-am installdirs maintainer-clean \ +.PHONY: CTAGS GTAGS all all-am check check-am clean clean-binPROGRAMS \ + clean-generic clean-libLTLIBRARIES clean-libtool ctags \ + distclean distclean-compile distclean-generic \ + distclean-libtool distclean-tags distdir dvi dvi-am html \ + html-am info info-am install install-am install-binPROGRAMS \ + install-data install-data-am install-dvi install-dvi-am \ + install-exec install-exec-am install-html install-html-am \ + install-includeHEADERS install-info install-info-am \ + install-libLTLIBRARIES install-man install-pdf install-pdf-am \ + install-ps install-ps-am install-strip installcheck \ + installcheck-am installdirs maintainer-clean \ maintainer-clean-generic mostlyclean mostlyclean-compile \ mostlyclean-generic mostlyclean-libtool pdf pdf-am ps ps-am \ - tags uninstall uninstall-am uninstall-includeHEADERS \ - uninstall-libLTLIBRARIES + tags uninstall uninstall-am uninstall-binPROGRAMS \ + uninstall-includeHEADERS uninstall-libLTLIBRARIES # Tell versions [3.59,3.63) of GNU make to not export all variables. # Otherwise a system limit (for SysV at least) may be exceeded. diff --git a/src/configfile.cc b/src/configfile.cc index 77c0725..68acd90 100644 --- a/src/configfile.cc +++ b/src/configfile.cc @@ -20,19 +20,22 @@ configfile::configfile(const string& fpath): unsigned MAX = 4096; char* buf = new char[MAX]; FILE* filp = fopen(file_path.c_str(), "r"); + if (!filp) + return; string secname = "", key = "", value = ""; while (getline(&buf, &MAX, filp) != -1) { if (is_comment(buf)) { /* drop it */ } else if (is_section_header(buf, &secname)) { - printf("\"%s\" is a section header\n", buf); + add_section(secname); } else if (is_key_value(buf, &key, &value)) { - printf("\"%s\" is a key value\n", buf); + if (secname != "") { + add_key(secname, key, value); + } } else { /* drop it too */ } memset(buf, 0, MAX); - secname = key = value = ""; } fclose(filp); filp = 0; @@ -69,8 +72,8 @@ configfile::get_keys (const string& secname) const } bool -configfile::get_key (const string& secname, const string& key, string* value) - const +configfile::get_key_value (const string& secname, const string& key, string* + value) const { unsigned index = 0; if (!get_section_index(secname, &index)) { @@ -141,13 +144,13 @@ bool configfile::is_section_header(const char* buf, string* secname) const { const unsigned MAX = 1024; - regmatch_t substr[2]; + regmatch_t substr[3]; char tmpbuf[MAX] = {0}; memset(substr, 0, sizeof(substr)); regex_t regex; regcomp(®ex, "^[ ]*\\[[ ]*([^ ]+)[ ]+\"([^ ]+)\"" "[ ]*\\][ ]*$", REG_EXTENDED | REG_NEWLINE); - if (regexec(®ex, buf, 2, substr, 0) == 0) { + if (regexec(®ex, buf, 3, substr, 0) == 0) { memset(tmpbuf, 0, MAX); extract_field(1, substr, buf, tmpbuf); *secname = tmpbuf; @@ -155,23 +158,33 @@ configfile::is_section_header(const char* buf, string* secname) const memset(tmpbuf, 0, MAX); extract_field(2, substr, buf, tmpbuf); *secname += tmpbuf; + regfree(®ex); + return true; + } + + regcomp(®ex, "^[ ]*\\[[ ]*([^ ]+)[ ]*\\][ ]*$", + REG_EXTENDED | REG_NEWLINE); + if (regexec(®ex, buf, 3, substr, 0) == 0) { + memset(tmpbuf, 0, MAX); + extract_field(1, substr, buf, tmpbuf); + *secname = tmpbuf; + regfree(®ex); return true; - } else { - return false; } + return false; } bool configfile::is_key_value(const char* buf, string* key, string* value) const { const unsigned MAX = 1024; - regmatch_t substr[2]; + regmatch_t substr[3]; char tmpbuf[MAX] = {0}; memset(substr, 0, sizeof(substr)); regex_t regex; regcomp(®ex, "^[ ]*([^ ].*[^ ])[ ]*=[ ]*" "([^ ].*[^ ])[ ]*$", REG_EXTENDED | REG_NEWLINE); - if (regexec(®ex, buf, 20, substr, 0) == 0) { + if (regexec(®ex, buf, 3, substr, 0) == 0) { memset(tmpbuf, 0, MAX); extract_field(1, substr, buf, tmpbuf); *key = tmpbuf; @@ -200,14 +213,24 @@ configfile::save_to (const string& fpath) const } data_struct::const_iterator pos; data_struct::const_iterator end = sections.end(); + string tmp1, tmp2; + unsigned index = 0; for (pos = sections.begin(); pos != end; ++pos) { - fprintf(filp, "[%s]\n", pos->first.c_str()); + index = pos->first.find('.', 0); + if (index != string::npos) { + tmp1 = pos->first.substr(0, index); + tmp2 = pos->first.substr(index + 1); + fprintf(filp, "[%s \"%s\"]\n", tmp1.c_str(), + tmp2.c_str()); + } else { + fprintf(filp, "[%s]\n", pos->first.c_str()); + } const vector >& key_values = pos->second; vector >::const_iterator kpos; vector >::const_iterator kend = key_values.end(); for (kpos = key_values.begin(); kpos != kend; ++kpos) { - fprintf(filp, "%s = %s \n", kpos->first.c_str(), + fprintf(filp, "\t%s = %s\n", kpos->first.c_str(), kpos->second.c_str()); } } diff --git a/src/configfile.h b/src/configfile.h index 6e1225b..722d1c2 100644 --- a/src/configfile.h +++ b/src/configfile.h @@ -26,8 +26,8 @@ class configfile configfile(const std::string& fpath); std::vector get_sections() const; std::vector get_keys(const std::string& secname) const; - bool get_key(const std::string& secname, const std::string& key, - std::string* value) const; + bool get_key_value(const std::string& secname, const std::string& key, + std::string* value) const; void add_section(const std::string& name); bool add_key(const std::string& secname, const std::string& key, const std::string& value); -- 2.11.4.GIT