annotate OrthancServer/Plugins/Samples/StorageArea/Plugin.cpp @ 5153:217863b09457 malloc-trim

malloc_trim doc
author Alain Mazy <am@osimis.io>
date Wed, 01 Feb 2023 19:23:58 +0100
parents 43e613a7756b
children 0ea402b4d901
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1136
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
1900
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1643
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
1288
6e7e5ed91c2d upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1136
diff changeset
4 * Department, University Hospital of Liege, Belgium
4870
43e613a7756b upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4831
diff changeset
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium
43e613a7756b upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4831
diff changeset
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
1136
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 *
1375
3d76e26b3865 fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1304
diff changeset
8 * This program is free software: you can redistribute it and/or
3d76e26b3865 fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1304
diff changeset
9 * modify it under the terms of the GNU General Public License as
3d76e26b3865 fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1304
diff changeset
10 * published by the Free Software Foundation, either version 3 of the
3d76e26b3865 fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1304
diff changeset
11 * License, or (at your option) any later version.
3d76e26b3865 fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1304
diff changeset
12 *
3d76e26b3865 fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1304
diff changeset
13 * This program is distributed in the hope that it will be useful, but
3d76e26b3865 fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1304
diff changeset
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
3d76e26b3865 fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1304
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3d76e26b3865 fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1304
diff changeset
16 * General Public License for more details.
1136
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 *
1375
3d76e26b3865 fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1304
diff changeset
18 * You should have received a copy of the GNU General Public License
3d76e26b3865 fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1304
diff changeset
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1136
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 **/
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
1425
97268448bdfc refactoring of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1375
diff changeset
23 #include <orthanc/OrthancCPlugin.h>
1136
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 #include <string.h>
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 #include <stdio.h>
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 #include <string>
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28
4483
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
29
1136
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 static OrthancPluginContext* context = NULL;
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 static std::string GetPath(const char* uuid)
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 {
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 return "plugin_" + std::string(uuid);
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 }
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38
4483
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
39 static bool ReadFile(std::string& content,
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
40 const std::string& path)
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
41 {
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
42 FILE* fp = fopen(path.c_str(), "rb");
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
43 if (!fp)
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
44 {
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
45 return false;
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
46 }
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
47
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
48 if (fseek(fp, 0, SEEK_END) < 0)
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
49 {
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
50 fclose(fp);
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
51 return false;
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
52 }
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
53
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
54 long size = ftell(fp);
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
55
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
56 if (fseek(fp, 0, SEEK_SET) < 0)
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
57 {
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
58 fclose(fp);
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
59 return false;
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
60 }
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
61 else
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
62 {
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
63 content.resize(size);
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
64
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
65 if (size != 0)
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
66 {
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
67 bool success = (fread(&content[0], size, 1, fp) == 1);
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
68 fclose(fp);
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
69 return success;
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
70 }
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
71 else
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
72 {
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
73 fclose(fp);
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
74 return true;
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
75 }
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
76 }
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
77 }
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
78
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
79
1627
da7854deb662 Plugin callbacks must now return explicit "OrthancPluginErrorCode" instead of integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
80 static OrthancPluginErrorCode StorageCreate(const char* uuid,
da7854deb662 Plugin callbacks must now return explicit "OrthancPluginErrorCode" instead of integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
81 const void* content,
da7854deb662 Plugin callbacks must now return explicit "OrthancPluginErrorCode" instead of integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
82 int64_t size,
da7854deb662 Plugin callbacks must now return explicit "OrthancPluginErrorCode" instead of integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
83 OrthancPluginContentType type)
1136
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 {
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 std::string path = GetPath(uuid);
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 FILE* fp = fopen(path.c_str(), "wb");
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 if (!fp)
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 {
1643
87c77b9b3679 provision for error codes in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1627
diff changeset
90 return OrthancPluginErrorCode_StorageAreaPlugin;
1136
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 }
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 bool ok = fwrite(content, size, 1, fp) == 1;
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 fclose(fp);
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95
1643
87c77b9b3679 provision for error codes in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1627
diff changeset
96 return ok ? OrthancPluginErrorCode_Success : OrthancPluginErrorCode_StorageAreaPlugin;
1136
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 }
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99
4483
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
100 #if USE_LEGACY_API == 1
1627
da7854deb662 Plugin callbacks must now return explicit "OrthancPluginErrorCode" instead of integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
101 static OrthancPluginErrorCode StorageRead(void** content,
da7854deb662 Plugin callbacks must now return explicit "OrthancPluginErrorCode" instead of integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
102 int64_t* size,
da7854deb662 Plugin callbacks must now return explicit "OrthancPluginErrorCode" instead of integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
103 const char* uuid,
da7854deb662 Plugin callbacks must now return explicit "OrthancPluginErrorCode" instead of integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
104 OrthancPluginContentType type)
1136
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 {
4483
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
106 const std::string path = GetPath(uuid);
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
107
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
108 std::string s;
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
109 if (ReadFile(s, path))
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
110 {
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
111 *size = s.size();
1136
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112
4483
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
113 if (s.size() == 0)
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
114 {
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
115 *content = NULL;
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
116 }
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
117 else
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
118 {
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
119 *content = malloc(s.size());
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
120 if (*content == NULL)
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
121 {
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
122 return OrthancPluginErrorCode_StorageAreaPlugin;
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
123 }
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
124
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
125 if (!s.empty())
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
126 {
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
127 memcpy(*content, s.c_str(), s.size());
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
128 }
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
129 }
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
130
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
131 return OrthancPluginErrorCode_Success;
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
132 }
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
133 else
1136
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 {
1643
87c77b9b3679 provision for error codes in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1627
diff changeset
135 return OrthancPluginErrorCode_StorageAreaPlugin;
1136
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 }
4483
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
137 }
1136
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138
4483
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
139 #else
1136
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140
4483
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
141 static OrthancPluginErrorCode StorageReadWhole(OrthancPluginMemoryBuffer64* target,
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
142 const char* uuid,
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
143 OrthancPluginContentType type)
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
144 {
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
145 const std::string path = GetPath(uuid);
1136
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146
4483
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
147 std::string s;
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
148 if (ReadFile(s, path))
1136
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 {
4483
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
150 if (OrthancPluginCreateMemoryBuffer64(context, target, s.size()) != OrthancPluginErrorCode_Success)
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
151 {
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
152 return OrthancPluginErrorCode_NotEnoughMemory;
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
153 }
1136
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154
4483
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
155 if (!s.empty())
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
156 {
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
157 memcpy(target->data, s.c_str(), s.size());
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
158 }
1136
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159
4483
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
160 return OrthancPluginErrorCode_Success;
1136
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 }
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 else
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 {
4483
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
164 return OrthancPluginErrorCode_StorageAreaPlugin;
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
165 }
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
166 }
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
167
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
168 static OrthancPluginErrorCode StorageReadRange(OrthancPluginMemoryBuffer64* target,
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
169 const char* uuid,
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
170 OrthancPluginContentType type,
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
171 uint64_t rangeStart)
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
172 {
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
173 const size_t rangeSize = target->size; // The buffer is allocated by Orthanc
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
174 const std::string path = GetPath(uuid);
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
175
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
176 std::string s;
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
177
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
178 if (rangeSize == 0)
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
179 {
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
180 return OrthancPluginErrorCode_Success;
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
181 }
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
182 else if (ReadFile(s, path))
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
183 {
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
184 if (rangeStart + rangeSize > s.size())
1136
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 {
4483
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
186 return OrthancPluginErrorCode_BadRange;
1136
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187 }
4483
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
188 else
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
189 {
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
190 memcpy(target->data, &s[rangeStart], rangeSize);
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
191 }
1136
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192
4483
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
193 return OrthancPluginErrorCode_Success;
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
194 }
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
195 else
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
196 {
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
197 return OrthancPluginErrorCode_StorageAreaPlugin;
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
198 }
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
199 }
1136
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200
4483
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
201 #endif
1136
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203
1627
da7854deb662 Plugin callbacks must now return explicit "OrthancPluginErrorCode" instead of integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
204 static OrthancPluginErrorCode StorageRemove(const char* uuid,
da7854deb662 Plugin callbacks must now return explicit "OrthancPluginErrorCode" instead of integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
205 OrthancPluginContentType type)
1136
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206 {
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207 std::string path = GetPath(uuid);
1627
da7854deb662 Plugin callbacks must now return explicit "OrthancPluginErrorCode" instead of integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
208
da7854deb662 Plugin callbacks must now return explicit "OrthancPluginErrorCode" instead of integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
209 if (remove(path.c_str()) == 0)
da7854deb662 Plugin callbacks must now return explicit "OrthancPluginErrorCode" instead of integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
210 {
da7854deb662 Plugin callbacks must now return explicit "OrthancPluginErrorCode" instead of integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
211 return OrthancPluginErrorCode_Success;
da7854deb662 Plugin callbacks must now return explicit "OrthancPluginErrorCode" instead of integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
212 }
da7854deb662 Plugin callbacks must now return explicit "OrthancPluginErrorCode" instead of integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
213 else
da7854deb662 Plugin callbacks must now return explicit "OrthancPluginErrorCode" instead of integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
214 {
1643
87c77b9b3679 provision for error codes in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1627
diff changeset
215 return OrthancPluginErrorCode_StorageAreaPlugin;
1627
da7854deb662 Plugin callbacks must now return explicit "OrthancPluginErrorCode" instead of integers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1425
diff changeset
216 }
1136
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217 }
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220 extern "C"
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221 {
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* c)
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223 {
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224 context = c;
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225 OrthancPluginLogWarning(context, "Storage plugin is initializing");
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
227 /* Check the version of the Orthanc core */
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
228 if (OrthancPluginCheckVersion(c) == 0)
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229 {
1304
7b6925b0890d cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
230 char info[1024];
1136
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231 sprintf(info, "Your version of Orthanc (%s) must be above %d.%d.%d to run this plugin",
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232 c->orthancVersion,
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER,
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234 ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER,
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235 ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER);
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 OrthancPluginLogError(context, info);
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237 return -1;
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238 }
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239
4483
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
240 #if USE_LEGACY_API == 1
1136
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241 OrthancPluginRegisterStorageArea(context, StorageCreate, StorageRead, StorageRemove);
4483
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
242 #else
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
243 OrthancPluginRegisterStorageArea2(context, StorageCreate, StorageReadWhole, StorageReadRange, StorageRemove);
a926f8995d0b sample for OrthancPluginRegisterStorageArea2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
244 #endif
1136
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
245
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246 return 0;
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247 }
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
248
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
249
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
250 ORTHANC_PLUGINS_API void OrthancPluginFinalize()
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
251 {
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
252 OrthancPluginLogWarning(context, "Storage plugin is finalizing");
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
253 }
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
254
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
255
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
256 ORTHANC_PLUGINS_API const char* OrthancPluginGetName()
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
257 {
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258 return "storage";
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
259 }
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
260
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
261
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
262 ORTHANC_PLUGINS_API const char* OrthancPluginGetVersion()
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
263 {
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
264 return "1.0";
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
265 }
208dc67b9bab sample custom storage plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
266 }