File: gout_c.c

package info (click to toggle)
scotch 5.0.1.dfsg-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 6,088 kB
  • ctags: 4,515
  • sloc: ansic: 46,851; makefile: 2,093; yacc: 588; lex: 263; fortran: 90
file content (762 lines) | stat: -rw-r--r-- 32,593 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
/* Copyright 2004,2007 ENSEIRB, INRIA & CNRS
**
** This file is part of the Scotch software package for static mapping,
** graph partitioning and sparse matrix ordering.
**
** This software is governed by the CeCILL-C license under French law
** and abiding by the rules of distribution of free software. You can
** use, modify and/or redistribute the software under the terms of the
** CeCILL-C license as circulated by CEA, CNRS and INRIA at the following
** URL: "http://www.cecill.info".
** 
** As a counterpart to the access to the source code and rights to copy,
** modify and redistribute granted by the license, users are provided
** only with a limited warranty and the software's author, the holder of
** the economic rights, and the successive licensors have only limited
** liability.
** 
** In this respect, the user's attention is drawn to the risks associated
** with loading, using, modifying and/or developing or reproducing the
** software by the user in light of its specific status of free software,
** that may mean that it is complicated to manipulate, and that also
** therefore means that it is reserved for developers and experienced
** professionals having in-depth computer knowledge. Users are therefore
** encouraged to load and test the software's suitability as regards
** their requirements in conditions enabling the security of their
** systems and/or data to be ensured and, more generally, to use and
** operate it in the same conditions as regards security.
** 
** The fact that you are presently reading this means that you have had
** knowledge of the CeCILL-C license and that you accept its terms.
*/
/************************************************************/
/**                                                        **/
/**   NAME       : gout_c.c                                **/
/**                                                        **/
/**   AUTHOR     : Francois PELLEGRINI                     **/
/**                                                        **/
/**   FUNCTION   : Part of a result viewer.                **/
/**                This module contains the main function. **/
/**                                                        **/
/**   DATES      : # Version 2.0  : from : 06 oct 1994     **/
/**                                 to     23 dec 1994     **/
/**                # Version 3.0  : from : 14 jul 1995     **/
/**                                 to     11 oct 1995     **/
/**                # Version 3.1  : from : 27 mar 1996     **/
/**                                 to     03 apr 1996     **/
/**                # Version 3.2  : from : 02 dec 1996     **/
/**                                 to     05 jun 1998     **/
/**                # Version 3.3  : from : 29 may 1999     **/
/**                                 to   : 03 jun 1999     **/
/**                # Version 3.4  : from : 03 feb 2000     **/
/**                                 to   : 03 feb 2000     **/
/**                # Version 4.0  : from : 11 dec 2001     **/
/**                                 to     08 feb 2004     **/
/**                # Version 5.0  : from : 25 may 2007     **/
/**                                 to     25 may 2007     **/
/**                                                        **/
/************************************************************/

/*
**  The defines and includes
*/

#define GOUT

#include "common.h"
#include "scotch.h"
#include "gout_c.h"
#include "gout_o.h"

/*
**  The static and global variables
*/

static int                  C_fileNum = 0;        /* Number of file in arg list */
File                        C_fileTab[C_FILENBR] = { /* The file array; public  */
                              { "-", NULL, "r" },
                              { "-", NULL, "r" },
                              { "-", NULL, "r" },
                              { "-", NULL, "w" } };

static unsigned int         C_geoFlag = C_GEOFLAGDEFAULT; /* Geometry flag */

