mirror of
https://github.com/zeek/zeek.git
synced 2025-10-07 00:58:19 +00:00
Replace safe_basename/safe_dirname w/ SafeBasename/SafeDirname.
So errors can be better handled.
This commit is contained in:
parent
96ed7aed1a
commit
3046013d69
5 changed files with 65 additions and 41 deletions
42
src/util.h
42
src/util.h
|
@ -22,6 +22,7 @@
|
|||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#include <magic.h>
|
||||
#include <libgen.h>
|
||||
#include "config.h"
|
||||
|
||||
#if __STDC__
|
||||
|
@ -208,13 +209,42 @@ extern const char* bro_path();
|
|||
extern const char* bro_magic_path();
|
||||
extern std::string bro_prefixes();
|
||||
|
||||
// Wrappers for dirname(3) that won't modify argument.
|
||||
std::string safe_dirname(const char* path);
|
||||
std::string safe_dirname(const std::string& path);
|
||||
/**
|
||||
* Wrapper class for functions like dirname(3) or basename(3) that won't
|
||||
* modify the path argument and may optionally abort execution on error.
|
||||
*/
|
||||
class SafePathOp {
|
||||
public:
|
||||
|
||||
// Wrappers for basename(3) that won't modify argument.
|
||||
std::string safe_basename(const char* path);
|
||||
std::string safe_basename(const std::string& path);
|
||||
typedef char*(*PathOpFn)(char*);
|
||||
|
||||
SafePathOp(PathOpFn fn, const char* path, bool error_aborts = true);
|
||||
SafePathOp(PathOpFn fn, const std::string& path, bool error_aborts = true);
|
||||
|
||||
std::string result;
|
||||
bool error;
|
||||
|
||||
private:
|
||||
|
||||
void DoFunc(PathOpFn fn, const std::string& path, bool error_aborts = true);
|
||||
};
|
||||
|
||||
class SafeDirname : public SafePathOp {
|
||||
public:
|
||||
|
||||
SafeDirname(const char* path, bool error_aborts = true)
|
||||
: SafePathOp(&dirname, path, error_aborts) { }
|
||||
SafeDirname(const std::string& path, bool error_aborts = true)
|
||||
: SafePathOp(&dirname, path, error_aborts) { }
|
||||
};
|
||||
|
||||
class SafeBasename : public SafePathOp {
|
||||
public:
|
||||
SafeBasename(const char* path, bool error_aborts = true)
|
||||
: SafePathOp(&basename, path, error_aborts) { }
|
||||
SafeBasename(const std::string& path, bool error_aborts = true)
|
||||
: SafePathOp(&basename, path, error_aborts) { }
|
||||
};
|
||||
|
||||
/**
|
||||
* Flatten a script name by replacing '/' path separators with '.'.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue