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
|
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int digit(char x) {
return (x >= '0') && (x <= '9');
}
void write_time(FILE *cnf_file) {
char char_buff[200];
char curr_char;
int bufn=0;
int i;
while (curr_char != EOF) {
bufn=0;
while ((curr_char != '\n') && (curr_char != EOF)) {
if (bufn < 195) {
char_buff[bufn]=curr_char;
bufn++;
}
curr_char = fgetc(cnf_file);
}
char_buff[bufn]=0;
//printf("testing, %s\n", char_buff);
if (strncmp(char_buff, "Total Run Time",14) == 0) {
//printf("found it, %s\n", char_buff);
for (i=0; i<bufn; i++) {
if (char_buff[i] == '.' || digit(char_buff[i]))
putchar(char_buff[i]);
}
return;
}
curr_char = fgetc(cnf_file);
}
printf("-1"); // no time found
}
void write_instance(FILE *cnf_file) {
char char_buff[100];
char curr_char;
int bufn=0;
int negative=0;
long curr_num=1;
long read_num;
curr_char = fgetc(cnf_file);
while (curr_char == ' ' ||
curr_char == '-' ||
digit(curr_char)) {
if (curr_char == ' ') {
char_buff[bufn]=0;
read_num = atol(char_buff);
while (curr_num < read_num) {
/* Some numbers may not be specified in the
satisfying instance. In that case, I assume
their value is irrelevant */
printf("X ");
curr_num++;
}
if (curr_num != read_num) {
/* I assume that all the numbers come in order */
printf("Error: Read in a number out of sequence: %s\n", char_buff);
exit(1);
}
/* Now we print out the value of the corresponding number */
if (negative == 1) {
printf("nil ");
} else {
printf("t ");
}
curr_num++;
bufn = 0;
negative=0;
} else if (curr_char == '-') {
negative=1;
} else if (bufn < 10) {
char_buff[bufn] = curr_char;
bufn++;
} else {
/* Note: I should be doing something more intelligent here but
it's too late to print out a good error message */
printf("Error: Number is too big to be read\n");
exit(1);
}
curr_char = fgetc(cnf_file);
}
}
int main(int nargs, char **argc) {
FILE *cnf_file;
char curr_char;
char char_buff[100];
int bufn=0;
if (nargs <= 1) {
printf("(Error \"No input file name given\")\n");
return 1;
}
cnf_file = fopen(argc[1], "r");
curr_char = fgetc(cnf_file);
while (curr_char != EOF) {
/* printf(".\n"); */
bufn=0;
while ((curr_char != '\n') && (curr_char != EOF)) {
if (bufn < 50) {
char_buff[bufn]=curr_char;
bufn++;
}
curr_char = fgetc(cnf_file);
}
char_buff[bufn]=0;
/* printf("Checking: %s, %d\n", char_buff, strcmp(char_buff, "Instance Satisfiable")); */
if (strcmp(char_buff, "Instance Unsatisfiable") == 0) {
/* Instance is unsatisfiable */
printf("(unsat)\n");
printf("(time \"");
write_time(cnf_file);
printf("\")\n");
fclose(cnf_file);
return 0;
}
if (strcmp(char_buff, "Instance Satisfiable") == 0) {
/* Instance is satisfiable */
printf("(sat ");
write_instance(cnf_file);
printf(")\n");
printf("(time \"");
write_time(cnf_file);
printf("\")\n");
fclose(cnf_file);
return 0;
}
if (curr_char != EOF) curr_char = fgetc(cnf_file);
}
printf("(Error \"no result\")\n");
fclose(cnf_file);
return 1;
}
|