static const char *         C_usageList[] = {     /* Usage */
  "gout [<input source file> [<input geometry file> [<input mapping file> [<output picture file>]]]] <options>",
  "  -g<arguments>       : Geometry parameters :",
  "                          n  : do not read geometry data (matrix display)",
  "                          p  : permute Y and Z geometry dimensions",
  "                          r  : rotate geometry by 90 degrees",
  "  -h                  : Display this help",
  "  -mn                 : Do not read mapping data",
  "  -Oi[{<arguments>}]  : Open Inventor mesh file :",
  "                          c        : color output",
  "                          g        : gray level output",
  "                          r        : remove cut edges",
  "                          v        : view cut edges",
  "  -Om[{<arguments>}]  : PostScript matrix file :",
  "                          e        : EPSF-type output",
  "                          f        : full-page output",
  "  -Op[{<arguments>}]  : PostScript mesh file :",
  "                          c        : color output",
  "                          g        : gray level output",
  "                          e        : EPSF-type output",
  "                          f        : full-page output",
  "                          s        : short clipping (disks excluded)",
  "                          l        : large clipping (disks included)",
  "                          a        : avoid displaying disks",
  "                          d        : display disks",
  "                          r        : remove cut edges",
  "                          v        : view cut edges",
  "                          X=<val>  : maximum x clipping ratio (in [0.0;1.0])",
  "                          x=<val>  : minimum x clipping ratio",
  "                          Y=<val>  : maximum y clipping ratio",
  "                          y=<val>  : minimum y clipping ratio",
  "  -V                  : Print program version and copyright",
  "",
  "Default option set is : -Oi{c,v}",
  NULL };

/*****************************/
/*                           */
/* This is the main function */
/*                           */
/*****************************/

int
main (
int                         argc,
char *                      argv[])
{
  C_Graph            grafdat;                     /* Source graph   */
  C_Geometry         geo;                         /* Graph geometry */
  C_Mapping          map;                         /* Result mapping */
  int                i, j;

  errorProg ("gout");

  if ((argc >= 2) && (argv[1][0] == '?')) {       /* If need for help */
    usagePrint (stdout, C_usageList);
    return     (0);
  }

  for (i = 0; i < C_FILENBR; i ++)                /* Set default stream pointers */
    C_fileTab[i].pntr = (C_fileTab[i].mode[0] == 'r') ? stdin : stdout;
  for (i = 1; i < argc; i ++) {                   /* Loop for all option codes */
    if ((argv[i][0] != '+') &&                    /* If found a file name      */
        ((argv[i][0] != '-') || (argv[i][1] == '\0'))) {
      if (C_fileNum < C_FILEARGNBR)               /* File name has been given */
        C_fileTab[C_fileNum ++].name = argv[i];
      else {
        errorPrint ("main: too many file names given");
        return     (1);
      }
    }
    else {                                        /* If found an option name */
      switch (argv[i][1]) {
        case 'G' :                                /* Geometry parameters */
        case 'g' :
          if ((j = C_geoParse (&argv[i][2])) != 0) {
            errorPrint ("main: error in geometry option string (%d)", j);
            return     (1);
          }
          break;
        case 'H' :                                /* Give the usage message */
        case 'h' :
          usagePrint (stdout, C_usageList);
          return     (0);
        case 'M' :                                /* No-mapping flag */
        case 'm' :
          if (((argv[i][2] != 'N') && (argv[i][2] != 'n')) || (argv[i][3] != '\0')) {
            errorPrint ("main: error in mapping option string (%s)", &argv[i][2]);
            return     (1);
          }
          C_filenamemapinp = "-";                 /* Default name to avoid opening   */
          C_filepntrmapinp = NULL;                /* NULL file pointer means no file */
          break;
        case 'O' :                                /* Output parameters */
        case 'o' :
          if ((j = outDrawParse (&argv[i][2])) != 0) {
            errorPrint ("main: error in output option string (%d)", j);
            return     (1);
          }
          break;
        case 'V' :
          fprintf (stderr, "gout, version %s - F. Pellegrini\n", SCOTCH_VERSION);
          fprintf (stderr, "Copyright 2004,2007 ENSEIRB, INRIA & CNRS, France\n");
          fprintf (stderr, "This software is libre/free software under CeCILL-C -- see the user's manual for more information\n");
          return  (0);
        default :
          errorPrint ("main: Unprocessed option (\"%s\")", argv[i]);
          return     (1);
      }
    }
  }

  for (i = 0; i < C_FILENBR; i ++) {              /* For all file names     */
    if ((C_fileTab[i].name[0] != '-') ||          /* If not standard stream */
        (C_fileTab[i].name[1] != '\0')) {
      if ((C_fileTab[i].pntr = fopen (C_fileTab[i].name, C_fileTab[i].mode)) == NULL) { /* Open the file */
        errorPrint ("main: cannot open file (%d)", i);
        return     (1);
      }
    }
  }

  SCOTCH_graphInit (&grafdat.grafdat);            /* Create graph structure         */
  SCOTCH_graphLoad (&grafdat.grafdat, C_filepntrsrcinp, 0, 3); /* Read source graph */
  SCOTCH_graphData (&grafdat.grafdat, &grafdat.baseval,
                    &grafdat.vertnbr, &grafdat.verttab, &grafdat.vendtab, NULL, &grafdat.vlbltab,
                    &grafdat.edgenbr, &grafdat.edgetab, NULL);

  C_geoInit (&geo, &grafdat);                     /* Create geometry structure */
  if (C_geoFlag & C_GEOFLAGUSE)                   /* If geometry is wanted     */
    C_geoLoad (&geo, C_filepntrgeoinp);           /* Read graph geometry       */

  C_mapInit (&map, &grafdat);                     /* Create mapping structure */
  C_mapLoad (&map, C_filepntrmapinp);             /* Read result mapping      */

  outDraw (&grafdat, &geo, &map, C_filepntrdatout); /* Build and write the output */

#ifdef SCOTCH_DEBUG_MAIN1
  C_mapExit        (&map);                        /* Free data structures */
  C_geoExit        (&geo);
  SCOTCH_graphExit (&grafdat);

  for (i = 0; i < C_FILENBR; i ++) {              /* For all file names     */
    if ((C_fileTab[i].name[0] != '-') ||          /* If not standard stream */
        (C_fileTab[i].name[1] != '\0')) {
      fclose (C_fileTab[i].pntr);                 /* Close the stream */
    }
  }
#endif /* SCOTCH_DEBUG_MAIN1 */

  return (0);
}

