comparison OrthancServer/Plugins/Samples/StorageArea/Plugin.cpp @ 4483:a926f8995d0b

sample for OrthancPluginRegisterStorageArea2()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 28 Jan 2021 16:59:40 +0100
parents d9473bd5ed43
children 5b929e6b3c36
comparison
equal deleted inserted replaced
4482:8efeaba1b7f9 4483:a926f8995d0b
23 23
24 #include <string.h> 24 #include <string.h>
25 #include <stdio.h> 25 #include <stdio.h>
26 #include <string> 26 #include <string>
27 27
28
29 #define USE_LEGACY_API 0
30
31
28 static OrthancPluginContext* context = NULL; 32 static OrthancPluginContext* context = NULL;
29 33
30 34
31 static std::string GetPath(const char* uuid) 35 static std::string GetPath(const char* uuid)
32 { 36 {
33 return "plugin_" + std::string(uuid); 37 return "plugin_" + std::string(uuid);
38 }
39
40
41 static bool ReadFile(std::string& content,
42 const std::string& path)
43 {
44 FILE* fp = fopen(path.c_str(), "rb");
45 if (!fp)
46 {
47 return false;
48 }
49
50 if (fseek(fp, 0, SEEK_END) < 0)
51 {
52 fclose(fp);
53 return false;
54 }
55
56 long size = ftell(fp);
57
58 if (fseek(fp, 0, SEEK_SET) < 0)
59 {
60 fclose(fp);
61 return false;
62 }
63 else
64 {
65 content.resize(size);
66
67 if (size != 0)
68 {
69 bool success = (fread(&content[0], size, 1, fp) == 1);
70 fclose(fp);
71 return success;
72 }
73 else
74 {
75 fclose(fp);
76 return true;
77 }
78 }
34 } 79 }
35 80
36 81
37 static OrthancPluginErrorCode StorageCreate(const char* uuid, 82 static OrthancPluginErrorCode StorageCreate(const char* uuid,
38 const void* content, 83 const void* content,
52 97
53 return ok ? OrthancPluginErrorCode_Success : OrthancPluginErrorCode_StorageAreaPlugin; 98 return ok ? OrthancPluginErrorCode_Success : OrthancPluginErrorCode_StorageAreaPlugin;
54 } 99 }
55 100
56 101
102 #if USE_LEGACY_API == 1
57 static OrthancPluginErrorCode StorageRead(void** content, 103 static OrthancPluginErrorCode StorageRead(void** content,
58 int64_t* size, 104 int64_t* size,
59 const char* uuid, 105 const char* uuid,
60 OrthancPluginContentType type) 106 OrthancPluginContentType type)
61 { 107 {
62 std::string path = GetPath(uuid); 108 const std::string path = GetPath(uuid);
63 109
64 FILE* fp = fopen(path.c_str(), "rb"); 110 std::string s;
65 if (!fp) 111 if (ReadFile(s, path))
66 { 112 {
67 return OrthancPluginErrorCode_StorageAreaPlugin; 113 *size = s.size();
68 } 114
69 115 if (s.size() == 0)
70 if (fseek(fp, 0, SEEK_END) < 0) 116 {
71 { 117 *content = NULL;
72 fclose(fp); 118 }
73 return OrthancPluginErrorCode_StorageAreaPlugin; 119 else
74 } 120 {
75 121 *content = malloc(s.size());
76 *size = ftell(fp); 122 if (*content == NULL)
77 123 {
78 if (fseek(fp, 0, SEEK_SET) < 0) 124 return OrthancPluginErrorCode_StorageAreaPlugin;
79 { 125 }
80 fclose(fp); 126
81 return OrthancPluginErrorCode_StorageAreaPlugin; 127 if (!s.empty())
82 } 128 {
83 129 memcpy(*content, s.c_str(), s.size());
84 bool ok = true; 130 }
85 131 }
86 if (*size == 0) 132
87 { 133 return OrthancPluginErrorCode_Success;
88 *content = NULL; 134 }
89 } 135 else
90 else 136 {
91 { 137 return OrthancPluginErrorCode_StorageAreaPlugin;
92 *content = malloc(*size); 138 }
93 if (*content == NULL || 139 }
94 fread(*content, *size, 1, fp) != 1) 140
95 { 141 #else
96 ok = false; 142
97 } 143 static OrthancPluginErrorCode StorageReadWhole(OrthancPluginMemoryBuffer64* target,
98 } 144 const char* uuid,
99 145 OrthancPluginContentType type)
100 fclose(fp); 146 {
101 147 const std::string path = GetPath(uuid);
102 return ok ? OrthancPluginErrorCode_Success : OrthancPluginErrorCode_StorageAreaPlugin; 148
103 } 149 std::string s;
150 if (ReadFile(s, path))
151 {
152 if (OrthancPluginCreateMemoryBuffer64(context, target, s.size()) != OrthancPluginErrorCode_Success)
153 {
154 return OrthancPluginErrorCode_NotEnoughMemory;
155 }
156
157 if (!s.empty())
158 {
159 memcpy(target->data, s.c_str(), s.size());
160 }
161
162 return OrthancPluginErrorCode_Success;
163 }
164 else
165 {
166 return OrthancPluginErrorCode_StorageAreaPlugin;
167 }
168 }
169
170 static OrthancPluginErrorCode StorageReadRange(OrthancPluginMemoryBuffer64* target,
171 const char* uuid,
172 OrthancPluginContentType type,
173 uint64_t rangeStart)
174 {
175 const size_t rangeSize = target->size; // The buffer is allocated by Orthanc
176 const std::string path = GetPath(uuid);
177
178 std::string s;
179
180 if (rangeSize == 0)
181 {
182 return OrthancPluginErrorCode_Success;
183 }
184 else if (ReadFile(s, path))
185 {
186 if (rangeStart + rangeSize > s.size())
187 {
188 return OrthancPluginErrorCode_BadRange;
189 }
190 else
191 {
192 memcpy(target->data, &s[rangeStart], rangeSize);
193 }
194
195 return OrthancPluginErrorCode_Success;
196 }
197 else
198 {
199 return OrthancPluginErrorCode_StorageAreaPlugin;
200 }
201 }
202
203 #endif
104 204
105 205
106 static OrthancPluginErrorCode StorageRemove(const char* uuid, 206 static OrthancPluginErrorCode StorageRemove(const char* uuid,
107 OrthancPluginContentType type) 207 OrthancPluginContentType type)
108 { 208 {
137 ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER); 237 ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER);
138 OrthancPluginLogError(context, info); 238 OrthancPluginLogError(context, info);
139 return -1; 239 return -1;
140 } 240 }
141 241
242 #if USE_LEGACY_API == 1
142 OrthancPluginRegisterStorageArea(context, StorageCreate, StorageRead, StorageRemove); 243 OrthancPluginRegisterStorageArea(context, StorageCreate, StorageRead, StorageRemove);
244 #else
245 OrthancPluginRegisterStorageArea2(context, StorageCreate, StorageReadWhole, StorageReadRange, StorageRemove);
246 #endif
143 247
144 return 0; 248 return 0;
145 } 249 }
146 250
147 251