forked from google/leveldb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename filesystem to db_pathadn refactor code a bit
- Loading branch information
Showing
5 changed files
with
150 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#ifndef STORAGE_LEVELDB_INCLUDE_DB_PATH_H_ | ||
#define STORAGE_LEVELDB_INCLUDE_DB_PATH_H_ | ||
|
||
#include <cstdint> | ||
#include <string> | ||
|
||
#include "leveldb/export.h" | ||
#include "leveldb/status.h" | ||
|
||
namespace leveldb { | ||
|
||
namespace path { | ||
|
||
class DbPath { | ||
public: | ||
// Constants | ||
static const char kDirectorySeparator = '\\'; | ||
static const char kAltDirecttorySeparator = '/'; | ||
static const char kVolumeSeparatorChar = ':'; | ||
|
||
virtual ~DbPath() {} | ||
|
||
const std::string& Name() const { return path_; } | ||
const char* CName() const { return path_.c_str(); } | ||
|
||
static bool IsDirectorySeparator(const char c); | ||
|
||
virtual bool IsAbsolute() const = 0; | ||
virtual bool IsRelative() const = 0; | ||
|
||
inline size_t Size() const { return path_.size(); } | ||
inline bool IsEmpty() const { return path_.empty(); } | ||
|
||
|
||
protected: | ||
DbPath() : path_("") {} | ||
DbPath(const std::string& path) : path_(path) {} | ||
|
||
std::string path_; | ||
|
||
virtual void Normalize() = 0; | ||
}; | ||
|
||
class WindowsDbPath : public DbPath { | ||
public: | ||
explicit WindowsDbPath(const std::string& path) : DbPath(path) { | ||
Normalize(); | ||
} | ||
~WindowsDbPath() {} | ||
|
||
static bool IsValidDriveChar(const char c); | ||
|
||
bool IsAbsolute() const override; | ||
bool IsRelative() const override; | ||
|
||
protected: | ||
void Normalize() override; | ||
}; | ||
|
||
|
||
// Factory | ||
class PathFactory { | ||
public: | ||
PathFactory() = delete; | ||
~PathFactory() = delete; | ||
|
||
static DbPath* Create(const std::string& path); | ||
}; | ||
|
||
} // namespace path | ||
} // namespace leveldb | ||
|
||
#endif // STORAGE_LEVELDB_INCLUDE_DB_PATH_H_ |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include "leveldb/db_path.h" | ||
|
||
namespace leveldb { | ||
namespace path { | ||
|
||
bool DbPath::IsDirectorySeparator(const char c) { | ||
return (c == DbPath::kDirectorySeparator || c == DbPath::kAltDirecttorySeparator); | ||
} | ||
|
||
// Windows | ||
|
||
bool WindowsDbPath::IsAbsolute() const { | ||
return path_.size() >= 3 && IsValidDriveChar(path_[0]) && | ||
path_[1] == DbPath::kVolumeSeparatorChar; | ||
}; | ||
|
||
bool WindowsDbPath::IsRelative() const { | ||
if (path_.size() < 2) { | ||
return true; | ||
} | ||
|
||
if (IsDirectorySeparator(path_[0])) { | ||
if (path_[1] != '?') { | ||
return !IsDirectorySeparator(path_[1]); | ||
} | ||
return false; | ||
} | ||
if (path_.size() >= 3 && path_[1] == DbPath::kVolumeSeparatorChar && | ||
IsDirectorySeparator(path_[2])) { | ||
return IsValidDriveChar(path_[0]); | ||
} | ||
return true; | ||
}; | ||
|
||
void WindowsDbPath::Normalize() { | ||
auto out = path_.begin(); | ||
|
||
for (const char c : path_) { | ||
if (!IsDirectorySeparator(c)) { | ||
*(out++) = c; | ||
} | ||
else if (out == path_.begin() || !IsDirectorySeparator(*std::prev(out))) { | ||
*(out++) = kDirectorySeparator; | ||
} | ||
else { | ||
continue; | ||
} | ||
} | ||
|
||
path_.erase(out, path_.end()); | ||
} | ||
|
||
bool WindowsDbPath::IsValidDriveChar(const char c) { | ||
const char drive_char = std::toupper(c); | ||
return drive_char >= 'A' && drive_char <= 'Z'; | ||
} | ||
|
||
// Windows path | ||
|
||
DbPath* PathFactory::Create(const std::string& path) { | ||
#ifdef LEVELDB_PLATFORM_WINDOWS | ||
return new WindowsDbPath(path); | ||
#elif LEVELDB_PLATFORM_POSIX | ||
return nullptr; | ||
#endif | ||
return nullptr; | ||
} | ||
|
||
} | ||
} | ||
|
||
|