File: copyg.c

package info (click to toggle)
nauty 2.4r2-1
  • links: PTS, VCS
  • area: non-free
  • in suites: wheezy
  • size: 2,540 kB
  • sloc: ansic: 30,296; sh: 2,545; makefile: 762
file content (156 lines) | stat: -rw-r--r-- 4,206 bytes parent folder | download | duplicates (2)
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
/* copyg.c version 1.0; B D McKay, Jan 1997 */

#define USAGE "copyg [-gsfp#:#qhx] [infile [outfile]]"

#define HELPTEXT \
"  Copy a file of graphs with possible format conversion.\n\
\n\
     -g  Use graph6 format for output\n\
     -s  Use sparse6 format for output\n\
         In the absence of -g and -s, the format depends on \n\
         the header or, if none, the first input line.\n\
\n\
     -p# -p#:#  \n\
         Specify range of input lines (first is 1)\n\
     -f  With -p, assume input lines of fixed length\n\
          (ignored if header or first line has sparse6 format).\n\
\n\
     -h  Write a header.\n\
     -x  Don't write a header.\n\
         In the absence of -h and -x, a header is written if\n\
         there is one in the input.\n\
\n\
     -q  Suppress auxiliary output.\n"

/***********************************************************************/

#include "gtools.h"


main(int argc, char *argv[])
{
        graph *g;
	int m,n,codetype;
	char *infilename,*outfilename;
	FILE *infile,*outfile;
	int outcode;
	long nin;
	int argnum,j;
	char *arg,sw;
	boolean sswitch,fswitch,pswitch,qswitch,gswitch;
	boolean hswitch,xswitch;
	boolean badargs;
	long pval1,pval2,maxin;

	HELP;

	sswitch = fswitch = pswitch = FALSE;
	gswitch = qswitch = xswitch = hswitch = FALSE;
	infilename = outfilename = NULL;

	argnum = 0;
	badargs = FALSE;
	for (j = 1; !badargs && j < argc; ++j)
	{
	    arg = argv[j];
	    if (arg[0] == '-' && arg[1] != '\0')
	    {
		++arg;
		while (*arg != '\0')
		{
		    sw = *arg++;
		         SWBOOLEAN('s',sswitch)
		    else SWBOOLEAN('g',gswitch)
		    else SWBOOLEAN('q',qswitch)
		    else SWBOOLEAN('f',fswitch)
		    else SWBOOLEAN('h',hswitch)
		    else SWBOOLEAN('x',xswitch)
		    else SWRANGE('p',":-",pswitch,pval1,pval2,"copyg -p")
		    else badargs = TRUE;
		}
	    }
	    else
	    {
		++argnum;
		if      (argnum == 1) infilename = arg;
	        else if (argnum == 2) outfilename = arg;
		else                  badargs = TRUE;
	    }
	}

	if (sswitch && gswitch) 
            gt_abort(">E copyg: -s and -g are incompatible\n");
	if (hswitch && xswitch) 
            gt_abort(">E copyg: -h and -x are incompatible\n");

	if (badargs || argnum > 2)
	{
	    fprintf(stderr,">E Usage: %s\n",USAGE);
	    GETHELP;
	    exit(1);
	}

	if (!qswitch)
	{
	    fprintf(stderr,">A copyg");
	    if (sswitch || gswitch || fswitch || pswitch || xswitch || hswitch)
		fprintf(stderr," -");
	    if (sswitch) fprintf(stderr,"s");
	    if (gswitch) fprintf(stderr,"g");
	    if (hswitch) fprintf(stderr,"h");
	    if (xswitch) fprintf(stderr,"x");
	    if (fswitch) fprintf(stderr,"f");
	    if (pswitch) writerange(stderr,'p',pval1,pval2);
	    if (argnum > 0) fprintf(stderr," %s",infilename);
	    if (argnum > 1) fprintf(stderr," %s",outfilename);
	    fprintf(stderr,"\n");
	    fflush(stderr);
	}

	if (infilename && infilename[0] == '-') infilename = NULL;
	infile = opengraphfile(infilename,&codetype,fswitch,
			       pswitch ? pval1 : 1);
	if (!infile) exit(1);
	if (!infilename) infilename = "stdin";

	if (!outfilename || outfilename[0] == '-')
	{
	    outfilename = "stdout";
	    outfile = stdout;
	}
	else if ((outfile = fopen(outfilename,"w")) == NULL)
	{
	    fprintf(stderr,"Can't open output file %s\n",outfilename);
	    gt_abort(NULL);
	}

	if (sswitch || !gswitch && (codetype&SPARSE6)) outcode = SPARSE6;
	else                                           outcode = GRAPH6;

	if (hswitch || !xswitch && (codetype&HAS_HEADER))
	{
	    if (outcode == SPARSE6) writeline(outfile,SPARSE6_HEADER);
	    else    		    writeline(outfile,GRAPH6_HEADER);
	}

	nin = 0;
	if (!pswitch || pval2 == NOLIMIT)
	    maxin = NOLIMIT;
	else if (pval1 < 1) maxin = pval2;
	else                maxin = pval2 - pval1 + 1;
	while (nin < maxin || maxin == NOLIMIT)
	{
	    if ((g = readg(infile,NULL,0,&m,&n)) == NULL) break;
	    ++nin;
	    if (outcode == readg_code)   writelast(outfile);
	    else if (outcode == SPARSE6) writes6(outfile,g,m,n);
	    else                         writeg6(outfile,g,m,n);
	    FREES(g);
	}

	if (!qswitch) 
	    fprintf(stderr,">Z  %ld graphs copied from %s to %s\n",
		           nin,infilename,outfilename);

	exit(0);
}