/***********************************/
/*                                 */
/* These routines handle geometry. */
/*                                 */
/***********************************/

/* This routine parses the source graph
** option string.
** It returns:
** - 0  : if string successfully scanned.
** - 1  : if invalid options
** - 2  : if invalid option arguments.
** - 3  : if syntax error in string.
*/

int
C_geoParse (
const char * const          string)
{
  const char *        cptr;

  for (cptr = string; ; cptr ++) {
    switch (*cptr) {
      case 'N' :                                  /* Do not read geometry data */
      case 'n' :
        C_geoFlag &= ~C_GEOFLAGUSE;
        break;
      case 'P' :                                  /* Permute Y and Z */
      case 'p' :
        C_geoFlag |= C_GEOFLAGPERMUT;
        break;
      case 'R' :                                  /* If want to rotate */
      case 'r' :
        C_geoFlag |= C_GEOFLAGROTATE;
        break;
      case '\0' :
        return (0);
      default   :
        return (1);
    }
  }
}

/* This routine creates a geometry with
** respect to a given source graph.
** It returns:
** - VOID  : in all cases.
*/

void
C_geoInit (
C_Geometry * const          geomptr,
const C_Graph * const       grafptr)
{
  geomptr->grafptr = grafptr;
  geomptr->verttab = NULL;
}

/* This routine deletes a geometry.
** It returns:
** - VOID  : in all cases.
*/

void
C_geoExit (
C_Geometry * const          geomptr)
{
  if (geomptr->verttab != NULL)                   /* If there is a geometry array */
    memFree (geomptr->verttab);                   /* Free it                      */
}

/* This routine loads a mapping.
** It returns:
** - 0   : on success.
** - !0  : on error.
*/

/* This is the comparison function used by the
   quicksort algorithm, to sort by increasing
   labels.                                     */

static
int
C_geoLoad2 (
const C_VertSort * const    vert0,
const C_VertSort * const    vert1)
{
  return ((vert0->labl > vert1->labl) ?  1 : -1);
}

/** This is the loading routine. **/

int
C_geoLoad (
C_Geometry * const          geomptr,
FILE * const                stream)
{
  C_VertSort *        vertsorttab;                /* Pointer to graph sorting array             */
  int                 vertsortflag;               /* Flag set if graph data sorted by label     */
  C_VertSort *        geomsorttab;                /* Pointer to geometric data sorting array    */
  int                 geomsortflag;               /* Flag set if geometric data sorted by label */
  int                 geomfiletype;               /* Type of geometry file                      */
  SCOTCH_Num          geomfilenbr;                /* Number of geometric coordinates in file    */
  long                geomfileval;                /* Value of maximum size for compatibility    */
  C_GeoVert *         geomfiletab;                /* Pointer to geometric data read from file   */
  long                vertlablval;                /* Value of maximum size for compatibility    */
  SCOTCH_Num          i, j;
  int                 o;

  if ((geomptr->verttab == NULL) &&               /* Allocate geometry if necessary */
      ((geomptr->verttab = (C_GeoVert *) memAlloc (geomptr->grafptr->vertnbr * sizeof (C_GeoVert))) == NULL)) {
    errorPrint ("C_geoLoad: out of memory (1)");
    return     (1);
  }

  if ((fscanf (stream, "%d%ld",                   /* Read type and number of geometry items */
               &geomfiletype,
               &geomfileval) != 2) ||
      (geomfiletype < 1)           ||
      (geomfiletype > 3)           ||
      (geomfileval  < 1)) {
    errorPrint ("C_geoLoad: bad input (1)");
    return     (1);
  }
  geomfilenbr = (SCOTCH_Num) geomfileval;

  if (((geomfiletab = (C_GeoVert  *) memAlloc (geomfilenbr               * sizeof (C_GeoVert)))  == NULL) ||
      ((geomsorttab = (C_VertSort *) memAlloc (geomfilenbr               * sizeof (C_VertSort))) == NULL) ||
      ((vertsorttab = (C_VertSort *) memAlloc (geomptr->grafptr->vertnbr * sizeof (C_VertSort))) == NULL)) {
    errorPrint ("C_geoLoad: out of memory (2)");
    if (geomfiletab != NULL) {
      if (geomsorttab != NULL)
        memFree (geomsorttab);
      memFree (geomfiletab);
    }
    return (1);
  }

  o = 0;
  geomsortflag = 1;                               /* Assume geometry data sorted */
  switch (geomfiletype) {
    case 1 :                                      /* Load 2D coordinates array */
      for (i = 0; (i < geomfilenbr) && (o == 0); i ++) {
        if (fscanf (stream, "%ld%lf", &vertlablval,
                    &geomfiletab[i].x) != 2)
          o = 1;
        geomsorttab[i].labl = (SCOTCH_Num) vertlablval;
        geomfiletab[i].y    =                     /* No Y and Z coordinates */
        geomfiletab[i].z    = 0.0;
        geomsorttab[i].num  = i;

        if (C_geoFlag & C_GEOFLAGROTATE) {        /* Rotate picture if necessary */
          double              t;                  /* Temporary swap variable     */

          t                = geomfiletab[i].y;
          geomfiletab[i].y = geomfiletab[i].x;
          geomfiletab[i].x = - t;
        }

        if ((i > 0) &&                            /* Check if geometry data sorted */
            (geomsorttab[i].labl < geomsorttab[i - 1].labl))
          geomsortflag = 0;                       /* Geometry data not sorted */
      }
      break;
    case 2 :                                      /* Load 2D coordinates array */
      for (i = 0; (i < geomfilenbr) && (o == 0); i ++) {
        if (fscanf (stream, "%ld%lf%lf", &vertlablval,
                    &geomfiletab[i].x,
                    &geomfiletab[i].y) != 3)
          o = 1;
        geomsorttab[i].labl = (SCOTCH_Num) vertlablval;
        geomfiletab[i].z    = 0.0;                /* No Z coordinate */
        geomsorttab[i].num  = i;

        if (C_geoFlag & C_GEOFLAGROTATE) {        /* Rotate picture if necessary */
          double              t;                  /* Temporary swap variable     */

          t                = geomfiletab[i].y;
          geomfiletab[i].y = geomfiletab[i].x;
          geomfiletab[i].x = - t;
        }

        if ((i > 0) &&                            /* Check if geometry data sorted */
            (geomsorttab[i].labl < geomsorttab[i - 1].labl))
          geomsortflag = 0;                       /* Geometry data are not sorted */
      }
      break;
    case 3 :                                      /* Load 3D coordinates array */
      for (i = 0; (i < geomfilenbr) && (o == 0); i ++) {
        if (fscanf (stream, "%ld%lf%lf%lf", &vertlablval,
                    &geomfiletab[i].x,
                    &geomfiletab[i].y,
                    &geomfiletab[i].z) != 4)
          o = 1;
        geomsorttab[i].labl = (SCOTCH_Num) vertlablval;
        geomsorttab[i].num  = i;

        if (C_geoFlag & C_GEOFLAGPERMUT) {        /* Rotate picture if necessary */
          double              t;                  /* Temporary swap variable     */

          t                = geomfiletab[i].z;
          geomfiletab[i].z = geomfiletab[i].y;
          geomfiletab[i].y = t;
        }
        if ((i > 0) &&                            /* Check if geometry data sorted */
            (geomsorttab[i].labl < geomsorttab[i - 1].labl))
          geomsortflag = 0;                       /* Geometry data not sorted */
      }
      break;
    default :
      errorPrint ("C_geoLoad: invalid geometry type (%d)", geomfiletype);
      memFree    (vertsorttab);
      memFree    (geomsorttab);
      memFree    (geomfiletab);
      return     (1);
  }
  if (o != 0) {
    errorPrint ("C_geoLoad: bad input (2)");
    memFree    (vertsorttab);
    memFree    (geomsorttab);
    memFree    (geomfiletab);
    return     (1);
  }

  if (geomsortflag != 1)                          /* If geometry data not sorted        */
    qsort ((char *) geomsorttab, geomfilenbr,     /* Sort sort area by ascending labels */
           sizeof (C_VertSort), (int (*) (const void *, const void *)) C_geoLoad2);
  for (i = 1; i < geomfilenbr; i ++) {            /* Check geometric data integrity */
    if (geomsorttab[i].labl == geomsorttab[i - 1].labl) {
      errorPrint ("C_geoLoad: duplicate vertex label");
      memFree    (vertsorttab);
      memFree    (geomsorttab);
      memFree    (geomfiletab);
      return     (1);
    }
  }

  if (geomptr->grafptr->vlbltab != NULL) {        /* If graph has vertex labels */
    vertsortflag = 1;                             /* Assume graph data sorted   */
    for (i = 0; i < geomptr->grafptr->vertnbr; i ++) {
      vertsorttab[i].labl = geomptr->grafptr->vlbltab[i];
      vertsorttab[i].num  = i;
      if ((i > 0) &&                              /* Check if graph data sorted */
          (vertsorttab[i].labl < vertsorttab[i - 1].labl))
        vertsortflag = 0;                         /* Graph data not sorted */
    }
    if (vertsortflag != 1)                        /* If graph data not sorted                       */
      qsort ((char *) vertsorttab, geomptr->grafptr->vertnbr, /* Sort sort area by ascending labels */
             sizeof (C_VertSort), (int (*) (const void *, const void *)) C_geoLoad2);
  }
  else {                                          /* Graph does not have vertex labels */
    for (i = 0; i < geomptr->grafptr->vertnbr; i ++) {
      vertsorttab[i].labl = i + geomsorttab[0].labl; /* Use first index as base value */
      vertsorttab[i].num  = i;
    }
  }

  for (i = 0, j = 0; i < geomptr->grafptr->vertnbr; i ++) { /* For all vertices in graph */
    while ((j < geomfilenbr) && (geomsorttab[j].labl < vertsorttab[i].labl))
      j ++;                                       /* Search geometry vertex with same label             */
    if ((j >= geomfilenbr) || (geomsorttab[j].labl > vertsorttab[i].labl)) { /* If label does not exist */
      errorPrint ("C_geoLoad: vertex geometry data not found (%d)",
                  vertsorttab[i].labl);
      memFree    (vertsorttab);
      memFree    (geomsorttab);
      memFree    (geomfiletab);
      return     (1);
    }
    geomptr->verttab[vertsorttab[i].num] = geomfiletab[geomsorttab[j ++].num];
  }

  memFree (vertsorttab);
  memFree (geomsorttab);
  memFree (geomfiletab);

  return (0);
}

