1 /* underlyingg.c  version 1.0; B D McKay, Dec 2017. */
2 
3 #define USAGE "underlyingg [-q] [infile [outfile]]"
4 
5 #define HELPTEXT \
6 " Take the underlying undirected graphs of a file of graphs.\n\
7 \n\
8     The output file has no header.\n\
9     Undirected graphs are passed through without change, while\n\
10     Underlying graphs of digraphs are written in sparse6 format.\n\
11 \n\
12     -q  Suppress auxiliary information.\n"
13 
14 /*************************************************************************/
15 
16 #include "gtools.h"
17 
18 /**************************************************************************/
19 
20 static void
underlying(graph * g,int m,int n)21 underlying(graph *g, int m, int n)
22 /* Replace g by its underlying graph */
23 {
24     int i,j;
25     graph *gi,*gj;
26 
27     for (i = 0, gi = g; i < n; ++i, gi += m)
28         for (j = i+1, gj = gi+m; j < n; ++j, gj += m)
29             if (ISELEMENT(gi,j) || ISELEMENT(gj,i))
30             {
31                 ADDELEMENT(gi,j);
32                 ADDELEMENT(gj,i);
33             }
34 }
35 
36 /**************************************************************************/
37 
38 int
main(int argc,char * argv[])39 main(int argc, char *argv[])
40 {
41     char *infilename,*outfilename;
42     FILE *infile,*outfile;
43     boolean badargs,quiet;
44     boolean digraph;
45     int j,m,n,argnum;
46     int codetype,outcode;
47     graph *g;
48     nauty_counter nin;
49     char *arg,sw;
50     double t;
51 
52     HELP; PUTVERSION;
53 
54     infilename = outfilename = NULL;
55     badargs = FALSE;
56     quiet = FALSE;
57 
58     argnum = 0;
59     badargs = FALSE;
60     for (j = 1; !badargs && j < argc; ++j)
61     {
62         arg = argv[j];
63         if (arg[0] == '-' && arg[1] != '\0')
64         {
65             ++arg;
66             while (*arg != '\0')
67             {
68                 sw = *arg++;
69                 SWBOOLEAN('q',quiet)
70                 else badargs = TRUE;
71             }
72         }
73         else
74         {
75             ++argnum;
76             if      (argnum == 1) infilename = arg;
77             else if (argnum == 2) outfilename = arg;
78             else                  badargs = TRUE;
79         }
80     }
81 
82     if (badargs)
83     {
84         fprintf(stderr,">E Usage: %s\n",USAGE);
85         GETHELP;
86         exit(1);
87     }
88 
89     if (!quiet)
90     {
91         fprintf(stderr,">A underlyingg");
92         if (argnum > 0) fprintf(stderr," %s",infilename);
93         if (argnum > 1) fprintf(stderr," %s",outfilename);
94         fprintf(stderr,"\n");
95         fflush(stderr);
96     }
97 
98     if (infilename && infilename[0] == '-') infilename = NULL;
99     infile = opengraphfile(infilename,&codetype,FALSE,1);
100     if (!infile) exit(1);
101     if (!infilename) infilename = "stdin";
102 
103     if (!outfilename || outfilename[0] == '-')
104     {
105         outfilename = "stdout";
106         outfile = stdout;
107     }
108     else if ((outfile = fopen(outfilename,"w")) == NULL)
109     {
110         fprintf(stderr,"Can't open output file %s\n",outfilename);
111         gt_abort(NULL);
112     }
113 
114 /*
115     if      (codetype&SPARSE6)  outcode = SPARSE6;
116     else if (codetype&DIGRAPH6) outcode = DIGRAPH6;
117     else                        outcode = GRAPH6;
118 
119     if (codetype&HAS_HEADER)
120     {
121         if (outcode == SPARSE6)       writeline(outfile,SPARSE6_HEADER);
122         else if (outcode == DIGRAPH6) writeline(outfile,DIGRAPH6_HEADER);
123         else          	              writeline(outfile,GRAPH6_HEADER);
124     }
125 */
126 
127     gtools_check(WORDSIZE,1,1,NAUTYVERSIONID);
128 
129     nin = 0;
130     t = CPUTIME;
131     while (TRUE)
132     {
133         if ((g = readgg(infile,NULL,0,&m,&n,&digraph)) == NULL) break;
134         ++nin;
135 
136         if (!digraph)
137 	    writelast(outfile);
138         else
139         {
140 	    underlying(g,m,n);
141 	    writes6(outfile,g,m,n);
142 	}
143         FREES(g);
144     }
145     t = CPUTIME - t;
146 
147     if (!quiet)
148         fprintf(stderr,">Z  " COUNTER_FMT
149                 " graphs converted from %s to %s in %3.2f sec.\n",
150                 nin,infilename,outfilename,t);
151 
152     exit(0);
153 }
154