summaryrefslogtreecommitdiff
path: root/src/main/RecordImplementation.c
blob: 311853e41b71de2c152a361e5f78e44fbbb5d433 (plain)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
 /*@@
   @file      RecordImplementation.c
   @date      Wed Jan 13 21:03:55 1999
   @author    Tom Goodale
   @desc 
   Keeps a register of implementations and thorns associated 
   with implementations.
   @enddesc 
   @version $Header$
 @@*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "StoreNamedData.h"

static const char *rcsid = "$Header$";

CCTK_FILEVERSION(main_RecordImplementation_c);

/********************************************************************
 *********************     Local Data Types   ***********************
 ********************************************************************/

typedef struct 
{
  int n_thorns;
  char **thornlist;
} t_ImplementationData;


/********************************************************************
 ********************* Local Routine Prototypes *********************
 ********************************************************************/

/********************************************************************
 ********************* Other Routine Prototypes *********************
 ********************************************************************/

/********************************************************************
 *********************     Local Data   *****************************
 ********************************************************************/

static pNamedData *implementation_data = NULL;

/********************************************************************
 *********************     External Routines   **********************
 ********************************************************************/

 /*@@
   @routine    CCTKi_RegisterImplementation
   @date       Wed Jan 13 23:23:00 1999
   @author     Tom Goodale
   @desc 
   
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 
   @var     implementation
   @vdesc   The implementation to register
   @vtype   const char *
   @vio     in
   @vcomment 
 
   @endvar 
   @var     thorn
   @vdesc   A thorn providing this implementation
   @vtype   const char *
   @vio     in
   @vcomment 
 
   @endvar 

   @returntype int
   @returndesc 
   0 - success
   1 - memory failure realloc of thorn list
   2 - memory failure creating implementation
   3 - memory failure creating thornlist
   4 - memory failure malloc of name of thorn
   @endreturndesc
@@*/
int CCTKi_RegisterImplementation(const char *implementation,
                                 const char *thorn)
{
  int retval;
  char **temp;

  t_ImplementationData *data;

  if((data = (t_ImplementationData *)GetNamedData(implementation_data, implementation)))
  {
    data->n_thorns++;
    temp = (char **)realloc(data->thornlist,data->n_thorns*sizeof(char *));
    if(temp)
    {
      data->thornlist = temp;
      data->thornlist[data->n_thorns-1] = (char *)malloc((strlen(thorn)+1)*sizeof(char));
      if(data->thornlist[data->n_thorns-1])
      {
        strcpy(data->thornlist[data->n_thorns-1], thorn);
        retval = 0;
      }
      else
      {
        retval = 4;
      }
    }
    else
    {
      fprintf(stderr, "Unable to allocate memory for new thorn %s\n", thorn);
      retval = 1;
    }
  }
  else
  {
    data = (t_ImplementationData *)malloc(sizeof(t_ImplementationData));

    if(data)
    {
      data->thornlist = (char **)malloc(sizeof(char *));
      if(data->thornlist)
      {
        data->thornlist[0] = (char *)malloc((strlen(thorn)+1)*sizeof(char));
        if(data->thornlist[0])
        {
          strcpy(data->thornlist[0], thorn);
          data->n_thorns = 1;
          StoreNamedData(&implementation_data,implementation, data);
          retval = 0;
        }
        else
        {
          retval = 4;
        }
      }
      else
      {
        fprintf(stderr, "Unable to allocate memory for new thorn %s\n", thorn);
        retval = 3;
      }
    }
    else
    {
      fprintf(stderr, "Unable to allocate memory for new implementation %s\n", implementation);
      retval = 2;
    }

  }

  return retval;
}
  
 /*@@
   @routine    GetImplementationThorns
   @date       Wed Jan 13 23:22:43 1999
   @author     Tom Goodale
   @desc 
   
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 
   @var     implementation
   @vdesc   name of the implementation
   @vtype   const char *
   @vio     in
   @vcomment 
 
   @endvar 
   @var     thornlist
   @vdesc   Pointer to array holding list of thorns
   @vtype   char ***
   @vio     out
   @vcomment 
 
   @endvar 

   @returntype int
   @returndesc 
   Number of thorns providing implementation
   @endreturndesc
@@*/
int GetImplementationThorns(const char *implementation, char ***thornlist)
{
  int retval;
  t_ImplementationData *data;

  if((data = (t_ImplementationData *)GetNamedData(implementation_data, implementation)))
  {
    *thornlist = data->thornlist;
    retval = data->n_thorns;
  }
  else
  {
    *thornlist = NULL;
    retval = 0;
  }

  return retval;
}

/********************************************************************
 *********************     Local Routines   *************************
 ********************************************************************/