Skip to content

Commit 2cf1df4

Browse files
committed
hds-v5: Add function hdsIsOpen
Needed so that the wrapper for hdsNew can determine if a file is already open prior to creating it.
1 parent 008fe4e commit 2cf1df4

File tree

6 files changed

+157
-0
lines changed

6 files changed

+157
-0
lines changed

Makefile.am

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ hdsDimF2C.c \
119119
hdsFree.c \
120120
hdsGroup.c \
121121
hdsInfoI.c \
122+
hdsIsOpen.c \
122123
hdsLock.c \
123124
hdsNew.c \
124125
hdsOpen.c \

hds.h

+7
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,13 @@ hdsGtune(const char *param_str, int *value, int *status);
10571057
int
10581058
hdsInfoI(const HDSLoc* locator, const char *topic_str, const char *extra, int *result, int *status);
10591059

1060+
/*=============================================*/
1061+
/* hdsIsOpen - Check if container file is open */
1062+
/*=============================================*/
1063+
1064+
int
1065+
hdsIsOpen(const char *file_str, int *result, int *status);
1066+
10601067
/*=================================*/
10611068
/* hdsLink - Link locator to group */
10621069
/*=================================*/

hdsIsOpen.c

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
*+
3+
* Name:
4+
* hdsIsOpen
5+
6+
* Purpose:
7+
* Check if a container file is already open in HDS V5.
8+
9+
* Language:
10+
* Starlink ANSI C
11+
12+
* Type of Module:
13+
* Library routine
14+
15+
* Invocation:
16+
* int hdsIsOpen( const char *file_str, int *isopen, int * status );
17+
18+
* Arguments:
19+
* file = const char * (Given)
20+
* Container file name. Use DAT__FLEXT (".h5sdf") if no suffix specified.
21+
* isopen = int * (Returned)
22+
* Pointer to a flag that is returned non-zero if the supplied
23+
* container file is already open within HDS V5.
24+
* status = int* (Given and Returned)
25+
* Pointer to global status.
26+
27+
* Description:
28+
* Checks if the supplied container file is already open within HDS V5.
29+
30+
* Returned Value:
31+
* int = inherited status on exit. This is for compatibility with the
32+
* original HDS API.
33+
34+
* Authors:
35+
* DSB: David S Berry (EAO):
36+
* {enter_new_authors_here}
37+
38+
* Notes:
39+
* - A file extension of DAT__FLEXT (".h5sdf") is the default.
40+
41+
* History:
42+
* 2020-10-15 (DSB):
43+
* Initial version
44+
* {enter_further_changes_here}
45+
46+
* Copyright:
47+
* Copyright (C) 2020 East Asian Observatory.
48+
* All Rights Reserved.
49+
50+
* Licence:
51+
* Redistribution and use in source and binary forms, with or
52+
* without modification, are permitted provided that the following
53+
* conditions are met:
54+
*
55+
* - Redistributions of source code must retain the above copyright
56+
* notice, this list of conditions and the following disclaimer.
57+
*
58+
* - Redistributions in binary form must reproduce the above
59+
* copyright notice, this list of conditions and the following
60+
* disclaimer in the documentation and/or other materials
61+
* provided with the distribution.
62+
*
63+
* - Neither the name of the {organization} nor the names of its
64+
* contributors may be used to endorse or promote products
65+
* derived from this software without specific prior written
66+
* permission.
67+
*
68+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
69+
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
70+
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
71+
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
72+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
73+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
74+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
75+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
76+
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
77+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
78+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
79+
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
80+
* THE POSSIBILITY OF SUCH DAMAGE.
81+
82+
* Bugs:
83+
* {note_any_bugs_here}
84+
*-
85+
*/
86+
87+
#include <stdlib.h>
88+
#include <string.h>
89+
#include <unistd.h>
90+
91+
#include "hds1.h"
92+
#include "dat1.h"
93+
#include "ems.h"
94+
#include "dat_err.h"
95+
#include "hds.h"
96+
#include "sae_par.h"
97+
98+
#include "star/one.h"
99+
100+
#include "hdf5.h"
101+
102+
int hdsIsOpen(const char *file_str, int *isopen, int *status) {
103+
104+
char *fname = NULL;
105+
106+
/* Returns the inherited status for compatibility reasons */
107+
if (*status != SAI__OK) return *status;
108+
109+
/* Configure the HDF5 library for our needs as this routine could be called
110+
before any others. */
111+
dat1InitHDF5();
112+
113+
/* Create buffer for file name so that we include the file extension */
114+
fname = dau1CheckFileName( file_str, status );
115+
116+
/* Check to see if the file is currently open. */
117+
*isopen = hds1IsOpen( fname, status );
118+
119+
/* Free allocated resource */
120+
CLEANUP:
121+
if (fname) MEM_FREE(fname);
122+
123+
return *status;
124+
}
125+
126+

hdsTest.c

+15
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ int main (void) {
110110
char buffer[1024]; /* plenty large enough */
111111
double darr[] = { 4.5, 2.5 };
112112
const hdsbool_t boolarr[] = { 1, 0, 1 };
113+
int isopen;
113114
double retdarr[2];
114115
void *mapv; /* Mapped void* */
115116
double *mapd; /* Mapped _DOUBLE */
@@ -674,9 +675,23 @@ int main (void) {
674675
printf("Query Locator status:\n");
675676
hdsShow("LOCATORS", &status);
676677

678+
/* Check it is now closed */
679+
hdsIsOpen( path, &isopen, &status );
680+
if( isopen && status == SAI__OK ) {
681+
status = DAT__FATAL;
682+
emsRepf( "SIZE","File %s should now be closed, but it isn't", &status, path);
683+
}
684+
677685
/* Re-open */
678686
hdsOpen( path, "UPDATE", &loc1, &status );
679687

688+
/* Check it is now open */
689+
hdsIsOpen( path, &isopen, &status );
690+
if( !isopen && status == SAI__OK ) {
691+
status = DAT__FATAL;
692+
emsRepf( "SIZE","File %s should now be open, but it isn't", &status, path);
693+
}
694+
680695
/* Look for the data array and map it */
681696
datFind( loc1, "DATA_ARRAY", &loc2, &status );
682697
printf("Query files after reopen:\n");

hds_v5.h

+7
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,13 @@ hdsLock_v5(const HDSLoc *locator, int *status);
10701070
int
10711071
hdsNew_v5(const char *file_str, const char *name_str, const char *type_str, int ndim, const hdsdim dims[], HDSLoc **locator, int *status);
10721072

1073+
/*=============================================*/
1074+
/* hdsIsOpen - Check if container file is open */
1075+
/*=============================================*/
1076+
1077+
int
1078+
hdsIsOpen_v5(const char *file_str, int *isopen, int *status);
1079+
10731080
/*========================================*/
10741081
/* hdsOpen - Open existing container file */
10751082
/*========================================*/

hds_v5_map.h

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@
146146
#define hdsInfoI hdsInfoI_v5
147147
#define hdsLink hdsLink_v5
148148
#define hdsLock hdsLock_v5
149+
#define hdsIsOpen hdsIsOpen_v5
149150
#define hdsNew hdsNew_v5
150151
#define hdsOpen hdsOpen_v5
151152
#define hdsShow hdsShow_v5

0 commit comments

Comments
 (0)