Config: implement support for wildcard includes

Fixes #2771
This commit is contained in:
Gunnar Beutner 2013-02-02 00:28:00 +01:00
parent 04fd703e6a
commit 11cf07fa9c
5 changed files with 79 additions and 2 deletions

View File

@ -35,6 +35,7 @@
#include <syslog.h> #include <syslog.h>
#include <sys/file.h> #include <sys/file.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <glob.h>
#include <ltdl.h> #include <ltdl.h>
typedef int SOCKET; typedef int SOCKET;

View File

@ -283,6 +283,9 @@ String Utility::DirName(const String& path)
} }
result = dir; result = dir;
if (result.IsEmpty())
result = ".";
#endif /* _WIN32 */ #endif /* _WIN32 */
free(dir); free(dir);
@ -438,3 +441,56 @@ String Utility::NewUUID(void)
return boost::lexical_cast<String>(uuid); return boost::lexical_cast<String>(uuid);
} }
/**
* Calls the specified callback for each file matching the path specification.
*
* @param pathSpec The path specification.
*/
bool Utility::Glob(const String& pathSpec, const function<void (const String&)>& callback)
{
#ifdef _WIN32
HANDLE handle;
WIN32_FIND_DATA wfd;
handle = FindFirstFile(pathSpec.CStr(), &wfd);
if (handle == INVALID_HANDLE_VALUE) {
DWORD errorCode = GetLastError();
if (errorCode == ERROR_FILE_NOT_FOUND)
return false;
throw_exception(Win32Exception("FindFirstFile() failed", errorCode));
}
do {
callback(wfd.cFileName);
} while (FindNextFile(handle, &wfd));
if (!FindClose(handle))
throw_exception(Win32Exception("FindClose() failed", GetLastError()));
return true;
#else /* _WIN32 */
glob_t gr;
int rc = glob(pathSpec.CStr(), GLOB_ERR | GLOB_NOSORT, NULL, &gr);
if (rc < 0) {
if (rc == GLOB_NOMATCH)
return false;
throw_exception(PosixException("glob() failed", errno));
}
size_t left;
char **gp;
for (gp = gr.gl_pathv, left = gr.gl_pathc; left > 0; gp++, left--) {
callback(*gp);
}
globfree(&gr);
return true;
#endif /* _WIN32 */
}

View File

@ -56,6 +56,8 @@ public:
static String NewUUID(void); static String NewUUID(void);
static bool Glob(const String& pathSpec, const function<void (const String&)>& callback);
static static
#ifdef _WIN32 #ifdef _WIN32
HMODULE HMODULE

View File

@ -165,6 +165,18 @@ vector<ConfigItem::Ptr> ConfigCompiler::CompileText(const String& path,
return CompileStream(path, &stream); return CompileStream(path, &stream);
} }
/**
* Compiles the specified file and returns the resulting config items in the passed vector.
*
* @param path The file that should be compiled.
* @param resultItems The vector that should be used to store the config items.
*/
void ConfigCompiler::CompileFileIncludeHelper(const String& path, vector<ConfigItem::Ptr>& resultItems)
{
vector<ConfigItem::Ptr> items = CompileFile(path);
std::copy(items.begin(), items.end(), std::back_inserter(resultItems));
}
/** /**
* Default include handler. Includes the file and returns a list of * Default include handler. Includes the file and returns a list of
* configuration items. * configuration items.
@ -195,8 +207,12 @@ vector<ConfigItem::Ptr> ConfigCompiler::HandleFileInclude(const String& include,
} }
} }
/* TODO: implement wildcard includes */ vector<ConfigItem::Ptr> items;
return CompileFile(includePath);
if (!Utility::Glob(includePath, boost::bind(&ConfigCompiler::CompileFileIncludeHelper, _1, boost::ref(items))))
throw_exception(invalid_argument("Include file '" + include + "' does not exist (or no files found for pattern)."));
return items;
} }
/** /**

View File

@ -73,6 +73,8 @@ private:
void InitializeScanner(void); void InitializeScanner(void);
void DestroyScanner(void); void DestroyScanner(void);
static void CompileFileIncludeHelper(const String& path, vector<ConfigItem::Ptr>& resultItems);
}; };
} }