/***********************************/
/*                                 */
/* These routines handle mappings. */
/*                                 */
/***********************************/

/* This routine creates a mapping with
** respect to a given source graph.
** It returns:
** - VOID  : in all cases.
*/

void
C_mapInit (
C_Mapping * const           mapptr,
const C_Graph * const       grafptr)
{
  mapptr->grafptr = grafptr;
  mapptr->labltab = NULL;
}

/* This routine deletes a mapping.
** It returns:
** - VOID  : in all cases.
*/

void
C_mapExit (
C_Mapping * const           mapptr)
{
  if (mapptr->labltab != NULL)                    /* If there is a domain array */
    memFree (mapptr->labltab);                    /* Free it                    */
}

/* This routine loads a mapping.
** It returns:
** - 0   : on success.
** - !0  : on error.
*/

int
C_mapLoad (
C_Mapping * const           mapptr,
FILE * const                stream)
{
  C_VertSort *        vertsorttab;                /* Pointer to graph sorting array           */
  int                 vertsortflag;               /* Flag set if graph data sorted by label   */
  C_VertSort *        mapsorttab;                 /* Pointer to mapping data sorting array    */
  int                 mapsortflag;                /* Flag set if mapping data sorted by label */
  long                mapsortval;                 /* Value of maximum size for compatibility   */
  long                mapfileval;                 /* Value of maximum size for compatibility   */
  SCOTCH_Num          mapfilenbr;                 /* Number of mapping pairs in file          */
  SCOTCH_Num *        mapfiletab;                 /* Pointer to mapping data read from file   */
  SCOTCH_Num          i, j;

  if ((mapptr->labltab == NULL) &&                /* Allocate array if necessary */
      ((mapptr->labltab = (SCOTCH_Num *) memAlloc (mapptr->grafptr->vertnbr * sizeof (SCOTCH_Num))) == NULL)) {
    errorPrint ("C_mapLoad: out of memory (1)");
    return     (1);
  }

  memset (mapptr->labltab, ~0, mapptr->grafptr->vertnbr * sizeof (SCOTCH_Num)); /* Pre-initialize mapping */

  if (stream == NULL)                             /* If stream is invalid */
    return (0);

  if ((fscanf (stream, "%ld",                     /* Read number of mapping pairs */
               &mapfileval) != 1) ||
      (mapfileval < 1)) {
    errorPrint ("C_mapLoad: bad input (1)");
    return     (1);
  }
  mapfilenbr = (SCOTCH_Num) mapfileval;

  if (((mapfiletab  = (SCOTCH_Num *) memAlloc (mapfilenbr               * sizeof (SCOTCH_Num))) == NULL) ||
      ((mapsorttab  = (C_VertSort *) memAlloc (mapfilenbr               * sizeof (C_VertSort))) == NULL) ||
      ((vertsorttab = (C_VertSort *) memAlloc (mapptr->grafptr->vertnbr * sizeof (C_VertSort))) == NULL)) {
    errorPrint ("C_mapLoad: out of memory (2)");
    if (mapfiletab != NULL) {
      if (mapsorttab != NULL)
        memFree (mapsorttab);
      memFree (mapfiletab);
    }
    return (1);
  }

  mapsortflag = 1;                                /* Assume mapping data sorted */
  for (i = 0; i < mapfilenbr; i ++) {
    if (fscanf (stream, "%ld%ld",
                &mapsortval, &mapfileval) != 2) {
      errorPrint ("C_mapLoad: bad input (2)");
      memFree    (vertsorttab);
      memFree    (mapsorttab);
      memFree    (mapfiletab);
      return     (1);
    }
    mapsorttab[i].labl = mapsortval;
    mapsorttab[i].num  = i;
    mapfiletab[i]      = mapfileval;

    if ((i > 0) &&                                /* Check if mapping data sorted */
        (mapsorttab[i].labl < mapsorttab[i - 1].labl))
      mapsortflag = 0;                            /* Mapping data not sorted */
  }
  if (mapsortflag != 1)                           /* If mapping data not sorted         */
      qsort ((char *) mapsorttab, mapfilenbr,     /* Sort sort area by ascending labels */
             sizeof (C_VertSort), (int (*) (const void *, const void *)) C_geoLoad2);
  for (i = 1; i < mapfilenbr; i ++) {             /* Check mapping data integrity */
    if (mapsorttab[i].labl == mapsorttab[i - 1].labl) {
      errorPrint ("C_mapLoad: duplicate vertex label");
      memFree    (vertsorttab);
      memFree    (mapsorttab);
      memFree    (mapfiletab);
      return     (1);
    }
  }

  if (mapptr->grafptr->vlbltab != NULL) {         /* If graph has vertex labels */
    vertsortflag = 1;                             /* Assume graph data sorted   */
    for (i = 0; i < mapptr->grafptr->vertnbr; i ++) {
      vertsorttab[i].labl = mapptr->grafptr->vlbltab[i];
      vertsorttab[i].num  = i;
      if ((i > 0) &&                              /* Check if graph data sorted */
          (vertsorttab[i].labl < vertsorttab[i - 1].labl))
        vertsortflag = 0;                         /* Graph data not sorted */
    }
    if (vertsortflag != 1)                        /* If graph data not sorted                      */
      qsort ((char *) vertsorttab, mapptr->grafptr->vertnbr, /* Sort sort area by ascending labels */
             sizeof (C_VertSort), (int (*) (const void *, const void *)) C_geoLoad2);
  }
  else {                                          /* Graph does not have vertex labels */
    for (i = 0; i < mapptr->grafptr->vertnbr; i ++) {
      vertsorttab[i].labl = i + mapsorttab[0].labl; /* Use first index as base value */
      vertsorttab[i].num  = i;
    }
  }

  for (i = 0, j = 0; i < mapptr->grafptr->vertnbr; i ++) { /* For all vertices in graph */
    while ((j < mapfilenbr) && (mapsorttab[j].labl < vertsorttab[i].labl))
      j ++;                                       /* Search mapping vertex with same label          */
    if ((j >= mapfilenbr) || (mapsorttab[j].labl > vertsorttab[i].labl)) /* If label does not exist */
      continue;                                   /* This vertex has no related mapping data        */
    mapptr->labltab[vertsorttab[i].num] = mapfiletab[mapsorttab[j ++].num];
  }

  memFree (vertsorttab);
  memFree (mapsorttab);
  memFree (mapfiletab);

  return (0);
}

