From 21acbb9efcbc5158f62e43d025db1ca7b2369fb8 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Fri, 26 Jun 2020 17:10:29 +0200 Subject: Add crystal_copy_deep() --- libcrystfel/src/crystal.c | 41 ++++++++++++++++++++++++++++++++++++++++- libcrystfel/src/crystal.h | 1 + 2 files changed, 41 insertions(+), 1 deletion(-) (limited to 'libcrystfel') diff --git a/libcrystfel/src/crystal.c b/libcrystfel/src/crystal.c index 9202c668..bec3a890 100644 --- a/libcrystfel/src/crystal.c +++ b/libcrystfel/src/crystal.c @@ -7,7 +7,7 @@ * a research centre of the Helmholtz Association. * * Authors: - * 2013-2016 Thomas White + * 2013-2020 Thomas White * 2016 Valerio Mariani * * This file is part of CrystFEL. @@ -33,6 +33,7 @@ #include "crystal.h" #include "utils.h" +#include "reflist-utils.h" /** @@ -126,6 +127,44 @@ Crystal *crystal_copy(const Crystal *cryst) } +/** + * \param cryst: A \ref Crystal to copy. + * + * Creates a new \ref Crystal which is a copy of \p cryst. The copy is a "deep + * copy", which means that copies ARE made of the data structures which + * \p cryst contains references to, for example its \ref RefList. + * + * \returns A (deep) copy of \p cryst, or NULL on failure. + * + */ +Crystal *crystal_copy_deep(const Crystal *cryst) +{ + Crystal *c; + + c = crystal_new(); + if ( c == NULL ) return NULL; + + memcpy(c, cryst, sizeof(Crystal)); + if ( c->notes != NULL ) c->notes = strdup(c->notes); + + if ( cryst->cell != NULL ) { + UnitCell *cell; + cell = cell_new_from_cell(cryst->cell); + if ( cell == NULL ) return NULL; + c->cell = cell; + } + + if ( cryst->reflections != NULL ) { + RefList *refls; + refls = copy_reflist(cryst->reflections); + if ( refls == NULL ) return NULL; + c->reflections = refls; + } + + return c; +} + + /** * \param cryst: A \ref Crystal to free. * diff --git a/libcrystfel/src/crystal.h b/libcrystfel/src/crystal.h index 669c173f..592a4b43 100644 --- a/libcrystfel/src/crystal.h +++ b/libcrystfel/src/crystal.h @@ -56,6 +56,7 @@ extern "C" { extern Crystal *crystal_new(void); extern Crystal *crystal_copy(const Crystal *cryst); +extern Crystal *crystal_copy_deep(const Crystal *cryst); extern void crystal_free(Crystal *cryst); extern UnitCell *crystal_get_cell(Crystal *cryst); -- cgit v1.2.3