annotate OrthancFramework/Sources/SystemToolbox.cpp @ 5660:e4d9a872998f

replaced incorrect macro WIN32 by _WIN32
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sun, 23 Jun 2024 10:24:31 +0200
parents f7adfb22e20e
children
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
5640
f7adfb22e20e updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5627
diff changeset
5 * Copyright (C) 2017-2023 Osimis S.A., Belgium
f7adfb22e20e updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5627
diff changeset
6 * Copyright (C) 2024-2024 Orthanc Team SRL, Belgium
5485
48b8dae6dc77 upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5432
diff changeset
7 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 *
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * 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
10 * 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
11 * 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
12 * the License, or (at your option) any later version.
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 *
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * 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
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * 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
17 * Lesser General Public License for more details.
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 *
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
19 * 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
20 * 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
21 * <http://www.gnu.org/licenses/>.
2143
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
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 #include "PrecompiledHeaders.h"
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 #include "SystemToolbox.h"
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 #if defined(_WIN32)
4538
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
30 # include <winsock2.h> // For GetMacAddresses(), must be included before "windows.h"
2173
1c42860097fc fix windows build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2172
diff changeset
31 # include <windows.h>
4538
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
32
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
33 # include <iphlpapi.h> // For GetMacAddresses()
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
34 # include <process.h> // For "_spawnvp()" and "_getpid()"
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
35 # include <stdlib.h> // For "environ"
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 #else
4538
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
37 # include <net/if.h> // For GetMacAddresses()
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
38 # include <netinet/in.h> // For GetMacAddresses()
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
39 # include <sys/ioctl.h> // For GetMacAddresses()
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
40 # include <sys/wait.h> // For "waitpid()"
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
41 # include <unistd.h> // For "execvp()"
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 #endif
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43
2173
1c42860097fc fix windows build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2172
diff changeset
44
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 #if defined(__APPLE__) && defined(__MACH__)
4538
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
46 # include <limits.h> // PATH_MAX
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
47 # include <mach-o/dyld.h> // _NSGetExecutablePath
4552
beb8ba8a0b12 fix build on freebsd
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4538
diff changeset
48 #endif
beb8ba8a0b12 fix build on freebsd
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4538
diff changeset
49
beb8ba8a0b12 fix build on freebsd
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4538
diff changeset
50
beb8ba8a0b12 fix build on freebsd
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4538
diff changeset
51 #if (defined(__APPLE__) && defined(__MACH__)) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__)
4538
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
52 # include <net/if_dl.h> // For GetMacAddresses()
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
53 # include <net/if_types.h> // For GetMacAddresses()
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
54 # include <sys/sysctl.h> // For GetMacAddresses()
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 #endif
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56
2173
1c42860097fc fix windows build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2172
diff changeset
57
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 #if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__)
4538
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
59 # include <limits.h> // PATH_MAX
2173
1c42860097fc fix windows build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2172
diff changeset
60 # include <signal.h>
1c42860097fc fix windows build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2172
diff changeset
61 # include <unistd.h>
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 #endif
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64
2365
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
65 #if defined(__OpenBSD__)
4538
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
66 # include <sys/sysctl.h> // For "sysctl", "CTL_KERN" and "KERN_PROC_ARGS"
2365
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
67 #endif
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
68
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
69
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 #include "Logging.h"
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 #include "OrthancException.h"
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 #include "Toolbox.h"
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73
4185
b289a1234822 giving a try to cross-platform compilation of SyncStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
74 #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
75 #include <boost/iostreams/stream.hpp>
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 #include <boost/filesystem.hpp>
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 #include <boost/filesystem/fstream.hpp>
2367
2aff870c2c58 refactoring of BoostConfiguration.cmake
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2365
diff changeset
78 #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
79 #include <boost/thread.hpp>
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80
4538
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
81 #include <cassert>
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
82 #include <string.h>
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
83
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
84
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85
2920
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
86 /*=========================================================================
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
87 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
88 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
89
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
90 Copyright Vladimir Prus 2002-2004.
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
91 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
92 (See accompanying file LICENSE_1_0.txt
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
93 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
94 =========================================================================*/
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
95
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
96 // 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
97 // (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
98 // 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
99 // 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
100 // To be safe, declare it here.
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
101
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
102 // 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
103 // available to dynamically linked libraries.
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
104 // 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
105 // 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
106 #if defined(__APPLE__) && defined(__DYNAMIC__)
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
107 // 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
108 // 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
109 // https://svn.boost.org/trac/boost/ticket/5053
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
110 extern "C"
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
111 {
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
112 extern char ***_NSGetEnviron(void);
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
113 }
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
114 # define environ (*_NSGetEnviron())
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
115 #else
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
116 # if defined(__MWERKS__)
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
117 # include <crtl.h>
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
118 # else
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
119 # if !defined(_WIN32) || defined(__COMO_VERSION__)
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
120 extern char** environ;
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
121 # endif
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
122 # endif
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
123 #endif
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
124
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
125
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
126 /*=========================================================================
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
127 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
128 =========================================================================*/
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
129
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
130
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 namespace Orthanc
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 static bool finish_;
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 static ServerBarrierEvent barrierEvent_;
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 #if defined(_WIN32)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 static BOOL WINAPI ConsoleControlHandler(DWORD dwCtrlType)
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 // 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
140 finish_ = true;
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 return true;
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 #else
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 static void SignalHandler(int signal)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 if (signal == SIGHUP)
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 barrierEvent_ = ServerBarrierEvent_Reload;
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 finish_ = true;
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 #endif
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 static ServerBarrierEvent ServerBarrierInternal(const bool* stopFlag)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 #if defined(_WIN32)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 SetConsoleCtrlHandler(ConsoleControlHandler, true);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 #else
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 signal(SIGINT, SignalHandler);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 signal(SIGQUIT, SignalHandler);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 signal(SIGTERM, SignalHandler);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 signal(SIGHUP, SignalHandler);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 #endif
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 // Active loop that awakens every 100ms
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 finish_ = false;
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 barrierEvent_ = ServerBarrierEvent_Stop;
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 while (!(*stopFlag || finish_))
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171 {
2242
4e8e0ad2001c move USleep() in SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2173
diff changeset
172 SystemToolbox::USleep(100 * 1000);
2143
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 #if defined(_WIN32)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 SetConsoleCtrlHandler(ConsoleControlHandler, false);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 #else
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 signal(SIGINT, NULL);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 signal(SIGQUIT, NULL);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 signal(SIGTERM, NULL);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 signal(SIGHUP, NULL);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 #endif
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184 return barrierEvent_;
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 ServerBarrierEvent SystemToolbox::ServerBarrier(const bool& stopFlag)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190 return ServerBarrierInternal(&stopFlag);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 ServerBarrierEvent SystemToolbox::ServerBarrier()
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 const bool stopFlag = false;
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 return ServerBarrierInternal(&stopFlag);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200
2242
4e8e0ad2001c move USleep() in SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2173
diff changeset
201 void SystemToolbox::USleep(uint64_t microSeconds)
4e8e0ad2001c move USleep() in SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2173
diff changeset
202 {
4e8e0ad2001c move USleep() in SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2173
diff changeset
203 #if defined(_WIN32)
4e8e0ad2001c move USleep() in SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2173
diff changeset
204 ::Sleep(static_cast<DWORD>(microSeconds / static_cast<uint64_t>(1000)));
2364
ae50eccd41b7 compilation succeeds on OpenBSD
jodogne
parents: 2332
diff changeset
205 #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
206 usleep(microSeconds);
4e8e0ad2001c move USleep() in SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2173
diff changeset
207 #else
4e8e0ad2001c move USleep() in SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2173
diff changeset
208 #error Support your platform here
4e8e0ad2001c move USleep() in SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2173
diff changeset
209 #endif
4e8e0ad2001c move USleep() in SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2173
diff changeset
210 }
4e8e0ad2001c move USleep() in SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2173
diff changeset
211
4e8e0ad2001c move USleep() in SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2173
diff changeset
212
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213 static std::streamsize GetStreamSize(std::istream& f)
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 // http://www.cplusplus.com/reference/iostream/istream/tellg/
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216 f.seekg(0, std::ios::end);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217 std::streamsize size = f.tellg();
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218 f.seekg(0, std::ios::beg);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220 return size;
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224 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
225 const std::string& path,
793c141be598 added an option not to log ReadFile errors
Alain Mazy <alain@mazy.be>
parents: 3435
diff changeset
226 bool log)
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
227 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
228 if (!IsRegularFile(path))
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229 {
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2933
diff changeset
230 throw OrthancException(ErrorCode_RegularFileExpected,
3521
793c141be598 added an option not to log ReadFile errors
Alain Mazy <alain@mazy.be>
parents: 3435
diff changeset
231 "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
232 log);
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234
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
235 try
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 {
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
237 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
238 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
239 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
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 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
242 "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
243 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
244 }
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
245
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
246 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
247 content.resize(static_cast<size_t>(size));
4342
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
248
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
249 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
250 {
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
251 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
252 "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
253 }
4342
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
254
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
255 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
256 {
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
257 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
258 }
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
259
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
260 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
261 }
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
262 catch (boost::filesystem::filesystem_error&)
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
263 {
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
264 throw OrthancException(ErrorCode_InexistentFile);
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
265 }
4402
b651989194d3 fix for older compilers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4399
diff changeset
266 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
267 {
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
268 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
269 }
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
270 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
271
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
272
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4279
diff changeset
273 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
274 {
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4239
diff changeset
275 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
276 }
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4239
diff changeset
277
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4239
diff changeset
278
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
279 bool SystemToolbox::ReadHeader(std::string& header,
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
280 const std::string& path,
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
281 size_t headerSize)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
282 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
283 if (!IsRegularFile(path))
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
284 {
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2933
diff changeset
285 throw OrthancException(ErrorCode_RegularFileExpected,
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2933
diff changeset
286 "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
287 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
288
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
289 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
290 {
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 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
292 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
293 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
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 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
296 }
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 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
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 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
302 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
303 {
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 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
305 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
306 }
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
307 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
308 {
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
309 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
310 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
311 }
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
312 }
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
313
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
314 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
315 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
316 {
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
317 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
318 }
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
319
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
320 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
321
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 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
323 }
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 catch (boost::filesystem::filesystem_error&)
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
325 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
326 throw OrthancException(ErrorCode_InexistentFile);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
327 }
4402
b651989194d3 fix for older compilers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4399
diff changeset
328 catch (...) // To catch "std::system_error&" in C++11
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
329 {
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
330 throw OrthancException(ErrorCode_InexistentFile);
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
331 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
332 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
333
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
334
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
335 void SystemToolbox::WriteFile(const void* content,
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
336 size_t size,
4185
b289a1234822 giving a try to cross-platform compilation of SyncStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
337 const std::string& path,
b289a1234822 giving a try to cross-platform compilation of SyncStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
338 bool callFsync)
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
339 {
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
340 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
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 //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
343 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
344
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
345 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
346 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
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 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
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 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
352 {
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 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
354
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 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
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 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
358 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
359 }
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 }
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
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 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
363 {
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 // 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
365 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
366
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 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
368
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 * "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
371 * 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
372 * 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
373 * 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
374 **/
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
375
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
376 #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
377 // 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
378 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
379 #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
380 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
381 #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
382 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
383 #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
384
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
385 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
386 {
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
387 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
388 }
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
389 }
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
390
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
391 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
392 }
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
393 catch (boost::filesystem::filesystem_error&)
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 throw OrthancException(ErrorCode_CannotWriteFile);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
396 }
4402
b651989194d3 fix for older compilers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4399
diff changeset
397 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
398 {
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
399 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
400 }
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
401 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
402
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
403
4279
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4239
diff changeset
404 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
405 {
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4239
diff changeset
406 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
407 }
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4239
diff changeset
408
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4239
diff changeset
409
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
410 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
411 const std::string& path,
b289a1234822 giving a try to cross-platform compilation of SyncStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
412 bool callFsync)
2143
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 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
415 content.size(), path, callFsync);
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
416 }
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
4279
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4239
diff changeset
419 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
420 {
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4239
diff changeset
421 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
422 }
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4239
diff changeset
423
ab4d015af660 moving inline methods to source files for ABI compatibility
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4239
diff changeset
424
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
425 void SystemToolbox::RemoveFile(const std::string& path)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
426 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
427 if (boost::filesystem::exists(path))
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
428 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
429 if (IsRegularFile(path))
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
430 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
431 boost::filesystem::remove(path);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
432 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
433 else
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
434 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
435 throw OrthancException(ErrorCode_RegularFileExpected);
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
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 uint64_t SystemToolbox::GetFileSize(const std::string& 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 try
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 return static_cast<uint64_t>(boost::filesystem::file_size(path));
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 catch (boost::filesystem::filesystem_error&)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
448 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
449 throw OrthancException(ErrorCode_InexistentFile);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
450 }
4402
b651989194d3 fix for older compilers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4399
diff changeset
451 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
452 {
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
453 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
454 }
2143
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 void SystemToolbox::MakeDirectory(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 if (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 if (!boost::filesystem::is_directory(path))
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 throw OrthancException(ErrorCode_DirectoryOverFile);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
465 }
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 else
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
468 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
469 if (!boost::filesystem::create_directories(path))
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
470 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
471 throw OrthancException(ErrorCode_MakeDirectory);
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 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
475
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
476
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
477 bool SystemToolbox::IsExistingFile(const std::string& path)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
478 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
479 return boost::filesystem::exists(path);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
480 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
481
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 #if defined(_WIN32)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
484 static std::string GetPathToExecutableInternal()
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 // 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
487 // required buffer size, so we use a big constant
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
488 std::vector<char> buffer(32768);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
489 /*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
490 return std::string(&buffer[0]);
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
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
493 #elif defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
494 static std::string GetPathToExecutableInternal()
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
495 {
2365
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
496 // NOTE: For FreeBSD, using KERN_PROC_PATHNAME might be a better alternative
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
497
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
498 std::vector<char> buffer(PATH_MAX + 1);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
499 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
500 if (bytes == 0)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
501 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
502 throw OrthancException(ErrorCode_PathToExecutable);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
503 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
504
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
505 return std::string(&buffer[0]);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
506 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
507
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
508 #elif defined(__APPLE__) && defined(__MACH__)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
509 static std::string GetPathToExecutableInternal()
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
510 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
511 char pathbuf[PATH_MAX + 1];
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
512 unsigned int bufsize = static_cast<int>(sizeof(pathbuf));
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
513
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
514 _NSGetExecutablePath( pathbuf, &bufsize);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
515
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
516 return std::string(pathbuf);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
517 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
518
2364
ae50eccd41b7 compilation succeeds on OpenBSD
jodogne
parents: 2332
diff changeset
519 #elif defined(__OpenBSD__)
ae50eccd41b7 compilation succeeds on OpenBSD
jodogne
parents: 2332
diff changeset
520 static std::string GetPathToExecutableInternal()
ae50eccd41b7 compilation succeeds on OpenBSD
jodogne
parents: 2332
diff changeset
521 {
2365
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
522 // This is an adapted version of the patch proposed in issue #64
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
523 // without an explicit call to "malloc()" to prevent memory leak
5432
59e3b6f8c5be migration to UCLouvain servers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5407
diff changeset
524 // https://orthanc.uclouvain.be/bugs/show_bug.cgi?id=64
2365
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
525 // https://stackoverflow.com/q/31494901/881731
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
526
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
527 const int mib[4] = { CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV };
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
528
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
529 size_t len;
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
530 if (sysctl(mib, 4, NULL, &len, NULL, 0) == -1)
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
531 {
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
532 throw OrthancException(ErrorCode_PathToExecutable);
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
533 }
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
534
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
535 std::string tmp;
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
536 tmp.resize(len);
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
537
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
538 char** buffer = reinterpret_cast<char**>(&tmp[0]);
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
539
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
540 if (sysctl(mib, 4, buffer, &len, NULL, 0) == -1)
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
541 {
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
542 throw OrthancException(ErrorCode_PathToExecutable);
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
543 }
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
544 else
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
545 {
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
546 return std::string(buffer[0]);
9e0eae8a5ea4 SystemToolbox::GetPathToExecutable() for OpenBSD
jodogne
parents: 2364
diff changeset
547 }
2364
ae50eccd41b7 compilation succeeds on OpenBSD
jodogne
parents: 2332
diff changeset
548 }
ae50eccd41b7 compilation succeeds on OpenBSD
jodogne
parents: 2332
diff changeset
549
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
550 #else
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
551 #error Support your platform here
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
552 #endif
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
553
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
554
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
555 std::string SystemToolbox::GetPathToExecutable()
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
556 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
557 boost::filesystem::path p(GetPathToExecutableInternal());
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
558 return boost::filesystem::absolute(p).string();
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
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 std::string SystemToolbox::GetDirectoryOfExecutable()
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
563 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
564 boost::filesystem::path p(GetPathToExecutableInternal());
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
565 return boost::filesystem::absolute(p.parent_path()).string();
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
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
568
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
569 void SystemToolbox::ExecuteSystemCommand(const std::string& command,
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
570 const std::vector<std::string>& arguments)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
571 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
572 // Convert the arguments as a C array
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
573 std::vector<char*> args(arguments.size() + 2);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
574
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
575 args.front() = const_cast<char*>(command.c_str());
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
576
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
577 for (size_t i = 0; i < arguments.size(); i++)
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 args[i + 1] = const_cast<char*>(arguments[i].c_str());
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
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
582 args.back() = NULL;
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 int status;
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
585
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
586 #if defined(_WIN32)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
587 // http://msdn.microsoft.com/en-us/library/275khfab.aspx
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
588 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
589
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
590 #else
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
591 int pid = fork();
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
592
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
593 if (pid == -1)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
594 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
595 // Error in fork()
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2933
diff changeset
596 throw OrthancException(ErrorCode_SystemCommand, "Cannot fork a child process");
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
597 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
598 else if (pid == 0)
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 // Execute the system command in the child process
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
601 execvp(command.c_str(), &args[0]);
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 // We should never get here
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
604 _exit(1);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
605 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
606 else
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
607 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
608 // Wait for the system command to exit
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
609 waitpid(pid, &status, 0);
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 #endif
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 if (status != 0)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
614 {
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2933
diff changeset
615 throw OrthancException(ErrorCode_SystemCommand,
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2933
diff changeset
616 "System command failed with status code " +
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2933
diff changeset
617 boost::lexical_cast<std::string>(status));
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
618 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
619 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
620
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
621
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
622 int SystemToolbox::GetProcessId()
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
623 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
624 #if defined(_WIN32)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
625 return static_cast<int>(_getpid());
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
626 #else
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
627 return static_cast<int>(getpid());
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
628 #endif
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 bool SystemToolbox::IsRegularFile(const std::string& path)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
633 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
634 try
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
635 {
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
636 if (boost::filesystem::exists(path))
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
637 {
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
638 boost::filesystem::file_status status = boost::filesystem::status(path);
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
639 return (status.type() == boost::filesystem::regular_file ||
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
640 status.type() == boost::filesystem::reparse_file); // Fix BitBucket issue #11
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 }
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
643 catch (boost::filesystem::filesystem_error&)
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
644 {
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
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
647 return false;
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
648 }
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
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
651 FILE* SystemToolbox::OpenFile(const std::string& path,
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
652 FileMode mode)
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 #if defined(_WIN32)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
655 // 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
656 #endif
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
657
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
658 const char* m;
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
659 switch (mode)
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
660 {
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
661 case FileMode_ReadBinary:
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
662 m = "rb";
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
663 break;
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
664
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
665 case FileMode_WriteBinary:
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
666 m = "wb";
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
667 break;
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
668
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
669 default:
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
670 throw OrthancException(ErrorCode_ParameterOutOfRange);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
671 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
672
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
673 return fopen(path.c_str(), m);
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
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
676
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
677 static boost::posix_time::ptime GetNow(bool utc)
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
678 {
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
679 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
680 {
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 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
682 }
8cc3ca64a534 Orthanc now uses UTC (universal time) instead of local time in its database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
683 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
684 {
8cc3ca64a534 Orthanc now uses UTC (universal time) instead of local time in its database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
685 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
686 }
8cc3ca64a534 Orthanc now uses UTC (universal time) instead of local time in its database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
687 }
8cc3ca64a534 Orthanc now uses UTC (universal time) instead of local time in its database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
688
8cc3ca64a534 Orthanc now uses UTC (universal time) instead of local time in its database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
689
8cc3ca64a534 Orthanc now uses UTC (universal time) instead of local time in its database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
690 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
691 {
8cc3ca64a534 Orthanc now uses UTC (universal time) instead of local time in its database
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
692 return boost::posix_time::to_iso_string(GetNow(utc));
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
693 }
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
694
2367
2aff870c2c58 refactoring of BoostConfiguration.cmake
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2365
diff changeset
695
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
696 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
697 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
698 bool utc)
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
699 {
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
700 boost::posix_time::ptime now = GetNow(utc);
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
701 tm tm = boost::posix_time::to_tm(now);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
702
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
703 char s[32];
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
704 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
705 date.assign(s);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
706
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
707 // TODO milliseconds
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
708 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
709 time.assign(s);
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
710 }
2794
f790999a250a Semaphore removed from graveyard, SystemToolbox::GetHardwareConcurrency()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2512
diff changeset
711
f790999a250a Semaphore removed from graveyard, SystemToolbox::GetHardwareConcurrency()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2512
diff changeset
712
f790999a250a Semaphore removed from graveyard, SystemToolbox::GetHardwareConcurrency()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2512
diff changeset
713 unsigned int SystemToolbox::GetHardwareConcurrency()
f790999a250a Semaphore removed from graveyard, SystemToolbox::GetHardwareConcurrency()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2512
diff changeset
714 {
f790999a250a Semaphore removed from graveyard, SystemToolbox::GetHardwareConcurrency()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2512
diff changeset
715 // 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
716 // CPUs or cores or hyperthreading units)
f790999a250a Semaphore removed from graveyard, SystemToolbox::GetHardwareConcurrency()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2512
diff changeset
717 unsigned int threads = boost::thread::hardware_concurrency();
f790999a250a Semaphore removed from graveyard, SystemToolbox::GetHardwareConcurrency()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2512
diff changeset
718
4203
4d42408da117 improving const-correctness in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4188
diff changeset
719 if (threads == 0)
2794
f790999a250a Semaphore removed from graveyard, SystemToolbox::GetHardwareConcurrency()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2512
diff changeset
720 {
f790999a250a Semaphore removed from graveyard, SystemToolbox::GetHardwareConcurrency()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2512
diff changeset
721 return 1;
f790999a250a Semaphore removed from graveyard, SystemToolbox::GetHardwareConcurrency()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2512
diff changeset
722 }
f790999a250a Semaphore removed from graveyard, SystemToolbox::GetHardwareConcurrency()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2512
diff changeset
723 else
f790999a250a Semaphore removed from graveyard, SystemToolbox::GetHardwareConcurrency()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2512
diff changeset
724 {
f790999a250a Semaphore removed from graveyard, SystemToolbox::GetHardwareConcurrency()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2512
diff changeset
725 return threads;
f790999a250a Semaphore removed from graveyard, SystemToolbox::GetHardwareConcurrency()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2512
diff changeset
726 }
f790999a250a Semaphore removed from graveyard, SystemToolbox::GetHardwareConcurrency()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2512
diff changeset
727 }
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
728
5407
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
729 bool SystemToolbox::IsContentCompressible(MimeType mime)
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
730 {
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
731 switch (mime)
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
732 {
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
733 case MimeType_Css:
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
734 case MimeType_Html:
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
735 case MimeType_JavaScript:
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
736 case MimeType_Json:
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
737 case MimeType_Pam:
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
738 case MimeType_Pdf:
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
739 case MimeType_PlainText:
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
740 case MimeType_WebAssembly:
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
741 case MimeType_Xml:
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
742 case MimeType_PrometheusText:
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
743 case MimeType_DicomWebJson:
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
744 case MimeType_DicomWebXml:
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
745 return true;
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
746 default: // for all other (JPEG, DICOM, binary, ...)
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
747 return false;
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
748 }
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
749 }
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
750
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
751 bool SystemToolbox::IsContentCompressible(const std::string& contentType)
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
752 {
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
753 if (contentType.empty())
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
754 {
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
755 return false;
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
756 }
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
757
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
758 if (contentType.find(MIME_JSON) != std::string::npos ||
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
759 contentType.find(MIME_XML) != std::string::npos ||
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
760 contentType.find(MIME_DICOM_WEB_JSON) != std::string::npos ||
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
761 contentType.find(MIME_DICOM_WEB_XML) != std::string::npos ||
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
762 contentType.find(MIME_PDF) != std::string::npos ||
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
763 contentType.find(MIME_CSS) != std::string::npos ||
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
764 contentType.find(MIME_HTML) != std::string::npos ||
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
765 contentType.find(MIME_JAVASCRIPT) != std::string::npos ||
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
766 contentType.find(MIME_PLAIN_TEXT) != std::string::npos ||
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
767 contentType.find(MIME_WEB_ASSEMBLY) != std::string::npos ||
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
768 contentType.find(MIME_XML_2) != std::string::npos)
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
769 {
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
770 return true;
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
771 }
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
772
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
773 return false;
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5348
diff changeset
774 }
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
775
2908
9d277f8ad698 new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2906
diff changeset
776 MimeType SystemToolbox::AutodetectMimeType(const std::string& path)
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
777 {
5627
a7c88563c8cc fix build with boost 1.85.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5485
diff changeset
778 std::string extension = boost::filesystem::path(path).extension().string();
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
779 Toolbox::ToLowerCase(extension);
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
780
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
781 // http://en.wikipedia.org/wiki/Mime_types
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
782 // Text types
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
783 if (extension == ".txt")
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
784 {
2908
9d277f8ad698 new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2906
diff changeset
785 return MimeType_PlainText;
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
786 }
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
787 else if (extension == ".html")
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
788 {
2908
9d277f8ad698 new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2906
diff changeset
789 return MimeType_Html;
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
790 }
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
791 else if (extension == ".xml")
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
792 {
2908
9d277f8ad698 new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2906
diff changeset
793 return MimeType_Xml;
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
794 }
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
795 else if (extension == ".css")
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
796 {
2908
9d277f8ad698 new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2906
diff changeset
797 return MimeType_Css;
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
798 }
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
799
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
800 // Application types
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
801 else if (extension == ".js")
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
802 {
2908
9d277f8ad698 new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2906
diff changeset
803 return MimeType_JavaScript;
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
804 }
2984
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
805 else if (extension == ".json" ||
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
806 extension == ".nmf" /* manifest */)
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
807 {
2908
9d277f8ad698 new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2906
diff changeset
808 return MimeType_Json;
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
809 }
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
810 else if (extension == ".pdf")
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
811 {
2908
9d277f8ad698 new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2906
diff changeset
812 return MimeType_Pdf;
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
813 }
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
814 else if (extension == ".wasm")
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
815 {
2908
9d277f8ad698 new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2906
diff changeset
816 return MimeType_WebAssembly;
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
817 }
2984
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
818 else if (extension == ".nexe")
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
819 {
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
820 return MimeType_NaCl;
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
821 }
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
822 else if (extension == ".pexe")
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
823 {
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
824 return MimeType_PNaCl;
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
825 }
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
826
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
827 // Images types
4239
c8754c4c1862 upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4223
diff changeset
828 else if (extension == ".dcm")
c8754c4c1862 upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4223
diff changeset
829 {
c8754c4c1862 upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4223
diff changeset
830 return MimeType_Dicom;
c8754c4c1862 upload DICOM using WebDAV
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4223
diff changeset
831 }
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
832 else if (extension == ".jpg" ||
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
833 extension == ".jpeg")
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
834 {
2908
9d277f8ad698 new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2906
diff changeset
835 return MimeType_Jpeg;
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
836 }
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
837 else if (extension == ".gif")
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
838 {
2908
9d277f8ad698 new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2906
diff changeset
839 return MimeType_Gif;
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
840 }
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
841 else if (extension == ".png")
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
842 {
2908
9d277f8ad698 new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2906
diff changeset
843 return MimeType_Png;
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
844 }
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
845 else if (extension == ".pam")
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
846 {
2908
9d277f8ad698 new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2906
diff changeset
847 return MimeType_Pam;
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
848 }
2984
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
849 else if (extension == ".svg")
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
850 {
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
851 return MimeType_Svg;
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
852 }
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
853
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
854 // Various types
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
855 else if (extension == ".woff")
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
856 {
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
857 return MimeType_Woff;
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
858 }
3435
c08bb6ac3b7f new mime type: font/woff2
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3402
diff changeset
859 else if (extension == ".woff2")
c08bb6ac3b7f new mime type: font/woff2
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3402
diff changeset
860 {
c08bb6ac3b7f new mime type: font/woff2
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3402
diff changeset
861 return MimeType_Woff2;
c08bb6ac3b7f new mime type: font/woff2
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3402
diff changeset
862 }
4887
a29a6bdba9dd mime type for .ico
Alain Mazy <am@osimis.io>
parents: 4870
diff changeset
863 else if (extension == ".ico")
a29a6bdba9dd mime type for .ico
Alain Mazy <am@osimis.io>
parents: 4870
diff changeset
864 {
a29a6bdba9dd mime type for .ico
Alain Mazy <am@osimis.io>
parents: 4870
diff changeset
865 return MimeType_Ico;
a29a6bdba9dd mime type for .ico
Alain Mazy <am@osimis.io>
parents: 4870
diff changeset
866 }
4943
47d734fa30f6 adding function OrthancPluginRegisterWebDavCollection() to the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4887
diff changeset
867 else if (extension == ".gz")
47d734fa30f6 adding function OrthancPluginRegisterWebDavCollection() to the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4887
diff changeset
868 {
47d734fa30f6 adding function OrthancPluginRegisterWebDavCollection() to the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4887
diff changeset
869 return MimeType_Gzip;
47d734fa30f6 adding function OrthancPluginRegisterWebDavCollection() to the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4887
diff changeset
870 }
47d734fa30f6 adding function OrthancPluginRegisterWebDavCollection() to the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4887
diff changeset
871 else if (extension == ".zip")
47d734fa30f6 adding function OrthancPluginRegisterWebDavCollection() to the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4887
diff changeset
872 {
47d734fa30f6 adding function OrthancPluginRegisterWebDavCollection() to the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4887
diff changeset
873 return MimeType_Zip;
47d734fa30f6 adding function OrthancPluginRegisterWebDavCollection() to the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4887
diff changeset
874 }
5348
303e930fff0f "/tools/create-dicom" can now be used to create Encapsulated 3D Manufacturing Model IODs (MTL, OBJ, or STL)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
875 else if (extension == ".mtl")
303e930fff0f "/tools/create-dicom" can now be used to create Encapsulated 3D Manufacturing Model IODs (MTL, OBJ, or STL)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
876 {
303e930fff0f "/tools/create-dicom" can now be used to create Encapsulated 3D Manufacturing Model IODs (MTL, OBJ, or STL)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
877 return MimeType_Mtl;
303e930fff0f "/tools/create-dicom" can now be used to create Encapsulated 3D Manufacturing Model IODs (MTL, OBJ, or STL)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
878 }
303e930fff0f "/tools/create-dicom" can now be used to create Encapsulated 3D Manufacturing Model IODs (MTL, OBJ, or STL)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
879 else if (extension == ".obj")
303e930fff0f "/tools/create-dicom" can now be used to create Encapsulated 3D Manufacturing Model IODs (MTL, OBJ, or STL)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
880 {
303e930fff0f "/tools/create-dicom" can now be used to create Encapsulated 3D Manufacturing Model IODs (MTL, OBJ, or STL)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
881 return MimeType_Obj;
303e930fff0f "/tools/create-dicom" can now be used to create Encapsulated 3D Manufacturing Model IODs (MTL, OBJ, or STL)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
882 }
303e930fff0f "/tools/create-dicom" can now be used to create Encapsulated 3D Manufacturing Model IODs (MTL, OBJ, or STL)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
883 else if (extension == ".stl")
303e930fff0f "/tools/create-dicom" can now be used to create Encapsulated 3D Manufacturing Model IODs (MTL, OBJ, or STL)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
884 {
303e930fff0f "/tools/create-dicom" can now be used to create Encapsulated 3D Manufacturing Model IODs (MTL, OBJ, or STL)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
885 return MimeType_Stl;
303e930fff0f "/tools/create-dicom" can now be used to create Encapsulated 3D Manufacturing Model IODs (MTL, OBJ, or STL)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
886 }
2984
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
887
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
888 // Default type
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
889 else
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
890 {
2984
db8f360fcb41 OrthancPluginAutodetectMimeType()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2954
diff changeset
891 LOG(INFO) << "Unknown MIME type for extension \"" << extension << "\"";
2908
9d277f8ad698 new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2906
diff changeset
892 return MimeType_Binary;
2906
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
893 }
2a504fef4ed7 AutodetectMimeType() now using boost::filesystem
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2794
diff changeset
894 }
2920
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
895
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
896
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
897 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
898 {
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
899 env.clear();
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
900
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
901 for (char **p = environ; *p != NULL; p++)
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
902 {
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
903 std::string v(*p);
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
904 size_t pos = v.find('=');
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
905
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
906 if (pos != std::string::npos)
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
907 {
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
908 std::string key = v.substr(0, pos);
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
909 std::string value = v.substr(pos + 1);
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
910 env[key] = value;
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
911 }
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
912 }
ad0e7def3338 Toolbox::SubstituteVariables and SystemToolbox::GetEnvironmentVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
913 }
2933
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
914
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
915
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
916 std::string SystemToolbox::InterpretRelativePath(const std::string& baseDirectory,
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
917 const std::string& relativePath)
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
918 {
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
919 boost::filesystem::path base(baseDirectory);
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
920 boost::filesystem::path relative(relativePath);
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
921
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
922 /**
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
923 The following lines should be equivalent to this one:
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
924
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
925 return (base / relative).string();
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
926
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
927 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
928 make the proper path resolution when "baseDirectory" is an
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
929 absolute path. So, a hack is used below.
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
930 **/
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
931
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
932 if (relative.is_absolute())
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
933 {
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
934 return relative.string();
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
935 }
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
936 else
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
937 {
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
938 return (base / relative).string();
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
939 }
4a38d7d4f0e0 new class: OrthancConfiguration
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2920
diff changeset
940 }
4342
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
941
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
942
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
943 void SystemToolbox::ReadFileRange(std::string& content,
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
944 const std::string& path,
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
945 uint64_t start, // Inclusive
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
946 uint64_t end, // Exclusive
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
947 bool throwIfOverflow)
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
948 {
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
949 if (start > end)
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
950 {
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
951 throw OrthancException(ErrorCode_ParameterOutOfRange);
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
952 }
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
953
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
954 if (!IsRegularFile(path))
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
955 {
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
956 throw OrthancException(ErrorCode_RegularFileExpected,
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
957 "The path does not point to a regular file: " + path);
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
958 }
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
959
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
960 boost::filesystem::ifstream f;
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
961 f.open(path, std::ifstream::in | std::ifstream::binary);
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
962 if (!f.good())
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
963 {
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
964 throw OrthancException(ErrorCode_InexistentFile,
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
965 "File not found: " + path);
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
966 }
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
967
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
968 uint64_t fileSize = static_cast<uint64_t>(GetStreamSize(f));
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
969 if (end > fileSize)
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
970 {
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
971 if (throwIfOverflow)
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
972 {
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
973 throw OrthancException(ErrorCode_ParameterOutOfRange,
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
974 "Reading beyond the end of a file");
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
975 }
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
976 else
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
977 {
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
978 end = fileSize;
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
979 }
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
980 }
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
981
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
982 if (start <= end)
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
983 {
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
984 content.resize(static_cast<size_t>(end - start));
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
985
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
986 if (static_cast<uint64_t>(content.size()) != (end - start))
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
987 {
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
988 throw OrthancException(ErrorCode_InternalError,
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
989 "Reading a file that is too large for a 32bit architecture");
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
990 }
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
991
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
992 if (!content.empty())
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
993 {
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
994 f.seekg(start, std::ios::beg);
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
995 f.read(&content[0], static_cast<std::streamsize>(content.size()));
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
996 }
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
997 }
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
998 else
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
999 {
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
1000 content.clear();
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
1001 }
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
1002
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
1003 f.close();
52166629239f SystemToolbox::ReadFileRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
1004 }
4538
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1005
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1006
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1007 #if defined(_WIN32)
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1008 void SystemToolbox::GetMacAddresses(std::set<std::string>& target)
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1009 {
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1010 target.clear();
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1011
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1012 // 15Ko is the recommanded size to start with
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1013 std::vector<char> buffer(15 * 1024);
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1014
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1015 for (unsigned int iteration = 0; iteration < 3; iteration++)
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1016 {
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1017 ULONG outBufLen = static_cast<ULONG>(buffer.size());
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1018 DWORD result = GetAdaptersAddresses
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1019 (AF_UNSPEC, 0, NULL,
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1020 reinterpret_cast<IP_ADAPTER_ADDRESSES*>(&buffer[0]), &outBufLen);
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1021
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1022 if (result == NO_ERROR)
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1023 {
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1024 IP_ADAPTER_ADDRESSES* current =
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1025 reinterpret_cast<IP_ADAPTER_ADDRESSES*>(&buffer[0]);
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1026
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1027 while (current != NULL)
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1028 {
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1029 if (current->PhysicalAddressLength == 6 &&
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1030 (current->PhysicalAddress[0] != 0 ||
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1031 current->PhysicalAddress[1] != 0 ||
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1032 current->PhysicalAddress[2] != 0 ||
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1033 current->PhysicalAddress[3] != 0 ||
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1034 current->PhysicalAddress[4] != 0 ||
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1035 current->PhysicalAddress[5] != 0))
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1036 {
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1037 char tmp[32];
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1038 sprintf(tmp, "%02x:%02x:%02x:%02x:%02x:%02x",
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1039 (unsigned char) current->PhysicalAddress[0],
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1040 (unsigned char) current->PhysicalAddress[1],
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1041 (unsigned char) current->PhysicalAddress[2],
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1042 (unsigned char) current->PhysicalAddress[3],
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1043 (unsigned char) current->PhysicalAddress[4],
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1044 (unsigned char) current->PhysicalAddress[5]);
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1045 target.insert(tmp);
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1046 }
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1047
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1048 current = current->Next;
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1049 }
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1050
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1051 return;
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1052 }
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1053 else if (result != ERROR_BUFFER_OVERFLOW ||
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1054 iteration >= 3 ||
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1055 outBufLen == 0)
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1056 {
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1057 return;
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1058 }
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1059 else
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1060 {
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1061 buffer.resize(outBufLen);
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1062 iteration++;
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1063 }
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1064 }
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1065 }
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1066
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1067 #else
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1068 namespace
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1069 {
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1070 class SocketRaii : public boost::noncopyable
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1071 {
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1072 private:
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1073 int socket_;
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1074
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1075 public:
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1076 SocketRaii()
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1077 {
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1078 socket_ = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1079 }
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1080
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1081 ~SocketRaii()
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1082 {
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1083 if (socket_ != -1)
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1084 {
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1085 close(socket_);
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1086 }
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1087 }
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1088
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1089 int GetDescriptor() const
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1090 {
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1091 return socket_;
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1092 }
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1093 };
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1094
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1095
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1096 class NetworkInterfaces : public boost::noncopyable
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1097 {
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1098 private:
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1099 struct if_nameindex* list_;
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1100 struct if_nameindex* current_;
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1101
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1102 public:
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1103 NetworkInterfaces()
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1104 {
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1105 list_ = if_nameindex();
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1106 current_ = list_;
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1107 }
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1108
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1109 ~NetworkInterfaces()
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1110 {
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1111 if (list_ != NULL)
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1112 {
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1113 if_freenameindex(list_);
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1114 }
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1115 }
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1116
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1117 bool IsDone() const
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1118 {
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1119 return (current_ == NULL ||
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1120 (current_->if_index == 0 &&
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1121 current_->if_name == NULL));
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1122 }
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1123
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1124 const char* GetCurrentName() const
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1125 {
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1126 assert(!IsDone());
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1127 return current_->if_name;
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1128 }
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1129
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1130 unsigned int GetCurrentIndex() const
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1131 {
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1132 assert(!IsDone());
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1133 return current_->if_index;
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1134 }
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1135
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1136 void Next()
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1137 {
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1138 assert(!IsDone());
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1139 current_++;
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1140 }
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1141 };
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1142 }
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1143
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1144
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1145 void SystemToolbox::GetMacAddresses(std::set<std::string>& target)
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1146 {
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1147 target.clear();
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1148
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1149 SocketRaii socket;
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1150
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1151 if (socket.GetDescriptor() != 1)
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1152 {
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1153 NetworkInterfaces interfaces;
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1154
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1155 while (!interfaces.IsDone())
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1156 {
4552
beb8ba8a0b12 fix build on freebsd
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4538
diff changeset
1157 #if (defined(__APPLE__) && defined(__MACH__)) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__)
4538
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1158 int mib[6];
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1159 mib[0] = CTL_NET;
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1160 mib[1] = AF_ROUTE;
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1161 mib[2] = 0;
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1162 mib[3] = AF_LINK;
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1163 mib[4] = NET_RT_IFLIST;
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1164 mib[5] = interfaces.GetCurrentIndex();
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1165
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1166 size_t len;
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1167 if (sysctl(mib, 6, NULL, &len, NULL, 0) == 0 &&
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1168 len > 0)
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1169 {
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1170 std::string tmp;
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1171 tmp.resize(len);
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1172 if (sysctl(mib, 6, &tmp[0], &len, NULL, 0) == 0)
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1173 {
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1174 struct if_msghdr* ifm = reinterpret_cast<struct if_msghdr*>(&tmp[0]);
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1175 struct sockaddr_dl* sdl = reinterpret_cast<struct sockaddr_dl*>(ifm + 1);
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1176
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1177 if (sdl->sdl_type == IFT_ETHER) // Only consider Ethernet interfaces
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1178 {
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1179 const unsigned char* mac = reinterpret_cast<const unsigned char*>(LLADDR(sdl));
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1180 char tmp[32];
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1181 sprintf(tmp, "%02x:%02x:%02x:%02x:%02x:%02x",
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1182 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1183 target.insert(tmp);
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1184 }
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1185 }
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1186 }
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1187
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1188 #else
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1189 struct ifreq ifr;
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1190 strcpy(ifr.ifr_name, interfaces.GetCurrentName());
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1191
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1192 if (ioctl(socket.GetDescriptor(), SIOCGIFFLAGS, &ifr) == 0 &&
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1193 !(ifr.ifr_flags & IFF_LOOPBACK) && // ignore loopback interface
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1194 ioctl(socket.GetDescriptor(), SIOCGIFHWADDR, &ifr) == 0)
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1195 {
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1196 const unsigned char* mac = reinterpret_cast<const unsigned char*>(ifr.ifr_hwaddr.sa_data);
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1197
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1198 char tmp[32];
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1199 sprintf(tmp, "%02x:%02x:%02x:%02x:%02x:%02x",
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1200 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1201 target.insert(tmp);
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1202 }
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1203 #endif
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1204
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1205 interfaces.Next();
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1206 }
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1207 }
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1208 }
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1209
8a7523da6a9b added SystemToolbox::GetMacAddresses()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
1210 #endif
2143
fd5875662670 creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1211 }