Debian Codegraph @

3rdparty/mshmet/src/sourceslib/libmeshb7.c

raw
1
2
3 /*----------------------------------------------------------------------------*/
4 /* */
5 /* LIBMESH V 7.35 */
6 /* */
7 /*----------------------------------------------------------------------------*/
8 /* */
9 /* Description: handles .meshb file format I/O */
10 /* Author: Loic MARECHAL */
11 /* Creation date: dec 09 1999 */
12 /* Last modification: mar 06 2018 */
13 /* */
14 /*----------------------------------------------------------------------------*/
15
16 /*----------------------------------------------------------------------------*/
17 /* Headers' macros */
18 /*----------------------------------------------------------------------------*/
19
20 #ifdef F77API
21
22 // Add a final underscore to Fortran procedure names
23 #ifdef F77_NO_UNDER_SCORE
24 #define NAMF77(c,f) f
25 #define APIF77(x) x
26 #else
27 #define NAMF77(c,f) f ## _
28 #define APIF77(x) x ## _
29 #endif
30
31 // Pass parameters as pointers in Fortran
32 #define VALF77(v) *v
33 #define TYPF77(t) t*
34
35 #else
36
37 // Pass parameters as values in C
38 #define NAMF77(c,f) c
39 #define VALF77(v) v
40 #define TYPF77(t) t
41
42 #endif
43
44
45 /*----------------------------------------------------------------------------*/
46 /* Includes */
47 /*----------------------------------------------------------------------------*/
48
49 #define _XOPEN_SOURCE 500
50
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <stdarg.h>
54 #include <string.h>
55 #include <float.h>
56 #include <math.h>
57 #include <ctype.h>
58 #include <setjmp.h>
59 #include <fcntl.h>
60
61
62 /*
63 * [Bruno] include the headers with the prototypes for
64 * open()/close()/write()/lseek()
65 * and define the constants to be used to open() a file.
66 * Under Windows,
67 * 1) _O_BINARY should be set in the flags.
68 * 2) 'mode' has a completely different meaning
69 */
70
71 #if defined(__unix__) || defined(__linux__) || defined(__APPLE__) || defined(__EMSCRIPTEN__)
72
73 #include <unistd.h>
74
75 #define OPEN_READ_FLAGS O_RDONLY
76 #define OPEN_WRITE_FLAGS O_CREAT | O_WRONLY | O_TRUNC
77 #define OPEN_READ_MODE 0666
78 #define OPEN_WRITE_MODE 0666
79
80 #elif defined(WIN32) || defined(_WIN64)
81
82 #define GMF_WINDOWS
83
84 #include <windows.h>
85 #include <io.h>
86 #include <sys/stat.h>
87 #include <sys/types.h>
88 #include <wchar.h>
89
90 #define OPEN_READ_FLAGS O_RDONLY | _O_BINARY
91 #define OPEN_WRITE_FLAGS O_CREAT | O_WRONLY | O_TRUNC | _O_BINARY
92 #define OPEN_READ_MODE _S_IREAD
93 #define OPEN_WRITE_MODE _S_IREAD | S_IWRITE
94
95 #endif
96
97 #include <errno.h>
98 #include <libmeshb7.h>
99
100 // [Bruno] Using portable printf modifier from pstdint.h
101 // (alternative: use "%zd" under Linux and "%Id" under Windows)
102
103 #ifdef PRINTF_INT64_MODIFIER
104 #define INT64_T_FMT "%" PRINTF_INT64_MODIFIER "d"
105 #else
106 # ifdef GMF_WINDOWS
107 # define INT64_T_FMT "%Id"
108 # else
109 # include <inttypes.h>
110 # define INT64_T_FMT "%" PRId64
111 # endif
112 #endif
113
114 // [Bruno] Made asynchronous I/O optional
115 #ifdef WITH_AIO
116 #include <aio.h>
117 #else
118
119 // Mockup aio library
120 struct aiocb
121 {
122 FILE *aio_fildes; // File descriptor
123 off_t aio_offset; // File offset
124 void *aio_buf; // Location of buffer
125 size_t aio_nbytes; // Length of transfer
126 int aio_lio_opcode; // Operation to be performed
127 };
128
129 int aio_error( const struct aiocb * aiocbp )
130 {
131 return(aiocbp->aio_lio_opcode);
132 }
133
134 // Set the file position and read a block of data
135 int aio_read( struct aiocb * aiocbp )
136 {
137 if( (fseek(aiocbp->aio_fildes, (off_t)aiocbp->aio_offset, SEEK_SET) == 0)
138 && (fread(aiocbp->aio_buf, 1, aiocbp->aio_nbytes, aiocbp->aio_fildes)
139 == aiocbp->aio_nbytes) )
140 {
141 aiocbp->aio_lio_opcode = 0;
142 }
143 else
144 {
145 aiocbp->aio_lio_opcode = -1;
146 }
147
148 return(aiocbp->aio_lio_opcode);
149 }
150
151 size_t aio_return( struct aiocb * aiocbp )
152 {
153 return(aiocbp->aio_nbytes);
154 }
155
156 // Set the file position and write a block of data
157 int aio_write( struct aiocb * aiocbp )
158 {
159 if( (fseek(aiocbp->aio_fildes, (off_t)aiocbp->aio_offset, SEEK_SET) == 0)
160 && (fwrite(aiocbp->aio_buf, 1, aiocbp->aio_nbytes, aiocbp->aio_fildes)
161 == aiocbp->aio_nbytes) )
162 {
163 aiocbp->aio_lio_opcode = 0;
164 }
165 else
166 {
167 aiocbp->aio_lio_opcode = -1;
168 }
169
170 return(aiocbp->aio_lio_opcode);
171 }
172
173 #endif
174
175
176
177 /*----------------------------------------------------------------------------*/
178 /* Defines */
179 /*----------------------------------------------------------------------------*/
180
181 #define Asc 1
182 #define Bin 2
183 #define MshFil 4
184 #define SolFil 8
185 #define InfKwd 1
186 #define RegKwd 2
187 #define SolKwd 3
188 #define CmtKwd 4
189 #define WrdSiz 4
190 #define FilStrSiz 64
191 #define BufSiz 10000
192 #define MaxArg 20
193
194
195 /*----------------------------------------------------------------------------*/
196 /* Structures */
197 /*----------------------------------------------------------------------------*/
198
199 typedef struct
200 {
201 int typ, deg, NmbNod, SolSiz, NmbWrd, NmbTyp, TypTab[ GmfMaxTyp ];
202 int *OrdTab;
203 int64_t NmbLin;
204 size_t pos;
205 char fmt[ GmfMaxTyp*9 ];
206 }KwdSct;
207
208 typedef struct
209 {
210 int dim, ver, mod, typ, cod, FilDes;
211 int64_t NexKwdPos, siz;
212 size_t pos;
213 jmp_buf err;
214 KwdSct KwdTab[ GmfMaxKwd + 1 ];
215 FILE *hdl;
216 int *IntBuf;
217 float *FltBuf;
218 char *buf;
219 char FilNam[ GmfStrSiz ];
220 double DblBuf[1000/8];
221 unsigned char blk[ BufSiz + 1000 ];
222 }GmfMshSct;
223
224
225 /*----------------------------------------------------------------------------*/
226 /* Global variables */
227 /*----------------------------------------------------------------------------*/
228
229 const char *GmfKwdFmt[ GmfMaxKwd + 1 ][3] =
230 {
231 {"Reserved", "", ""},
232 {"MeshVersionFormatted", "", "i"},
233 {"Reserved", "", ""},
234 {"Dimension", "", "i"},
235 {"Vertices", "i", "dri"},
236 {"Edges", "i", "iii"},
237 {"Triangles", "i", "iiii"},
238 {"Quadrilaterals", "i", "iiiii"},
239 {"Tetrahedra", "i", "iiiii"},
240 {"Prisms", "i", "iiiiiii"},
241 {"Hexahedra", "i", "iiiiiiiii"},
242 {"Reserved", "", ""},
243 {"Reserved", "", ""},
244 {"Corners", "i", "i"},
245 {"Ridges", "i", "i"},
246 {"RequiredVertices", "i", "i"},
247 {"RequiredEdges", "i", "i"},
248 {"RequiredTriangles", "i", "i"},
249 {"RequiredQuadrilaterals", "i", "i"},
250 {"TangentAtEdgeVertices", "i", "iii"},
251 {"NormalAtVertices", "i", "ii"},
252 {"NormalAtTriangleVertices", "i", "iii"},
253 {"NormalAtQuadrilateralVertices", "i", "iiii"},
254 {"AngleOfCornerBound", "", "r"},
255 {"TrianglesP2", "i", "iiiiiii"},
256 {"EdgesP2", "i", "iiii"},
257 {"SolAtPyramids", "i", "sr"},
258 {"QuadrilateralsQ2", "i", "iiiiiiiiii"},
259 {"ISolAtPyramids", "i", "iiiii"},
260 {"SubDomainFromGeom", "i", "iii"},
261 {"TetrahedraP2", "i", "iiiiiiiiiii"},
262 {"Fault_NearTri", "i", "i"},
263 {"Fault_Inter", "i", "i"},
264 {"HexahedraQ2", "i", "iiiiiiiiiiiiiiiiiiiiiiiiiiii"},
265 {"ExtraVerticesAtEdges", "i", "in"},
266 {"ExtraVerticesAtTriangles", "i", "in"},
267 {"ExtraVerticesAtQuadrilaterals", "i", "in"},
268 {"ExtraVerticesAtTetrahedra", "i", "in"},
269 {"ExtraVerticesAtPrisms", "i", "in"},
270 {"ExtraVerticesAtHexahedra", "i", "in"},
271 {"VerticesOnGeometricVertices", "i", "ii"},
272 {"VerticesOnGeometricEdges", "i", "iirr"},
273 {"VerticesOnGeometricTriangles", "i", "iirrr"},
274 {"VerticesOnGeometricQuadrilaterals", "i", "iirrr"},
275 {"EdgesOnGeometricEdges", "i", "ii"},
276 {"Fault_FreeEdge", "i", "i"},
277 {"Polyhedra", "i", "iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii"},
278 {"Polygons", "", "iiiiiiiii"},
279 {"Fault_Overlap", "i", "i"},
280 {"Pyramids", "i", "iiiiii"},
281 {"BoundingBox", "", "drdr"},
282 {"Reserved", "", ""},
283 {"PrivateTable", "i", "i"},
284 {"Fault_BadShape", "i", "i"},
285 {"End", "", ""},
286 {"TrianglesOnGeometricTriangles", "i", "ii"},
287 {"TrianglesOnGeometricQuadrilaterals", "i", "ii"},
288 {"QuadrilateralsOnGeometricTriangles", "i", "ii"},
289 {"QuadrilateralsOnGeometricQuadrilaterals", "i", "ii"},
290 {"Tangents", "i", "dr"},
291 {"Normals", "i", "dr"},
292 {"TangentAtVertices", "i", "ii"},
293 {"SolAtVertices", "i", "sr"},
294 {"SolAtEdges", "i", "sr"},
295 {"SolAtTriangles", "i", "sr"},
296 {"SolAtQuadrilaterals", "i", "sr"},
297 {"SolAtTetrahedra", "i", "sr"},
298 {"SolAtPrisms", "i", "sr"},
299 {"SolAtHexahedra", "i", "sr"},
300 {"DSolAtVertices", "i", "sr"},
301 {"ISolAtVertices", "i", "i"},
302 {"ISolAtEdges", "i", "ii"},
303 {"ISolAtTriangles", "i", "iii"},
304 {"ISolAtQuadrilaterals", "i", "iiii"},
305 {"ISolAtTetrahedra", "i", "iiii"},
306 {"ISolAtPrisms", "i", "iiiiii"},
307 {"ISolAtHexahedra", "i", "iiiiiiii"},
308 {"Iterations", "", "i"},
309 {"Time", "", "r"},
310 {"Fault_SmallTri", "i", "i"},
311 {"CoarseHexahedra", "i", "i"},
312 {"Comments", "i", "c"},
313 {"PeriodicVertices", "i", "ii"},
314 {"PeriodicEdges", "i", "ii"},
315 {"PeriodicTriangles", "i", "ii"},
316 {"PeriodicQuadrilaterals", "i", "ii"},
317 {"PrismsP2", "i", "iiiiiiiiiiiiiiiiiii"},
318 {"PyramidsP2", "i", "iiiiiiiiiiiiiii"},
319 {"QuadrilateralsQ3", "i", "iiiiiiiiiiiiiiiii"},
320 {"QuadrilateralsQ4", "i", "iiiiiiiiiiiiiiiiiiiiiiiiii"},
321 {"TrianglesP3", "i", "iiiiiiiiiii"},
322 {"TrianglesP4", "i", "iiiiiiiiiiiiiiii"},
323 {"EdgesP3", "i", "iiiii"},
324 {"EdgesP4", "i", "iiiiii"},
325 {"IRefGroups", "i", "ciii"},
326 {"DRefGroups", "i", "iii"},
327 {"TetrahedraP3", "i", "iiiiiiiiiiiiiiiiiiiii"},
328 {"TetrahedraP4", "i", "iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii"},
329 {"HexahedraQ3", "i", "iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii"},
330 {"HexahedraQ4", "i", "iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii"},
331 {"PyramidsP3", "i", "iiiiiiiiiiiiiiiiiiiiiiiiiiiiiii"},
332 {"PyramidsP4", "i", "iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii"},
333 {"PrismsP3", "i", "iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii"},
334 {"PrismsP4", "i", "iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii"},
335 {"HOSolAtEdgesP1", "i", "hr"},
336 {"HOSolAtEdgesP2", "i", "hr"},
337 {"HOSolAtEdgesP3", "i", "hr"},
338 {"HOSolAtTrianglesP1", "i", "hr"},
339 {"HOSolAtTrianglesP2", "i", "hr"},
340 {"HOSolAtTrianglesP3", "i", "hr"},
341 {"HOSolAtQuadrilateralsQ1", "i", "hr"},
342 {"HOSolAtQuadrilateralsQ2", "i", "hr"},
343 {"HOSolAtQuadrilateralsQ3", "i", "hr"},
344 {"HOSolAtTetrahedraP1", "i", "hr"},
345 {"HOSolAtTetrahedraP2", "i", "hr"},
346 {"HOSolAtTetrahedraP3", "i", "hr"},
347 {"HOSolAtPyramidsP1", "i", "hr"},
348 {"HOSolAtPyramidsP2", "i", "hr"},
349 {"HOSolAtPyramidsP3", "i", "hr"},
350 {"HOSolAtPrismsP1", "i", "hr"},
351 {"HOSolAtPrismsP2", "i", "hr"},
352 {"HOSolAtPrismsP3", "i", "hr"},
353 {"HOSolAtHexahedraQ1", "i", "hr"},
354 {"HOSolAtHexahedraQ2", "i", "hr"},
355 {"HOSolAtHexahedraQ3", "i", "hr"},
356 {"BezierMode", "", "i"},
357 {"ByteFlow", "i", "i"},
358 {"EdgesP2Ordering", "i", "i"},
359 {"EdgesP3Ordering", "i", "i"},
360 {"TrianglesP2Ordering", "i", "iii"},
361 {"TrianglesP3Ordering", "i", "iii"},
362 {"QuadrilateralsQ2Ordering", "i", "ii"},
363 {"QuadrilateralsQ3Ordering", "i", "ii"},
364 {"TetrahedraP2Ordering", "i", "iiii"},
365 {"TetrahedraP3Ordering", "i", "iiii"},
366 {"PyramidsP2Ordering", "i", "iii"},
367 {"PyramidsP3Ordering", "i", "iii"},
368 {"PrismsP2Ordering", "i", "iiii"},
369 {"PrismsP3Ordering", "i", "iiii"},
370 {"HexahedraQ2Ordering", "i", "iii"},
371 {"HexahedraQ3Ordering", "i", "iii"},
372 {"EdgesP1Ordering", "i", "i"},
373 {"EdgesP4Ordering", "i", "i"},
374 {"TrianglesP1Ordering", "i", "iii"},
375 {"TrianglesP4Ordering", "i", "iii"},
376 {"QuadrilateralsQ1Ordering", "i", "ii"},
377 {"QuadrilateralsQ4Ordering", "i", "ii"},
378 {"TetrahedraP1Ordering", "i", "iiii"},
379 {"TetrahedraP4Ordering", "i", "iiii"},
380 {"PyramidsP1Ordering", "i", "iii"},
381 {"PyramidsP4Ordering", "i", "iii"},
382 {"PrismsP1Ordering", "i", "iiii"},
383 {"PrismsP4Ordering", "i", "iiii"},
384 {"HexahedraQ1Ordering", "i", "iii"},
385 {"HexahedraQ4Ordering", "i", "iii"}
386 };
387
388 #ifdef TRANSMESH
389 int GmfMaxRefTab[ GmfMaxKwd + 1 ];
390 #endif
391
392
393 /*----------------------------------------------------------------------------*/
394 /* Prototypes of local procedures */
395 /*----------------------------------------------------------------------------*/
396
397 static void ScaWrd (GmfMshSct *, void *);
398 static void ScaDblWrd(GmfMshSct *, void *);
399 static int64_t GetPos (GmfMshSct *);
400 static void RecWrd (GmfMshSct *, const void *);
401 static void RecDblWrd(GmfMshSct *, const void *);
402 static void RecBlk (GmfMshSct *, const void *, int);
403 static void SetPos (GmfMshSct *, int64_t);
404 static int ScaKwdTab(GmfMshSct *);
405 static void ExpFmt (GmfMshSct *, int);
406 static void ScaKwdHdr(GmfMshSct *, int);
407 static void SwpWrd (char *, int);
408 static int SetFilPos(GmfMshSct *, int64_t);
409 static int64_t GetFilPos(GmfMshSct *msh);
410 static int64_t GetFilSiz(GmfMshSct *);
411 #ifdef F77API
412 static void CalF77Prc(int64_t, int64_t, void *, int, void **);
413 #endif
414
415
416 /*----------------------------------------------------------------------------*/
417 /* Fscanf and fgets checking for errors */
418 /*----------------------------------------------------------------------------*/
419
420 #define safe_fscanf(hdl, format, ptr, JmpErr) \
421 do { \
422 if( fscanf(hdl, format, ptr) != 1 ) \
423 longjmp( JmpErr, -1); \
424 } while(0)
425
426
427 #define safe_fgets(ptr, siz, hdl, JmpErr) \
428 do { \
429 if( fgets(ptr, siz, hdl) == NULL ) \
430 longjmp( JmpErr, -1); \
431 } while(0)
432
433
434 #define safe_fread(ptr, siz, nit, str, JmpErr) \
435 do { \
436 if( fread(ptr, siz, nit, str) != nit ) \
437 longjmp( JmpErr, -1); \
438 } while(0)
439
440
441 /*----------------------------------------------------------------------------*/
442 /* Open a mesh file in read or write mode */
443 /*----------------------------------------------------------------------------*/
444
445 int64_t GmfOpenMesh(const char *FilNam, int mod, ...)
446 {
447 int KwdCod, res, *PtrVer, *PtrDim;
448 int64_t MshIdx;
449 char str[ GmfStrSiz ];
450 va_list VarArg;
451 GmfMshSct *msh;
452
453 /*---------------------*/
454 /* MESH STRUCTURE INIT */
455 /*---------------------*/
456
457 if(!(msh = calloc(1, sizeof(GmfMshSct))))
458 return(0);
459
460 MshIdx = (int64_t)msh;
461
462 // Save the current stack environment for longjmp
463 if(setjmp(msh->err) != 0)
464 {
465 if(msh->hdl != NULL)
466 fclose(msh->hdl);
467
468 if(msh->FilDes != 0)
469 close(msh->FilDes);
470
471 free(msh);
472 return(0);
473 }
474
475 // Copy the FilNam into the structure
476 if(strlen(FilNam) + 7 >= GmfStrSiz)
477 longjmp(msh->err, -1);
478
479 strcpy(msh->FilNam, FilNam);
480
481 // Store the opening mod (read or write) and guess
482 // the filetype (binary or ascii) depending on the extension
483 msh->mod = mod;
484 msh->buf = (void *)msh->DblBuf;
485 msh->FltBuf = (void *)msh->DblBuf;
486 msh->IntBuf = (void *)msh->DblBuf;
487
488 if(strstr(msh->FilNam, ".meshb"))
489 msh->typ |= (Bin | MshFil);
490 else if(strstr(msh->FilNam, ".mesh"))
491 msh->typ |= (Asc | MshFil);
492 else if(strstr(msh->FilNam, ".solb"))
493 msh->typ |= (Bin | SolFil);
494 else if(strstr(msh->FilNam, ".sol"))
495 msh->typ |= (Asc | SolFil);
496 else
497 longjmp(msh->err, -1);
498
499 // Open the file in the required mod and initialize the mesh structure
500 if(msh->mod == GmfRead)
501 {
502
503 /*-----------------------*/
504 /* OPEN FILE FOR READING */
505 /*-----------------------*/
506
507 va_start(VarArg, mod);
508 PtrVer = va_arg(VarArg, int *);
509 PtrDim = va_arg(VarArg, int *);
510 va_end(VarArg);
511
512 // Read the endian coding tag, the mesh version
513 // and the mesh dimension (mandatory kwd)
514 if(msh->typ & Bin)
515 {
516 // Create the name string and open the file
517 #ifdef WITH_AIO
518 // [Bruno] added binary flag (necessary under Windows)
519 msh->FilDes = open(msh->FilNam, OPEN_READ_FLAGS, OPEN_READ_MODE);
520
521 if(msh->FilDes <= 0)
522 longjmp(msh->err, -1);
523
524 // Read the endian coding tag
525 if(read(msh->FilDes, &msh->cod, WrdSiz) != WrdSiz)
526 longjmp(msh->err, -1);
527 #else
528 // [Bruno] added binary flag (necessary under Windows)
529 if(!(msh->hdl = fopen(msh->FilNam, "rb")))
530 longjmp(msh->err, -1);
531
532 // Read the endian coding tag
533 safe_fread(&msh->cod, WrdSiz, 1, msh->hdl, msh->err);
534 #endif
535
536 // Read the mesh version and the mesh dimension (mandatory kwd)
537 if( (msh->cod != 1) && (msh->cod != 16777216) )
538 longjmp(msh->err, -1);
539
540 ScaWrd(msh, (unsigned char *)&msh->ver);
541
542 if( (msh->ver < 1) || (msh->ver > 4) )
543 longjmp(msh->err, -1);
544
545 if( (msh->ver >= 3) && (sizeof(int64_t) != 8) )
546 longjmp(msh->err, -1);
547
548 ScaWrd(msh, (unsigned char *)&KwdCod);
549
550 if(KwdCod != GmfDimension)
551 longjmp(msh->err, -1);
552
553 GetPos(msh);
554 ScaWrd(msh, (unsigned char *)&msh->dim);
555 }
556 else
557 {
558 // Create the name string and open the file
559 if(!(msh->hdl = fopen(msh->FilNam, "rb")))
560 longjmp(msh->err, -1);
561
562 do
563 {
564 res = fscanf(msh->hdl, "%s", str);
565 }while( (res != EOF) && strcmp(str, "MeshVersionFormatted") );
566
567 if(res == EOF)
568 longjmp(msh->err, -1);
569
570 safe_fscanf(msh->hdl, "%d", &msh->ver, msh->err);
571
572 if( (msh->ver < 1) || (msh->ver > 4) )
573 longjmp(msh->err, -1);
574
575 do
576 {
577 res = fscanf(msh->hdl, "%s", str);
578 }while( (res != EOF) && strcmp(str, "Dimension") );
579
580 if(res == EOF)
581 longjmp(msh->err, -1);
582
583 safe_fscanf(msh->hdl, "%d", &msh->dim, msh->err);
584 }
585
586 if( (msh->dim != 2) && (msh->dim != 3) )
587 longjmp(msh->err, -1);
588
589 (*PtrVer) = msh->ver;
590 (*PtrDim) = msh->dim;
591
592 /*------------*/
593 /* KW READING */
594 /*------------*/
595
596 // Read the list of kw present in the file
597 if(!ScaKwdTab(msh))
598 return(0);
599
600 return(MshIdx);
601 }
602 else if(msh->mod == GmfWrite)
603 {
604
605 /*-----------------------*/
606 /* OPEN FILE FOR WRITING */
607 /*-----------------------*/
608
609 msh->cod = 1;
610
611 // Check if the user provided a valid version number and dimension
612 va_start(VarArg, mod);
613 msh->ver = va_arg(VarArg, int);
614 msh->dim = va_arg(VarArg, int);
615 va_end(VarArg);
616
617 if( (msh->ver < 1) || (msh->ver > 4) )
618 longjmp(msh->err, -1);
619
620 if( (msh->ver >= 3) && (sizeof(int64_t) != 8) )
621 longjmp(msh->err, -1);
622
623 if( (msh->dim != 2) && (msh->dim != 3) )
624 longjmp(msh->err, -1);
625
626 // Create the mesh file
627 if(msh->typ & Bin)
628 {
629 /*
630 * [Bruno] replaced previous call to creat():
631 * with a call to open(), because Windows needs the
632 * binary flag to be specified.
633 */
634 #ifdef WITH_AIO
635 msh->FilDes = open(msh->FilNam, OPEN_WRITE_FLAGS, OPEN_WRITE_MODE);
636
637 if(msh->FilDes <= 0)
638 longjmp(msh->err, -1);
639 #else
640 if(!(msh->hdl = fopen(msh->FilNam, "wb")))
641 longjmp(msh->err, -1);
642 #endif
643 }
644 else if(!(msh->hdl = fopen(msh->FilNam, "wb")))
645 longjmp(msh->err, -1);
646
647
648 /*------------*/
649 /* KW WRITING */
650 /*------------*/
651
652 // Write the mesh version and dimension
653 if(msh->typ & Asc)
654 {
655 fprintf(msh->hdl, "%s %d\n\n",
656 GmfKwdFmt[ GmfVersionFormatted ][0], msh->ver);
657 fprintf(msh->hdl, "%s %d\n",
658 GmfKwdFmt[ GmfDimension ][0], msh->dim);
659 }
660 else
661 {
662 RecWrd(msh, (unsigned char *)&msh->cod);
663 RecWrd(msh, (unsigned char *)&msh->ver);
664 GmfSetKwd(MshIdx, GmfDimension, 0);
665 RecWrd(msh, (unsigned char *)&msh->dim);
666 }
667
668 return(MshIdx);
669 }
670 else
671 {
672 free(msh);
673 return(0);
674 }
675 }
676
677
678 /*----------------------------------------------------------------------------*/
679 /* Close a meshfile in the right way */
680 /*----------------------------------------------------------------------------*/
681
682 int GmfCloseMesh(int64_t MshIdx)
683 {
684 int res = 1;
685 GmfMshSct *msh = (GmfMshSct *)MshIdx;
686
687 RecBlk(msh, msh->buf, 0);
688
689 // In write down the "End" kw in write mode
690 if(msh->mod == GmfWrite)
691 {
692 if(msh->typ & Asc)
693 fprintf(msh->hdl, "\n%s\n", GmfKwdFmt[ GmfEnd ][0]);
694 else
695 GmfSetKwd(MshIdx, GmfEnd, 0);
696 }
697
698 // Close the file and free the mesh structure
699 if(msh->typ & Bin)
700 #ifdef WITH_AIO
701 close(msh->FilDes);
702 #else
703 fclose(msh->hdl);
704 #endif
705 else if(fclose(msh->hdl))
706 res = 0;
707
708 free(msh);
709
710 return(res);
711 }
712
713
714 /*----------------------------------------------------------------------------*/
715 /* Read the number of lines and set the position to this kwd */
716 /*----------------------------------------------------------------------------*/
717
718 int64_t GmfStatKwd(int64_t MshIdx, int KwdCod, ...)
719 {
720 int i, *PtrNmbTyp, *PtrSolSiz, *TypTab, *PtrDeg, *PtrNmbNod;
721 GmfMshSct *msh = (GmfMshSct *)MshIdx;
722 KwdSct *kwd;
723 va_list VarArg;
724
725 if( (KwdCod < 1) || (KwdCod > GmfMaxKwd) )
726 return(0);
727
728 kwd = &msh->KwdTab[ KwdCod ];
729
730 if(!kwd->NmbLin)
731 return(0);
732
733 // Read further arguments if this kw is a sol
734 if(kwd->typ == SolKwd)
735 {
736 va_start(VarArg, KwdCod);
737
738 PtrNmbTyp = va_arg(VarArg, int *);
739 *PtrNmbTyp = kwd->NmbTyp;
740
741 PtrSolSiz = va_arg(VarArg, int *);
742 *PtrSolSiz = kwd->SolSiz;
743
744 TypTab = va_arg(VarArg, int *);
745
746 for(i=0;i<kwd->NmbTyp;i++)
747 TypTab[i] = kwd->TypTab[i];
748
749 // Add two extra paramaters for HO elements: degree and nmb nodes
750 if(!strcmp("hr", GmfKwdFmt[ KwdCod ][2]) )
751 {
752 PtrDeg = va_arg(VarArg, int *);
753 *PtrDeg = kwd->deg;
754
755 PtrNmbNod = va_arg(VarArg, int *);
756 *PtrNmbNod = kwd->NmbNod;
757 }
758
759 va_end(VarArg);
760 }
761
762 return(kwd->NmbLin);
763 }
764
765
766 /*----------------------------------------------------------------------------*/
767 /* Set the current file position to a given kwd */
768 /*----------------------------------------------------------------------------*/
769
770 int GmfGotoKwd(int64_t MshIdx, int KwdCod)
771 {
772 GmfMshSct *msh = (GmfMshSct *)MshIdx;
773 KwdSct *kwd = &msh->KwdTab[ KwdCod ];
774
775 if( (KwdCod < 1) || (KwdCod > GmfMaxKwd) || !kwd->NmbLin )
776 return(0);
777
778 return(SetFilPos(msh, kwd->pos));
779 }
780
781
782 /*----------------------------------------------------------------------------*/
783 /* Write the kwd and set the number of lines */
784 /*----------------------------------------------------------------------------*/
785
786 int GmfSetKwd(int64_t MshIdx, int KwdCod, int64_t NmbLin, ...)
787 {
788 int i, *TypTab;
789 int64_t CurPos;
790 va_list VarArg;
791 GmfMshSct *msh = (GmfMshSct *)MshIdx;
792 KwdSct *kwd;
793
794 RecBlk(msh, msh->buf, 0);
795
796 if( (KwdCod < 1) || (KwdCod > GmfMaxKwd) )
797 return(0);
798
799 kwd = &msh->KwdTab[ KwdCod ];
800
801 // Read further arguments if this kw is a solution
802 if(!strcmp(GmfKwdFmt[ KwdCod ][2], "sr")
803 || !strcmp(GmfKwdFmt[ KwdCod ][2], "hr"))
804 {
805 va_start(VarArg, NmbLin);
806
807 kwd->NmbTyp = va_arg(VarArg, int);
808 TypTab = va_arg(VarArg, int *);
809
810 for(i=0;i<kwd->NmbTyp;i++)
811 kwd->TypTab[i] = TypTab[i];
812
813 // Add two extra paramaters for HO elements: degree and nmb nodes
814 if(!strcmp("hr", GmfKwdFmt[ KwdCod ][2]))
815 {
816 kwd->deg = va_arg(VarArg, int);
817 kwd->NmbNod = va_arg(VarArg, int);
818 }
819
820 va_end(VarArg);
821 }
822
823 // Setup the kwd info
824 ExpFmt(msh, KwdCod);
825
826 if(!kwd->typ)
827 return(0);
828 else if(kwd->typ == InfKwd)
829 kwd->NmbLin = 1;
830 else
831 kwd->NmbLin = NmbLin;
832
833 // Store the next kwd position in binary file
834 if( (msh->typ & Bin) && msh->NexKwdPos )
835 {
836 CurPos = GetFilPos(msh);
837
838 if(!SetFilPos(msh, msh->NexKwdPos))
839 return(0);
840
841 SetPos(msh, CurPos);
842
843 if(!SetFilPos(msh, CurPos))
844 return(0);
845 }
846
847 // Write the header
848 if(msh->typ & Asc)
849 {
850 fprintf(msh->hdl, "\n%s\n", GmfKwdFmt[ KwdCod ][0]);
851
852 if(kwd->typ != InfKwd)
853 fprintf(msh->hdl, INT64_T_FMT"\n", kwd->NmbLin);
854
855 // In case of solution field, write the extended header
856 if(kwd->typ == SolKwd)
857 {
858 fprintf(msh->hdl, "%d ", kwd->NmbTyp);
859
860 for(i=0;i<kwd->NmbTyp;i++)
861 fprintf(msh->hdl, "%d ", kwd->TypTab[i]);
862
863 fprintf(msh->hdl, "\n");
864 }
865
866 if(!strcmp("hr", GmfKwdFmt[ KwdCod ][2]))
867 fprintf(msh->hdl, "%d %d\n", kwd->deg, kwd->NmbNod);
868 }
869 else
870 {
871 RecWrd(msh, (unsigned char *)&KwdCod);
872 msh->NexKwdPos = GetFilPos(msh);
873 SetPos(msh, 0);
874
875 if(kwd->typ != InfKwd)
876 {
877 if(msh->ver < 4)
878 {
879 i = (int)kwd->NmbLin;
880 RecWrd(msh, (unsigned char *)&i);
881 }
882 else
883 RecDblWrd(msh, (unsigned char *)&kwd->NmbLin);
884 }
885
886 // In case of solution field, write the extended header at once
887 if(kwd->typ == SolKwd)
888 {
889 RecWrd(msh, (unsigned char *)&kwd->NmbTyp);
890
891 for(i=0;i<kwd->NmbTyp;i++)
892 RecWrd(msh, (unsigned char *)&kwd->TypTab[i]);
893
894 if(!strcmp("hr", GmfKwdFmt[ KwdCod ][2]))
895 {
896 RecWrd(msh, (unsigned char *)&kwd->deg);
897 RecWrd(msh, (unsigned char *)&kwd->NmbNod);
898 }
899 }
900 }
901
902 // Reset write buffer position
903 msh->pos = 0;
904
905 // Compute the total file size and check if it crosses the 2GB threshold
906 msh->siz += kwd->NmbLin * kwd->NmbWrd * WrdSiz;
907
908 return(1);
909 }
910
911
912 /*----------------------------------------------------------------------------*/
913 /* Read a full line from the current kwd */
914 /*----------------------------------------------------------------------------*/
915
916 int NAMF77(GmfGetLin, gmfgetlin)(TYPF77(int64_t)MshIdx, TYPF77(int)KwdCod, ...)
917 {
918 int i;
919 float *FltSolTab, FltVal, *PtrFlt;
920 double *DblSolTab, *PtrDbl;
921 va_list VarArg;
922 GmfMshSct *msh = (GmfMshSct *) VALF77(MshIdx);
923 KwdSct *kwd = &msh->KwdTab[ VALF77(KwdCod) ];
924
925 if( (VALF77(KwdCod) < 1) || (VALF77(KwdCod) > GmfMaxKwd) )
926 return(0);
927
928 // Save the current stack environment for longjmp
929 if(setjmp(msh->err) != 0)
930 return(0);
931
932 // Start decoding the arguments
933 va_start(VarArg, KwdCod);
934
935 switch(kwd->typ)
936 {
937 case InfKwd : case RegKwd : case CmtKwd :
938 {
939 if(msh->typ & Asc)
940 {
941 for(i=0;i<kwd->SolSiz;i++)
942 {
943 if(kwd->fmt[i] == 'r')
944 {
945 if(msh->ver <= 1)
946 {
947 safe_fscanf(msh->hdl, "%f", &FltVal, msh->err);
948 PtrDbl = va_arg(VarArg, double *);
949 PtrFlt = (float *)PtrDbl;
950 *PtrFlt = FltVal;
951 }
952 else
953 {
954 safe_fscanf(msh->hdl, "%lf",
955 va_arg(VarArg, double *), msh->err);
956 }
957 }
958 else if(kwd->fmt[i] == 'i')
959 {
960 if(msh->ver <= 3)
961 {
962 safe_fscanf(msh->hdl, "%d",
963 va_arg(VarArg, int *), msh->err);
964 }
965 else
966 {
967 // [Bruno] %ld -> INT64_T_FMT
968 safe_fscanf(msh->hdl, INT64_T_FMT,
969 va_arg(VarArg, int64_t *), msh->err);
970 }
971 }
972 else if(kwd->fmt[i] == 'c')
973 {
974 safe_fgets( va_arg(VarArg, char *),
975 WrdSiz * FilStrSiz, msh->hdl, msh->err);
976 }
977 }
978 }
979 else
980 {
981 for(i=0;i<kwd->SolSiz;i++)
982 if(kwd->fmt[i] == 'r')
983 if(msh->ver <= 1)
984 ScaWrd(msh, (unsigned char *)va_arg(VarArg, float *));
985 else
986 ScaDblWrd(msh, (unsigned char *)va_arg(VarArg, double *));
987 else if(kwd->fmt[i] == 'i')
988 if(msh->ver <= 3)
989 ScaWrd(msh, (unsigned char *)va_arg(VarArg, int *));
990 else
991 ScaDblWrd(msh, (unsigned char *)va_arg(VarArg, int64_t *));
992 else if(kwd->fmt[i] == 'c')
993 // [Bruno] added error control
994 safe_fread(va_arg(VarArg, char *), WrdSiz, FilStrSiz, msh->hdl, msh->err);
995 }
996 }break;
997
998 case SolKwd :
999 {
1000 if(msh->ver == 1)
1001 {
1002 FltSolTab = va_arg(VarArg, float *);
1003
1004 if(msh->typ & Asc)
1005 for(i=0; i<kwd->SolSiz; i++)
1006 safe_fscanf(msh->hdl, "%f", &FltSolTab[i], msh->err);
1007 else
1008 for(i=0; i<kwd->SolSiz; i++)
1009 ScaWrd(msh, (unsigned char *)&FltSolTab[i]);
1010 }
1011 else
1012 {
1013 DblSolTab = va_arg(VarArg, double *);
1014
1015 if(msh->typ & Asc)
1016 for(i=0; i<kwd->SolSiz; i++)
1017 safe_fscanf(msh->hdl, "%lf", &DblSolTab[i], msh->err);
1018 else
1019 for(i=0; i<kwd->SolSiz; i++)
1020 ScaDblWrd(msh, (unsigned char *)&DblSolTab[i]);
1021 }
1022 }break;
1023 }
1024
1025 va_end(VarArg);
1026
1027 return(1);
1028 }
1029
1030
1031 /*----------------------------------------------------------------------------*/
1032 /* Write a full line from the current kwd */
1033 /*----------------------------------------------------------------------------*/
1034
1035 int NAMF77(GmfSetLin, gmfsetlin)(TYPF77(int64_t) MshIdx, TYPF77(int) KwdCod, ...)
1036 {
1037 int i, pos, *IntBuf;
1038 int64_t *LngBuf;
1039 float *FltSolTab, *FltBuf;
1040 double *DblSolTab, *DblBuf;
1041 va_list VarArg;
1042 GmfMshSct *msh = (GmfMshSct *) VALF77(MshIdx);
1043 KwdSct *kwd = &msh->KwdTab[ VALF77(KwdCod) ];
1044
1045 if( ( VALF77(KwdCod) < 1) || ( VALF77(KwdCod) > GmfMaxKwd) )
1046 return(0);
1047
1048 // Start decoding the arguments
1049 va_start(VarArg, KwdCod);
1050
1051 if(kwd->typ != SolKwd)
1052 {
1053 if(msh->typ & Asc)
1054 {
1055 for(i=0;i<kwd->SolSiz;i++)
1056 {
1057 if(kwd->fmt[i] == 'r')
1058 {
1059 if(msh->ver <= 1)
1060 #ifdef F77API
1061 fprintf(msh->hdl, "%g ", *(va_arg(VarArg, float *)));
1062 #else
1063 fprintf(msh->hdl, "%g ", va_arg(VarArg, double));
1064 #endif
1065 else
1066 fprintf(msh->hdl, "%.15g ", VALF77(va_arg(VarArg, TYPF77(double))));
1067 }
1068 else if(kwd->fmt[i] == 'i')
1069 {
1070 if(msh->ver <= 3)
1071 fprintf(msh->hdl, "%d ", VALF77(va_arg(VarArg, TYPF77(int))));
1072 else
1073 {
1074 // [Bruno] %ld -> INT64_T_FMT
1075 fprintf( msh->hdl, INT64_T_FMT " ",
1076 VALF77(va_arg(VarArg, TYPF77(int64_t))));
1077 }
1078 }
1079 else if(kwd->fmt[i] == 'c')
1080 fprintf(msh->hdl, "%s ", va_arg(VarArg, char *));
1081 }
1082 }
1083 else
1084 {
1085 pos = 0;
1086
1087 for(i=0;i<kwd->SolSiz;i++)
1088 {
1089 if(kwd->fmt[i] == 'r')
1090 {
1091 if(msh->ver <= 1)
1092 {
1093 FltBuf = (void *)&msh->buf[ pos ];
1094 #ifdef F77API
1095 *FltBuf = (float)*(va_arg(VarArg, float *));
1096 #else
1097 *FltBuf = (float)va_arg(VarArg, double);
1098 #endif
1099 pos += 4;
1100 }
1101 else
1102 {
1103 DblBuf = (void *)&msh->buf[ pos ];
1104 *DblBuf = VALF77(va_arg(VarArg, TYPF77(double)));
1105 pos += 8;
1106 }
1107 }
1108 else if(kwd->fmt[i] == 'i')
1109 {
1110 if(msh->ver <= 3)
1111 {
1112 IntBuf = (void *)&msh->buf[ pos ];
1113 *IntBuf = VALF77(va_arg(VarArg, TYPF77(int)));
1114 pos += 4;
1115 }
1116 else
1117 {
1118 LngBuf = (void *)&msh->buf[ pos ];
1119 *LngBuf = VALF77(va_arg(VarArg, TYPF77(int64_t)));
1120 pos += 8;
1121 }
1122 }
1123 else if(kwd->fmt[i] == 'c')
1124 {
1125 memset(&msh->buf[ pos ], 0, FilStrSiz * WrdSiz);
1126 strncpy(&msh->buf[ pos ], va_arg(VarArg, char *), FilStrSiz * WrdSiz);
1127 pos += FilStrSiz;
1128 }
1129 }
1130
1131 RecBlk(msh, msh->buf, kwd->NmbWrd);
1132 }
1133 }
1134 else
1135 {
1136 if(msh->ver == 1)
1137 {
1138 FltSolTab = va_arg(VarArg, float *);
1139
1140 if(msh->typ & Asc)
1141 for(i=0; i<kwd->SolSiz; i++)
1142 fprintf(msh->hdl, "%g ", (double)FltSolTab[i]);
1143 else
1144 RecBlk(msh, (unsigned char *)FltSolTab, kwd->NmbWrd);
1145 }
1146 else
1147 {
1148 DblSolTab = va_arg(VarArg, double *);
1149
1150 if(msh->typ & Asc)
1151 for(i=0; i<kwd->SolSiz; i++)
1152 fprintf(msh->hdl, "%.15g ", DblSolTab[i]);
1153 else
1154 RecBlk(msh, (unsigned char *)DblSolTab, kwd->NmbWrd);
1155 }
1156 }
1157
1158 va_end(VarArg);
1159
1160 if(msh->typ & Asc)
1161 fprintf(msh->hdl, "\n");
1162
1163 return(1);
1164 }
1165
1166
1167 /*----------------------------------------------------------------------------*/
1168 /* Private procedure for transmesh : copy a whole line */
1169 /*----------------------------------------------------------------------------*/
1170
1171 #ifdef TRANSMESH
1172
1173 int GmfCpyLin(int64_t InpIdx, int64_t OutIdx, int KwdCod)
1174 {
1175 char s[ WrdSiz * FilStrSiz ];
1176 double d;
1177 float f;
1178 int i, a;
1179 int64_t l;
1180 GmfMshSct *InpMsh = (GmfMshSct *)InpIdx, *OutMsh = (GmfMshSct *)OutIdx;
1181 KwdSct *kwd = &InpMsh->KwdTab[ KwdCod ];
1182
1183 // Save the current stack environment for longjmp
1184 if(setjmp(InpMsh->err) != 0)
1185 return(0);
1186
1187 for(i=0;i<kwd->SolSiz;i++)
1188 {
1189 if(kwd->fmt[i] == 'r')
1190 {
1191 if(InpMsh->ver == 1)
1192 {
1193 if(InpMsh->typ & Asc)
1194 safe_fscanf(InpMsh->hdl, "%f", &f, InpMsh->err);
1195 else
1196 ScaWrd(InpMsh, (unsigned char *)&f);
1197
1198 d = (double)f;
1199 }
1200 else
1201 {
1202 if(InpMsh->typ & Asc)
1203 safe_fscanf(InpMsh->hdl, "%lf", &d, InpMsh->err);
1204 else
1205 ScaDblWrd(InpMsh, (unsigned char *)&d);
1206
1207 f = (float)d;
1208 }
1209
1210 if(OutMsh->ver == 1)
1211 if(OutMsh->typ & Asc)
1212 fprintf(OutMsh->hdl, "%g ", (double)f);
1213 else
1214 RecWrd(OutMsh, (unsigned char *)&f);
1215 else
1216 if(OutMsh->typ & Asc)
1217 fprintf(OutMsh->hdl, "%.15g ", d);
1218 else
1219 RecDblWrd(OutMsh, (unsigned char *)&d);
1220 }
1221 else if(kwd->fmt[i] == 'i')
1222 {
1223 if(InpMsh->ver <= 3)
1224 {
1225 if(InpMsh->typ & Asc)
1226 safe_fscanf(InpMsh->hdl, "%d", &a, InpMsh->err);
1227 else
1228 ScaWrd(InpMsh, (unsigned char *)&a);
1229
1230 l = (int64_t)a;
1231 }
1232 else
1233 {
1234 if(InpMsh->typ & Asc)
1235 safe_fscanf(InpMsh->hdl, INT64_T_FMT, &l, InpMsh->err);
1236 else
1237 ScaDblWrd(InpMsh, (unsigned char *)&l);
1238
1239 a = (int)l;
1240 }
1241
1242 if( (i == kwd->SolSiz-1) && (a > GmfMaxRefTab[ KwdCod ]) )
1243 GmfMaxRefTab[ KwdCod ] = a;
1244
1245 if(OutMsh->ver <= 3)
1246 {
1247 if(OutMsh->typ & Asc)
1248 fprintf(OutMsh->hdl, "%d ", a);
1249 else
1250 RecWrd(OutMsh, (unsigned char *)&a);
1251 }
1252 else
1253 {
1254 if(OutMsh->typ & Asc)
1255 fprintf(OutMsh->hdl, INT64_T_FMT" ", l);
1256 else
1257 RecDblWrd(OutMsh, (unsigned char *)&l);
1258 }
1259 }
1260 else if(kwd->fmt[i] == 'c')
1261 {
1262 memset(s, 0, FilStrSiz * WrdSiz);
1263
1264 if(InpMsh->typ & Asc)
1265 safe_fgets(s, WrdSiz * FilStrSiz, InpMsh->hdl, InpMsh->err);
1266 else
1267 #ifdef WITH_AIO
1268 read(InpMsh->FilDes, s, WrdSiz * FilStrSiz);
1269 #else
1270 safe_fread(s, WrdSiz, FilStrSiz, InpMsh->hdl, InpMsh->err);
1271 #endif
1272 if(OutMsh->typ & Asc)
1273 fprintf(OutMsh->hdl, "%s ", s);
1274 else
1275 #ifdef WITH_AIO
1276 write(OutMsh->FilDes, s, WrdSiz * FilStrSiz);
1277 #else
1278 fwrite(s, WrdSiz, FilStrSiz, OutMsh->hdl);
1279 #endif
1280 }
1281 }
1282
1283 if(OutMsh->typ & Asc)
1284 fprintf(OutMsh->hdl, "\n");
1285
1286 return(1);
1287 }
1288
1289 #endif
1290
1291 // [Bruno] Made asynchronous I/O optional
1292 #ifndef WITHOUT_AIO
1293
1294 /*----------------------------------------------------------------------------*/
1295 /* Bufferized asynchronous reading of all keyword's lines */
1296 /*----------------------------------------------------------------------------*/
1297
1298 int NAMF77(GmfGetBlock, gmfgetblock)( TYPF77(int64_t) MshIdx,
1299 TYPF77(int) KwdCod,
1300 TYPF77(int64_t) BegIdx,
1301 TYPF77(int64_t) EndIdx,
1302 TYPF77(int) MapTyp,
1303 void *MapTab,
1304 void *prc, ... )
1305 {
1306 char *UsrDat[ GmfMaxTyp ], *UsrBas[ GmfMaxTyp ], *FilPos, *EndUsrDat;
1307 char *FilBuf = NULL, *FrtBuf = NULL, *BckBuf = NULL;
1308 char *StrTab[5] = { "", "%f", "%lf", "%d", INT64_T_FMT };
1309 int b, i, j, k, LinSiz, *FilPtrI32, *UsrPtrI32, FilTyp[ GmfMaxTyp ];
1310 int UsrTyp[ GmfMaxTyp ], NmbBlk, SizTab[5] = {0,4,8,4,8};
1311 int *IntMapTab = NULL, err;
1312 float *FilPtrR32, *UsrPtrR32;
1313 double *FilPtrR64, *UsrPtrR64;
1314 int64_t BlkNmbLin, *FilPtrI64, *UsrPtrI64, BlkBegIdx, BlkEndIdx = 0;
1315 int64_t *LngMapTab = NULL, OldIdx = 0, RepCnt, UsrNmbLin;
1316 int64_t FilBegIdx = VALF77(BegIdx), FilEndIdx = VALF77(EndIdx);
1317 void (*UsrPrc)(int64_t, int64_t, void *) = NULL;
1318 size_t UsrLen[ GmfMaxTyp ], ret;
1319 va_list VarArg;
1320 GmfMshSct *msh = (GmfMshSct *) VALF77(MshIdx);
1321 KwdSct *kwd = &msh->KwdTab[ VALF77(KwdCod) ];
1322 struct aiocb aio;
1323 #ifdef F77API
1324 int NmbArg = 0;
1325 void *ArgTab[ MaxArg ];
1326 #else
1327 char *UsrArg = NULL;
1328 #endif
1329
1330 // Save the current stack environment for longjmp
1331 if(setjmp(msh->err) != 0)
1332 {
1333 if(BckBuf)
1334 free(BckBuf);
1335
1336 if(FrtBuf)
1337 free(FrtBuf);
1338
1339 return(0);
1340 }
1341
1342 // Check mesh and keyword
1343 if( (VALF77(KwdCod) < 1) || (VALF77(KwdCod) > GmfMaxKwd) || !kwd->NmbLin )
1344 return(0);
1345
1346 // Make sure it's not a simple information keyword
1347 if( (kwd->typ != RegKwd) && (kwd->typ != SolKwd) )
1348 return(0);
1349
1350 // Check user's bounds
1351 if( (FilBegIdx < 1) || (FilBegIdx > FilEndIdx) || (FilEndIdx > kwd->NmbLin) )
1352 return(0);
1353
1354 // Compute the number of lines to be read
1355 UsrNmbLin = FilEndIdx - FilBegIdx + 1;
1356
1357 // Get the renumbering map if any
1358 if(VALF77(MapTyp) == GmfInt)
1359 IntMapTab = (int *)MapTab;
1360 else if(VALF77(MapTyp) == GmfLong)
1361 LngMapTab = (int64_t *)MapTab;
1362
1363 // Start decoding the arguments
1364 va_start(VarArg, prc);
1365 LinSiz = 0;
1366
1367 // Get the user's preporcessing procedure and argument adresses, if any
1368 #ifdef F77API
1369 if(prc)
1370 {
1371 UsrPrc = (void (*)(int64_t, int64_t, void *))prc;
1372 NmbArg = *(va_arg(VarArg, int *));
1373
1374 for(i=0;i<NmbArg;i++)
1375 ArgTab[i] = va_arg(VarArg, void *);
1376 }
1377 #else
1378 if(prc)
1379 {
1380 UsrPrc = (void (*)(int64_t, int64_t, void *))prc;
1381 UsrArg = va_arg(VarArg, void *);
1382 }
1383 #endif
1384
1385 // Get the user's data type and pointers to first
1386 // and last adresses in order to compute the stride
1387 if(kwd->typ == RegKwd)
1388 {
1389 for(i=0;i<kwd->SolSiz;i++)
1390 {
1391 // Get the type from the variable arguments
1392 UsrTyp[i] = VALF77(va_arg(VarArg, TYPF77(int)));;
1393
1394 // If a table is given, read its size in the next argument
1395 // and automatically fill the pointer table by incrementing
1396 // as many times the base user address
1397 if(UsrTyp[i] == GmfIntTab)
1398 {
1399 RepCnt = VALF77(va_arg(VarArg, TYPF77(int)));
1400 UsrTyp[i] = GmfInt;
1401 }
1402 else if(UsrTyp[i] == GmfLongTab)
1403 {
1404 RepCnt = VALF77(va_arg(VarArg, TYPF77(int)));
1405 UsrTyp[i] = GmfLong;
1406 }
1407 else
1408 RepCnt = 0;
1409
1410 // Get the begin and end pointers from the variable arguments
1411 UsrDat[i] = UsrBas[i] = va_arg(VarArg, char *);
1412 EndUsrDat = va_arg(VarArg, char *);
1413
1414 if(UsrNmbLin > 1)
1415 UsrLen[i] = (size_t)(EndUsrDat - UsrDat[i]) / (UsrNmbLin - 1);
1416 else
1417 UsrLen[i] = 0;
1418
1419 // Replicate the table data type and increment the base address
1420 if(RepCnt >= 2)
1421 {
1422 for(j=i+1; j<i+RepCnt; j++)
1423 {
1424 UsrTyp[j] = UsrTyp[i];
1425 UsrDat[j] = UsrBas[j] = UsrDat[ j-1 ] + SizTab[ UsrTyp[i] ];
1426 UsrLen[j] = UsrLen[i];
1427 }
1428
1429 i += RepCnt - 1;
1430 }
1431 }
1432 }
1433 else if(kwd->typ == SolKwd)
1434 {
1435 // Get the type, begin and end pointers from the variable arguments
1436 UsrTyp[0] = VALF77(va_arg(VarArg, TYPF77(int)));;
1437 UsrDat[0] = UsrBas[0] = va_arg(VarArg, char *);
1438 EndUsrDat = va_arg(VarArg, char *);
1439
1440 if(UsrNmbLin > 1)
1441 UsrLen[0] = (size_t)(EndUsrDat - UsrDat[0]) / (UsrNmbLin - 1);
1442 else
1443 UsrLen[0] = 0;
1444
1445 // Solutions use only on set of type/begin/end pointers
1446 // and the base adress is incremented for each entry
1447 for(i=1;i<kwd->SolSiz;i++)
1448 {
1449 UsrTyp[i] = UsrTyp[0];
1450 UsrDat[i] = UsrBas[i] = UsrDat[ i-1 ] + SizTab[ UsrTyp[0] ];
1451 UsrLen[i] = UsrLen[0];
1452 }
1453 }
1454 else
1455 return(0);
1456
1457 // Get the file's data type
1458 for(i=0;i<kwd->SolSiz;i++)
1459 {
1460 if(kwd->fmt[i] == 'r')
1461 if(msh->ver <= 1)
1462 FilTyp[i] = GmfFloat;
1463 else
1464 FilTyp[i] = GmfDouble;
1465 else
1466 if(msh->ver <= 3)
1467 FilTyp[i] = GmfInt;
1468 else
1469 FilTyp[i] = GmfLong;
1470
1471 // Compute the file stride
1472 LinSiz += SizTab[ FilTyp[i] ];
1473 }
1474
1475 va_end(VarArg);
1476
1477 // Move file pointer to the keyword data
1478 SetFilPos(msh, kwd->pos);
1479
1480 // Read the whole kwd data
1481 if(msh->typ & Asc)
1482 {
1483 for(i=1;i<=FilEndIdx;i++)
1484 for(j=0;j<kwd->SolSiz;j++)
1485 {
1486 // Reorder HO nodes on the fly
1487 if(kwd->OrdTab && (j != kwd->SolSiz-1))
1488 k = kwd->OrdTab[j];
1489 else
1490 k = j;
1491
1492 safe_fscanf(msh->hdl, StrTab[ UsrTyp[k] ], UsrDat[k], msh->err);
1493
1494 // Move to the next user's data line only when the desired
1495 // begining position in the ascii file has been reached since
1496 // we cannot move directly to an arbitrary position
1497 if(i >= FilBegIdx)
1498 UsrDat[k] += UsrLen[k];
1499 }
1500
1501 // Call the user's preprocessing procedure
1502 if(UsrPrc)
1503 #ifdef F77API
1504 CalF77Prc(1, kwd->NmbLin, UsrPrc, NmbArg, ArgTab);
1505 #else
1506 UsrPrc(1, kwd->NmbLin, UsrArg);
1507 #endif
1508 }
1509 else
1510 {
1511 // Allocate both front and back buffers
1512 if(!(BckBuf = malloc((size_t)BufSiz * (size_t)LinSiz)))
1513 return(0);
1514
1515 if(!(FrtBuf = malloc((size_t)BufSiz * (size_t)LinSiz)))
1516 return(0);
1517
1518 // Setup the ansynchonous parameters
1519 memset(&aio, 0, sizeof(struct aiocb));
1520 FilBuf = BckBuf;
1521 aio.aio_buf = BckBuf;
1522 #ifdef WITH_AIO
1523 aio.aio_fildes = msh->FilDes;
1524 #else
1525 aio.aio_fildes = msh->hdl;
1526 #endif
1527 aio.aio_offset = GetFilPos(msh) + (FilBegIdx-1) * (size_t)LinSiz;
1528
1529 NmbBlk = UsrNmbLin / BufSiz;
1530
1531 // Loop over N+1 blocks
1532 for(b=0;b<=NmbBlk+1;b++)
1533 {
1534 // Wait for the previous block read to complete except
1535 // for the first loop interation
1536 if(b)
1537 {
1538 while(aio_error(&aio) == EINPROGRESS);
1539
1540 err = aio_error(&aio);
1541 ret = aio_return(&aio);
1542
1543 if (err != 0) {
1544 printf (" Error at aio_error() : %s\n", strerror (err));
1545 exit(1);
1546 }
1547
1548 if (ret != aio.aio_nbytes) {
1549 printf(" Error at aio_return()\n");
1550 exit(1);
1551 }
1552
1553 // Increment the reading position
1554 aio.aio_offset += aio.aio_nbytes;
1555
1556 // and swap the buffers
1557 if(aio.aio_buf == BckBuf)
1558 {
1559 aio.aio_buf = FrtBuf;
1560 FilBuf = BckBuf;
1561 }
1562 else
1563 {
1564 aio.aio_buf = BckBuf;
1565 FilBuf = FrtBuf;
1566 }
1567 }
1568
1569 // Read a chunk of data except for the last loop interarion
1570 if(b <= NmbBlk)
1571 {
1572 // The last block is shorter than the others
1573 if(b == NmbBlk)
1574 BlkNmbLin = UsrNmbLin - b * BufSiz;
1575 else
1576 BlkNmbLin = BufSiz;
1577
1578 aio.aio_nbytes = BlkNmbLin * LinSiz;
1579
1580 if(aio_read(&aio) == -1)
1581 {
1582 printf("block = %d / %d\n", b+1, NmbBlk+1);
1583 printf("size = "INT64_T_FMT" lines\n", BlkNmbLin);
1584 #ifdef WITH_AIO
1585 printf("aio_fildes = %d\n",aio.aio_fildes);
1586 #else
1587 printf("aio_fildes = %p\n",aio.aio_fildes);
1588 #endif
1589 printf("aio_buf = %p\n",aio.aio_buf);
1590 printf("aio_offset = " INT64_T_FMT "\n",(int64_t)aio.aio_offset);
1591 printf("aio_nbytes = " INT64_T_FMT "\n",(int64_t)aio.aio_nbytes);
1592 printf("errno = %d\n",errno);
1593 exit(1);
1594 }
1595 }
1596
1597 // Then decode the block and store it in the user's data structure
1598 // except for the first loop interation
1599 if(b)
1600 {
1601 // The last block is shorter than the others
1602 if(b-1 == NmbBlk)
1603 BlkNmbLin = UsrNmbLin - (b-1) * BufSiz;
1604 else
1605 BlkNmbLin = BufSiz;
1606
1607 BlkBegIdx = BlkEndIdx+1;
1608 BlkEndIdx += BlkNmbLin;
1609 FilPos = FilBuf;
1610
1611 for(i=0;i<BlkNmbLin;i++)
1612 {
1613 OldIdx++;
1614
1615 for(j=0;j<kwd->SolSiz;j++)
1616 {
1617 if(msh->cod != 1)
1618 SwpWrd(FilPos, SizTab[ FilTyp[j] ]);
1619
1620 // Reorder HO nodes on the fly
1621 if(kwd->OrdTab && (j != kwd->SolSiz-1))
1622 k = kwd->OrdTab[j];
1623 else
1624 k = j;
1625
1626 if(IntMapTab)
1627 UsrDat[j] = UsrBas[k] + (IntMapTab[ OldIdx ] - 1) * UsrLen[k];
1628 else if(LngMapTab)
1629 UsrDat[j] = UsrBas[k] + (LngMapTab[ OldIdx ] - 1) * UsrLen[k];
1630 else
1631 UsrDat[j] = UsrBas[k] + (OldIdx - 1) * UsrLen[k];
1632
1633 if(FilTyp[j] == GmfInt)
1634 {
1635 FilPtrI32 = (int *)FilPos;
1636
1637 if(UsrTyp[j] == GmfInt)
1638 {
1639 UsrPtrI32 = (int *)UsrDat[j];
1640 *UsrPtrI32 = *FilPtrI32;
1641 }
1642 else
1643 {
1644 UsrPtrI64 = (int64_t *)UsrDat[j];
1645 *UsrPtrI64 = (int64_t)*FilPtrI32;
1646 }
1647 }
1648 else if(FilTyp[j] == GmfLong)
1649 {
1650 FilPtrI64 = (int64_t *)FilPos;
1651
1652 if(UsrTyp[j] == GmfLong)
1653 {
1654 UsrPtrI64 = (int64_t *)UsrDat[j];
1655 *UsrPtrI64 = *FilPtrI64;
1656 }
1657 else
1658 {
1659 UsrPtrI32 = (int *)UsrDat[j];
1660 *UsrPtrI32 = (int)*FilPtrI64;
1661 }
1662 }
1663 else if(FilTyp[j] == GmfFloat)
1664 {
1665 FilPtrR32 = (float *)FilPos;
1666
1667 if(UsrTyp[j] == GmfFloat)
1668 {
1669 UsrPtrR32 = (float *)UsrDat[j];
1670 *UsrPtrR32 = *FilPtrR32;
1671 }
1672 else
1673 {
1674 UsrPtrR64 = (double *)UsrDat[j];
1675 *UsrPtrR64 = (double)*FilPtrR32;
1676 }
1677 }
1678 else if(FilTyp[j] == GmfDouble)
1679 {
1680 FilPtrR64 = (double *)FilPos;
1681
1682 if(UsrTyp[j] == GmfDouble)
1683 {
1684 UsrPtrR64 = (double *)UsrDat[j];
1685 *UsrPtrR64 = *FilPtrR64;
1686 }
1687 else
1688 {
1689 UsrPtrR32 = (float *)UsrDat[j];
1690 *UsrPtrR32 = (float)*FilPtrR64;
1691 }
1692 }
1693
1694 FilPos += SizTab[ FilTyp[j] ];
1695 }
1696 }
1697
1698 // Call the user's preprocessing procedure
1699 if(UsrPrc)
1700 #ifdef F77API
1701 CalF77Prc(BlkBegIdx, BlkEndIdx, UsrPrc, NmbArg, ArgTab);
1702 #else
1703 UsrPrc(BlkBegIdx, BlkEndIdx, UsrArg);
1704 #endif
1705 }
1706 }
1707
1708 free(BckBuf);
1709 free(FrtBuf);
1710 }
1711
1712 return(1);
1713 }
1714
1715
1716 /*----------------------------------------------------------------------------*/
1717 /* Bufferized writing of all keyword's lines */
1718 /*----------------------------------------------------------------------------*/
1719
1720 int NAMF77(GmfSetBlock, gmfsetblock)( TYPF77(int64_t) MshIdx,
1721 TYPF77(int) KwdCod,
1722 TYPF77(int64_t) BegIdx,
1723 TYPF77(int64_t) EndIdx,
1724 TYPF77(int) MapTyp,
1725 void *MapTab,
1726 void *prc, ... )
1727 {
1728 char *UsrDat[ GmfMaxTyp ], *UsrBas[ GmfMaxTyp ], *EndUsrDat;
1729 char *StrTab[5] = { "", "%g", "%.15g", "%d", "%lld" }, *FilPos;
1730 char *FilBuf = NULL, *FrtBuf = NULL, *BckBuf = NULL;
1731 int i, j, LinSiz, *FilPtrI32, *UsrPtrI32, FilTyp[ GmfMaxTyp ];
1732 int UsrTyp[ GmfMaxTyp ], NmbBlk, b, SizTab[5] = {0,4,8,4,8};
1733 int err, *IntMapTab = NULL, RepCnt;
1734 float *FilPtrR32, *UsrPtrR32;
1735 double *FilPtrR64, *UsrPtrR64;
1736 int64_t UsrNmbLin, BlkNmbLin = 0, BlkBegIdx, BlkEndIdx = 0;
1737 int64_t *FilPtrI64, *UsrPtrI64, *LngMapTab = NULL, OldIdx = 0;
1738 int64_t FilBegIdx = VALF77(BegIdx), FilEndIdx = VALF77(EndIdx);
1739 void (*UsrPrc)(int64_t, int64_t, void *) = NULL;
1740 size_t UsrLen[ GmfMaxTyp ], ret;
1741 va_list VarArg;
1742 GmfMshSct *msh = (GmfMshSct *) VALF77(MshIdx);
1743 KwdSct *kwd = &msh->KwdTab[ VALF77(KwdCod) ];
1744 struct aiocb aio;
1745 #ifdef F77API
1746 int NmbArg = 0;
1747 void *ArgTab[ MaxArg ];
1748 #else
1749 char *UsrArg = NULL;
1750 #endif
1751
1752 // Save the current stack environment for longjmp
1753 if(setjmp(msh->err) != 0)
1754 {
1755 if(FilBuf)
1756 free(FilBuf);
1757
1758 return(0);
1759 }
1760
1761 // Check mesh and keyword
1762 if( (VALF77(KwdCod) < 1) || (VALF77(KwdCod) > GmfMaxKwd) || !kwd->NmbLin )
1763 return(0);
1764
1765 // Make sure it's not a simple information keyword
1766 if( (kwd->typ != RegKwd) && (kwd->typ != SolKwd) )
1767 return(0);
1768
1769 // Temporarily overwright the given begin and end values
1770 // as arbitrary position block write is not yet implemented
1771 FilBegIdx = 1;
1772 FilEndIdx = kwd->NmbLin;
1773
1774 // Check user's bounds
1775 if( (FilBegIdx < 1) || (FilBegIdx > FilEndIdx) || (FilEndIdx > kwd->NmbLin) )
1776 return(0);
1777
1778 // Compute the number of lines to be written
1779 UsrNmbLin = FilEndIdx - FilBegIdx + 1;
1780
1781 // Get the renumbering map if any
1782 if(VALF77(MapTyp) == GmfInt)
1783 IntMapTab = (int *)MapTab;
1784 else if(VALF77(MapTyp) == GmfLong)
1785 LngMapTab = (int64_t *)MapTab;
1786
1787 // Start decoding the arguments
1788 va_start(VarArg, prc);
1789 LinSiz = 0;
1790
1791 // Get the user's postprocessing procedure and argument adresses, if any
1792 #ifdef F77API
1793 if(prc)
1794 {
1795 UsrPrc = (void (*)(int64_t, int64_t, void *))prc;
1796 NmbArg = *(va_arg(VarArg, int *));
1797
1798 for(i=0;i<NmbArg;i++)
1799 ArgTab[i] = va_arg(VarArg, void *);
1800 }
1801 #else
1802 if(prc)
1803 {
1804 UsrPrc = (void (*)(int64_t, int64_t, void *))prc;
1805 UsrArg = va_arg(VarArg, void *);
1806 }
1807 #endif
1808
1809 // Get the user's data type and pointers to first
1810 // and last adresses in order to compute the stride
1811 if(kwd->typ == RegKwd)
1812 {
1813 for(i=0;i<kwd->SolSiz;i++)
1814 {
1815 // Get the type from the variable arguments
1816 UsrTyp[i] = VALF77(va_arg(VarArg, TYPF77(int)));
1817
1818 // If a table is given, read its size in the next argument
1819 // and automatically fill the pointer table by incrementing
1820 // as many times the base user address
1821 if(UsrTyp[i] == GmfIntTab)
1822 {
1823 RepCnt = VALF77(va_arg(VarArg, TYPF77(int)));
1824 UsrTyp[i] = GmfInt;
1825 }
1826 else if(UsrTyp[i] == GmfLongTab)
1827 {
1828 RepCnt = VALF77(va_arg(VarArg, TYPF77(int)));
1829 UsrTyp[i] = GmfLong;
1830 }
1831 else
1832 RepCnt = 0;
1833
1834 // Get the begin and end pointers from the variable arguments
1835 UsrDat[i] = UsrBas[i] = va_arg(VarArg, char *);
1836 EndUsrDat = va_arg(VarArg, char *);
1837
1838 if(UsrNmbLin > 1)
1839 UsrLen[i] = (size_t)(EndUsrDat - UsrDat[i]) / (UsrNmbLin - 1);
1840 else
1841 UsrLen[i] = 0;
1842
1843 // Replicate the table data type and increment the base address
1844 if(RepCnt >= 2)
1845 {
1846 for(j=i+1; j<i+RepCnt; j++)
1847 {
1848 UsrTyp[j] = UsrTyp[i];
1849 UsrDat[j] = UsrBas[j] = UsrDat[ j-1 ] + SizTab[ UsrTyp[i] ];
1850 UsrLen[j] = UsrLen[i];
1851 }
1852
1853 i += RepCnt - 1;
1854 }
1855 }
1856 }
1857 else if(kwd->typ == SolKwd)
1858 {
1859 // Get the type, begin and end pointers from the variable arguments
1860 UsrTyp[0] = VALF77(va_arg(VarArg, TYPF77(int)));;
1861 UsrDat[0] = UsrBas[0] = va_arg(VarArg, char *);
1862 EndUsrDat = va_arg(VarArg, char *);
1863
1864 if(UsrNmbLin > 1)
1865 UsrLen[0] = (size_t)(EndUsrDat - UsrDat[0]) / (UsrNmbLin - 1);
1866 else
1867 UsrLen[0] = 0;
1868
1869 // Solutions use only on set of type/begin/end pointers
1870 // and the base adress is incremented for each entry
1871 for(i=1;i<kwd->SolSiz;i++)
1872 {
1873 UsrTyp[i] = UsrTyp[0];
1874 UsrDat[i] = UsrBas[i] = UsrDat[ i-1 ] + SizTab[ UsrTyp[0] ];
1875 UsrLen[i] = UsrLen[0];
1876 }
1877 }
1878 else
1879 return(0);
1880
1881 // Get the file's data type
1882 for(i=0;i<kwd->SolSiz;i++)
1883 {
1884 if(kwd->fmt[i] == 'r')
1885 if(msh->ver <= 1)
1886 FilTyp[i] = GmfFloat;
1887 else
1888 FilTyp[i] = GmfDouble;
1889 else
1890 if(msh->ver <= 3)
1891 FilTyp[i] = GmfInt;
1892 else
1893 FilTyp[i] = GmfLong;
1894
1895 // Compute the file stride
1896 LinSiz += SizTab[ FilTyp[i] ];
1897 }
1898
1899 va_end(VarArg);
1900
1901 // Write the whole kwd data
1902 if(msh->typ & Asc)
1903 {
1904 if(UsrPrc)
1905 #ifdef F77API
1906 CalF77Prc(1, kwd->NmbLin, UsrPrc, NmbArg, ArgTab);
1907 #else
1908 UsrPrc(1, kwd->NmbLin, UsrArg);
1909 #endif
1910
1911 for(i=FilBegIdx; i<=FilEndIdx; i++)
1912 for(j=0;j<kwd->SolSiz;j++)
1913 {
1914 if(UsrTyp[j] == GmfFloat)
1915 {
1916 UsrPtrR32 = (float *)UsrDat[j];
1917 fprintf(msh->hdl, StrTab[ UsrTyp[j] ], (double)*UsrPtrR32);
1918 }
1919 else if(UsrTyp[j] == GmfDouble)
1920 {
1921 UsrPtrR64 = (double *)UsrDat[j];
1922 fprintf(msh->hdl, StrTab[ UsrTyp[j] ], *UsrPtrR64);
1923 }
1924 else if(UsrTyp[j] == GmfInt)
1925 {
1926 UsrPtrI32 = (int *)UsrDat[j];
1927 fprintf(msh->hdl, StrTab[ UsrTyp[j] ], *UsrPtrI32);
1928 }
1929 else if(UsrTyp[j] == GmfLong)
1930 {
1931 UsrPtrI64 = (int64_t *)UsrDat[j];
1932 fprintf(msh->hdl, StrTab[ UsrTyp[j] ], *UsrPtrI64);
1933 }
1934
1935 if(j < kwd->SolSiz -1)
1936 fprintf(msh->hdl, " ");
1937 else
1938 fprintf(msh->hdl, "\n");
1939
1940 UsrDat[j] += UsrLen[j];
1941 }
1942 }
1943 else
1944 {
1945 // Allocate the front and back buffers
1946 if(!(BckBuf = malloc((size_t)BufSiz * (size_t)LinSiz)))
1947 return(0);
1948
1949 if(!(FrtBuf = malloc((size_t)BufSiz * (size_t)LinSiz)))
1950 return(0);
1951
1952 // Setup the asynchronous parameters
1953 memset(&aio, 0, sizeof(struct aiocb));
1954 FilBuf = BckBuf;
1955 #ifdef WITH_AIO
1956 aio.aio_fildes = msh->FilDes;
1957 #else
1958 aio.aio_fildes = msh->hdl;
1959 #endif
1960 aio.aio_offset = GetFilPos(msh);
1961
1962 NmbBlk = UsrNmbLin / BufSiz;
1963
1964 // Loop over N+1 blocks
1965 for(b=0;b<=NmbBlk+1;b++)
1966 {
1967 // Launch an asynchronous block write
1968 // except for the first loop iteration
1969 if(b)
1970 {
1971 aio.aio_nbytes = BlkNmbLin * LinSiz;
1972
1973 if(aio_write(&aio) == -1)
1974 {
1975 #ifdef WITH_AIO
1976 printf("aio_fildes = %d\n",aio.aio_fildes);
1977 #else
1978 printf("aio_fildes = %p\n",aio.aio_fildes);
1979 #endif
1980 printf("aio_buf = %p\n",aio.aio_buf);
1981 printf("aio_offset = " INT64_T_FMT "\n",(int64_t)aio.aio_offset);
1982 printf("aio_nbytes = " INT64_T_FMT "\n",(int64_t)aio.aio_nbytes);
1983 printf("errno = %d\n",errno);
1984 exit(1);
1985 }
1986 }
1987
1988 // Parse the block data except at the last loop iteration
1989 if(b<=NmbBlk)
1990 {
1991 // The last block is shorter
1992 if(b == NmbBlk)
1993 BlkNmbLin = UsrNmbLin - b * BufSiz;
1994 else
1995 BlkNmbLin = BufSiz;
1996
1997 FilPos = FilBuf;
1998 BlkBegIdx = BlkEndIdx+1;
1999 BlkEndIdx += BlkNmbLin;
2000
2001 // Call user's preprocessing first
2002 if(UsrPrc)
2003 #ifdef F77API
2004 CalF77Prc(BlkBegIdx, BlkEndIdx, UsrPrc, NmbArg, ArgTab);
2005 #else
2006 UsrPrc(BlkBegIdx, BlkEndIdx, UsrArg);
2007 #endif
2008
2009 // Then copy it's data to the file buffer
2010 for(i=0;i<BlkNmbLin;i++)
2011 {
2012 OldIdx++;
2013
2014 for(j=0;j<kwd->SolSiz;j++)
2015 {
2016 if(IntMapTab)
2017 UsrDat[j] = UsrBas[j] + (IntMapTab[ OldIdx ] - 1) * UsrLen[j];
2018 else if(LngMapTab)
2019 UsrDat[j] = UsrBas[j] + (LngMapTab[ OldIdx ] - 1) * UsrLen[j];
2020 else
2021 UsrDat[j] = UsrBas[j] + (OldIdx - 1) * UsrLen[j];
2022
2023 if(FilTyp[j] == GmfInt)
2024 {
2025 FilPtrI32 = (int *)FilPos;
2026
2027 if(UsrTyp[j] == GmfInt)
2028 {
2029 UsrPtrI32 = (int *)UsrDat[j];
2030 *FilPtrI32 = *UsrPtrI32;
2031 }
2032 else
2033 {
2034 UsrPtrI64 = (int64_t *)UsrDat[j];
2035 *FilPtrI32 = (int)*UsrPtrI64;
2036 }
2037 }
2038 else if(FilTyp[j] == GmfLong)
2039 {
2040 FilPtrI64 = (int64_t *)FilPos;
2041
2042 if(UsrTyp[j] == GmfLong)
2043 {
2044 UsrPtrI64 = (int64_t *)UsrDat[j];
2045 *FilPtrI64 = *UsrPtrI64;
2046 }
2047 else
2048 {
2049 UsrPtrI32 = (int *)UsrDat[j];
2050 *FilPtrI64 = (int64_t)*UsrPtrI32;
2051 }
2052 }
2053 else if(FilTyp[j] == GmfFloat)
2054 {
2055 FilPtrR32 = (float *)FilPos;
2056
2057 if(UsrTyp[j] == GmfFloat)
2058 {
2059 UsrPtrR32 = (float *)UsrDat[j];
2060 *FilPtrR32 = *UsrPtrR32;
2061 }
2062 else
2063 {
2064 UsrPtrR64 = (double *)UsrDat[j];
2065 *FilPtrR32 = (float)*UsrPtrR64;
2066 }
2067 }
2068 else if(FilTyp[j] == GmfDouble)
2069 {
2070 FilPtrR64 = (double *)FilPos;
2071
2072 if(UsrTyp[j] == GmfDouble)
2073 {
2074 UsrPtrR64 = (double *)UsrDat[j];
2075 *FilPtrR64 = *UsrPtrR64;
2076 }
2077 else
2078 {
2079 UsrPtrR32 = (float *)UsrDat[j];
2080 *FilPtrR64 = (double)*UsrPtrR32;
2081 }
2082 }
2083
2084 FilPos += SizTab[ FilTyp[j] ];
2085 }
2086 }
2087 }
2088
2089 // Wait for write completion execpt at the first loop iteration
2090 if(b)
2091 {
2092 while(aio_error(&aio) == EINPROGRESS);
2093
2094 err = aio_error(&aio);
2095 ret = aio_return(&aio);
2096
2097 if (err != 0) {
2098 printf (" Error at aio_error() : %s\n", strerror (err));
2099 exit(1);
2100 }
2101
2102 if (ret != aio.aio_nbytes) {
2103 printf(" Error at aio_return()\n");
2104 exit(1);
2105 }
2106
2107 // Move the write position
2108 aio.aio_offset += aio.aio_nbytes;
2109 }
2110
2111 // Swap the buffers
2112 if(FilBuf == BckBuf)
2113 {
2114 aio.aio_buf = BckBuf;
2115 FilBuf = FrtBuf;
2116 }
2117 else
2118 {
2119 aio.aio_buf = FrtBuf;
2120 FilBuf = BckBuf;
2121 }
2122 }
2123
2124 SetFilPos(msh, aio.aio_offset);
2125 free(BckBuf);
2126 free(FrtBuf);
2127 }
2128
2129 return(1);
2130 }
2131
2132
2133 /*----------------------------------------------------------------------------*/
2134 /* Map two HO element's nodes numbering orders */
2135 /*----------------------------------------------------------------------------*/
2136
2137 int GmfSetHONodesOrdering(int64_t MshIdx, int KwdCod, int *BasTab, int *OrdTab)
2138 {
2139 int i, j, k, flg, NmbNod, NmbCrd;
2140 GmfMshSct *msh = (GmfMshSct *)MshIdx;
2141 KwdSct *kwd;
2142
2143 if( (KwdCod < 1) || (KwdCod > GmfMaxKwd) )
2144 return(0);
2145
2146 kwd = &msh->KwdTab[ KwdCod ];
2147
2148 // Find the Bezier indices dimension according to the element's kind
2149 switch(KwdCod)
2150 {
2151 case GmfEdgesP2 : NmbNod = 3; NmbCrd = 1; break;
2152 case GmfEdgesP3 : NmbNod = 4; NmbCrd = 1; break;
2153 case GmfTrianglesP2 : NmbNod = 6; NmbCrd = 3; break;
2154 case GmfTrianglesP3 : NmbNod = 10; NmbCrd = 3; break;
2155 case GmfQuadrilateralsQ2 : NmbNod = 9; NmbCrd = 2; break;
2156 case GmfQuadrilateralsQ3 : NmbNod = 16; NmbCrd = 2; break;
2157 case GmfTetrahedraP2 : NmbNod = 10; NmbCrd = 4; break;
2158 case GmfTetrahedraP3 : NmbNod = 20; NmbCrd = 4; break;
2159 case GmfPyramidsP2 : NmbNod = 14; NmbCrd = 3; break;
2160 case GmfPyramidsP3 : NmbNod = 30; NmbCrd = 3; break;
2161 case GmfPrismsP2 : NmbNod = 18; NmbCrd = 4; break;
2162 case GmfPrismsP3 : NmbNod = 40; NmbCrd = 4; break;
2163 case GmfHexahedraQ2 : NmbNod = 27; NmbCrd = 3; break;
2164 case GmfHexahedraQ3 : NmbNod = 64; NmbCrd = 3; break;
2165 default : return(0);
2166 }
2167
2168 // Free and rebuild the mapping table it there were already one
2169 if(kwd->OrdTab)
2170 free(kwd->OrdTab);
2171
2172 if(!(kwd->OrdTab = malloc(NmbNod * sizeof(int))))
2173 return(0);
2174
2175 // Find the corresponding Bezier coordinates from the source table
2176 for(i=0;i<NmbNod;i++)
2177 {
2178 for(j=0;j<NmbNod;j++)
2179 {
2180 flg = 1;
2181
2182 for(k=0;k<NmbCrd;k++)
2183 if(BasTab[ i * NmbCrd + k ] != OrdTab[ j * NmbCrd + k ])
2184 {
2185 flg = 0;
2186 break;
2187 }
2188
2189 if(flg)
2190 kwd->OrdTab[j] = i;
2191 }
2192 }
2193
2194 for(i=0;i<NmbNod;i++)
2195 printf("%d : %d\n",i,kwd->OrdTab[i]);
2196
2197 return(1);
2198 }
2199
2200 #endif
2201
2202
2203 #ifndef F77API
2204
2205
2206 /*----------------------------------------------------------------------------*/
2207 /* Read an EGADS binary CAD and return the byte flow and its exact byte size */
2208 /*----------------------------------------------------------------------------*/
2209
2210 char *GmfReadByteFlow(int64_t MshIdx, int *NmbByt)
2211 {
2212 int i, cod, *WrdTab, NmbWrd;
2213 GmfMshSct *msh = (GmfMshSct *)MshIdx;
2214
2215 if(!(NmbWrd = GmfStatKwd(MshIdx, GmfByteFlow)))
2216 return(NULL);
2217
2218 if(!(WrdTab = malloc(NmbWrd * WrdSiz)))
2219 return(NULL);
2220
2221 cod = msh->cod;
2222 msh->cod = 1;
2223
2224 GmfGotoKwd(MshIdx, GmfByteFlow);
2225 GmfGetLin(MshIdx, GmfByteFlow, NmbByt);
2226
2227 for(i=0;i<NmbWrd;i++)
2228 GmfGetLin(MshIdx, GmfByteFlow, &WrdTab[i]);
2229
2230 msh->cod = cod;
2231
2232 return((char *)WrdTab);
2233 }
2234
2235
2236 /*----------------------------------------------------------------------------*/
2237 /* Write an EGADS binary CAD as an integer table whose first entry is the size*/
2238 /*----------------------------------------------------------------------------*/
2239
2240 int GmfWriteByteFlow(int64_t MshIdx, char *BytTab, int NmbByt)
2241 {
2242 int i, *WrdTab = (int *)BytTab, NmbWrd = NmbByt / WrdSiz + 1;
2243
2244 if(!GmfSetKwd(MshIdx, GmfByteFlow, NmbWrd + 1))
2245 return(0);
2246
2247 GmfSetLin(MshIdx, GmfByteFlow, NmbByt);
2248
2249 for(i=0;i<NmbWrd;i++)
2250 GmfSetLin(MshIdx, GmfByteFlow, WrdTab[i]);
2251
2252 return(1);
2253 }
2254 #endif
2255
2256
2257 /*----------------------------------------------------------------------------*/
2258 /* Find every kw present in a meshfile */
2259 /*----------------------------------------------------------------------------*/
2260
2261 static int ScaKwdTab(GmfMshSct *msh)
2262 {
2263 int KwdCod, c;
2264 int64_t NexPos, EndPos;
2265 char str[ GmfStrSiz ];
2266
2267 if(msh->typ & Asc)
2268 {
2269 // Scan each string in the file until the end
2270 while(fscanf(msh->hdl, "%s", str) != EOF)
2271 {
2272 // Fast test in order to reject quickly the numeric values
2273 if(isalpha(str[0]))
2274 {
2275 // Search which kwd code this string is associated with, then get its
2276 // header and save the curent position in file (just before the data)
2277 for(KwdCod=1; KwdCod<= GmfMaxKwd; KwdCod++)
2278 if(!strcmp(str, GmfKwdFmt[ KwdCod ][0]))
2279 {
2280 ScaKwdHdr(msh, KwdCod);
2281 break;
2282 }
2283 }
2284 else if(str[0] == '#')
2285 while((c = fgetc(msh->hdl)) != '\n' && c != EOF);
2286 }
2287 }
2288 else
2289 {
2290 // Get file size
2291 EndPos = GetFilSiz(msh);
2292
2293 // Jump through kwd positions in the file
2294 do
2295 {
2296 // Get the kwd code and the next kwd position
2297 ScaWrd(msh, ( char *)&KwdCod);
2298 NexPos = GetPos(msh);
2299
2300 if(NexPos > EndPos)
2301 longjmp(msh->err, -1);
2302
2303 // Check if this kwd belongs to this mesh version
2304 if( (KwdCod >= 1) && (KwdCod <= GmfMaxKwd) )
2305 ScaKwdHdr(msh, KwdCod);
2306
2307 // Go to the next kwd
2308 if(NexPos && !(SetFilPos(msh, NexPos)))
2309 longjmp(msh->err, -1);
2310 }while(NexPos && (KwdCod != GmfEnd));
2311 }
2312
2313 return(1);
2314 }
2315
2316
2317 /*----------------------------------------------------------------------------*/
2318 /* Read and setup the keyword's header */
2319 /*----------------------------------------------------------------------------*/
2320
2321 static void ScaKwdHdr(GmfMshSct *msh, int KwdCod)
2322 {
2323 int i;
2324 KwdSct *kwd = &msh->KwdTab[ KwdCod ];
2325
2326 if(!strcmp("i", GmfKwdFmt[ KwdCod ][1]))
2327 if(msh->typ & Asc)
2328 safe_fscanf(msh->hdl, INT64_T_FMT, &kwd->NmbLin, msh->err);
2329 else
2330 if(msh->ver <= 3)
2331 {
2332 ScaWrd(msh, (unsigned char *)&i);
2333 kwd->NmbLin = i;
2334 }
2335 else
2336 ScaDblWrd(msh, (unsigned char *)&kwd->NmbLin);
2337 else
2338 kwd->NmbLin = 1;
2339
2340 if(!strcmp("sr", GmfKwdFmt[ KwdCod ][2])
2341 || !strcmp("hr", GmfKwdFmt[ KwdCod ][2]) )
2342 {
2343 if(msh->typ & Asc)
2344 {
2345 safe_fscanf(msh->hdl, "%d", &kwd->NmbTyp, msh->err);
2346
2347 for(i=0;i<kwd->NmbTyp;i++)
2348 safe_fscanf(msh->hdl, "%d", &kwd->TypTab[i], msh->err);
2349
2350 // Scan two extra fields for HO solutions: deg and nmb Nodes
2351 if(!strcmp("hr", GmfKwdFmt[ KwdCod ][2]))
2352 {
2353 safe_fscanf(msh->hdl, "%d", &kwd->deg, msh->err);
2354 safe_fscanf(msh->hdl, "%d", &kwd->NmbNod, msh->err);
2355 }
2356 else
2357 {
2358 kwd->deg = 0;
2359 kwd->NmbNod = 1;
2360 }
2361
2362 }
2363 else
2364 {
2365 ScaWrd(msh, (unsigned char *)&kwd->NmbTyp);
2366
2367 for(i=0;i<kwd->NmbTyp;i++)
2368 ScaWrd(msh, (unsigned char *)&kwd->TypTab[i]);
2369
2370 // Scan two extra fields for HO solutions: deg and nmb Nodes
2371 if(!strcmp("hr", GmfKwdFmt[ KwdCod ][2]))
2372 {
2373 ScaWrd(msh, (unsigned char *)&kwd->deg);
2374 ScaWrd(msh, (unsigned char *)&kwd->NmbNod);
2375 }
2376 else
2377 {
2378 kwd->deg = 0;
2379 kwd->NmbNod = 1;
2380 }
2381 }
2382 }
2383
2384 ExpFmt(msh, KwdCod);
2385 kwd->pos = GetFilPos(msh);
2386 }
2387
2388
2389 /*----------------------------------------------------------------------------*/
2390 /* Expand the compacted format and compute the line size */
2391 /*----------------------------------------------------------------------------*/
2392
2393 static void ExpFmt(GmfMshSct *msh, int KwdCod)
2394 {
2395 int i, j, TmpSiz=0, IntWrd, FltWrd;
2396 char chr;
2397 const char *InpFmt = GmfKwdFmt[ KwdCod ][2];
2398 KwdSct *kwd = &msh->KwdTab[ KwdCod ];
2399
2400 // Set the kwd's type
2401 if(!strlen(GmfKwdFmt[ KwdCod ][1]))
2402 kwd->typ = InfKwd;
2403 else if( !strcmp(InpFmt, "sr") || !strcmp(InpFmt, "hr") )
2404 kwd->typ = SolKwd;
2405 else
2406 kwd->typ = RegKwd;
2407
2408 // Get the solution-field's size
2409 if(kwd->typ == SolKwd)
2410 for(i=0;i<kwd->NmbTyp;i++)
2411 switch(kwd->TypTab[i])
2412 {
2413 case GmfSca : TmpSiz += 1; break;
2414 case GmfVec : TmpSiz += msh->dim; break;
2415 case GmfSymMat : TmpSiz += (msh->dim * (msh->dim+1)) / 2; break;
2416 case GmfMat : TmpSiz += msh->dim * msh->dim; break;
2417 }
2418
2419 // Scan each character from the format string
2420 i = kwd->SolSiz = kwd->NmbWrd = 0;
2421
2422 while(i < (int)strlen(InpFmt))
2423 {
2424 chr = InpFmt[ i++ ];
2425
2426 if(chr == 'd')
2427 {
2428 chr = InpFmt[i++];
2429
2430 for(j=0;j<msh->dim;j++)
2431 kwd->fmt[ kwd->SolSiz++ ] = chr;
2432 }
2433 else if((chr == 's')||(chr == 'h'))
2434 {
2435 chr = InpFmt[i++];
2436
2437 for(j=0;j<TmpSiz;j++)
2438 kwd->fmt[ kwd->SolSiz++ ] = chr;
2439 }
2440 else
2441 kwd->fmt[ kwd->SolSiz++ ] = chr;
2442 }
2443
2444 if(msh->ver <= 1)
2445 FltWrd = 1;
2446 else
2447 FltWrd = 2;
2448
2449 if(msh->ver <= 3)
2450 IntWrd = 1;
2451 else
2452 IntWrd = 2;
2453
2454 for(i=0;i<kwd->SolSiz;i++)
2455 switch(kwd->fmt[i])
2456 {
2457 case 'i' : kwd->NmbWrd += IntWrd; break;
2458 case 'c' : kwd->NmbWrd += FilStrSiz; break;
2459 case 'r' : kwd->NmbWrd += FltWrd;break;
2460 }
2461
2462 // HO solution: duplicate the format as many times as the number of nodes
2463 if( !strcmp(InpFmt, "hr") && (kwd->NmbNod > 1) )
2464 {
2465 for(i=1;i<=kwd->NmbNod;i++)
2466 for(j=0;j<kwd->SolSiz;j++)
2467 kwd->fmt[ i * kwd->SolSiz + j ] = kwd->fmt[j];
2468
2469 kwd->SolSiz *= kwd->NmbNod;
2470 kwd->NmbWrd *= kwd->NmbNod;
2471 }
2472 }
2473
2474
2475 /*----------------------------------------------------------------------------*/
2476 /* Read a four bytes word from a mesh file */
2477 /*----------------------------------------------------------------------------*/
2478
2479 static void ScaWrd(GmfMshSct *msh, void *ptr)
2480 {
2481 #ifdef WITH_AIO
2482 if(read(msh->FilDes, ptr, WrdSiz) != WrdSiz)
2483 #else
2484 if(fread(ptr, WrdSiz, 1, msh->hdl) != 1)
2485 #endif
2486 longjmp(msh->err, -1);
2487
2488 if(msh->cod != 1)
2489 SwpWrd((char *)ptr, WrdSiz);
2490 }
2491
2492
2493 /*----------------------------------------------------------------------------*/
2494 /* Read an eight bytes word from a mesh file */
2495 /*----------------------------------------------------------------------------*/
2496
2497 static void ScaDblWrd(GmfMshSct *msh, void *ptr)
2498 {
2499 #ifdef WITH_AIO
2500 if(read(msh->FilDes, ptr, WrdSiz * 2) != WrdSiz * 2)
2501 #else
2502 if( fread(ptr, WrdSiz, 2, msh->hdl) != 2 )
2503 #endif
2504 longjmp(msh->err, -1);
2505
2506 if(msh->cod != 1)
2507 SwpWrd((char *)ptr, 2 * WrdSiz);
2508 }
2509
2510
2511 /*----------------------------------------------------------------------------*/
2512 /* Read a 4 or 8 bytes position in mesh file */
2513 /*----------------------------------------------------------------------------*/
2514
2515 static int64_t GetPos(GmfMshSct *msh)
2516 {
2517 int IntVal;
2518 int64_t pos;
2519
2520 if(msh->ver >= 3)
2521 ScaDblWrd(msh, (unsigned char*)&pos);
2522 else
2523 {
2524 ScaWrd(msh, (unsigned char*)&IntVal);
2525 pos = (int64_t)IntVal;
2526 }
2527
2528 return(pos);
2529 }
2530
2531
2532 /*----------------------------------------------------------------------------*/
2533 /* Write a four bytes word to a mesh file */
2534 /*----------------------------------------------------------------------------*/
2535
2536 static void RecWrd(GmfMshSct *msh, const void *wrd)
2537 {
2538 // [Bruno] added error control
2539 #ifdef WITH_AIO
2540 if(write(msh->FilDes, wrd, WrdSiz) != WrdSiz)
2541 #else
2542 if(fwrite(wrd, WrdSiz, 1, msh->hdl) != 1)
2543 #endif
2544 longjmp(msh->err,-1);
2545 }
2546
2547
2548 /*----------------------------------------------------------------------------*/
2549 /* Write an eight bytes word to a mesh file */
2550 /*----------------------------------------------------------------------------*/
2551
2552 static void RecDblWrd(GmfMshSct *msh, const void *wrd)
2553 {
2554 // [Bruno] added error control
2555 #ifdef WITH_AIO
2556 if(write(msh->FilDes, wrd, WrdSiz * 2) != WrdSiz*2)
2557 #else
2558 if(fwrite(wrd, WrdSiz, 2, msh->hdl) != 2)
2559 #endif
2560 longjmp(msh->err,-1);
2561 }
2562
2563
2564 /*----------------------------------------------------------------------------*/
2565 /* Write a block of four bytes word to a mesh file */
2566 /*----------------------------------------------------------------------------*/
2567
2568 static void RecBlk(GmfMshSct *msh, const void *blk, int siz)
2569 {
2570 // Copy this line-block into the main mesh buffer
2571 if(siz)
2572 {
2573 memcpy(&msh->blk[ msh->pos ], blk, (size_t)(siz * WrdSiz));
2574 msh->pos += siz * WrdSiz;
2575 }
2576
2577 // When the buffer is full or this procedure is APIF77ed with a 0 size,
2578 // flush the cache on disk
2579
2580 if( (msh->pos > BufSiz) || (!siz && msh->pos) )
2581 {
2582 #ifdef GMF_WINDOWS
2583 /*
2584 * [Bruno] TODO: check that msh->pos is smaller
2585 * than 4G (fits in 32 bits).
2586 * Note: for now, when trying to write more than 4Gb, it will
2587 * trigger an error (longjmp).
2588 * As far as I understand:
2589 * Given that this function just flushes the cache, and given that
2590 * the cache size is 10000 words, this is much much smaller than 4Gb
2591 * so there is probably no problem.
2592 */
2593 #ifdef WITH_AIO
2594 if((int64_t)write(msh->FilDes, msh->blk, (int)msh->pos) != msh->pos)
2595 #else
2596 if(fwrite(msh->blk, 1, (size_t)msh->pos, msh->hdl) != msh->pos)
2597 #endif
2598 longjmp(msh->err, -1);
2599 #else
2600 #ifdef WITH_AIO
2601 if(write(msh->FilDes, msh->blk, msh->pos) != msh->pos)
2602 #else
2603 if(fwrite(msh->blk, 1, msh->pos, msh->hdl) != msh->pos)
2604 #endif
2605 longjmp(msh->err, -1);
2606 #endif
2607 msh->pos = 0;
2608 }
2609 }
2610
2611
2612 /*----------------------------------------------------------------------------*/
2613 /* Write a 4 or 8 bytes position in a mesh file */
2614 /*----------------------------------------------------------------------------*/
2615
2616 static void SetPos(GmfMshSct *msh, int64_t pos)
2617 {
2618 int IntVal;
2619
2620 if(msh->ver >= 3)
2621 RecDblWrd(msh, (unsigned char*)&pos);
2622 else
2623 {
2624 IntVal = (int)pos;
2625 RecWrd(msh, (unsigned char*)&IntVal);
2626 }
2627 }
2628
2629
2630 /*----------------------------------------------------------------------------*/
2631 /* Endianness conversion */
2632 /*----------------------------------------------------------------------------*/
2633
2634 static void SwpWrd(char *wrd, int siz)
2635 {
2636 char swp;
2637 int i;
2638
2639 for(i=0;i<siz/2;i++)
2640 {
2641 swp = wrd[ siz-i-1 ];
2642 wrd[ siz-i-1 ] = wrd[i];
2643 wrd[i] = swp;
2644 }
2645 }
2646
2647
2648 /*----------------------------------------------------------------------------*/
2649 /* Set current position in a file */
2650 /*----------------------------------------------------------------------------*/
2651
2652 static int SetFilPos(GmfMshSct *msh, int64_t pos)
2653 {
2654 if(msh->typ & Bin)
2655 #ifdef WITH_AIO
2656 return((lseek(msh->FilDes, (off_t)pos, 0) != -1));
2657 #else
2658 return((fseek(msh->hdl, (off_t)pos, SEEK_SET) == 0));
2659 #endif
2660 else
2661 return((fseek(msh->hdl, (off_t)pos, SEEK_SET) == 0));
2662 }
2663
2664
2665 /*----------------------------------------------------------------------------*/
2666 /* Get current position in a file */
2667 /*----------------------------------------------------------------------------*/
2668
2669 static int64_t GetFilPos(GmfMshSct *msh)
2670 {
2671 if(msh->typ & Bin)
2672 #ifdef WITH_AIO
2673 return(lseek(msh->FilDes, 0, 1));
2674 #else
2675 return(ftell(msh->hdl));
2676 #endif
2677 else
2678 return(ftell(msh->hdl));
2679 }
2680
2681
2682 /*----------------------------------------------------------------------------*/
2683 /* Move the position to the end of file and return the size */
2684 /*----------------------------------------------------------------------------*/
2685
2686 static int64_t GetFilSiz(GmfMshSct *msh)
2687 {
2688 int64_t CurPos, EndPos = 0;
2689
2690 if(msh->typ & Bin)
2691 {
2692 #ifdef WITH_AIO
2693 CurPos = lseek(msh->FilDes, 0, 1);
2694 EndPos = lseek(msh->FilDes, 0, 2);
2695 lseek(msh->FilDes, (off_t)CurPos, 0);
2696 #else
2697 CurPos = ftell(msh->hdl);
2698
2699 if(fseek(msh->hdl, 0, SEEK_END) != 0)
2700 longjmp(msh->err, -1);
2701
2702 EndPos = ftell(msh->hdl);
2703
2704 if(fseek(msh->hdl, (off_t)CurPos, SEEK_SET) != 0)
2705 longjmp(msh->err, -1);
2706 #endif
2707 }
2708 else
2709 {
2710 CurPos = ftell(msh->hdl);
2711
2712 if(fseek(msh->hdl, 0, SEEK_END) != 0)
2713 longjmp(msh->err, -1);
2714
2715 EndPos = ftell(msh->hdl);
2716
2717 if(fseek(msh->hdl, (off_t)CurPos, SEEK_SET) != 0)
2718 longjmp(msh->err, -1);
2719 }
2720
2721 return(EndPos);
2722 }
2723
2724
2725 /*----------------------------------------------------------------------------*/
2726 /* Fortran 77 API */
2727 /*----------------------------------------------------------------------------*/
2728
2729 #ifdef F77API
2730
2731 int64_t APIF77(gmfopenmesh)( char *FilNam, int *mod,
2732 int *ver, int *dim, int StrSiz )
2733 {
2734 int i = 0;
2735 char TmpNam[ GmfStrSiz ];
2736
2737 if(StrSiz <= 0)
2738 return(0);
2739
2740 // Trim trailing spaces from the fortran string
2741 while(isspace(FilNam[ StrSiz-1 ]))
2742 StrSiz--;
2743
2744 for(i=0;i<StrSiz;i++)
2745 TmpNam[i] = FilNam[i];
2746
2747 TmpNam[ StrSiz ] = 0;
2748
2749 if(*mod == GmfRead)
2750 return(GmfOpenMesh(TmpNam, *mod, ver, dim));
2751 else
2752 return(GmfOpenMesh(TmpNam, *mod, *ver, *dim));
2753 }
2754
2755 int APIF77(gmfclosemesh)(int64_t *idx)
2756 {
2757 return(GmfCloseMesh(*idx));
2758 }
2759
2760 int APIF77(gmfgotokwd)(int64_t *MshIdx, int *KwdIdx)
2761 {
2762 return(GmfGotoKwd(*MshIdx, *KwdIdx));
2763 }
2764
2765 int APIF77(gmfstatkwd)( int64_t *MshIdx, int *KwdIdx, int *NmbTyp,
2766 int *SolSiz, int *TypTab)
2767 {
2768 if(!strcmp(GmfKwdFmt[ *KwdIdx ][2], "sr"))
2769 return(GmfStatKwd(*MshIdx, *KwdIdx, NmbTyp, SolSiz, TypTab));
2770 else
2771 return(GmfStatKwd(*MshIdx, *KwdIdx));
2772 }
2773
2774 int APIF77(gmfsetkwd)( int64_t *MshIdx, int *KwdIdx, int *NmbLin,
2775 int *NmbTyp, int *TypTab)
2776 {
2777 if(!strcmp(GmfKwdFmt[ *KwdIdx ][2], "sr"))
2778 return(GmfSetKwd(*MshIdx, *KwdIdx, *NmbLin, *NmbTyp, TypTab));
2779 else
2780 return(GmfSetKwd(*MshIdx, *KwdIdx, *NmbLin));
2781 }
2782
2783
2784 /*----------------------------------------------------------------------------*/
2785 /* Duplication macros */
2786 /*----------------------------------------------------------------------------*/
2787
2788 #define DUP(s,n) DUP ## n (s)
2789 #define DUP1(s) s
2790 #define DUP2(s) DUP1(s),s
2791 #define DUP3(s) DUP2(s),s
2792 #define DUP4(s) DUP3(s),s
2793 #define DUP5(s) DUP4(s),s
2794 #define DUP6(s) DUP5(s),s
2795 #define DUP7(s) DUP6(s),s
2796 #define DUP8(s) DUP7(s),s
2797 #define DUP9(s) DUP8(s),s
2798 #define DUP10(s) DUP9(s),s
2799 #define DUP11(s) DUP10(s),s
2800 #define DUP12(s) DUP11(s),s
2801 #define DUP13(s) DUP12(s),s
2802 #define DUP14(s) DUP13(s),s
2803 #define DUP15(s) DUP14(s),s
2804 #define DUP16(s) DUP15(s),s
2805 #define DUP17(s) DUP16(s),s
2806 #define DUP18(s) DUP17(s),s
2807 #define DUP19(s) DUP18(s),s
2808 #define DUP20(s) DUP19(s),s
2809
2810
2811 #define ARG(a,n) ARG ## n (a)
2812 #define ARG1(a) a[0]
2813 #define ARG2(a) ARG1(a),a[1]
2814 #define ARG3(a) ARG2(a),a[2]
2815 #define ARG4(a) ARG3(a),a[3]
2816 #define ARG5(a) ARG4(a),a[4]
2817 #define ARG6(a) ARG5(a),a[5]
2818 #define ARG7(a) ARG6(a),a[6]
2819 #define ARG8(a) ARG7(a),a[7]
2820 #define ARG9(a) ARG8(a),a[8]
2821 #define ARG10(a) ARG9(a),a[9]
2822 #define ARG11(a) ARG10(a),a[10]
2823 #define ARG12(a) ARG11(a),a[11]
2824 #define ARG13(a) ARG12(a),a[12]
2825 #define ARG14(a) ARG13(a),a[13]
2826 #define ARG15(a) ARG14(a),a[14]
2827 #define ARG16(a) ARG15(a),a[15]
2828 #define ARG17(a) ARG16(a),a[16]
2829 #define ARG18(a) ARG17(a),a[17]
2830 #define ARG19(a) ARG18(a),a[18]
2831 #define ARG20(a) ARG19(a),a[19]
2832
2833
2834 /*----------------------------------------------------------------------------*/
2835 /* Call a fortran thread with 1 to 20 arguments */
2836 /*----------------------------------------------------------------------------*/
2837
2838 static void CalF77Prc( int64_t BegIdx, int64_t EndIdx,
2839 void *prc, int NmbArg, void **ArgTab )
2840 {
2841 switch(NmbArg)
2842 {
2843 case 1 :
2844 {
2845 void (*prc1)(int64_t *, int64_t *, DUP(void *, 1)) =
2846 (void (*)(int64_t *, int64_t *, DUP(void *, 1)))prc;
2847 prc1(&BegIdx, &EndIdx, ARG(ArgTab, 1));
2848 }break;
2849
2850 case 2 :
2851 {
2852 void (*prc1)(int64_t *, int64_t *, DUP(void *, 2)) =
2853 (void (*)(int64_t *, int64_t *, DUP(void *, 2)))prc;
2854 prc1(&BegIdx, &EndIdx, ARG(ArgTab, 2));
2855 }break;
2856
2857 case 3 :
2858 {
2859 void (*prc1)(int64_t *, int64_t *, DUP(void *, 3)) =
2860 (void (*)(int64_t *, int64_t *, DUP(void *, 3)))prc;
2861 prc1(&BegIdx, &EndIdx, ARG(ArgTab, 3));
2862 }break;
2863
2864 case 4 :
2865 {
2866 void (*prc1)(int64_t *, int64_t *, DUP(void *, 4)) =
2867 (void (*)(int64_t *, int64_t *, DUP(void *, 4)))prc;
2868 prc1(&BegIdx, &EndIdx, ARG(ArgTab, 4));
2869 }break;
2870
2871 case 5 :
2872 {
2873 void (*prc1)(int64_t *, int64_t *, DUP(void *, 5)) =
2874 (void (*)(int64_t *, int64_t *, DUP(void *, 5)))prc;
2875 prc1(&BegIdx, &EndIdx, ARG(ArgTab, 5));
2876 }break;
2877
2878 case 6 :
2879 {
2880 void (*prc1)(int64_t *, int64_t *, DUP(void *, 6)) =
2881 (void (*)(int64_t *, int64_t *, DUP(void *, 6)))prc;
2882 prc1(&BegIdx, &EndIdx, ARG(ArgTab, 6));
2883 }break;
2884
2885 case 7 :
2886 {
2887 void (*prc1)(int64_t *, int64_t *, DUP(void *, 7)) =
2888 (void (*)(int64_t *, int64_t *, DUP(void *, 7)))prc;
2889 prc1(&BegIdx, &EndIdx, ARG(ArgTab, 7));
2890 }break;
2891
2892 case 8 :
2893 {
2894 void (*prc1)(int64_t *, int64_t *, DUP(void *, 8)) =
2895 (void (*)(int64_t *, int64_t *, DUP(void *, 8)))prc;
2896 prc1(&BegIdx, &EndIdx, ARG(ArgTab, 8));
2897 }break;
2898
2899 case 9 :
2900 {
2901 void (*prc1)(int64_t *, int64_t *, DUP(void *, 9)) =
2902 (void (*)(int64_t *, int64_t *, DUP(void *, 9)))prc;
2903 prc1(&BegIdx, &EndIdx, ARG(ArgTab, 9));
2904 }break;
2905
2906 case 10 :
2907 {
2908 void (*prc1)(int64_t *, int64_t *, DUP(void *, 10)) =
2909 (void (*)(int64_t *, int64_t *, DUP(void *, 10)))prc;
2910 prc1(&BegIdx, &EndIdx, ARG(ArgTab, 10));
2911 }break;
2912
2913 case 11 :
2914 {
2915 void (*prc1)(int64_t *, int64_t *, DUP(void *, 11)) =
2916 (void (*)(int64_t *, int64_t *, DUP(void *, 11)))prc;
2917 prc1(&BegIdx, &EndIdx, ARG(ArgTab, 11));
2918 }break;
2919
2920 case 12 :
2921 {
2922 void (*prc1)(int64_t *, int64_t *, DUP(void *, 12)) =
2923 (void (*)(int64_t *, int64_t *, DUP(void *, 12)))prc;
2924 prc1(&BegIdx, &EndIdx, ARG(ArgTab, 12));
2925 }break;
2926
2927 case 13 :
2928 {
2929 void (*prc1)(int64_t *, int64_t *, DUP(void *, 13)) =
2930 (void (*)(int64_t *, int64_t *, DUP(void *, 13)))prc;
2931 prc1(&BegIdx, &EndIdx, ARG(ArgTab, 13));
2932 }break;
2933
2934 case 14 :
2935 {
2936 void (*prc1)(int64_t *, int64_t *, DUP(void *, 14)) =
2937 (void (*)(int64_t *, int64_t *, DUP(void *, 14)))prc;
2938 prc1(&BegIdx, &EndIdx, ARG(ArgTab, 14));
2939 }break;
2940
2941 case 15 :
2942 {
2943 void (*prc1)(int64_t *, int64_t *, DUP(void *, 15)) =
2944 (void (*)(int64_t *, int64_t *, DUP(void *, 15)))prc;
2945 prc1(&BegIdx, &EndIdx, ARG(ArgTab, 15));
2946 }break;
2947
2948 case 16 :
2949 {
2950 void (*prc1)(int64_t *, int64_t *, DUP(void *, 16)) =
2951 (void (*)(int64_t *, int64_t *, DUP(void *, 16)))prc;
2952 prc1(&BegIdx, &EndIdx, ARG(ArgTab, 16));
2953 }break;
2954
2955 case 17 :
2956 {
2957 void (*prc1)(int64_t *, int64_t *, DUP(void *, 17)) =
2958 (void (*)(int64_t *, int64_t *, DUP(void *, 17)))prc;
2959 prc1(&BegIdx, &EndIdx, ARG(ArgTab, 17));
2960 }break;
2961
2962 case 18 :
2963 {
2964 void (*prc1)(int64_t *, int64_t *, DUP(void *, 18)) =
2965 (void (*)(int64_t *, int64_t *, DUP(void *, 18)))prc;
2966 prc1(&BegIdx, &EndIdx, ARG(ArgTab, 18));
2967 }break;
2968
2969 case 19 :
2970 {
2971 void (*prc1)(int64_t *, int64_t *, DUP(void *, 19)) =
2972 (void (*)(int64_t *, int64_t *, DUP(void *, 19)))prc;
2973 prc1(&BegIdx, &EndIdx, ARG(ArgTab, 19));
2974 }break;
2975
2976 case 20 :
2977 {
2978 void (*prc1)(int64_t *, int64_t *, DUP(void *, 20)) =
2979 (void (*)(int64_t *, int64_t *, DUP(void *, 20)))prc;
2980 prc1(&BegIdx, &EndIdx, ARG(ArgTab, 20));
2981 }break;
2982 }
2983 }
2984
2985 #endif