comparison OrthancFramework/Sources/Compression/HierarchicalZipWriter.cpp @ 4044:d25f4c0fa160 framework

splitting code into OrthancFramework and OrthancServer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Jun 2020 20:30:34 +0200
parents Core/Compression/HierarchicalZipWriter.cpp@94f4a18a79cc
children bf7b9edf6b81
comparison
equal deleted inserted replaced
4043:6c6239aec462 4044:d25f4c0fa160
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * In addition, as a special exception, the copyright holders of this
13 * program give permission to link the code of its release with the
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
15 * that use the same license as the "OpenSSL" library), and distribute
16 * the linked executables. You must obey the GNU General Public License
17 * in all respects for all of the code used other than "OpenSSL". If you
18 * modify file(s) with this exception, you may extend this exception to
19 * your version of the file(s), but you are not obligated to do so. If
20 * you do not wish to do so, delete this exception statement from your
21 * version. If you delete this exception statement from all source files
22 * in the program, then also delete it here.
23 *
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 **/
32
33
34 #include "../PrecompiledHeaders.h"
35 #include "HierarchicalZipWriter.h"
36
37 #include "../Toolbox.h"
38 #include "../OrthancException.h"
39
40 #include <boost/lexical_cast.hpp>
41
42 namespace Orthanc
43 {
44 std::string HierarchicalZipWriter::Index::KeepAlphanumeric(const std::string& source)
45 {
46 std::string result;
47
48 bool lastSpace = false;
49
50 result.reserve(source.size());
51 for (size_t i = 0; i < source.size(); i++)
52 {
53 char c = source[i];
54 if (c == '^')
55 c = ' ';
56
57 if (c <= 127 &&
58 c >= 0)
59 {
60 if (isspace(c))
61 {
62 if (!lastSpace)
63 {
64 lastSpace = true;
65 result.push_back(' ');
66 }
67 }
68 else if (isalnum(c) ||
69 c == '.' ||
70 c == '_')
71 {
72 result.push_back(c);
73 lastSpace = false;
74 }
75 }
76 }
77
78 return Toolbox::StripSpaces(result);
79 }
80
81 std::string HierarchicalZipWriter::Index::GetCurrentDirectoryPath() const
82 {
83 std::string result;
84
85 Stack::const_iterator it = stack_.begin();
86 ++it; // Skip the root node (to avoid absolute paths)
87
88 while (it != stack_.end())
89 {
90 result += (*it)->name_ + "/";
91 ++it;
92 }
93
94 return result;
95 }
96
97 std::string HierarchicalZipWriter::Index::EnsureUniqueFilename(const char* filename)
98 {
99 std::string standardized = KeepAlphanumeric(filename);
100
101 Directory& d = *stack_.back();
102 Directory::Content::iterator it = d.content_.find(standardized);
103
104 if (it == d.content_.end())
105 {
106 d.content_[standardized] = 1;
107 return standardized;
108 }
109 else
110 {
111 it->second++;
112 return standardized + "-" + boost::lexical_cast<std::string>(it->second);
113 }
114 }
115
116 HierarchicalZipWriter::Index::Index()
117 {
118 stack_.push_back(new Directory);
119 }
120
121 HierarchicalZipWriter::Index::~Index()
122 {
123 for (Stack::iterator it = stack_.begin(); it != stack_.end(); ++it)
124 {
125 delete *it;
126 }
127 }
128
129 std::string HierarchicalZipWriter::Index::OpenFile(const char* name)
130 {
131 return GetCurrentDirectoryPath() + EnsureUniqueFilename(name);
132 }
133
134 void HierarchicalZipWriter::Index::OpenDirectory(const char* name)
135 {
136 std::string d = EnsureUniqueFilename(name);
137
138 // Push the new directory onto the stack
139 stack_.push_back(new Directory);
140 stack_.back()->name_ = d;
141 }
142
143 void HierarchicalZipWriter::Index::CloseDirectory()
144 {
145 if (IsRoot())
146 {
147 // Cannot close the root node
148 throw OrthancException(ErrorCode_BadSequenceOfCalls);
149 }
150
151 delete stack_.back();
152 stack_.pop_back();
153 }
154
155
156 HierarchicalZipWriter::HierarchicalZipWriter(const char* path)
157 {
158 writer_.SetOutputPath(path);
159 writer_.Open();
160 }
161
162 HierarchicalZipWriter::~HierarchicalZipWriter()
163 {
164 writer_.Close();
165 }
166
167 void HierarchicalZipWriter::OpenFile(const char* name)
168 {
169 std::string p = indexer_.OpenFile(name);
170 writer_.OpenFile(p.c_str());
171 }
172
173 void HierarchicalZipWriter::OpenDirectory(const char* name)
174 {
175 indexer_.OpenDirectory(name);
176 }
177
178 void HierarchicalZipWriter::CloseDirectory()
179 {
180 indexer_.CloseDirectory();
181 }
182 }