annotate OrthancFramework/Sources/SystemToolbox.cpp @ 4529:5774fe497ff2

fix decoding of images on big-endian architectures
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 24 Feb 2021 21:06:34 +0100
parents c1f36fd13730
children 8a7523da6a9b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
4437
d9473bd5ed43 upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4402
diff changeset
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
8 * modify it under the terms of the GNU Lesser General Public License
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
10 * the License, or (at your option) any later version.
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
15 * Lesser General Public License for more details.
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 *
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
18 * License along with this program. If not, see
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
19 * <http://www.gnu.org/licenses/>.
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 **/
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 #include "PrecompiledHeaders.h"
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include "SystemToolbox.h"
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 #if defined(_WIN32)
2173
1c42860097fc fix windows build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2172
diff changeset
28 # include <windows.h>
1c42860097fc fix windows build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2172
diff changeset
29 # include <process.h> // For "_spawnvp()" and "_getpid()"
2920
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
30 # include <stdlib.h> // For "environ"
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 #else
2173
1c42860097fc fix windows build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2172
diff changeset
32 # include <unistd.h> // For "execvp()"
1c42860097fc fix windows build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2172
diff changeset
33 # include <sys/wait.h> // For "waitpid()"
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 #endif
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35
2173
1c42860097fc fix windows build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2172
diff changeset
36
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 #if defined(__APPLE__) && defined(__MACH__)
2173
1c42860097fc fix windows build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2172
diff changeset
38 # include <mach-o/dyld.h> /* _NSGetExecutablePath */
1c42860097fc fix windows build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2172
diff changeset
39 # include <limits.h> /* PATH_MAX */
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 #endif
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41
2173
1c42860097fc fix windows build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2172
diff changeset
42
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 #if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__)
2173
1c42860097fc fix windows build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2172
diff changeset
44 # include <limits.h> /* PATH_MAX */
1c42860097fc fix windows build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2172
diff changeset
45 # include <signal.h>
1c42860097fc fix windows build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2172
diff changeset
46 # include <unistd.h>
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 #endif
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49
2365
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
50 #if defined(__OpenBSD__)
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
51 # include <sys/sysctl.h> // For "sysctl", "CTL_KERN" and "KERN_PROC_ARGS"
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
52 #endif
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
53
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
54
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 #include "Logging.h"
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 #include "OrthancException.h"
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 #include "Toolbox.h"
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58
4185
b289a1234822 giving a try to cross-platform compilation of SyncStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
59 #include <boost/iostreams/device/file_descriptor.hpp>
b289a1234822 giving a try to cross-platform compilation of SyncStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
60 #include <boost/iostreams/stream.hpp>
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 #include <boost/filesystem.hpp>
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 #include <boost/filesystem/fstream.hpp>
2367
2aff870c2c58 refactoring of BoostConfiguration.cmake
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2365
diff changeset
63 #include <boost/date_time/posix_time/posix_time.hpp>
2794
f790999a250a Semaphore removed from graveyard, SystemToolbox::GetHardwareConcurrency()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2512
diff changeset
64 #include <boost/thread.hpp>
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66
2920
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
67 /*=========================================================================
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
68 The section below comes from the Boost 1.68.0 project:
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
69 https://github.com/boostorg/program_options/blob/boost-1.68.0/src/parsers.cpp
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
70
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
71 Copyright Vladimir Prus 2002-2004.
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
72 Distributed under the Boost Software License, Version 1.0.
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
73 (See accompanying file LICENSE_1_0.txt
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
74 or copy at http://www.boost.org/LICENSE_1_0.txt)
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
75 =========================================================================*/
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
76
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
77 // The 'environ' should be declared in some cases. E.g. Linux man page says:
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
78 // (This variable must be declared in the user program, but is declared in
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
79 // the header file unistd.h in case the header files came from libc4 or libc5,
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
80 // and in case they came from glibc and _GNU_SOURCE was defined.)
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
81 // To be safe, declare it here.
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
82
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
83 // It appears that on Mac OS X the 'environ' variable is not
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
84 // available to dynamically linked libraries.
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
85 // See: http://article.gmane.org/gmane.comp.lib.boost.devel/103843
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
86 // See: http://lists.gnu.org/archive/html/bug-guile/2004-01/msg00013.html
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
87 #if defined(__APPLE__) && defined(__DYNAMIC__)
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
88 // The proper include for this is crt_externs.h, however it's not
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
89 // available on iOS. The right replacement is not known. See
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
90 // https://svn.boost.org/trac/boost/ticket/5053
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
91 extern "C"
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
92 {
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
93 extern char ***_NSGetEnviron(void);
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
94 }
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
95 # define environ (*_NSGetEnviron())
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
96 #else
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
97 # if defined(__MWERKS__)
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
98 # include <crtl.h>
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
99 # else
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
100 # if !defined(_WIN32) || defined(__COMO_VERSION__)
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
101 extern char** environ;
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
102 # endif
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
103 # endif
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
104 #endif
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
105
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
106
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
107 /*=========================================================================
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
108 End of section from the Boost 1.68.0 project
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
109 =========================================================================*/
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
110
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
111
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 namespace Orthanc
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 static bool finish_;
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 static ServerBarrierEvent barrierEvent_;
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 #if defined(_WIN32)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 static BOOL WINAPI ConsoleControlHandler(DWORD dwCtrlType)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 // http://msdn.microsoft.com/en-us/library/ms683242(v=vs.85).aspx
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 finish_ = true;
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 return true;
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 #else
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 static void SignalHandler(int signal)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 if (signal == SIGHUP)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 barrierEvent_ = ServerBarrierEvent_Reload;
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 finish_ = true;
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 #endif
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 static ServerBarrierEvent ServerBarrierInternal(const bool* stopFlag)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 #if defined(_WIN32)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 SetConsoleCtrlHandler(ConsoleControlHandler, true);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 #else
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 signal(SIGINT, SignalHandler);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 signal(SIGQUIT, SignalHandler);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 signal(SIGTERM, SignalHandler);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 signal(SIGHUP, SignalHandler);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 #endif
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 // Active loop that awakens every 100ms
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 finish_ = false;
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 barrierEvent_ = ServerBarrierEvent_Stop;
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 while (!(*stopFlag || finish_))
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 {
2242
4e8e0ad2001c move USleep() in SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2173
diff changeset
153 SystemToolbox::USleep(100 * 1000);
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 #if defined(_WIN32)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 SetConsoleCtrlHandler(ConsoleControlHandler, false);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 #else
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 signal(SIGINT, NULL);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 signal(SIGQUIT, NULL);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 signal(SIGTERM, NULL);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 signal(SIGHUP, NULL);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 #endif
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 return barrierEvent_;
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 ServerBarrierEvent SystemToolbox::ServerBarrier(const bool& stopFlag)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171 return ServerBarrierInternal(&stopFlag);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 ServerBarrierEvent SystemToolbox::ServerBarrier()
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 const bool stopFlag = false;
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 return ServerBarrierInternal(&stopFlag);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181
2242
4e8e0ad2001c move USleep() in SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2173
diff changeset
182 void SystemToolbox::USleep(uint64_t microSeconds)
4e8e0ad2001c move USleep() in SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2173
diff changeset
183 {
4e8e0ad2001c move USleep() in SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2173
diff changeset
184 #if defined(_WIN32)
4e8e0ad2001c move USleep() in SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2173
diff changeset
185 ::Sleep(static_cast<DWORD>(microSeconds / static_cast<uint64_t>(1000)));
2364
ae50eccd41b7 compilation succeeds on OpenBSD
jodogne
parents: 2332
diff changeset
186 #elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__native_client__)
2242
4e8e0ad2001c move USleep() in SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2173
diff changeset
187 usleep(microSeconds);
4e8e0ad2001c move USleep() in SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2173
diff changeset
188 #else
4e8e0ad2001c move USleep() in SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2173
diff changeset
189 #error Support your platform here
4e8e0ad2001c move USleep() in SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2173
diff changeset
190 #endif
4e8e0ad2001c move USleep() in SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2173
diff changeset
191 }
4e8e0ad2001c move USleep() in SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2173
diff changeset
192
4e8e0ad2001c move USleep() in SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2173
diff changeset
193
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 static std::streamsize GetStreamSize(std::istream& f)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196 // http://www.cplusplus.com/reference/iostream/istream/tellg/
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 f.seekg(0, std::ios::end);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198 std::streamsize size = f.tellg();
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 f.seekg(0, std::ios::beg);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 return size;
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205 void SystemToolbox::ReadFile(std::string& content,
3521
793c141be598 added an option not to log ReadFile errors
Alain Mazy <alain@mazy.be>
parents: 3435
diff changeset
206 const std::string& path,
793c141be598 added an option not to log ReadFile errors
Alain Mazy <alain@mazy.be>
parents: 3435
diff changeset
207 bool log)
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209 if (!IsRegularFile(path))
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210 {
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2933
diff changeset
211 throw OrthancException(ErrorCode_RegularFileExpected,
3521
793c141be598 added an option not to log ReadFile errors
Alain Mazy <alain@mazy.be>
parents: 3435
diff changeset
212 "The path does not point to a regular file: " + path,
793c141be598 added an option not to log ReadFile errors
Alain Mazy <alain@mazy.be>
parents: 3435
diff changeset
213 log);
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215
4399
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
216 try
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217 {
4399
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
218 boost::filesystem::ifstream f;
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
219 f.open(path, std::ifstream::in | std::ifstream::binary);
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
220 if (!f.good())
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
221 {
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
222 throw OrthancException(ErrorCode_InexistentFile,
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
223 "File not found: " + path,
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
224 log);
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
225 }
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226
4399
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
227 std::streamsize size = GetStreamSize(f);
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
228 content.resize(static_cast<size_t>(size));
4342
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
229
4399
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
230 if (static_cast<std::streamsize>(content.size()) != size)
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
231 {
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
232 throw OrthancException(ErrorCode_InternalError,
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
233 "Reading a file that is too large for a 32bit architecture");
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
234 }
4342
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
235
4399
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
236 if (size != 0)
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
237 {
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
238 f.read(&content[0], size);
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
239 }
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
240
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
241 f.close();
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
242 }
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
243 catch (boost::filesystem::filesystem_error&)
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244 {
4399
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
245 throw OrthancException(ErrorCode_InexistentFile);
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246 }
4402
b651989194d3 fix for older compilers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4399
diff changeset
247 catch (...) // To catch "std::system_error&" in C++11
4399
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
248 {
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
249 throw OrthancException(ErrorCode_InexistentFile);
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
250 }
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
251 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
252
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
253
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4279
diff changeset
254 void SystemToolbox::ReadFile(std::string &content, const std::string &path)
4279
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4239
diff changeset
255 {
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4239
diff changeset
256 ReadFile(content, path, true /* log */);
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4239
diff changeset
257 }
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4239
diff changeset
258
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4239
diff changeset
259
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
260 bool SystemToolbox::ReadHeader(std::string& header,
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
261 const std::string& path,
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
262 size_t headerSize)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
263 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
264 if (!IsRegularFile(path))
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
265 {
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2933
diff changeset
266 throw OrthancException(ErrorCode_RegularFileExpected,
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2933
diff changeset
267 "The path does not point to a regular file: " + path);
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
268 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
269
4399
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
270 try
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
271 {
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
272 boost::filesystem::ifstream f;
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
273 f.open(path, std::ifstream::in | std::ifstream::binary);
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
274 if (!f.good())
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
275 {
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
276 throw OrthancException(ErrorCode_InexistentFile);
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
277 }
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
278
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
279 bool full = true;
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
280
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
281 {
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
282 std::streamsize size = GetStreamSize(f);
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
283 if (size <= 0)
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
284 {
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
285 headerSize = 0;
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
286 full = false;
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
287 }
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
288 else if (static_cast<size_t>(size) < headerSize)
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
289 {
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
290 headerSize = static_cast<size_t>(size); // Truncate to the size of the file
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
291 full = false;
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
292 }
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
293 }
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
294
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
295 header.resize(headerSize);
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
296 if (headerSize != 0)
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
297 {
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
298 f.read(&header[0], headerSize);
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
299 }
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
300
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
301 f.close();
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
302
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
303 return full;
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
304 }
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
305 catch (boost::filesystem::filesystem_error&)
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
306 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
307 throw OrthancException(ErrorCode_InexistentFile);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
308 }
4402
b651989194d3 fix for older compilers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4399
diff changeset
309 catch (...) // To catch "std::system_error&" in C++11
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
310 {
4399
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
311 throw OrthancException(ErrorCode_InexistentFile);
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
312 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
313 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
314
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
315
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
316 void SystemToolbox::WriteFile(const void* content,
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
317 size_t size,
4185
b289a1234822 giving a try to cross-platform compilation of SyncStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
318 const std::string& path,
b289a1234822 giving a try to cross-platform compilation of SyncStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
319 bool callFsync)
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
320 {
4399
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
321 try
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
322 {
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
323 //boost::filesystem::ofstream f;
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
324 boost::iostreams::stream<boost::iostreams::file_descriptor_sink> f;
4185
b289a1234822 giving a try to cross-platform compilation of SyncStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
325
4399
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
326 f.open(path, std::ofstream::out | std::ofstream::binary);
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
327 if (!f.good())
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
328 {
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
329 throw OrthancException(ErrorCode_CannotWriteFile);
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
330 }
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
331
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
332 if (size != 0)
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
333 {
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
334 f.write(reinterpret_cast<const char*>(content), size);
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
335
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
336 if (!f.good())
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
337 {
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
338 f.close();
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
339 throw OrthancException(ErrorCode_CannotWriteFile);
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
340 }
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
341 }
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
342
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
343 if (callFsync)
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
344 {
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
345 // https://stackoverflow.com/a/23826489/881731
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
346 f.flush();
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
347
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
348 bool success;
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
349
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
350 /**
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
351 * "f->handle()" corresponds to "FILE*" (aka "HANDLE") on
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
352 * Microsoft Windows, and to "int" (file descriptor) on other
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
353 * systems:
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
354 * https://github.com/boostorg/iostreams/blob/develop/include/boost/iostreams/detail/file_handle.hpp
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
355 **/
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
356
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
357 #if defined(_WIN32)
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
358 // https://docs.microsoft.com/fr-fr/windows/win32/api/fileapi/nf-fileapi-flushfilebuffers
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
359 success = (::FlushFileBuffers(f->handle()) != 0);
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
360 #elif (_POSIX_C_SOURCE >= 199309L || _XOPEN_SOURCE >= 500)
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
361 success = (::fdatasync(f->handle()) == 0);
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
362 #else
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
363 success = (::fsync(f->handle()) == 0);
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
364 #endif
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
365
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
366 if (!success)
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
367 {
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
368 throw OrthancException(ErrorCode_CannotWriteFile, "Cannot force flush to disk");
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
369 }
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
370 }
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
371
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
372 f.close();
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
373 }
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
374 catch (boost::filesystem::filesystem_error&)
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
375 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
376 throw OrthancException(ErrorCode_CannotWriteFile);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
377 }
4402
b651989194d3 fix for older compilers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4399
diff changeset
378 catch (...) // To catch "std::system_error&" in C++11
4185
b289a1234822 giving a try to cross-platform compilation of SyncStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
379 {
4399
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
380 throw OrthancException(ErrorCode_CannotWriteFile);
4185
b289a1234822 giving a try to cross-platform compilation of SyncStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
381 }
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
382 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
383
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
384
4279
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4239
diff changeset
385 void SystemToolbox::WriteFile(const void *content, size_t size, const std::string &path)
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4239
diff changeset
386 {
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4239
diff changeset
387 WriteFile(content, size, path, false /* don't automatically call fsync */);
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4239
diff changeset
388 }
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4239
diff changeset
389
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4239
diff changeset
390
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
391 void SystemToolbox::WriteFile(const std::string& content,
4185
b289a1234822 giving a try to cross-platform compilation of SyncStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
392 const std::string& path,
b289a1234822 giving a try to cross-platform compilation of SyncStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
393 bool callFsync)
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
394 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
395 WriteFile(content.size() > 0 ? content.c_str() : NULL,
4185
b289a1234822 giving a try to cross-platform compilation of SyncStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
396 content.size(), path, callFsync);
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
397 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
398
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
399
4279
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4239
diff changeset
400 void SystemToolbox::WriteFile(const std::string &content, const std::string &path)
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4239
diff changeset
401 {
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4239
diff changeset
402 WriteFile(content, path, false /* don't automatically call fsync */);
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4239
diff changeset
403 }
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4239
diff changeset
404
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4239
diff changeset
405
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
406 void SystemToolbox::RemoveFile(const std::string& path)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
407 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
408 if (boost::filesystem::exists(path))
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
409 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
410 if (IsRegularFile(path))
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
411 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
412 boost::filesystem::remove(path);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
413 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
414 else
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
415 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
416 throw OrthancException(ErrorCode_RegularFileExpected);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
417 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
418 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
419 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
420
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
421
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
422 uint64_t SystemToolbox::GetFileSize(const std::string& path)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
423 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
424 try
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
425 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
426 return static_cast<uint64_t>(boost::filesystem::file_size(path));
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
427 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
428 catch (boost::filesystem::filesystem_error&)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
429 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
430 throw OrthancException(ErrorCode_InexistentFile);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
431 }
4402
b651989194d3 fix for older compilers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4399
diff changeset
432 catch (...) // To catch "std::system_error&" in C++11
4399
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
433 {
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
434 throw OrthancException(ErrorCode_InexistentFile);
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
435 }
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
436 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
437
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
438
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
439 void SystemToolbox::MakeDirectory(const std::string& path)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
440 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
441 if (boost::filesystem::exists(path))
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
442 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
443 if (!boost::filesystem::is_directory(path))
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
444 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
445 throw OrthancException(ErrorCode_DirectoryOverFile);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
446 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
447 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
448 else
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
449 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
450 if (!boost::filesystem::create_directories(path))
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
451 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
452 throw OrthancException(ErrorCode_MakeDirectory);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
453 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
454 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
455 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
456
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
457
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
458 bool SystemToolbox::IsExistingFile(const std::string& path)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
459 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
460 return boost::filesystem::exists(path);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
461 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
462
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
463
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
464 #if defined(_WIN32)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
465 static std::string GetPathToExecutableInternal()
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
466 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
467 // Yes, this is ugly, but there is no simple way to get the
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
468 // required buffer size, so we use a big constant
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
469 std::vector<char> buffer(32768);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
470 /*int bytes =*/ GetModuleFileNameA(NULL, &buffer[0], static_cast<DWORD>(buffer.size() - 1));
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
471 return std::string(&buffer[0]);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
472 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
473
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
474 #elif defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
475 static std::string GetPathToExecutableInternal()
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
476 {
2365
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
477 // NOTE: For FreeBSD, using KERN_PROC_PATHNAME might be a better alternative
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
478
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
479 std::vector<char> buffer(PATH_MAX + 1);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
480 ssize_t bytes = readlink("/proc/self/exe", &buffer[0], buffer.size() - 1);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
481 if (bytes == 0)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
482 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
483 throw OrthancException(ErrorCode_PathToExecutable);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
484 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
485
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
486 return std::string(&buffer[0]);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
487 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
488
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
489 #elif defined(__APPLE__) && defined(__MACH__)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
490 static std::string GetPathToExecutableInternal()
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
491 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
492 char pathbuf[PATH_MAX + 1];
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
493 unsigned int bufsize = static_cast<int>(sizeof(pathbuf));
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
494
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
495 _NSGetExecutablePath( pathbuf, &bufsize);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
496
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
497 return std::string(pathbuf);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
498 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
499
2364
ae50eccd41b7 compilation succeeds on OpenBSD
jodogne
parents: 2332
diff changeset
500 #elif defined(__OpenBSD__)
ae50eccd41b7 compilation succeeds on OpenBSD
jodogne
parents: 2332
diff changeset
501 static std::string GetPathToExecutableInternal()
ae50eccd41b7 compilation succeeds on OpenBSD
jodogne
parents: 2332
diff changeset
502 {
2365
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
503 // This is an adapted version of the patch proposed in issue #64
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
504 // without an explicit call to "malloc()" to prevent memory leak
4476
c1f36fd13730 migrate remaining links to issues in bitbucket to mercurial server
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
505 // https://bugs.orthanc-server.com/show_bug.cgi?id=64
2365
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
506 // https://stackoverflow.com/q/31494901/881731
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
507
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
508 const int mib[4] = { CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV };
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
509
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
510 size_t len;
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
511 if (sysctl(mib, 4, NULL, &len, NULL, 0) == -1)
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
512 {
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
513 throw OrthancException(ErrorCode_PathToExecutable);
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
514 }
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
515
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
516 std::string tmp;
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
517 tmp.resize(len);
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
518
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
519 char** buffer = reinterpret_cast<char**>(&tmp[0]);
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
520
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
521 if (sysctl(mib, 4, buffer, &len, NULL, 0) == -1)
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
522 {
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
523 throw OrthancException(ErrorCode_PathToExecutable);
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
524 }
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
525 else
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
526 {
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
527 return std::string(buffer[0]);
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
528 }
2364
ae50eccd41b7 compilation succeeds on OpenBSD
jodogne
parents: 2332
diff changeset
529 }
ae50eccd41b7 compilation succeeds on OpenBSD
jodogne
parents: 2332
diff changeset
530
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
531 #else
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
532 #error Support your platform here
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
533 #endif
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
534
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
535
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
536 std::string SystemToolbox::GetPathToExecutable()
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
537 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
538 boost::filesystem::path p(GetPathToExecutableInternal());
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
539 return boost::filesystem::absolute(p).string();
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
540 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
541
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
542
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
543 std::string SystemToolbox::GetDirectoryOfExecutable()
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
544 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
545 boost::filesystem::path p(GetPathToExecutableInternal());
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
546 return boost::filesystem::absolute(p.parent_path()).string();
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
547 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
548
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
549
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
550 void SystemToolbox::ExecuteSystemCommand(const std::string& command,
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
551 const std::vector<std::string>& arguments)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
552 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
553 // Convert the arguments as a C array
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
554 std::vector<char*> args(arguments.size() + 2);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
555
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
556 args.front() = const_cast<char*>(command.c_str());
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
557
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
558 for (size_t i = 0; i < arguments.size(); i++)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
559 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
560 args[i + 1] = const_cast<char*>(arguments[i].c_str());
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
561 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
562
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
563 args.back() = NULL;
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
564
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
565 int status;
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
566
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
567 #if defined(_WIN32)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
568 // http://msdn.microsoft.com/en-us/library/275khfab.aspx
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
569 status = static_cast<int>(_spawnvp(_P_OVERLAY, command.c_str(), &args[0]));
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
570
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
571 #else
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
572 int pid = fork();
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
573
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
574 if (pid == -1)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
575 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
576 // Error in fork()
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2933
diff changeset
577 throw OrthancException(ErrorCode_SystemCommand, "Cannot fork a child process");
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
578 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
579 else if (pid == 0)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
580 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
581 // Execute the system command in the child process
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
582 execvp(command.c_str(), &args[0]);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
583
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
584 // We should never get here
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
585 _exit(1);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
586 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
587 else
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
588 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
589 // Wait for the system command to exit
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
590 waitpid(pid, &status, 0);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
591 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
592 #endif
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
593
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
594 if (status != 0)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
595 {
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2933
diff changeset
596 throw OrthancException(ErrorCode_SystemCommand,
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2933
diff changeset
597 "System command failed with status code " +
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2933
diff changeset
598 boost::lexical_cast<std::string>(status));
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
599 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
600 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
601
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
602
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
603 int SystemToolbox::GetProcessId()
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
604 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
605 #if defined(_WIN32)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
606 return static_cast<int>(_getpid());
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
607 #else
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
608 return static_cast<int>(getpid());
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
609 #endif
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
610 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
611
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
612
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
613 bool SystemToolbox::IsRegularFile(const std::string& path)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
614 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
615 try
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
616 {
4399
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
617 if (boost::filesystem::exists(path))
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
618 {
4399
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
619 boost::filesystem::file_status status = boost::filesystem::status(path);
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
620 return (status.type() == boost::filesystem::regular_file ||
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
621 status.type() == boost::filesystem::reparse_file); // Fix BitBucket issue #11
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
622 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
623 }
4399
80fd140b12ba New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4342
diff changeset
624 catch (boost::filesystem::filesystem_error&)
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
625 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
626 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
627
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
628 return false;
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
629 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
630
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
631
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
632 FILE* SystemToolbox::OpenFile(const std::string& path,
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
633 FileMode mode)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
634 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
635 #if defined(_WIN32)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
636 // TODO Deal with special characters by converting to the current locale
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
637 #endif
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
638
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
639 const char* m;
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
640 switch (mode)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
641 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
642 case FileMode_ReadBinary:
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
643 m = "rb";
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
644 break;
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
645
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
646 case FileMode_WriteBinary:
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
647 m = "wb";
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
648 break;
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
649
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
650 default:
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
651 throw OrthancException(ErrorCode_ParameterOutOfRange);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
652 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
653
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
654 return fopen(path.c_str(), m);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
655 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
656
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
657
2475
8cc3ca64a534 Orthanc now uses UTC (universal time) instead of local time in its database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
658 static boost::posix_time::ptime GetNow(bool utc)
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
659 {
2475
8cc3ca64a534 Orthanc now uses UTC (universal time) instead of local time in its database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
660 if (utc)
8cc3ca64a534 Orthanc now uses UTC (universal time) instead of local time in its database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
661 {
8cc3ca64a534 Orthanc now uses UTC (universal time) instead of local time in its database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
662 return boost::posix_time::second_clock::universal_time();
8cc3ca64a534 Orthanc now uses UTC (universal time) instead of local time in its database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
663 }
8cc3ca64a534 Orthanc now uses UTC (universal time) instead of local time in its database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
664 else
8cc3ca64a534 Orthanc now uses UTC (universal time) instead of local time in its database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
665 {
8cc3ca64a534 Orthanc now uses UTC (universal time) instead of local time in its database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
666 return boost::posix_time::second_clock::local_time();
8cc3ca64a534 Orthanc now uses UTC (universal time) instead of local time in its database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
667 }
8cc3ca64a534 Orthanc now uses UTC (universal time) instead of local time in its database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
668 }
8cc3ca64a534 Orthanc now uses UTC (universal time) instead of local time in its database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
669
8cc3ca64a534 Orthanc now uses UTC (universal time) instead of local time in its database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
670
8cc3ca64a534 Orthanc now uses UTC (universal time) instead of local time in its database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
671 std::string SystemToolbox::GetNowIsoString(bool utc)
8cc3ca64a534 Orthanc now uses UTC (universal time) instead of local time in its database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
672 {
8cc3ca64a534 Orthanc now uses UTC (universal time) instead of local time in its database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
673 return boost::posix_time::to_iso_string(GetNow(utc));
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
674 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
675
2367
2aff870c2c58 refactoring of BoostConfiguration.cmake
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2365
diff changeset
676
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
677 void SystemToolbox::GetNowDicom(std::string& date,
2475
8cc3ca64a534 Orthanc now uses UTC (universal time) instead of local time in its database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
678 std::string& time,
8cc3ca64a534 Orthanc now uses UTC (universal time) instead of local time in its database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
679 bool utc)
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
680 {
2475
8cc3ca64a534 Orthanc now uses UTC (universal time) instead of local time in its database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
681 boost::posix_time::ptime now = GetNow(utc);
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
682 tm tm = boost::posix_time::to_tm(now);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
683
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
684 char s[32];
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
685 sprintf(s, "%04d%02d%02d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
686 date.assign(s);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
687
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
688 // TODO milliseconds
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
689 sprintf(s, "%02d%02d%02d.%06d", tm.tm_hour, tm.tm_min, tm.tm_sec, 0);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
690 time.assign(s);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
691 }
2794
f790999a250a Semaphore removed from graveyard, SystemToolbox::GetHardwareConcurrency()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2512
diff changeset
692
f790999a250a Semaphore removed from graveyard, SystemToolbox::GetHardwareConcurrency()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2512
diff changeset
693
f790999a250a Semaphore removed from graveyard, SystemToolbox::GetHardwareConcurrency()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2512
diff changeset
694 unsigned int SystemToolbox::GetHardwareConcurrency()
f790999a250a Semaphore removed from graveyard, SystemToolbox::GetHardwareConcurrency()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2512
diff changeset
695 {
f790999a250a Semaphore removed from graveyard, SystemToolbox::GetHardwareConcurrency()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2512
diff changeset
696 // Get the number of available hardware threads (e.g. number of
f790999a250a Semaphore removed from graveyard, SystemToolbox::GetHardwareConcurrency()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2512
diff changeset
697 // CPUs or cores or hyperthreading units)
f790999a250a Semaphore removed from graveyard, SystemToolbox::GetHardwareConcurrency()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2512
diff changeset
698 unsigned int threads = boost::thread::hardware_concurrency();
f790999a250a Semaphore removed from graveyard, SystemToolbox::GetHardwareConcurrency()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2512
diff changeset
699
4203
4d42408da117 improving const-correctness in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4188
diff changeset
700 if (threads == 0)
2794
f790999a250a Semaphore removed from graveyard, SystemToolbox::GetHardwareConcurrency()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2512
diff changeset
701 {
f790999a250a Semaphore removed from graveyard, SystemToolbox::GetHardwareConcurrency()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2512
diff changeset
702 return 1;
f790999a250a Semaphore removed from graveyard, SystemToolbox::GetHardwareConcurrency()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2512
diff changeset
703 }
f790999a250a Semaphore removed from graveyard, SystemToolbox::GetHardwareConcurrency()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2512
diff changeset
704 else
f790999a250a Semaphore removed from graveyard, SystemToolbox::GetHardwareConcurrency()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2512
diff changeset
705 {
f790999a250a Semaphore removed from graveyard, SystemToolbox::GetHardwareConcurrency()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2512
diff changeset
706 return threads;
f790999a250a Semaphore removed from graveyard, SystemToolbox::GetHardwareConcurrency()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2512
diff changeset
707 }
f790999a250a Semaphore removed from graveyard, SystemToolbox::GetHardwareConcurrency()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2512
diff changeset
708 }
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
709
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
710
2908
9d277f8ad698 new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2906
diff changeset
711 MimeType SystemToolbox::AutodetectMimeType(const std::string& path)
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
712 {
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
713 std::string extension = boost::filesystem::extension(path);
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
714 Toolbox::ToLowerCase(extension);
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
715
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
716 // http://en.wikipedia.org/wiki/Mime_types
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
717 // Text types
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
718 if (extension == ".txt")
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
719 {
2908
9d277f8ad698 new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2906
diff changeset
720 return MimeType_PlainText;
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
721 }
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
722 else if (extension == ".html")
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
723 {
2908
9d277f8ad698 new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2906
diff changeset
724 return MimeType_Html;
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
725 }
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
726 else if (extension == ".xml")
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
727 {
2908
9d277f8ad698 new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2906
diff changeset
728 return MimeType_Xml;
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
729 }
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
730 else if (extension == ".css")
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
731 {
2908
9d277f8ad698 new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2906
diff changeset
732 return MimeType_Css;
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
733 }
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
734
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
735 // Application types
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
736 else if (extension == ".js")
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
737 {
2908
9d277f8ad698 new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2906
diff changeset
738 return MimeType_JavaScript;
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
739 }
2984
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
740 else if (extension == ".json" ||
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
741 extension == ".nmf" /* manifest */)
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
742 {
2908
9d277f8ad698 new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2906
diff changeset
743 return MimeType_Json;
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
744 }
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
745 else if (extension == ".pdf")
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
746 {
2908
9d277f8ad698 new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2906
diff changeset
747 return MimeType_Pdf;
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
748 }
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
749 else if (extension == ".wasm")
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
750 {
2908
9d277f8ad698 new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2906
diff changeset
751 return MimeType_WebAssembly;
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
752 }
2984
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
753 else if (extension == ".nexe")
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
754 {
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
755 return MimeType_NaCl;
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
756 }
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
757 else if (extension == ".pexe")
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
758 {
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
759 return MimeType_PNaCl;
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
760 }
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
761
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
762 // Images types
4239
c8754c4c1862 upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4223
diff changeset
763 else if (extension == ".dcm")
c8754c4c1862 upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4223
diff changeset
764 {
c8754c4c1862 upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4223
diff changeset
765 return MimeType_Dicom;
c8754c4c1862 upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4223
diff changeset
766 }
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
767 else if (extension == ".jpg" ||
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
768 extension == ".jpeg")
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
769 {
2908
9d277f8ad698 new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2906
diff changeset
770 return MimeType_Jpeg;
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
771 }
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
772 else if (extension == ".gif")
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
773 {
2908
9d277f8ad698 new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2906
diff changeset
774 return MimeType_Gif;
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
775 }
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
776 else if (extension == ".png")
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
777 {
2908
9d277f8ad698 new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2906
diff changeset
778 return MimeType_Png;
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
779 }
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
780 else if (extension == ".pam")
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
781 {
2908
9d277f8ad698 new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2906
diff changeset
782 return MimeType_Pam;
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
783 }
2984
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
784 else if (extension == ".svg")
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
785 {
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
786 return MimeType_Svg;
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
787 }
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
788
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
789 // Various types
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
790 else if (extension == ".woff")
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
791 {
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
792 return MimeType_Woff;
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
793 }
3435
c08bb6ac3b7f new mime type: font/woff2
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3402
diff changeset
794 else if (extension == ".woff2")
c08bb6ac3b7f new mime type: font/woff2
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3402
diff changeset
795 {
c08bb6ac3b7f new mime type: font/woff2
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3402
diff changeset
796 return MimeType_Woff2;
c08bb6ac3b7f new mime type: font/woff2
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3402
diff changeset
797 }
2984
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
798
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
799 // Default type
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
800 else
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
801 {
2984
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
802 LOG(INFO) << "Unknown MIME type for extension \"" << extension << "\"";
2908
9d277f8ad698 new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2906
diff changeset
803 return MimeType_Binary;
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
804 }
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
805 }
2920
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
806
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
807
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
808 void SystemToolbox::GetEnvironmentVariables(std::map<std::string, std::string>& env)
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
809 {
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
810 env.clear();
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
811
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
812 for (char **p = environ; *p != NULL; p++)
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
813 {
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
814 std::string v(*p);
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
815 size_t pos = v.find('=');
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
816
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
817 if (pos != std::string::npos)
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
818 {
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
819 std::string key = v.substr(0, pos);
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
820 std::string value = v.substr(pos + 1);
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
821 env[key] = value;
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
822 }
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
823 }
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
824 }
2933
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
825
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
826
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
827 std::string SystemToolbox::InterpretRelativePath(const std::string& baseDirectory,
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
828 const std::string& relativePath)
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
829 {
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
830 boost::filesystem::path base(baseDirectory);
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
831 boost::filesystem::path relative(relativePath);
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
832
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
833 /**
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
834 The following lines should be equivalent to this one:
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
835
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
836 return (base / relative).string();
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
837
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
838 However, for some unknown reason, some versions of Boost do not
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
839 make the proper path resolution when "baseDirectory" is an
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
840 absolute path. So, a hack is used below.
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
841 **/
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
842
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
843 if (relative.is_absolute())
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
844 {
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
845 return relative.string();
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
846 }
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
847 else
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
848 {
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
849 return (base / relative).string();
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
850 }
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
851 }
4342
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
852
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
853
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
854 void SystemToolbox::ReadFileRange(std::string& content,
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
855 const std::string& path,
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
856 uint64_t start, // Inclusive
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
857 uint64_t end, // Exclusive
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
858 bool throwIfOverflow)
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
859 {
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
860 if (start > end)
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
861 {
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
862 throw OrthancException(ErrorCode_ParameterOutOfRange);
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
863 }
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
864
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
865 if (!IsRegularFile(path))
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
866 {
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
867 throw OrthancException(ErrorCode_RegularFileExpected,
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
868 "The path does not point to a regular file: " + path);
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
869 }
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
870
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
871 boost::filesystem::ifstream f;
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
872 f.open(path, std::ifstream::in | std::ifstream::binary);
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
873 if (!f.good())
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
874 {
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
875 throw OrthancException(ErrorCode_InexistentFile,
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
876 "File not found: " + path);
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
877 }
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
878
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
879 uint64_t fileSize = static_cast<uint64_t>(GetStreamSize(f));
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
880 if (end > fileSize)
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
881 {
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
882 if (throwIfOverflow)
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
883 {
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
884 throw OrthancException(ErrorCode_ParameterOutOfRange,
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
885 "Reading beyond the end of a file");
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
886 }
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
887 else
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
888 {
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
889 end = fileSize;
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
890 }
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
891 }
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
892
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
893 if (start <= end)
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
894 {
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
895 content.resize(static_cast<size_t>(end - start));
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
896
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
897 if (static_cast<uint64_t>(content.size()) != (end - start))
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
898 {
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
899 throw OrthancException(ErrorCode_InternalError,
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
900 "Reading a file that is too large for a 32bit architecture");
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
901 }
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
902
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
903 if (!content.empty())
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
904 {
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
905 f.seekg(start, std::ios::beg);
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
906 f.read(&content[0], static_cast<std::streamsize>(content.size()));
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
907 }
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
908 }
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
909 else
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
910 {
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
911 content.clear();
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
912 }
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
913
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
914 f.close();
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
915 }
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
916 }