star-cad

Geometric operators for computer-aided design
git clone git://git.meso-star.fr/star-cad.git
Log | Files | Refs | README | LICENSE

commit 27a1adf59e9be8ed26748e3b102005a0fccd9d80
parent a2dd7abd68c66b2973bc8e79686507149f521fe2
Author: Christophe Coustet <christophe.coustet@meso-star.com>
Date:   Wed, 28 Dec 2022 18:58:11 +0100

Add a scad_geometry_swap_names function and make scad_geometry_get_name returning const internal string

Diffstat:
Msrc/scad.h | 8+++++++-
Msrc/scad_geometry.c | 44+++++++++++++++++++++++++++++++++++++++++---
2 files changed, 48 insertions(+), 4 deletions(-)

diff --git a/src/scad.h b/src/scad.h @@ -166,7 +166,13 @@ scad_geometry_get_count SCAD_API res_T scad_geometry_get_name (const struct scad_geometry* geom, - char** name); + const char** name); + +/* Swap names of `geom1' and `geom2' */ +SCAD_API res_T +scad_geometry_swap_names + (struct scad_geometry* geom1, + struct scad_geometry* geom2); /* Get the `mass' of the geometry `geom'. It means area for a 2D geometry and * volume for a 3D geometry. */ diff --git a/src/scad_geometry.c b/src/scad_geometry.c @@ -357,7 +357,7 @@ error: res_T scad_geometry_get_name (const struct scad_geometry* geom, - char** name) + const char** name) { res_T res = RES_OK; @@ -368,8 +368,7 @@ scad_geometry_get_name ERR(check_device(FUNC_NAME)); - *name = malloc((strlen(str_cget(&geom->name)) + 1)*sizeof(char)); - strcpy(*name, str_cget(&geom->name)); + *name = str_cget(&geom->name); exit: return res; @@ -378,6 +377,45 @@ error: } res_T +scad_geometry_swap_names + (struct scad_geometry* geom1, + struct scad_geometry* geom2) +{ + res_T res = RES_OK; + int init = 0; + struct str tmp; + struct scad_device* dev = get_device(); + struct mem_allocator* allocator = NULL; + + if(!geom1 || !geom2) { + res = RES_BAD_ARG; + goto error; + } + + ERR(check_device(FUNC_NAME)); + allocator = dev->allocator; + + if(!str_is_empty(&geom1->name)) { + ERR(htable_names_set(&dev->geometry_names, &geom1->name, &geom2)); + } + if(!str_is_empty(&geom2->name)) { + ERR(htable_names_set(&dev->geometry_names, &geom2->name, &geom1)); + } + + str_init(allocator, & tmp); + init = 1; + ERR(str_copy(&tmp, &geom1->name)); + ERR(str_copy(&geom1->name, &geom2->name)); + ERR(str_copy(&geom2->name, &tmp)); + +exit: + if(init) str_release(&tmp); + return res; +error: + goto exit; +} + +res_T scad_geometry_get_mass (struct scad_geometry* geom, double* mass)