annotate Core/Toolbox.cpp @ 95:3f9569917745

preparation for release
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 28 Sep 2012 15:18:39 +0200
parents 8517e2c44283
children 3b45473c0a73
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
59
c996319e90bc renaming in Core
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 50
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege,
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Belgium
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 *
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 * This program is free software: you can redistribute it and/or
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * modify it under the terms of the GNU General Public License as
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * published by the Free Software Foundation, either version 3 of the
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * License, or (at your option) any later version.
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 *
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 * This program is distributed in the hope that it will be useful, but
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * General Public License for more details.
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 *
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * You should have received a copy of the GNU General Public License
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 **/
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 #include "Toolbox.h"
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
59
c996319e90bc renaming in Core
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 50
diff changeset
23 #include "OrthancException.h"
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 #include <string.h>
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 #include <boost/filesystem.hpp>
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 #include <boost/filesystem/fstream.hpp>
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 #include <algorithm>
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 #if defined(_WIN32)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 #include <windows.h>
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 #endif
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33
87
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
34 #if defined(__APPLE__) && defined(__MACH__)
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
35 #include <mach-o/dyld.h> /* _NSGetExecutablePath */
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
36 #include <limits.h> /* PATH_MAX */
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
37 #endif
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
38
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 #if defined(__linux)
87
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
40 #include <limits.h> /* PATH_MAX */
10
27e2bbc11200 Better signal handling
Administrator@jodogne-w01
parents: 8
diff changeset
41 #include <signal.h>
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 #include <unistd.h>
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 #endif
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44
22
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
45 #include "../Resources/md5/md5.h"
24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
46 #include "../Resources/base64/base64.h"
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47
59
c996319e90bc renaming in Core
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 50
diff changeset
48 namespace Orthanc
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 static bool finish;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51
10
27e2bbc11200 Better signal handling
Administrator@jodogne-w01
parents: 8
diff changeset
52 #if defined(_WIN32)
22
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
53 static BOOL WINAPI ConsoleControlHandler(DWORD dwCtrlType)
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
54 {
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
55 // http://msdn.microsoft.com/en-us/library/ms683242(v=vs.85).aspx
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
56 finish = true;
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
57 return true;
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
58 }
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
59 #else
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 static void SignalHandler(int)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 finish = true;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 }
22
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
64 #endif
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 void Toolbox::Sleep(uint32_t seconds)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 #if defined(_WIN32)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 ::Sleep(static_cast<DWORD>(seconds) * static_cast<DWORD>(1000));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 #elif defined(__linux)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 usleep(static_cast<uint64_t>(seconds) * static_cast<uint64_t>(1000000));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 #else
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 #error Support your platform here
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 #endif
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 void Toolbox::USleep(uint64_t microSeconds)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 #if defined(_WIN32)
8
3a584803783e build with visual studio
jodogne
parents: 0
diff changeset
80 ::Sleep(static_cast<DWORD>(microSeconds / static_cast<uint64_t>(1000)));
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 #elif defined(__linux)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 usleep(microSeconds);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 #else
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 #error Support your platform here
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 #endif
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 void Toolbox::ServerBarrier()
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 {
10
27e2bbc11200 Better signal handling
Administrator@jodogne-w01
parents: 8
diff changeset
91 #if defined(_WIN32)
27e2bbc11200 Better signal handling
Administrator@jodogne-w01
parents: 8
diff changeset
92 SetConsoleCtrlHandler(ConsoleControlHandler, true);
27e2bbc11200 Better signal handling
Administrator@jodogne-w01
parents: 8
diff changeset
93 #else
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 signal(SIGINT, SignalHandler);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 signal(SIGQUIT, SignalHandler);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 #endif
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 finish = false;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 while (!finish)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 USleep(100000);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103
10
27e2bbc11200 Better signal handling
Administrator@jodogne-w01
parents: 8
diff changeset
104 #if defined(_WIN32)
27e2bbc11200 Better signal handling
Administrator@jodogne-w01
parents: 8
diff changeset
105 SetConsoleCtrlHandler(ConsoleControlHandler, false);
27e2bbc11200 Better signal handling
Administrator@jodogne-w01
parents: 8
diff changeset
106 #else
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 signal(SIGINT, NULL);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 signal(SIGQUIT, NULL);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 #endif
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 void Toolbox::ToUpperCase(std::string& s)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 std::transform(s.begin(), s.end(), s.begin(), toupper);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 void Toolbox::ToLowerCase(std::string& s)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 std::transform(s.begin(), s.end(), s.begin(), tolower);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 void Toolbox::ReadFile(std::string& content,
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 const std::string& path)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 boost::filesystem::ifstream f;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 f.open(path, std::ifstream::in | std::ios::binary);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 if (!f.good())
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 {
59
c996319e90bc renaming in Core
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 50
diff changeset
134 throw OrthancException("Unable to open a file");
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 // http://www.cplusplus.com/reference/iostream/istream/tellg/
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 f.seekg(0, std::ios::end);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 std::streamsize size = f.tellg();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 f.seekg(0, std::ios::beg);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 content.resize(size);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 if (size != 0)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 f.read(reinterpret_cast<char*>(&content[0]), size);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 f.close();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 void Toolbox::RemoveFile(const std::string& path)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 if (boost::filesystem::exists(path))
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 if (boost::filesystem::is_regular_file(path))
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 boost::filesystem::remove(path);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 else
59
c996319e90bc renaming in Core
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 50
diff changeset
159 throw OrthancException("The path is not a regular file: " + path);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 void Toolbox::SplitUriComponents(UriComponents& components,
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 const std::string& uri)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 static const char URI_SEPARATOR = '/';
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 components.clear();
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 if (uri.size() == 0 ||
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 uri[0] != URI_SEPARATOR)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 {
59
c996319e90bc renaming in Core
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 50
diff changeset
175 throw OrthancException(ErrorCode_UriSyntax);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 // Count the number of slashes in the URI to make an assumption
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 // about the number of components in the URI
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 unsigned int estimatedSize = 0;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 for (unsigned int i = 0; i < uri.size(); i++)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 if (uri[i] == URI_SEPARATOR)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184 estimatedSize++;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187 components.reserve(estimatedSize - 1);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189 unsigned int start = 1;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190 unsigned int end = 1;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 while (end < uri.size())
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 // This is the loop invariant
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 assert(uri[start - 1] == '/' && (end >= start));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196 if (uri[end] == '/')
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198 components.push_back(std::string(&uri[start], end - start));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 end++;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 start = end;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 else
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204 end++;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208 if (start < uri.size())
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210 components.push_back(std::string(&uri[start], end - start));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215 bool Toolbox::IsChildUri(const UriComponents& baseUri,
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216 const UriComponents& testedUri)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218 if (testedUri.size() < baseUri.size())
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220 return false;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223 for (size_t i = 0; i < baseUri.size(); i++)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225 if (baseUri[i] != testedUri[i])
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226 return false;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
227 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
228
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229 return true;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
230 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 std::string Toolbox::AutodetectMimeType(const std::string& path)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235 std::string contentType;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 size_t lastDot = path.rfind('.');
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237 size_t lastSlash = path.rfind('/');
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239 if (lastDot == std::string::npos ||
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 (lastSlash != std::string::npos && lastDot < lastSlash))
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242 // No trailing dot, unable to detect the content type
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
243 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244 else
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
245 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246 const char* extension = &path[lastDot + 1];
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
248 // http://en.wikipedia.org/wiki/Mime_types
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
249 // Text types
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
250 if (!strcmp(extension, "txt"))
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
251 contentType = "text/plain";
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
252 else if (!strcmp(extension, "html"))
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
253 contentType = "text/html";
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
254 else if (!strcmp(extension, "xml"))
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
255 contentType = "text/xml";
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
256 else if (!strcmp(extension, "css"))
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
257 contentType = "text/css";
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
259 // Application types
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
260 else if (!strcmp(extension, "js"))
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
261 contentType = "application/javascript";
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
262 else if (!strcmp(extension, "json"))
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
263 contentType = "application/json";
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
264 else if (!strcmp(extension, "pdf"))
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
265 contentType = "application/pdf";
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
266
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
267 // Images types
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
268 else if (!strcmp(extension, "jpg") || !strcmp(extension, "jpeg"))
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
269 contentType = "image/jpeg";
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
270 else if (!strcmp(extension, "gif"))
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
271 contentType = "image/gif";
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
272 else if (!strcmp(extension, "png"))
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
273 contentType = "image/png";
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
274 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
275
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
276 return contentType;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
277 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
278
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
279
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
280 std::string Toolbox::FlattenUri(const UriComponents& components,
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
281 size_t fromLevel)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
282 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
283 if (components.size() <= fromLevel)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
284 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
285 return "/";
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
286 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
287 else
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
288 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
289 std::string r;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
290
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
291 for (size_t i = fromLevel; i < components.size(); i++)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
292 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
293 r += "/" + components[i];
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
294 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
295
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
296 return r;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
297 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
298 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
299
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
300
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
301
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
302 uint64_t Toolbox::GetFileSize(const std::string& path)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
303 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
304 try
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
305 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
306 return static_cast<uint64_t>(boost::filesystem::file_size(path));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
307 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
308 catch (boost::filesystem::filesystem_error)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
309 {
59
c996319e90bc renaming in Core
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 50
diff changeset
310 throw OrthancException(ErrorCode_InexistentFile);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
311 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
312 }
22
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
313
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
314
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
315 static char GetHexadecimalCharacter(uint8_t value)
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
316 {
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
317 assert(value < 16);
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
318
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
319 if (value < 10)
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
320 return value + '0';
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
321 else
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
322 return (value - 10) + 'a';
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
323 }
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
324
23
62bd05fe4b7c support for ssl
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
325
22
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
326 void Toolbox::ComputeMD5(std::string& result,
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
327 const std::string& data)
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
328 {
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
329 md5_state_s state;
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
330 md5_init(&state);
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
331
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
332 if (data.size() > 0)
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
333 {
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
334 md5_append(&state, reinterpret_cast<const md5_byte_t*>(&data[0]),
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
335 static_cast<int>(data.size()));
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
336 }
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
337
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
338 md5_byte_t actualHash[16];
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
339 md5_finish(&state, actualHash);
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
340
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
341 result.resize(32);
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
342 for (unsigned int i = 0; i < 16; i++)
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
343 {
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
344 result[2 * i] = GetHexadecimalCharacter(actualHash[i] / 16);
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
345 result[2 * i + 1] = GetHexadecimalCharacter(actualHash[i] % 16);
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
346 }
1bc6327d1de3 MD5 sum
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 10
diff changeset
347 }
24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
348
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
349
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
350 std::string Toolbox::EncodeBase64(const std::string& data)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
351 {
86
de8a5329b069 fix of base64
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 59
diff changeset
352 return base64_encode(data);
24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
353 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
354
87
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
355
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
356 #if defined(_WIN32)
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
357 std::string Toolbox::GetPathToExecutable()
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
358 {
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
359 // Yes, this is ugly, but there is no simple way to get the
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
360 // required buffer size, so we use a big constant
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
361 std::vector<char> buffer(32768);
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
362 /*int bytes =*/ GetModuleFileNameA(NULL, &buffer[0], static_cast<DWORD>(buffer.size() - 1));
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
363 return std::string(&buffer[0]);
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
364 }
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
365
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
366 #elif defined(__linux)
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
367 std::string Toolbox::GetPathToExecutable()
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
368 {
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
369 std::vector<char> buffer(PATH_MAX + 1);
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
370 ssize_t bytes = readlink("/proc/self/exe", &buffer[0], buffer.size() - 1);
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
371 if (bytes == 0)
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
372 {
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
373 throw OrthancException("Unable to get the path to the executable");
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
374 }
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
375
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
376 return std::string(&buffer[0]);
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
377 }
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
378
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
379 #elif defined(__APPLE__) && defined(__MACH__)
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
380 std::string Toolbox::GetPathToExecutable()
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
381 {
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
382 char pathbuf[PATH_MAX + 1];
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
383 unsigned int bufsize = static_cast<int>(sizeof(pathbuf));
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
384
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
385 _NSGetExecutablePath( pathbuf, &bufsize);
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
386
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
387 return std::string(pathbuf);
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
388 }
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
389
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
390 #else
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
391 #error Support your platform here
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
392 #endif
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
393
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
394
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
395 std::string Toolbox::GetDirectoryOfExecutable()
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
396 {
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
397 boost::filesystem::path p(GetPathToExecutable());
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
398 return p.parent_path().string();
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
399 }
8517e2c44283 path to configuration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 86
diff changeset
400
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
401 }