NeXus  4.4.3
 All Data Structures Files Functions Variables Typedefs Macros Pages
napi_fortran_helper.c
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------
2  NeXus - Neutron & X-ray Common Data Format
3 
4  Application Program Interface Routines
5 
6  Copyright (C) 2015 NeXus International Advisory Committee
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Lesser General Public
10  License as published by the Free Software Foundation; either
11  version 2 of the License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public
19  License along with this library; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 
22  For further information, see <http://www.nexusformat.org>
23 
24 ----------------------------------------------------------------------------*/
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <string.h>
30 
31 #include <napi_fortran_helper.h>
32 #include <napi_internal.h>
33 #include "nxstack.h"
34 
35 /*----------------------------------------------------------------------
36  F77 - API - Support - Routines
37  ----------------------------------------------------------------------*/
38  /*
39  * We store the whole of the NeXus file in the array - that way
40  * we can just pass the array name to C as it will be a valid
41  * NXhandle. We could store the NXhandle value in the FORTRAN array
42  * instead, but that would mean writing far more wrappers
43  */
44 NXstatus NXfopen(char *filename, NXaccess * am, NXhandle pHandle)
45 {
46  NXstatus ret;
47  NXhandle fileid = NULL;
48  ret = NXopen(filename, *am, &fileid);
49  if (ret == NX_OK) {
50  memcpy(pHandle, fileid, getFileStackSize());
51  } else {
52  memset(pHandle, 0, getFileStackSize());
53  }
54  if (fileid != NULL) {
55  free(fileid);
56  }
57  return ret;
58 }
59 
60 /*
61  * The pHandle from FORTRAN is a pointer to a static FORTRAN
62  * array holding the NexusFunction structure. We need to malloc()
63  * a temporary copy as NXclose will try to free() this
64  */
65 NXstatus NXfclose(NXhandle pHandle)
66 {
67  NXhandle h;
68  NXstatus ret;
69  h = (NXhandle) malloc(getFileStackSize());
70  memcpy(h, pHandle, getFileStackSize());
71  ret = NXclose(&h); /* does free(h) */
72  memset(pHandle, 0, getFileStackSize());
73  return ret;
74 }
75 
76 /*---------------------------------------------------------------------*/
77 NXstatus NXfflush(NXhandle pHandle)
78 {
79  NXhandle h;
80  NXstatus ret;
81  h = (NXhandle) malloc(getFileStackSize());
82  memcpy(h, pHandle, getFileStackSize());
83  ret = NXflush(&h); /* modifies and reallocates h */
84  memcpy(pHandle, h, getFileStackSize());
85  return ret;
86 }
87 
88 /*----------------------------------------------------------------------*/
89 NXstatus NXfmakedata(NXhandle fid, char *name, int *pDatatype,
90  int *pRank, int dimensions[])
91 {
92  NXstatus ret;
93  static char buffer[256];
94  int i, *reversed_dimensions;
95  reversed_dimensions = (int *)malloc(*pRank * sizeof(int));
96  if (reversed_dimensions == NULL) {
97  sprintf(buffer,
98  "ERROR: Cannot allocate space for array rank of %d in NXfmakedata",
99  *pRank);
100  NXReportError(buffer);
101  return NX_ERROR;
102  }
103 /*
104  * Reverse dimensions array as FORTRAN is column major, C row major
105  */
106  for (i = 0; i < *pRank; i++) {
107  reversed_dimensions[i] = dimensions[*pRank - i - 1];
108  }
109  ret = NXmakedata(fid, name, *pDatatype, *pRank, reversed_dimensions);
110  free(reversed_dimensions);
111  return ret;
112 }
113 
114 /*-----------------------------------------------------------------------*/
115 NXstatus NXfcompmakedata(NXhandle fid, char *name,
116  int *pDatatype,
117  int *pRank, int dimensions[],
118  int *compression_type, int chunk[])
119 {
120  NXstatus ret;
121  static char buffer[256];
122  int i, *reversed_dimensions, *reversed_chunk;
123  reversed_dimensions = (int *)malloc(*pRank * sizeof(int));
124  reversed_chunk = (int *)malloc(*pRank * sizeof(int));
125  if (reversed_dimensions == NULL || reversed_chunk == NULL) {
126  sprintf(buffer,
127  "ERROR: Cannot allocate space for array rank of %d in NXfcompmakedata",
128  *pRank);
129  NXReportError(buffer);
130  return NX_ERROR;
131  }
132 /*
133  * Reverse dimensions array as FORTRAN is column major, C row major
134  */
135  for (i = 0; i < *pRank; i++) {
136  reversed_dimensions[i] = dimensions[*pRank - i - 1];
137  reversed_chunk[i] = chunk[*pRank - i - 1];
138  }
139  ret = NXcompmakedata(fid, name, *pDatatype, *pRank,
140  reversed_dimensions, *compression_type,
141  reversed_chunk);
142  free(reversed_dimensions);
143  free(reversed_chunk);
144  return ret;
145 }
146 
147 /*-----------------------------------------------------------------------*/
148 NXstatus NXfcompress(NXhandle fid, int *compr_type)
149 {
150  return NXcompress(fid, *compr_type);
151 }
152 
153 /*-----------------------------------------------------------------------*/
154 NXstatus NXfputattr(NXhandle fid, const char *name, const void *data,
155  int *pDatalen, int *pIType)
156 {
157  return NXputattr(fid, name, data, *pDatalen, *pIType);
158 }
159 
160  /*
161  * implement snprintf when it is not available
162  */
163 int nxisnprintf(char *buffer, int len, const char *format, ...)
164 {
165  int ret;
166  va_list valist;
167  va_start(valist, format);
168  ret = vsprintf(buffer, format, valist);
169  va_end(valist);
170  return ret;
171 }
172 
173 /*--------------------------------------------------------------------------*/
174 NXstatus NXfgetpath(NXhandle fid, char *path, int *pathlen)
175 {
176  return NXgetpath(fid, path, *pathlen);
177 }
NXstatus NXgetpath(NXhandle fid, char *path, int pathlen)
Definition: napi.c:1936
#define NXfcompmakedata
#define NXfputattr
NXstatus NXopen(CONSTCHAR *userfilename, NXaccess am, NXhandle *gHandle)
Definition: napi.c:463
NXstatus NXputattr(NXhandle fid, CONSTCHAR *name, const void *data, int datalen, int iType)
Definition: napi.c:1016
#define NXfopen
NXstatus NXcompmakedata(NXhandle fid, CONSTCHAR *name, int datatype, int rank, int dimensions[], int compress_type, int chunk_size[])
Definition: napi.c:887
int nxisnprintf(char *buffer, int len, const char *format,...)
#define NXfclose
#define NXfcompress
#define NXfflush
NXstatus NXflush(NXhandle *pHandle)
Definition: napi.c:1111
NXstatus NXcompress(NXhandle fid, int compress_type)
Definition: napi.c:923
NXstatus NXclose(NXhandle *fid)
Definition: napi.c:688
void NXReportError(char *string)
Definition: napi.c:305
#define NXfgetpath
int getFileStackSize()
Definition: nxstack.c:69
NXstatus NXmakedata(NXhandle fid, CONSTCHAR *name, int datatype, int rank, int dimensions[])
Definition: napi.c:858
#define NXfmakedata