Ice Sheet System Model  4.18
Code documentation
DescriptorIndex.cpp
Go to the documentation of this file.
1 
7 #ifdef HAVE_CONFIG_H
8  #include <config.h>
9 #else
10 #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
11 #endif
12 
13 #include <stdio.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include "../Enum/Enum.h"
17 #include "../io/io.h"
18 #include "../Exceptions/exceptions.h"
19 
20 int DescriptorIndex(char* root, int* pindex,char* descriptor){ //We assume root has already been allocated, and we just have to copy into it.
21 
22  char * pch=NULL;
23 
24  /*retrieve first token, separated by underscore: */
25  pch = strtok (descriptor,"_");
26  if(!pch)_error_("descriptor " << descriptor << " is not correctly formatted!");
27 
28  if (strncmp(pch,"scaled",6)==0){
29  /*we have a scaled variable. recover the root: */
30  pch = strtok (NULL, "_");
31  if(!pch)_error_("scaled descriptor " << descriptor << " is not correctly formatted!");
32  memcpy(root,pch,(strlen(pch)+1)*sizeof(char));
33 
34  /*now recover the index if it exists: */
35  pch = strtok (NULL, "_");
36  if(!pch){
37  *pindex=-1;
38  }
39  else{
40  sscanf(pch,"%i",pindex);
41  }
42  return ScaledEnum;
43  }
44  else if (strncmp(pch,"indexed",7)==0){
45  /*we have an indexed variable. recover the root: */
46  pch = strtok (NULL, "_");
47  if(!pch)_error_("indexed descriptor " << descriptor << " is not correctly formatted!");
48  memcpy(root,pch,(strlen(pch)+1)*sizeof(char));
49  /*now recover the index: */
50  pch = strtok (NULL, "_");
51  if(!pch)_error_("indexed descriptor " << descriptor << " is not correctly formatted!");
52  sscanf(pch,"%i",pindex);
53  return IndexedEnum;
54  }
55  else if (strncmp(pch,"nodal",5)==0){
56  /*we have an indexed variable. recover the root: */
57  pch = strtok (NULL, "_");
58  if(!pch)_error_("nodal descriptor " << descriptor << " is not correctly formatted!");
59  memcpy(root,pch,(strlen(pch)+1)*sizeof(char));
60  /*now recover the index: */
61  pch = strtok (NULL, "_");
62  if(!pch)_error_("nodal descriptor " << descriptor << " is not correctly formatted!");
63  sscanf(pch,"%i",pindex);
64  return NodalEnum;
65  }
66  else{
67  /*We don't have _ in the name, this is a regular variable: */
68  memcpy(root,pch,(strlen(pch)+1)*sizeof(char));
69  *pindex=-1;
70  return RegularEnum;
71  }
72 }
DescriptorIndex
int DescriptorIndex(char *root, int *pindex, char *descriptor)
Definition: DescriptorIndex.cpp:20
ScaledEnum
@ ScaledEnum
Definition: EnumDefinitions.h:1258
RegularEnum
@ RegularEnum
Definition: EnumDefinitions.h:1238
_error_
#define _error_(StreamArgs)
Definition: exceptions.h:49
IndexedEnum
@ IndexedEnum
Definition: EnumDefinitions.h:1119
NodalEnum
@ NodalEnum
Definition: EnumDefinitions.h:1198