aboutsummaryrefslogtreecommitdiff
path: root/src/Ell_DBstructure.c
blob: 1e75a948e58b55ec1ea5ea5a0d86e1ea8d268aad (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "cctk.h"
#include "cctk_Parameters.h"
#include "cctk_WarnLevel.h"
#include "cctk_FortranString.h"

#include "StoreNamedData.h"

#include "Ell_DBstructure.h"

static pNamedData *EllInfoDB;

struct t_ellthingy
{
  int type;
  int been_set;
  union 
  {
    CCTK_REAL r;
    CCTK_INT  i;
    char *s;
  } vals;
};

int Ell_CreateKey(int vartype, const char *keychain) {

  DECLARE_CCTK_PARAMETERS

  struct t_ellthingy*  new;
  int retval;

  if ((struct t_ellthingy*)GetNamedData(EllInfoDB, keychain)) {
    retval = ELLCREATE_TWICE;
  } else {

    new = (struct t_ellthingy*)malloc(sizeof(struct t_ellthingy));
    
    new->type     = vartype;
    new->been_set = 0;
    new->vals.r   = 0.0;
    new->vals.i   = 0;
    new->vals.s   = NULL;
    
    if (StoreNamedData(&EllInfoDB, keychain, new)!=0) 
    {
      if (!CCTK_Equals(elliptic_verbose,"no")) 
      {
	char *msg;
	char *name=CCTK_VarTypeName(vartype);
	msg = (char *)malloc( 
	     (200 + strlen(keychain) + strlen(name) )*sizeof(char) );
        sprintf(msg,"Failed to create %s (%s)",keychain,name);
	CCTK_INFO(msg);
	free(msg);
      }
      retval = ELLCREATE_FAILED;
    }
    else
    {
      if (!CCTK_Equals(elliptic_verbose,"no")) 
      {
	char *msg;
	char *name=CCTK_VarTypeName(vartype);
	msg = (char *)malloc( 
	     (200 + strlen(keychain) + strlen(name) )*sizeof(char) );
        sprintf(msg,"Created %s (%s)",keychain,name);
	CCTK_INFO(msg);
	free(msg);
      }
      retval = 0;
    }

  }
    
  return(retval);

  USE_CCTK_PARAMETERS

} 

int Ell_IsKey(const char *keychain) {
  int retval=ELL_ISNOKEY;
  if ((struct t_ellthingy*)GetNamedData(EllInfoDB, keychain)) {
    retval = 0;
  } else
    retval = ELL_ISNOKEY;
  return(retval);
}

void FMODIFIER FORTRAN_NAME(Ell_IsKey)(int *ierr, ONE_FORTSTRING_ARG) {
  ONE_FORTSTRING_CREATE(key)
  *ierr = Ell_IsKey(key);
  free(key);
}

int Ell_UnsetKey(const char *keychain) {

  struct t_ellthingy*  getme;
  int retval;

  getme = (struct t_ellthingy*)GetNamedData(EllInfoDB, keychain);
  
  if (!(getme)) {
    printf("Ell_GetRealKey: Cannot get structure with key >%s< \n",keychain);
    printf("Ell_GetRealKey: Create first!\n");
    retval = ELLGET_NOKEY;
  } else {
    getme->been_set = ELL_NO;
    retval = 0;
  }
  return(retval);
}

int Ell_DeleteKey(const char *keychain) {
  int retval;
  printf("NO IMPLEMENTED YET!");
  retval = 1;
  return(retval);
}

void FMODIFIER FORTRAN_NAME(Ell_DeleteKey)(int *ierr, ONE_FORTSTRING_ARG){
  ONE_FORTSTRING_CREATE(key);
  *ierr = Ell_DeleteKey(key);
  free(key);
}
   

/*** SET OPERATIONS ***/

int Ell_SetRealKey(CCTK_REAL value, const char *keychain) {
  int retval;
  struct t_ellthingy *setme;

  setme = GetNamedData(EllInfoDB, keychain);
  if (!(setme)) { 
    retval = ELLSET_FAILED;
  }  else if (setme->type!=CCTK_VARIABLE_REAL) {
    retval = ELLSET_BADTYPE;
    printf("Ell_SetRealKey: The key you try to set is not of type CCTK_REAL: >%s< (type %d)\n",
	   keychain,setme->type);
  }
  else {
    setme->type     = CCTK_VARIABLE_REAL;
    setme->vals.r   = value;
    setme->been_set = ELL_YES;
    retval=0;
  }

  return(retval);
}

void FMODIFIER FORTRAN_NAME(Ell_SetRealKey)
     (int *ierr, CCTK_REAL *value, ONE_FORTSTRING_ARG) {
  ONE_FORTSTRING_CREATE(key)
  *ierr = Ell_SetRealKey(*value, key);
  free(key);
} 

int Ell_SetIntKey(CCTK_INT value, const char *keychain) {
  int retval;
  struct t_ellthingy *setme;

  setme = GetNamedData(EllInfoDB, keychain);
  if (!(setme)) { 
    retval = ELLSET_FAILED;
  }
  else if (setme->type!=CCTK_VARIABLE_INT) {
    retval = ELLSET_BADTYPE;
    printf("Ell_SetIntKey: The key you try to set is not of type CCTK_INT: >%s< (type %d)\n",
	   keychain,setme->type);
  }
  else {
    setme->type     = CCTK_VARIABLE_INT;
    setme->vals.i   = value;
    setme->been_set = ELL_YES;
    retval=0;
  }
  return(retval);
}

void FMODIFIER FORTRAN_NAME(Ell_SetIntKey)
     (int *ierr, CCTK_INT *value, ONE_FORTSTRING_ARG) {
  ONE_FORTSTRING_CREATE(key)
  *ierr = Ell_SetIntKey(*value, key);
  free(key);
} 

int Ell_SetStrKey(char *value, const char *keychain) {
  int retval;
  struct t_ellthingy *setme;

  setme = GetNamedData(EllInfoDB, keychain);
  if (!(setme)) { 
    retval = ELLSET_FAILED;
  } else if (setme->type!=CCTK_VARIABLE_STRING) {
    retval = ELLSET_BADTYPE;
    printf("Ell_SetStrKey: The key you try to set is not of type STRING: >%s< (type %d)\n",
	   keychain,setme->type);
  }
  else {
    setme->type     = CCTK_VARIABLE_STRING;
    setme->vals.s   = value;
    setme->been_set = ELL_YES;
    retval=0;
  }
  return(retval);
}

void FMODIFIER FORTRAN_NAME(Ell_SetStringKey)
     (int *ierr, TWO_FORTSTRINGS_ARGS) {
  TWO_FORTSTRINGS_CREATE(value,key)
  *ierr = Ell_SetStrKey(value, key);
  free(value);
  free(key);
} 






/*** GET OPERATIONS ***/


int Ell_GetRealKey(CCTK_REAL *value, const char *keychain) {

  struct t_ellthingy *getme=NULL;
  int retval;

  getme = (struct t_ellthingy*)GetNamedData(EllInfoDB, keychain);

  if (!(getme)) {
    printf("Ell_GetRealKey: Cannot get structure with key >%s< \n",keychain);
    printf("Ell_GetRealKey: Create first!\n");
    retval = ELLGET_NOKEY;
  }
  else if (getme->type!=CCTK_VARIABLE_REAL) {
    printf("Ell_GetRealKey: You are not getting a CCTK_REAL value off this key: >%s< type: %d (need %d)\n",
	   keychain,getme->type,CCTK_VARIABLE_REAL);
    retval = ELLGET_BADTYPE;
  }
  else if (getme->been_set==ELL_NO) {
    printf("Ell_GetRealKey: This key has not been set to any value: >%s< \n",keychain);
    retval = ELLGET_NOTSET;
  }
  else {
    *value=getme->vals.r;
    retval=0;
  }
  printf("GetReal: %f %f\n",*value,getme->vals.r);
  return(retval);
}

void FMODIFIER FORTRAN_NAME(Ell_GetRealKey)
     (int *ierr, CCTK_REAL *value, ONE_FORTSTRING_ARG) {
  ONE_FORTSTRING_CREATE(key)
  *ierr = Ell_GetRealKey(value, key); 
  free(key);
}

int Ell_GetIntKey(CCTK_INT *value,const char *keychain) {

  struct t_ellthingy *getme=NULL;
  int retval;

  getme = (struct t_ellthingy*)GetNamedData(EllInfoDB, keychain);

  if (!(getme)) {
    printf("Ell_GetIntKey: Cannot get structure with key >%s< \n",keychain);
    printf("Ell_GetIntKey: Create first!\n");
    retval = ELLGET_NOKEY;
  }
  else if (getme->type!=CCTK_VARIABLE_INT) {
    printf("Ell_GetIntKey: You are not getting a CCTK_INT value off this key: >%s< type: %d\n",
	   keychain,getme->type);
    retval = ELLGET_BADTYPE;
  } 
  else if (getme->been_set==ELL_NO) {
    printf("Ell_GetIntKey: Key has not been set to any value: >%s< type %d\n",keychain,getme->type);
    retval = ELLGET_NOTSET;
  }  else {
    *value=getme->vals.i;
    retval=0;
  }  

  return(retval);
}

void FMODIFIER FORTRAN_NAME(Ell_GetIntKey)
     (int *ierr, CCTK_INT *value, ONE_FORTSTRING_ARG) {
  ONE_FORTSTRING_CREATE(key)
  *ierr = Ell_GetIntKey(value, key);
  free(key);
}

int Ell_GetStrKey(char *value, const char *keychain) {

  struct t_ellthingy *getme=NULL;
  int retval;

  getme = (struct t_ellthingy*)GetNamedData(EllInfoDB, keychain);

  if (!(getme)) {
    printf("Ell_GetRealKey: Cannot get structure with key >%s< \n",keychain);
    printf("Ell_GetRealKey: Create first!\n");
    retval = ELLGET_NOKEY;
  }
  else if (getme->type!=CCTK_VARIABLE_STRING) {
    printf("You are not getting a CCTK_REAL value off this key: >%s< type: %d\n",
	   keychain,getme->type);
    retval = ELLGET_BADTYPE;
  }
  else if (getme->been_set==ELL_NO) {
    printf("Key has not been set\n");
    retval = ELLGET_NOTSET;
  }  else {
    *value=getme->vals.r;
    retval=0;
  }
  return(retval);
}

void FMODIFIER FORTRAN_NAME(Ell_GetStrKey)
     (int *nchar, char **cstring,ONE_FORTSTRING_ARG)
{   
   int i;
  ONE_FORTSTRING_CREATE(fstring)
  ONE_FORTSTRING_PTR(fptr)

  if (strlen(*cstring) > cctk_strlen1) 
  {
    char *message;
    message = (char *)malloc( (200+strlen(*cstring))*sizeof(char) );
    sprintf(message,"Cannot output %s to char* of length %d\n",
	    *cstring,cctk_strlen1);
    CCTK_Warn (1,__LINE__,__FILE__,"Cactus",message);
    free(message);
    *nchar = -1;
  }

  for (i=0;i<strlen(*cstring);i++) 
  {
    fptr[i] = (*cstring)[i];
  }

  for (i=strlen(*cstring);i<cctk_strlen1;i++)
  {
    fptr[i] = ' ';
  }

  fptr[strlen(*cstring)] = '\0';

  *nchar = strlen(*cstring);
}