min_set.cpp

 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
/**
* Fingding minimal covering set.
* args: <inputFile> <outputFile>
*
* inputFile format (each line is one set of blocks)
* <fileName>;<setName>
*/
#include <stdio.h>
#include <string>
#include <set>
#include "InputSet.h"
#define SET_LIST_DELIMITER ';'
#define MAX_BUF 0xFF
void trim(char *cStr) {
size_t len = strlen(cStr);
for (int i = len - 1; i >=0; i++) {
if (isspace(cStr[i])) cStr[i] = '\0';
else break;
}
}
int main(int argc, char *argv[])
{
if (argc < 3) {
printf("Expected param: <input list file> <output list file>");
return 1;
}
FILE *listFile = fopen(argv[1], "r");
if (!listFile) {
printf("Cannot open the list file: %s\n", argv[1]);
return 2;
}
char buffer[MAX_BUF];
while (!feof(listFile)) {
if (!fgets(buffer, MAX_BUF, listFile)) break;
trim(buffer);
size_t len = strlen(buffer);
std::string setName = "";
std::string fileName = "";
char* pos = strchr(buffer, SET_LIST_DELIMITER);
if (pos && (pos - buffer) < len) {
char *ptr = pos + 1;
(*pos) = '\0';
setName = ptr;
}
fileName = buffer;
if (setName.length() == 0) setName = fileName;
InputSet::parse(fileName, setName);
}
fclose(listFile);
while(InputSet::algoStep()) { }
FILE *outFile = fopen(argv[2], "w");
if (!outFile) {
printf("Cannot open the output list file: %s\n", argv[2]);
return -1;
}
InputSet::printSets(InputSet::chosen, outFile);
fclose(outFile);
printf("Done! Coverage set in: %s\n!", argv[2]);
return 0;
}