Mercurial > hg > orthanc-wsi
diff Resources/Orthanc/Core/Toolbox.cpp @ 136:0a1443204818
sync
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 10 Apr 2018 07:24:24 +0200 |
parents | a18bfe1fdd62 |
children |
line wrap: on
line diff
--- a/Resources/Orthanc/Core/Toolbox.cpp Mon Mar 26 11:04:50 2018 +0200 +++ b/Resources/Orthanc/Core/Toolbox.cpp Tue Apr 10 07:24:24 2018 +0200 @@ -87,8 +87,94 @@ #endif +// Inclusions for UUID +// http://stackoverflow.com/a/1626302 + +extern "C" +{ +#if defined(_WIN32) +# include <rpc.h> +#else +# include <uuid/uuid.h> +#endif +} + + + namespace Orthanc { + void Toolbox::LinesIterator::FindEndOfLine() + { + lineEnd_ = lineStart_; + + while (lineEnd_ < content_.size() && + content_[lineEnd_] != '\n' && + content_[lineEnd_] != '\r') + { + lineEnd_ += 1; + } + } + + + Toolbox::LinesIterator::LinesIterator(const std::string& content) : + content_(content), + lineStart_(0) + { + FindEndOfLine(); + } + + + bool Toolbox::LinesIterator::GetLine(std::string& target) const + { + assert(lineStart_ <= content_.size() && + lineEnd_ <= content_.size() && + lineStart_ <= lineEnd_); + + if (lineStart_ == content_.size()) + { + return false; + } + else + { + target = content_.substr(lineStart_, lineEnd_ - lineStart_); + return true; + } + } + + + void Toolbox::LinesIterator::Next() + { + lineStart_ = lineEnd_; + + if (lineStart_ != content_.size()) + { + assert(content_[lineStart_] == '\r' || + content_[lineStart_] == '\n'); + + char second; + + if (content_[lineStart_] == '\r') + { + second = '\n'; + } + else + { + second = '\r'; + } + + lineStart_ += 1; + + if (lineStart_ < content_.size() && + content_[lineStart_] == second) + { + lineStart_ += 1; + } + + FindEndOfLine(); + } + } + + void Toolbox::ToUpperCase(std::string& s) { std::transform(s.begin(), s.end(), s.begin(), toupper); @@ -1376,4 +1462,65 @@ return boost::locale::conv::utf_to_utf<char>(w); } #endif + + + std::string Toolbox::GenerateUuid() + { +#ifdef WIN32 + UUID uuid; + UuidCreate ( &uuid ); + + unsigned char * str; + UuidToStringA ( &uuid, &str ); + + std::string s( ( char* ) str ); + + RpcStringFreeA ( &str ); +#else + uuid_t uuid; + uuid_generate_random ( uuid ); + char s[37]; + uuid_unparse ( uuid, s ); +#endif + return s; + } } + + + +OrthancLinesIterator* OrthancLinesIterator_Create(const std::string& content) +{ + return reinterpret_cast<OrthancLinesIterator*>(new Orthanc::Toolbox::LinesIterator(content)); +} + + +bool OrthancLinesIterator_GetLine(std::string& target, + const OrthancLinesIterator* iterator) +{ + if (iterator != NULL) + { + return reinterpret_cast<const Orthanc::Toolbox::LinesIterator*>(iterator)->GetLine(target); + } + else + { + return false; + } +} + + +void OrthancLinesIterator_Next(OrthancLinesIterator* iterator) +{ + if (iterator != NULL) + { + reinterpret_cast<Orthanc::Toolbox::LinesIterator*>(iterator)->Next(); + } +} + + +void OrthancLinesIterator_Free(OrthancLinesIterator* iterator) +{ + if (iterator != NULL) + { + delete reinterpret_cast<const Orthanc::Toolbox::LinesIterator*>(iterator); + } +}