comparison Plugins/Samples/StorageArea/Plugin.cpp @ 1136:208dc67b9bab

sample custom storage plugin
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 09 Sep 2014 16:47:04 +0200
parents
children 6e7e5ed91c2d
comparison
equal deleted inserted replaced
1135:67c3c1e4a6e0 1136:208dc67b9bab
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege,
4 * Belgium
5 *
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use, copy,
10 * modify, merge, publish, distribute, sublicense, and/or sell copies
11 * of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 **/
26
27
28 #include <OrthancCPlugin.h>
29
30 #include <string.h>
31 #include <stdio.h>
32 #include <string>
33
34 static OrthancPluginContext* context = NULL;
35
36
37 static std::string GetPath(const char* uuid)
38 {
39 return "plugin_" + std::string(uuid);
40 }
41
42
43 static int32_t StorageCreate(const char* uuid,
44 const void* content,
45 int64_t size,
46 OrthancPluginContentType type)
47 {
48 std::string path = GetPath(uuid);
49
50 FILE* fp = fopen(path.c_str(), "wb");
51 if (!fp)
52 {
53 return -1;
54 }
55
56 bool ok = fwrite(content, size, 1, fp) == 1;
57 fclose(fp);
58
59 return ok ? 0 : -1;
60 }
61
62
63 static int32_t StorageRead(void** content,
64 int64_t* size,
65 const char* uuid,
66 OrthancPluginContentType type)
67 {
68 std::string path = GetPath(uuid);
69
70 FILE* fp = fopen(path.c_str(), "rb");
71 if (!fp)
72 {
73 return -1;
74 }
75
76 if (fseek(fp, 0, SEEK_END) < 0)
77 {
78 fclose(fp);
79 return -1;
80 }
81
82 *size = ftell(fp);
83
84 if (fseek(fp, 0, SEEK_SET) < 0)
85 {
86 fclose(fp);
87 return -1;
88 }
89
90 bool ok = true;
91
92 if (*size == 0)
93 {
94 *content = NULL;
95 }
96 else
97 {
98 *content = malloc(*size);
99 if (content == NULL ||
100 fread(*content, *size, 1, fp) != 1)
101 {
102 ok = false;
103 }
104 }
105
106 fclose(fp);
107
108 return ok ? 0 : -1;
109 }
110
111
112 static int32_t StorageRemove(const char* uuid,
113 OrthancPluginContentType type)
114 {
115 std::string path = GetPath(uuid);
116 return remove(path.c_str());
117 }
118
119
120 extern "C"
121 {
122 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* c)
123 {
124 char info[1024];
125
126 context = c;
127 OrthancPluginLogWarning(context, "Storage plugin is initializing");
128
129 /* Check the version of the Orthanc core */
130 if (OrthancPluginCheckVersion(c) == 0)
131 {
132 sprintf(info, "Your version of Orthanc (%s) must be above %d.%d.%d to run this plugin",
133 c->orthancVersion,
134 ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER,
135 ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER,
136 ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER);
137 OrthancPluginLogError(context, info);
138 return -1;
139 }
140
141 OrthancPluginRegisterStorageArea(context, StorageCreate, StorageRead, StorageRemove);
142
143 return 0;
144 }
145
146
147 ORTHANC_PLUGINS_API void OrthancPluginFinalize()
148 {
149 OrthancPluginLogWarning(context, "Storage plugin is finalizing");
150 }
151
152
153 ORTHANC_PLUGINS_API const char* OrthancPluginGetName()
154 {
155 return "storage";
156 }
157
158
159 ORTHANC_PLUGINS_API const char* OrthancPluginGetVersion()
160 {
161 return "1.0";
162 }
163 }