Mantid
Loading...
Searching...
No Matches
inverted_napi.cpp
Go to the documentation of this file.
1#include <string.h>
2
4#include "MantidNexus/NexusFile.h"
5#include "MantidNexus/inverted_napi.h"
6
7/* ----------------------------------------------------------------------
8
9 This is the inverted NAPI
10 The remaining functions are simply shims to call Nexus::File
11 They exist for no other reason than the tests in NapiUnutTest to run
12 Those tests persist to document how string data and attributes were
13 formerly loaded, in case a manual test raises a regression
14
15 --------------------------------------------------------------------- */
16
17NXstatus NXmakegroup(NXhandle fid, std::string const &name, std::string const &nxclass) {
18 try {
19 fid.makeGroup(name, nxclass, false);
20 } catch (Mantid::Nexus::Exception const &) {
21 return NXstatus::NX_ERROR;
22 }
23 return NXstatus::NX_OK;
24}
25
26/*------------------------------------------------------------------------*/
27
28NXstatus NXopengroup(NXhandle fid, std::string const &name, std::string const &nxclass) {
29 try {
30 fid.openGroup(name, nxclass);
31 } catch (Mantid::Nexus::Exception const &) {
32 return NXstatus::NX_ERROR;
33 }
34 return NXstatus::NX_OK;
35}
36
37/* ------------------------------------------------------------------- */
38
39NXstatus NXclosegroup(NXhandle fid) {
40 try {
41 fid.closeGroup();
42 } catch (Mantid::Nexus::Exception const &) {
43 return NXstatus::NX_ERROR;
44 }
45 return NXstatus::NX_OK;
46}
47
48/* --------------------------------------------------------------------- */
49
50NXstatus NXmakedata64(NXhandle fid, std::string const &name, NXnumtype const datatype, std::size_t const rank,
51 Mantid::Nexus::DimVector const &dims) {
52 UNUSED_ARG(rank);
53 try {
54 fid.makeData(name, datatype, dims, false);
55 } catch (Mantid::Nexus::Exception const &) {
56 return NXstatus::NX_ERROR;
57 }
58 return NXstatus::NX_OK;
59}
60
61/* --------------------------------------------------------------------- */
62
63NXstatus NXopendata(NXhandle fid, std::string const &name) {
64 try {
65 fid.openData(name);
66 } catch (Mantid::Nexus::Exception const &) {
67 return NXstatus::NX_ERROR;
68 }
69 return NXstatus::NX_OK;
70}
71
72/* ----------------------------------------------------------------- */
73
74NXstatus NXclosedata(NXhandle fid) {
75 try {
76 fid.closeData();
77 } catch (Mantid::Nexus::Exception const &) {
78 return NXstatus::NX_ERROR;
79 }
80 return NXstatus::NX_OK;
81}
82
83/* ------------------------------------------------------------------- */
84
85NXstatus NXputdata(NXhandle fid, char const *data) {
86 try {
87 fid.putData(std::string(data));
88 } catch (Mantid::Nexus::Exception const &) {
89 return NXstatus::NX_ERROR;
90 }
91 return NXstatus::NX_OK;
92}
93
94/* ------------------------------------------------------------------- */
95
96NXstatus NXputattr(NXhandle fid, std::string const &name, char const *data, std::size_t const datalen,
97 NXnumtype const iType) {
98 UNUSED_ARG(datalen);
99 UNUSED_ARG(iType);
100 try {
101 std::string value(data);
102 fid.putAttr(name, value);
103 } catch (Mantid::Nexus::Exception const &) {
104 return NXstatus::NX_ERROR;
105 }
106 return NXstatus::NX_OK;
107}
108
109/*-------------------------------------------------------------------------*/
110
111NXstatus NXgetdata(NXhandle fid, char *data) {
112 try {
113 fid.getData(data);
114 } catch (Mantid::Nexus::Exception const &) {
115 return NXstatus::NX_ERROR;
116 }
117 return NXstatus::NX_OK;
118}
119
120/*-------------------------------------------------------------------------*/
121
122NXstatus NXgetinfo64(NXhandle fid, std::size_t &rank, Mantid::Nexus::DimVector &dims, NXnumtype &iType) {
123 try {
124 Mantid::Nexus::Info info(fid.getInfo());
125 rank = info.dims.size();
126 dims = info.dims;
127 iType = info.type;
128 } catch (Mantid::Nexus::Exception const &) {
129 return NXstatus::NX_ERROR;
130 }
131 return NXstatus::NX_OK;
132}
133
134/*-------------------------------------------------------------------------*/
135
136NXstatus NXgetattr(NXhandle fid, std::string const &name, char *data, std::size_t &datalen, NXnumtype &iType) {
137 // NOTE this is written to mimic NXgetattr with readStringAttributeN
138 // readStringAttribute fetched a string value with correct handling of the length
139 // readStringAttributeN took that value and copied it with `strncpy(data, vdat, maxlen)`
140 // then for some reason set `data[maxlen - 1] = '\0'`, which truncated the result by one char
141 // and then NXgetattr set `datalen` to the `strlen` of the truncated result
142 try {
143 std::string value = fid.getStrAttr(name); // get correct string value
144 strncpy(data, value.c_str(), datalen); // copy first datalen chars
145 data[datalen - 1] = '\0'; // set a null terminator
146 datalen = strlen(data); // changevalue of datalen
147 iType = NXnumtype::CHAR;
148 } catch (Mantid::Nexus::Exception const &) {
149 return NXstatus::NX_ERROR;
150 }
151 return NXstatus::NX_OK;
152}
153
154/*--------------------------------------------------------------------
155 Ah yeah, everyone! It's NeXus Time! B-)
156 Chill out, relax, and let's party like it's stil 1989
157 ---------------------------------------------------------------------*/
std::string name
Definition Run.cpp:60
double value
The value of the point.
Definition FitMW.cpp:51
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
Definition System.h:48
Class that provides for a standard Nexus exception.
The primitive types published by this API.
static unsigned short constexpr CHAR
NXstatus NXclosegroup(NXhandle fid)
NXstatus NXputattr(NXhandle fid, std::string const &name, char const *data, std::size_t const datalen, NXnumtype const iType)
NXstatus NXgetdata(NXhandle fid, char *data)
NXstatus NXopengroup(NXhandle fid, std::string const &name, std::string const &nxclass)
NXstatus NXmakedata64(NXhandle fid, std::string const &name, NXnumtype const datatype, std::size_t const rank, Mantid::Nexus::DimVector const &dims)
NXstatus NXputdata(NXhandle fid, char const *data)
NXstatus NXclosedata(NXhandle fid)
NXstatus NXgetinfo64(NXhandle fid, std::size_t &rank, Mantid::Nexus::DimVector &dims, NXnumtype &iType)
NXstatus NXgetattr(NXhandle fid, std::string const &name, char *data, std::size_t &datalen, NXnumtype &iType)
NXstatus NXmakegroup(NXhandle fid, std::string const &name, std::string const &nxclass)
NXstatus NXopendata(NXhandle fid, std::string const &name)
std::vector< dimsize_t > DimVector
This structure holds the type and dimensions of a primative field/array.
DimVector dims
The dimensions of the file.
NXnumtype type
The primative type for the field.