1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
/*********************************************************************
Arithmetic operations on data structures.
This is part of GNU Astronomy Utilities (Gnuastro) package.
Original author:
Mohammad Akhlaghi <mohammad@akhlaghi.org>
Contributing author(s):
Copyright (C) 2021-2024 Free Software Foundation, Inc.
Gnuastro is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
Gnuastro is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with Gnuastro. If not, see <http://www.gnu.org/licenses/>.
**********************************************************************/
#include <config.h>
#include <stdio.h>
#include <errno.h>
#include <error.h>
#include <stdlib.h>
#include <string.h>
#include <gnuastro/list.h>
#include <gnuastro-internal/checkset.h>
#include <gnuastro-internal/arithmetic-set.h>
/* Remove a name from the list of names and return the dataset it points
to. */
static gal_data_t *
arithmetic_set_remove_name(struct gal_arithmetic_set_params *p,
char *name)
{
gal_data_t *tmp, *removed=NULL, *prev=NULL;
/* Go over all the given names. */
for(tmp=p->named;tmp!=NULL;tmp=tmp->next)
{
if( !strcmp(tmp->name, name) )
{
removed=tmp;
if(prev) prev->next = tmp->next;
else p->named = tmp->next;
}
/* Set this node as the 'prev' pointer. */
prev=tmp;
}
/* A small sanity check. */
if(removed==NULL)
error(EXIT_FAILURE, 0, "%s: a bug! Please contact us at %s to "
"fix the problem. 'removed' must not be NULL at this point",
__func__, PACKAGE_BUGREPORT);
/* Nothing in the list points to it now. So we can safely modify and
return it. */
free(removed->name);
removed->next=NULL;
removed->name=NULL;
return removed;
}
/* Pop a dataset and keep it in the 'named' list for later use. */
void
gal_arithmetic_set_name(struct gal_arithmetic_set_params *p, char *token)
{
gal_data_t *tmp, *tofree;
char *varname=&token[ GAL_ARITHMETIC_SET_PREFIX_LENGTH ];
/* If a dataset with this name already exists, it will be removed/deleted
so we can use the name for the newly designated dataset. */
for(tmp=p->named; tmp!=NULL; tmp=tmp->next)
if( !strcmp(varname, tmp->name) )
{
tofree=arithmetic_set_remove_name(p, varname);
gal_data_free(tofree);
/* IMPORTANT: we MUST break here! 'tmp' does't point to the right
place any more. We can define a 'prev' node and modify it on
every attempt, but since there is only one dataset with a given
name, that is redundant and will just make the program slow. */
break;
}
/* Pop the top operand, then add it to the list of named datasets, but
only if it is used in later tokens. If it isn't, free the popped
dataset. The latter case (to define a name, but not use it), is
obviously a redundant operation, but that is upto the user and may
happen in scripts where the operands and operators list is
automatically generated. We should just have everything in place, so
no crashes occur or no extra memory is consumed. */
if( p->used_later(p, varname) )
{
/* Add the top popped operand to the list of names. */
gal_list_data_add(&p->named, p->pop(p));
/* Write the requested name into this dataset. But note that 'name'
MUST be already empty. So to be safe, we'll do a sanity check. */
if(p->named->name)
error(EXIT_FAILURE, 0, "%s: a bug! Please contact us at %s to "
"fix the problem. The 'name' element should be NULL at "
"this point, but it isn't", __func__, PACKAGE_BUGREPORT);
if(p->named->unit)
{ free(p->named->unit); p->named->unit=NULL; }
if(p->named->comment)
{ free(p->named->comment); p->named->comment=NULL; }
gal_checkset_allocate_copy(varname, &p->named->name);
}
else
{
/* Pop the top operand, then free it: for example the user has ran
'set-i', but forgot to actually use it (happens a lot due to human
error!). */
tmp=p->pop(p);
gal_data_free(tmp);
}
}
/* See if a given token is the name of a variable. */
int
gal_arithmetic_set_is_name(gal_data_t *named, char *token)
{
gal_data_t *tmp;
/* Make sure the variable name hasn't been set before. */
for(tmp=named; tmp!=NULL; tmp=tmp->next)
if( !strcmp(token, tmp->name) )
return 1;
/* If control reaches here, then there was no match*/
return 0;
}
/* Return a copy of the named dataset. */
gal_data_t *
gal_arithmetic_set_copy_named(struct gal_arithmetic_set_params *p,
char *name)
{
gal_data_t *out=NULL, *tmp;
/* Find the proper named element to use. */
for(tmp=p->named;tmp!=NULL;tmp=tmp->next)
{
if( !strcmp(tmp->name, name) )
{
/* If the named operand is used later, then copy it into the
output. */
if( p->used_later(p, name) )
{
out=gal_data_copy(tmp);
out->next=NULL;
if(out->name) { free(out->name); out->name=NULL; }
if(out->unit) { free(out->unit); out->unit=NULL; }
if(out->comment) { free(out->comment); out->comment=NULL; }
}
/* The named operand is not used any more. Remove it from the list
of named datasets and continue. */
else out=arithmetic_set_remove_name(p, name);
}
}
/* A small sanity check. */
if(out==NULL)
error(EXIT_FAILURE, 0, "%s: a bug! please contact us at %s to fix the "
"problem. The requested name '%s' couldn't be found in the list",
__func__, PACKAGE_BUGREPORT, name);
/* Return. */
return out;
}
|