TPCCLIB
Loading...
Searching...
No Matches
mtac.c
Go to the documentation of this file.
1
4/*****************************************************************************/
5#include "tpcclibConfig.h"
6/*****************************************************************************/
7#include "tpcift.h"
8#include "tpcisotope.h"
9/*****************************************************************************/
10#include <stdio.h>
11#include <stdlib.h>
12#include <math.h>
13#include <time.h>
14#include <string.h>
15/*****************************************************************************/
16#include "tpctac.h"
17/*****************************************************************************/
18
19/*****************************************************************************/
25 MTAC *mtac
26) {
27 if(mtac==NULL) return;
28 mtac->nr=mtac->_nr=0;
29 mtac->tac=NULL;
30}
31/*****************************************************************************/
32
33/*****************************************************************************/
40 MTAC *mtac
41) {
42 if(mtac==NULL) return;
43 /* Free allocated TACs. */
44 for(int i=0; i<mtac->_nr; i++) tacFree(&mtac->tac[i]);
45 free(mtac->tac);
46 // then set everything to zero or NULL again
47 mtacInit(mtac);
48}
49/*****************************************************************************/
50
51/*****************************************************************************/
59 MTAC *mtac,
61 int nr
62) {
63 if(mtac==NULL) return TPCERROR_FAIL;
64 /* Delete any previous contents */
65 mtacFree(mtac);
66 /* If no memory is requested, then just return */
67 if(nr<1) return TPCERROR_OK;
68 /* Allocate the list of TACs */
69 mtac->tac=(TAC*)malloc(nr*sizeof(TAC));
70 if(mtac->tac==NULL) return TPCERROR_OUT_OF_MEMORY;
71 for(int i=0; i<nr; i++) tacInit(&mtac->tac[i]);
72 mtac->_nr=nr;
73 return TPCERROR_OK;
74}
75/*****************************************************************************/
76
77/*****************************************************************************/
86 MTAC *mtac,
89 int nr
90) {
91 if(mtac==NULL) return TPCERROR_FAIL;
92
93 /* If not allocated before, then use mtacAllocate */
94 if(mtac->_nr==0) return(mtacAllocate(mtac, nr));
95
96 /* Check if there is enough space already */
97 int newNr, addNr;
98 newNr=mtac->nr + nr;
99 addNr=newNr - mtac->_nr;
100 if(addNr<=0) return TPCERROR_OK;
101
102 /* Reallocate memory for TACC data */
103 TAC *tacPtr;
104 tacPtr=realloc(mtac->tac, sizeof(TAC)*newNr);
105 if(tacPtr==NULL) return TPCERROR_OUT_OF_MEMORY;
106 mtac->tac=tacPtr;
107
108 /* Update the nr of allocated TACs and initiate new TACs */
109 for(int i=mtac->_nr; i<newNr; i++) tacInit(&mtac->tac[i]);
110 mtac->_nr=newNr;
111
112 return TPCERROR_OK;
113}
114/*****************************************************************************/
115
116/*****************************************************************************/
123 MTAC *mtac,
125 TAC *tac
126) {
127 if(mtac==NULL || tac==NULL) return TPCERROR_FAIL;
128 /* Make room for the TAC */
129 int ret=mtacAllocateMore(mtac, 1); if(ret!=TPCERROR_OK) return(ret);
130 /* Copy TAC into the end of the list */
131 ret=tacDuplicate(tac, &mtac->tac[mtac->nr]); if(ret!=TPCERROR_OK) return(ret);
132 mtac->nr++;
133 return TPCERROR_OK;
134}
135/*****************************************************************************/
136
137/*****************************************************************************/
int mtacAllocateMore(MTAC *mtac, int nr)
Definition mtac.c:83
int mtacAllocate(MTAC *mtac, int nr)
Definition mtac.c:56
int mtacAddTAC(MTAC *mtac, TAC *tac)
Definition mtac.c:121
void mtacFree(MTAC *mtac)
Definition mtac.c:38
void mtacInit(MTAC *mtac)
Definition mtac.c:23
Definition tpctac.h:149
TAC * tac
Definition tpctac.h:151
int _nr
Definition tpctac.h:155
int nr
Definition tpctac.h:153
Definition tpctac.h:87
void tacFree(TAC *tac)
Definition tac.c:106
int tacDuplicate(TAC *tac1, TAC *tac2)
Make a duplicate of TAC structure.
Definition tac.c:356
void tacInit(TAC *tac)
Definition tac.c:24
@ TPCERROR_FAIL
General error.
@ TPCERROR_OUT_OF_MEMORY
Cannot allocate memory.
@ TPCERROR_OK
No error.
Header file for library libtpcift.
Header file for library libtpcisotope.
Header file for library libtpctac.