/**************************************/
/*                                    */
/* The option string parsing routine. */
/*                                    */
/**************************************/

/* This routine parses an option string.
** It returns:
** - 0 : if string successfully scanned.
** - 1 : if invalid code name.
** - 2 : if invalid arguments for the code.
** - 3 : if syntax error in string.
*/

int
C_parse (
const C_ParseCode * const   codeptr,             /* Pointer to the code array          */
const C_ParseArg * const    argptr,              /* Pointer to the code argument array */
uint * const                codeval,             /* Pointer to the code value to set   */
char * const                string)              /* Pointer to the string to parse     */
{
  uint                code;                      /* Code found                       */
  uint                codelen;                   /* Code name length                 */
  char                argbuf[128];               /* Buffer for argument scanning     */
  uint                arglen;                    /* Length of the current argument   */
  char *              argbeg;                    /* Pointer to beginning of argument */
  char *              argend;                    /* Pointer to end of argument       */
  char *              argequ;                    /* Position of the '=' character    */
  uint                i, j;

  codelen = 0;                                   /* No code recognized yet              */
  for (i = 0; codeptr[i].name != NULL; i ++) {   /* For all the codes                   */
    if ((strncasecmp (string,                    /* Find the longest matching code name */
                      codeptr[i].name,
                      j = strlen (codeptr[i].name)) == 0) &&
        (j > codelen)) {
      code    = codeptr[i].code;
      codelen = j;
    }
  }
  if (codelen == 0)                              /* If no code recognized  */
    return (1);                                  /* Return the error value */
  *codeval = code;                               /* Set the code value     */

  argbeg = string + codelen;                     /* Point to the end of the code name */
  if (*argbeg == '{') {                          /* If there are arguments            */
    argbeg ++;                                   /* Point to argument beginning       */
    do {                                         /* For all arguments                 */
      argend = strpbrk (argbeg, ",}\0");         /* Search for the argument end       */
      if (*argend == '\0')                       /* If there is no end delimiter      */
        return (3);                              /* Return the syntax error value     */

      arglen = ((argend - argbeg) < 127)         /* Get argument bounded length   */
               ? (argend - argbeg)
               : 127;
      strncpy (argbuf, argbeg, arglen);          /* Copy the argument to the buffer */
      argbuf[arglen] = '\0';                     /* Mark the end of the argument    */
      argequ = strpbrk (argbuf, "=");            /* Search for the '=' character    */
      if (argequ != NULL)                        /* If it exists                    */
        *argequ++ = '\0';                        /* Turn it into a separating null  */

      for (i = 0, j = (unsigned int) -1;         /* Scan all the possible arguments */
           argptr[i].name != NULL;
           i ++) {
        if ((argptr[i].code == code) &&          /* If the proper name is found */
            (strcmp (argbuf, argptr[i].name) == 0)) {
          j = i;                                 /* Record the position */
          break;                                 /* Exit the loop       */
        }
      }
      if (j == (unsigned int) -1)                /* If invalid argument     */
        return (2);                              /* Return the proper value */

      if (argptr[j].format != NULL) {            /* If there is a value to read    */
        if (argequ == NULL)                      /* If none has been given however */
          return (2);                            /* Return the error value         */
        if (sscanf (argequ,                      /* Try to read the argument value */
                    argptr[j].format,
                    argptr[j].ptr) != 1)
          return (2);                            /* Return if error                */
        if (argptr[j].func != NULL)              /* If there is a control function */
          if (argptr[j].func (argptr[j].ptr) != 0) /* If the function fails        */
            return (2);                          /* Return the error value         */
      }
      else {                                     /* If no value needed           */
        if (argequ != NULL)                      /* If there is one however      */
          return (2);                            /* Return the error code        */
        *((char *) argptr[j].ptr) = argbuf[0];   /* Copy the first argument char */
      }

      argbeg = argend + 1;                       /* Skip the processed argument         */
    } while (*argend != '}');                    /* Loop as long as there are arguments */
  }

  return ((*argbeg == '\0') ? 0 : 3);            /* Check if no extraneous characters */
}