Skip to content

Commit

Permalink
hds: Refactor datCopy and datCcopy code
Browse files Browse the repository at this point in the history
+ Realise that all the _v4 _v5 macro shenanigans are not
  needed as hds_select.c will do the right thing anyhow.
+ Break datCopyXtoY.c into separate files to allow
  other routines to call them.
+ datCcopy-like routine now used for first level of copying.
  This simplifies the code quite a lot.
  • Loading branch information
timj committed Oct 31, 2014
1 parent f537e63 commit 31790f2
Show file tree
Hide file tree
Showing 14 changed files with 593 additions and 401 deletions.
5 changes: 3 additions & 2 deletions libraries/hds/Makefile.am
Expand Up @@ -79,9 +79,10 @@ F_C_ROUTINES = hds_run.c fortran_interface.c hdsFind.c
BLOCK_DATA_FILES = cmp_blk.f

C_ROUTINES = \
dat1CcopyLocXtoY.c dat1CcopyXtoY.c \
dat1CopyPrimXtoY.c dat1CopyStrucXtoY.c dat1CopyXtoY.c \
datExportFloc.c datImportFloc.c dat1_import_floc.c hdsdim.c \
hds_select.c datCopy5to4.c datCopy4to5.c \
datCcopy5to4.c datCcopy4to5.c
hds_select.c

DAT_PAR: dat_par_f$(EXEEXT)
./dat_par_f > DAT_PAR
Expand Down
17 changes: 11 additions & 6 deletions libraries/hds/dat1.h
Expand Up @@ -62,23 +62,28 @@
/* compiler to be able to know how to read the version. */
#define HDS_USE_INTERNAL_STRUCT 1
typedef struct LOC { int hds_version; } HDSLoc;
#include "hds_types.h"

HDSLoc *
dat1_import_floc ( const char flocator[DAT__SZLOC], int loc_length, int * status);

int
datCopy5to4(const HDSLoc *locatorX, const HDSLoc *locatorY, const char *name_c, int *status);
dat1CopyXtoY(const HDSLoc *locatorX, const HDSLoc *locatorY, const char *name_c, int *status);

int
datCopy4to5(const HDSLoc *locatorX, const HDSLoc *locatorY, const char *name_c, int *status);
dat1CcopyXtoY(const HDSLoc *locator1X, const HDSLoc *locator2Y, const char *name,
HDSLoc **locator3Y, int *status );

HDSLoc *
dat1CcopyLocXtoY(const HDSLoc *locatorX, const HDSLoc *locatorY, const char *name,
hdsbool_t * struc, int *status );

int
datCcopy4to5(const HDSLoc *locator1X, const HDSLoc *locator2Y, const char *name,
HDSLoc **locator3Y, int *status );
dat1CopyPrimXtoY( const HDSLoc *locatorX, HDSLoc *locatorY, int *status );

int
datCcopy5to4(const HDSLoc *locator1X, const HDSLoc *locator2Y, const char *name,
HDSLoc **locator3Y, int *status );
dat1CopyStrucXtoY( const HDSLoc *locatorX, const HDSLoc *locatorY,
int *status );

#endif

133 changes: 133 additions & 0 deletions libraries/hds/dat1CcopyLocXtoY.c
@@ -0,0 +1,133 @@
/*
*+
* Name:
* dat1CcopyLocXtoY
* Purpose:
* Variant of datCcopy that returns the locator
* Language:
* Starlink ANSI C
* Type of Module:
* Library routine
* Invocation:
* loc = dat1CcopyLocXtoY(const HDSLoc *locatorX, const HDSLoc *locatorY, const char *name,
* hdsbool_t * struc, int *status ) {( int * status );
* Arguments:
* locator1X = const HDSLoc * (Given)
* Object locator to copy. In version X.
* locator2Y = const HDSLoc * (Given)
* Locator of structure to receive copy of object. Structure is in
* a version Y file.
* name = const char * (Given)
* Name of object when copied into structure.
* struc = hdsbool_t * (Returned)
* True if a structure was created, false if a primitive was created.
* status = int* (Given and Returned)
* Pointer to global status.
* Description:
* Identical to dat1CcopyXtoY except that the interface is changed such that a locator
* is returned directly and a boolean argument is set to indicate whether a structure
* or primitive was created.
* Returned Value:
* loc = HDSLoc *
* Locator to the object that was created.
* Authors:
* TIMJ: Tim Jenness (Cornell)
* {enter_new_authors_here}
* Notes:
* - Will work even if the two locators are from different versions.
* History:
* 2014-10-31 (TIMJ):
* Initial version
* {enter_further_changes_here}
* Copyright:
* Copyright (C) 2014 Cornell University
* All Rights Reserved.
* Licence:
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* - Neither the name of the {organization} nor the names of its
* contributors may be used to endorse or promote products
* derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
* Bugs:
* {note_any_bugs_here}
*-
*/

