1 // GetDP - Copyright (C) 1997-2021 P. Dular and C. Geuzaine, University of Liege
2 //
3 // See the LICENSE.txt file for license information. Please report all
4 // issues on https://gitlab.onelab.info/getdp/getdp/issues.
5 
6 #include <string.h>
7 #include "ProData.h"
8 #include "ProDefine.h"
9 #include "Message.h"
10 
11 /* --------------------------------------------------------------------------- */
12 /*  G e t   S t r i n g ,   D e f i n e ,   F u n c t i o n ,   P o i n t e r  */
13 /* --------------------------------------------------------------------------- */
14 
Get_DefineForString(struct StringXDefine SXD[],const char * string,int * FlagError)15 int Get_DefineForString(struct StringXDefine SXD[], const char *string,
16 			 int *FlagError)
17 {
18   int i = 0, define ;
19 
20   while ((SXD[i].string != NULL) && (strcmp(SXD[i].string, string)))  i++ ;
21   define = SXD[i].define ;
22   *FlagError = (SXD[i].string == NULL)? 1 : 0 ;
23 
24   return(define) ;
25 }
26 
Get_Define1NbrForString(struct StringXDefine1Nbr SXD[],const char * string,int * FlagError,int * Nbr1)27 int Get_Define1NbrForString(struct StringXDefine1Nbr SXD[], const char *string,
28 			     int *FlagError, int *Nbr1)
29 {
30   int i = 0, define ;
31 
32   while ((SXD[i].string != NULL) && (strcmp(SXD[i].string, string)))  i++ ;
33   define = SXD[i].define ; *Nbr1 = SXD[i].Nbr1 ;
34   *FlagError = (SXD[i].string == NULL)? 1 : 0 ;
35 
36   return(define) ;
37 }
38 
39 
Get_PointerForString(struct StringXPointer SXF[],const char * string,int * FlagError,void ** Pointer)40 void Get_PointerForString(struct StringXPointer SXF[], const char *string,
41 			  int *FlagError, void **Pointer)
42 {
43   int i = 0 ;
44 
45   while ((SXF[i].string != NULL) && (strcmp(SXF[i].string, string)))  i++ ;
46   *Pointer = SXF[i].Pointer ;
47   *FlagError = (SXF[i].string == NULL)? 1 : 0 ;
48 }
49 
Get_3Function3NbrForString(struct StringX3Function3Nbr SXF[],const char * string,int * FlagError,void (** Function1)(),void (** Function2)(),void (** Function3)(),double * Nbr1,int * Nbr2,int * Nbr3)50 void Get_3Function3NbrForString(struct StringX3Function3Nbr SXF[], const char *string,
51 				int *FlagError, void (**Function1)(),
52 				void (**Function2)(), void (**Function3)(),
53 				double *Nbr1, int *Nbr2, int *Nbr3)
54 {
55   int i = 0 ;
56 
57   while ((SXF[i].string != NULL) && (strcmp(SXF[i].string, string)))  i++ ;
58   *Function1 = SXF[i].Function1 ; *Function2 = SXF[i].Function2 ;
59   *Function3 = SXF[i].Function3 ; *Nbr1 = SXF[i].Nbr1 ; *Nbr2 = SXF[i].Nbr2 ; *Nbr3 = SXF[i].Nbr3 ;
60   *FlagError = (SXF[i].string == NULL)? 1 : 0 ;
61 }
62 
63 
Get_Function2NbrForString(struct StringXFunction2Nbr SXF[],const char * string,int * FlagError,void (** Function)(),int * Nbr1,int * Nbr2)64 void Get_Function2NbrForString(struct StringXFunction2Nbr SXF[], const char *string,
65 			       int *FlagError, void (**Function)(),
66 			       int *Nbr1, int *Nbr2)
67 {
68   int i = 0 ;
69 
70   while ((SXF[i].string != NULL) && (strcmp(SXF[i].string, string)))  i++ ;
71   *Function = SXF[i].Function  ;
72   *Nbr1 = SXF[i].Nbr1 ; *Nbr2 = SXF[i].Nbr2 ;
73   *FlagError = (SXF[i].string == NULL)? 1 : 0 ;
74 }
75 
Get_FunctionForFunction(struct FunctionXFunction FXF[],void (* Function1)(),int * FlagError,void (** Function2)())76 void Get_FunctionForFunction(struct FunctionXFunction  FXF[], void (*Function1)(),
77 			     int *FlagError, void (**Function2)() )
78 {
79   int i = 0 ;
80 
81   while ((FXF[i].Function1 != NULL) && (FXF[i].Function1 != Function1))  i++ ;
82   *Function2 = FXF[i].Function2  ;
83   *FlagError = (FXF[i].Function1 == NULL)? 1 : 0 ;
84 }
85 
Get_FunctionForDefine(struct DefineXFunction DXF[],int define,int * FlagError,void (** Function)())86 void Get_FunctionForDefine(struct DefineXFunction DXF[], int define,
87 			   int *FlagError, void (**Function)())
88 {
89   int i = 0 ;
90 
91   while ((DXF[i].define != 0) && (DXF[i].define != define))  i++ ;
92   *Function = DXF[i].Function ;
93   *FlagError = (DXF[i].define == 0)? 1 : 0 ;
94 }
95 
Get_StringForDefine(struct StringXDefine SXD[],int define)96 const char *Get_StringForDefine(struct StringXDefine SXD[], int define)
97 {
98   int i = 0 ; const char *string ;
99 
100   while ((SXD[i].string != NULL) && (SXD[i].define != define))  i++ ;
101   if (SXD[i].string != NULL)  string = SXD[i].string ;  else  string = "None" ;
102 
103   return(string) ;
104 }
105 
Get_StringForDefine1Nbr(struct StringXDefine1Nbr SXD[],int define)106 const char  *Get_StringForDefine1Nbr(struct StringXDefine1Nbr SXD[], int define)
107 {
108   int i = 0 ; const char *string ;
109 
110   while ((SXD[i].string != NULL) && (SXD[i].define != define))  i++ ;
111   if (SXD[i].string != NULL)  string = SXD[i].string ;  else  string = "?" ;
112 
113   return(string) ;
114 }
115 
116 
Get_StringForPointer(struct StringXPointer SXF[],void * Pointer)117 const char *Get_StringForPointer(struct StringXPointer SXF[], void *Pointer)
118 {
119   int i = 0 ; const char *string ;
120 
121   while ((SXF[i].string != NULL) && (SXF[i].Pointer != Pointer))  i++ ;
122   if (SXF[i].string != NULL)  string = SXF[i].string ;  else  string = "?" ;
123 
124   return(string) ;
125 }
126 
127 
Get_StringFor3Function3Nbr(struct StringX3Function3Nbr SXF[],void (* Function1)())128 const char *Get_StringFor3Function3Nbr(struct StringX3Function3Nbr SXF[], void (*Function1)())
129 {
130   int i = 0 ; const char *string ;
131 
132   while ((SXF[i].string != NULL) && (SXF[i].Function1 != Function1))  i++ ;
133   if (SXF[i].string != NULL)  string = SXF[i].string ;  else  string = "?" ;
134 
135   return(string) ;
136 }
137 
138 
Get_StringForFunction2Nbr(struct StringXFunction2Nbr SXF[],void (* Function)())139 const char *Get_StringForFunction2Nbr(struct StringXFunction2Nbr SXF[], void (*Function)())
140 {
141   int i = 0 ; const char *string ;
142 
143   while ((SXF[i].string != NULL) && (SXF[i].Function != Function))  i++ ;
144   if (SXF[i].string != NULL)  string = SXF[i].string ;  else  string = "?" ;
145 
146   return(string) ;
147 }
148 
149 
150 /* ------------------------------------------------------------------------
151     Get_Valid_XXX
152    ------------------------------------------------------------------------ */
153 
154 static char Valid[5000];
155 
156 #define GV(value, Get_Valid_X)                                          \
157   int i = 0;                                                            \
158   Message::Direct("Value '%s' not amongst valid choices:", value);      \
159   while (V[i].string != NULL){                                          \
160     if(!(i%3)){                                                         \
161       if(i) Message::Direct("  %s", Valid);                             \
162       strcpy(Valid, V[i].string);                                       \
163     }                                                                   \
164     else strcat(Valid, V[i].string);                                    \
165     strcat(Valid, " ");                                                 \
166     i++ ;                                                               \
167   }                                                                     \
168   Message::Direct("  %s", Valid);
169 
Get_Valid_SXD(const char * value,struct StringXDefine V[])170 void Get_Valid_SXD   (const char *value, struct StringXDefine V[])
171 { GV(value, "Get_Valid_SXD"); }
Get_Valid_SXD1N(const char * value,struct StringXDefine1Nbr V[])172 void Get_Valid_SXD1N (const char *value, struct StringXDefine1Nbr V[])
173 { GV(value, "Get_Valid_SXD1N"); }
Get_Valid_SXP(const char * value,struct StringXPointer V[])174 void Get_Valid_SXP   (const char *value, struct StringXPointer V[])
175 { GV(value, "Get_Valid_SXP"); }
Get_Valid_SX3F3N(const char * value,struct StringX3Function3Nbr V[])176 void Get_Valid_SX3F3N(const char *value, struct StringX3Function3Nbr V[])
177 { GV(value, "Get_Valid_SX3F3N"); }
Get_Valid_SXF2N(const char * value,struct StringXFunction2Nbr V[])178 void Get_Valid_SXF2N (const char *value, struct StringXFunction2Nbr V[])
179 { GV(value, "Get_Valid_SXF2N"); }
180 
181 #undef GV
182 
183 /* ------------------------------------------------------------------------
184     Gmsh/GetDP element types
185    ------------------------------------------------------------------------ */
186 
Gmsh2GetDP(int Type)187 int Gmsh2GetDP(int Type)
188 {
189   switch(Type){
190   case 15 : return POINT_ELEMENT;
191 
192   case 1  : return LINE;
193   case 2  : return TRIANGLE;
194   case 3  : return QUADRANGLE;
195   case 4  : return TETRAHEDRON;
196   case 5  : return HEXAHEDRON;
197   case 6  : return PRISM;
198   case 7  : return PYRAMID;
199 
200   case 8  : return LINE_2;
201   case 9  : return TRIANGLE_2;
202   case 10 : return QUADRANGLE_2;
203   case 11 : return TETRAHEDRON_2;
204   case 12 : return HEXAHEDRON_2;
205   case 13 : return PRISM_2;
206   case 14 : return PYRAMID_2;
207 
208   case 16 : return QUADRANGLE_2_8N;
209   case 17 : return HEXAHEDRON_2_20N;
210   case 18 : return PRISM_2_15N;
211   case 19 : return PYRAMID_2_13N;
212 
213   case 26 : return LINE_3;
214   case 21 : return TRIANGLE_3;
215   case 36 : return QUADRANGLE_3;
216   case 29 : return TETRAHEDRON_3;
217   case 92 : return HEXAHEDRON_3;
218   case 90 : return PRISM_3;
219   case 118 : return PYRAMID_3;
220 
221   case 27 : return LINE_4;
222   case 23 : return TRIANGLE_4;
223   case 37 : return QUADRANGLE_4;
224   case 30 : return TETRAHEDRON_4;
225   case 93 : return HEXAHEDRON_4;
226   case 91 : return PRISM_4;
227   //case 119 : return PYRAMID_4;
228 
229   default :
230     Message::Error("Unknown Gmsh element type %d", Type);
231     return -1;
232   }
233 }
234 
GetDP2Gmsh(int Type)235 int GetDP2Gmsh(int Type)
236 {
237   switch(Type){
238   case POINT_ELEMENT : return 15;
239 
240   case LINE          : return 1;
241   case TRIANGLE      : return 2;
242   case QUADRANGLE    : return 3;
243   case TETRAHEDRON   : return 4;
244   case HEXAHEDRON    : return 5;
245   case PRISM         : return 6;
246   case PYRAMID       : return 7;
247 
248   case LINE_2        : return 8;
249   case TRIANGLE_2    : return 9;
250   case QUADRANGLE_2  : return 10;
251   case TETRAHEDRON_2 : return 11;
252   case HEXAHEDRON_2  : return 12;
253   case PRISM_2       : return 13;
254   case PYRAMID_2     : return 14;
255 
256   case QUADRANGLE_2_8N: return 16;
257   case HEXAHEDRON_2_20N : return 17;
258   case PRISM_2_15N   : return 18;
259   case PYRAMID_2_13N : return 19;
260 
261   case LINE_3        : return 26;
262   case TRIANGLE_3    : return 21;
263   case QUADRANGLE_3  : return 36;
264   case TETRAHEDRON_3 : return 29;
265   case HEXAHEDRON_3  : return 92;
266   case PRISM_3       : return 90;
267   case PYRAMID_3     : return 118;
268 
269   case LINE_4        : return 27;
270   case TRIANGLE_4    : return 23;
271   case QUADRANGLE_4  : return 37;
272   case TETRAHEDRON_4 : return 30;
273   case HEXAHEDRON_4  : return 93;
274   case PRISM_4       : return 91;
275   //case PYRAMID_4     : return 119;
276 
277   default :
278     Message::Error("Unknown GetDP element type %d", Type);
279     return -1;
280   }
281 }
282