1 /* converseg.c  version 2.0; B D McKay, Jun 2015. */
2 
3 #define USAGE "converseg [-q] [infile [outfile]]"
4 
5 #define HELPTEXT \
6 " Take the converse digraphs of a file of directed graphs.\n\
7 \n\
8     The output file has a header if and only if the input file does.\n\
9     Undirected graphs are passed through without change, while\n\
10     directed graphs are written in digraph6 format.\n\
11 \n\
12     -q  Suppress auxiliary information.\n"
13 
14 /*************************************************************************/
15 
16 #include "gtools.h"
17 
18 /**************************************************************************/
19 
20 static void
21 conv(graph *g, int m, int n)
22 /* Replace g by its converse */
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)!=0) + (ISELEMENT(gj,i)!=0) == 1)
30             {
31                 FLIPELEMENT(gi,j);
32                 FLIPELEMENT(gj,i);
33             }
34 }
35 
36 /**************************************************************************/
37 
38 int
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     // size_t ii,ned,nedc,nn,loops,loopsc,gwords;
49     nauty_counter nin;
50     char *arg,sw;
51     double t;
52 
53     HELP; PUTVERSION;
54 
55     infilename = outfilename = NULL;
56     badargs = FALSE;
57     quiet = FALSE;
58 
59     argnum = 0;
60     badargs = FALSE;
61     for (j = 1; !badargs && j < argc; ++j)
62     {
63         arg = argv[j];
64         if (arg[0] == '-' && arg[1] != '\0')
65         {
66             ++arg;
67             while (*arg != '\0')
68             {
69                 sw = *arg++;
70                 SWBOOLEAN('q',quiet)
71                 else badargs = TRUE;
72             }
73         }
74         else
75         {
76             ++argnum;
77             if      (argnum == 1) infilename = arg;
78             else if (argnum == 2) outfilename = arg;
79             else                  badargs = TRUE;
80         }
81     }
82 
83     if (badargs)
84     {
85         fprintf(stderr,">E Usage: %s\n",USAGE);
86         GETHELP;
87         exit(1);
88     }
89 
90     if (!quiet)
91     {
92         fprintf(stderr,">A converseg");
93         if (argnum > 0) fprintf(stderr," %s",infilename);
94         if (argnum > 1) fprintf(stderr," %s",outfilename);
95         fprintf(stderr,"\n");
96         fflush(stderr);
97     }
98 
99     if (infilename && infilename[0] == '-') infilename = NULL;
100     infile = opengraphfile(infilename,&codetype,FALSE,1);
101     if (!infile) exit(1);
102     if (!infilename) infilename = "stdin";
103 
104     if (!outfilename || outfilename[0] == '-')
105     {
106         outfilename = "stdout";
107         outfile = stdout;
108     }
109     else if ((outfile = fopen(outfilename,"w")) == NULL)
110     {
111         fprintf(stderr,"Can't open output file %s\n",outfilename);
112         gt_abort(NULL);
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 
encode(insn,opcode,param_val,param_ch)126     gtools_check(WORDSIZE,1,1,NAUTYVERSIONID);
127 
128     nin = 0;
129     t = CPUTIME;
130     while (TRUE)
131     {
132         if ((g = readgg(infile,NULL,0,&m,&n,&digraph)) == NULL) break;
133         ++nin;
134 
135         if (!digraph)
136 	    writelast(outfile);
137         else
138         {
139 	    conv(g,m,n);
140 	    writed6(outfile,g,m,n);
141 	}
142         FREES(g);
143     }
144     t = CPUTIME - t;
145 
146     if (!quiet)
147         fprintf(stderr,">Z  " COUNTER_FMT
148                 " graphs converted from %s to %s in %3.2f sec.\n",
149                 nin,infilename,outfilename,t);
150 
151     exit(0);
152 }
153