diff options
Diffstat (limited to 'whiteboard.cpp')
-rw-r--r-- | whiteboard.cpp | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/whiteboard.cpp b/whiteboard.cpp index 09674b5..b3367cf 100644 --- a/whiteboard.cpp +++ b/whiteboard.cpp @@ -36,23 +36,31 @@ namespace { return result & 0x7FFFFFFF; } + fs::path data_path; + std::string generate_id() { static std::random_device r; static std::default_random_engine e1(r()); static std::uniform_int_distribution<int> uniform_dist(0, 15); - std::string result; - for (int i = 0; i < 6; i++) { - char c{static_cast<char>('0' + uniform_dist(e1))}; - if (c > '9') - c = c - '9' + 'a'; - result.push_back(c); + // limit tries + for (int j = 0; j < 100000; j++) { + std::string result; + for (int i = 0; i < 6; i++) { + char c{static_cast<char>('0' + uniform_dist(e1))}; + if (c > '9') + c = c - '9' + 'a'; + result.push_back(c); + } + fs::path path{data_path / result}; + + if (!fs::exists(path)) + return result; } - return result; - } - fs::path data_path; + return "endofcodes"; + } void setFileById(const std::string& data, const std::string& id) { |