#include "ems.h"
#include "sae_par.h"

#include "dat1.h"
#include "hds.h"

HDSLoc *
dat1CcopyLocXtoY(const HDSLoc *locatorX, const HDSLoc *locatorY, const char *name,
hdsbool_t * struc, int *status ) {
char type_str[DAT__SZTYP+1];
hdsdim hdims[DAT__MXDIM];
int ndims;
HDSLoc *outloc = NULL;
hdsbool_t isstruct = 0;

if (*status != SAI__OK) return NULL;

/* Always create the output and get a locator to it*/
datType( locatorX, type_str, status );
datShape( locatorX, DAT__MXDIM, hdims, &ndims, status );
datNew( locatorY, name, type_str, ndims, hdims, status );
datFind( locatorY, name, &outloc, status );

/* What happens next depends on whether we have a structure
as we only do more if we have a primitive that is defined. */
datStruc( locatorX, &isstruct, status );

if (!isstruct) {
hdsbool_t state = 0;
/* We only copy if the primitive object is defined */
datState( locatorX, &state, status );
if ( state ) dat1CopyPrimXtoY( locatorX, outloc, status );
}

if (*status != SAI__OK) {
if (outloc) datAnnul(&outloc, status);
}
if (struc) *struc = isstruct;
return outloc;
}
48 changes: 8 additions & 40 deletions libraries/hds/datCcopyXtoY.c → libraries/hds/dat1CcopyXtoY.c
@@ -1,7 +1,7 @@
/*
*+
* Name:
* datCcopyXtoY
* dat1CcopyXtoY
* Purpose:
* Copy one structure level from version X locator to version Y locator
Expand All @@ -13,8 +13,8 @@
* Library routine
* Invocation:
* datCcopyXtoY(const HDSLoc *locator1X, const HDSLoc *locator2Y, const char *name,
HDSLoc **locator3Y, int *status );
* dat1CcopyXtoY(const HDSLoc *locator1X, const HDSLoc *locator2Y, const char *name,
* HDSLoc **locator3Y, int *status );
* Arguments:
* locator1X = const HDSLoc * (Given)
Expand All @@ -40,6 +40,8 @@
* {enter_new_authors_here}
* Notes:
* - Use datCcopy directly when the source and target locator
* are from the same version.
* - When copying primitive types the data will be mapped and
* copied from one location to another.
* - In general this routine will be no less efficient than
Expand Down Expand Up @@ -75,46 +77,12 @@

#include "sae_par.h"
#include "dat1.h"
#include "star/hds_v4.h"
#include "star/hds_v5.h"
#include "hds.h"

int
datCcopyXtoY(const HDSLoc *locator1X, const HDSLoc *locator2Y, const char *name,
dat1CcopyXtoY(const HDSLoc *locator1X, const HDSLoc *locator2Y, const char *name,
HDSLoc **locator3Y, int *status ) {
char type_str[DAT__SZTYP+1];
hdsdim hdims[DAT__MXDIM];
int ndims;
hdsbool_t struc = 0;

if (*status != SAI__OK) return *status;

datStruc_vX( locator1X, &struc, status );

if (struc) {

/* need the type and dimensionality of the structure to create
in new location */
datType_vX( locator1X, type_str, status );
datShape_vX( locator1X, DAT__MXDIM, hdims, &ndims, status );
datNew_vY( locator2Y, name, type_str, ndims, hdims, status );

} else {
hdsbool_t state = 0;
/* We only copy if the primitive object is defined */
datState_vX( locator1X, &state, status );
if ( state ) {
datCopyXtoY( locator1X, locator2Y, name, status );
} else {
/* Undefined so just make something of the right shape and type */
datType_vX( locator1X, type_str, status );
datShape_vX( locator1X, DAT__MXDIM, hdims, &ndims, status );
datNew_vY( locator2Y, name, type_str, ndims, hdims, status );
}

}

/* and get a locator to the copied entity */
datFind_vY( locator2Y, name, locator3Y, status );

*locator3Y = dat1CcopyLocXtoY( locator1X, locator2Y, name, NULL, status );
return *status;
}

0 comments on commit 31790f2

Please sign in to comment.