MeVisLab Toolbox Reference
mlDICOMCachedIOFileHandle.h
Go to the documentation of this file.
1// Copyright (c) Fraunhofer MEVIS, Germany. All rights reserved.
2// **InsertLicense** code
3//----------------------------------------------------------------------------------
6
11//----------------------------------------------------------------------------------
12
13#pragma once
14
18
19#include <mlUtilsSystemC.h>
20#include <mlFileSystem.h>
21#include <FMEThirdPartyWarningsDisable.h>
22#include <string>
23#include <vector>
24#include <FMEThirdPartyWarningsRestore.h>
25
26ML_START_NAMESPACE
27
28//----------------------------------------------------------------------------------
39//----------------------------------------------------------------------------------
40template<typename CT>
42public std::basic_string<CT>
43{
44
45public:
48
50 inline DICOMCachedIOFileHandleBase(const std::basic_string<CT>& str)
51 {
52 (*this).erase();
53 (*this).assign(str);
54 }
55
59 inline bool isMultiFrameHandle() const
60 {
61 // Do emptiness check and check for "*" to speed up the check in normal cases.
62 return !this->empty() && ((*this)[0] == '*') && (_getMLintFrameIndex() >= 0);
63 }
64
70 inline bool fileExists(bool allowEmpty) const
71 {
72 if (this->empty() && allowEmpty) {
73 return true;
74 }
75 return MLFileExists((this->isMultiFrameHandle() ? getOriginalFrameHandle() : *this).c_str()) != 0;
76 }
77
83 {
84 DICOMCachedIOFileHandleBase retHandle = *this;
85
86 // Extract original handle if it is a multi-frame handle.
87 if (_getMLintFrameIndex() >= 0){
88 const size_t hashPos = this->rfind('#');
89 retHandle = this->substr(1, hashPos - 1);
90 }
91 return retHandle;
92 }
93
99 inline unsigned int getFrameIndex() const
100 {
101 const MLint index = _getMLintFrameIndex();
102 return index < 0 ? 0u : static_cast<unsigned int>(index);
103 }
104
111 inline std::basic_string<CT> getFilename() const
112 {
113 std::basic_string<CT> filename = "";
114 if(!this->empty()) {
116 const std::vector<DICOMCachedIOFileHandlePlugin *> &plugins = fileHandleProvider.getConstFileHandlePlugins();
117 std::vector<DICOMCachedIOFileHandlePlugin *>::const_iterator iter = plugins.begin(),endIt=plugins.end();
118 for( ; iter!=endIt; ++iter) {
119 if (*iter) {
120 filename = (*iter)->resolveFilename(*this);
121 if (!filename.empty()) {
122 break;
123 }
124 }
125 }
126 }
127 return filename;
128 }
129
130 private:
131
134 inline MLint _getMLintFrameIndex() const
135 {
136 // Extract original handle if it is a multi-frame handle.
137 MLint index = -1;
138 if (!this->empty() && ((*this)[0] == '*')){
139 const size_t hashPos = this->rfind('#');
140 if (hashPos != this->npos){
141 // Looks like a multi-frame handle, extract its index as string.
142 const std::string idxStr = this->substr(hashPos + 1);
143 if (1 != sscanf(idxStr.c_str(), "%lld", &index)) {
144 // We expect exactly one scanned argument so we failed if we
145 // reach here. Reset returned index to 0 in this case.
146 index = -1;
147 }
148 // Check for valid range.
149 if ((index < 0) || (index > ML_UINT32_MAX)){
150 index = -1;
151 }
152 }
153 }
154 return index;
155 }
156};
157
159typedef DICOMCachedIOFileHandleBase<char> DICOMCachedIOFileHandle;
160
161ML_END_NAMESPACE
Project global and OS specific declarations.
#define MLDICOMCachedIO_EXPORT
If included by external modules, exported symbols are declared as import symbols.
std::basic_string< CT > getFilename() const
DICOMCachedIOFileHandleBase(const std::basic_string< CT > &str)
Copy constructor.
DICOMCachedIOFileHandleBase getOriginalFrameHandle() const
bool fileExists(bool allowEmpty) const
DICOMCachedIOFileHandleBase()
Default Constructor creating a handle with an empty string.
const std::vector< DICOMCachedIOFileHandlePlugin * > & getConstFileHandlePlugins() const
Returns a const vector of file handle Plugins.
static DICOMCachedIOFileHandleProvider & singleton()
ML_UTILS_EXPORT int MLFileExists(const char *fileName)
MLint64 MLint
Definition mlTypeDefs.h:489
#define ML_UINT32_MAX
Definition mlTypeDefs.h:190
DICOMCachedIOFileHandleBase< char > DICOMCachedIOFileHandle
"Forward" to DICOMCachedIOFileHandle.