Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e09c463

Browse files
authored
Merge pull request #16 from VincentDerk/master
Changed DIMAC cnf file parsing
2 parents fd880bd + 8136bc2 commit e09c463

File tree

1 file changed

+56
-74
lines changed

1 file changed

+56
-74
lines changed

src/src_sharpSAT/MainSolver/InstanceGraph/InstanceGraph.cpp

Lines changed: 56 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include <math.h>
33
#include<fstream>
44
#include<stdio.h>
5+
#include<limits>
6+
#include<sstream>
57

68
CRunAnalyzer theRunAn;
79

@@ -615,13 +617,7 @@ bool CInstanceGraph::prep_CleanUpPool()
615617

616618
bool CInstanceGraph::createfromFile(const char* lpstrFileName)
617619
{
618-
619-
const int BUF_SZ = 65536;
620-
const int TOK_SZ = 255;
621-
622-
char buf[BUF_SZ];
623-
char token[TOK_SZ];
624-
unsigned int line = 0;
620+
unsigned int nLine = 0;
625621
unsigned int nVars, nCls;
626622
int lit;
627623
vector<int> litVec;
@@ -646,73 +642,59 @@ bool CInstanceGraph::createfromFile(const char* lpstrFileName)
646642
fclose(filedesc);
647643

648644
ifstream inFile(lpstrFileName, ios::in);
649-
650-
// read the preamble of the cnf file
651-
while (inFile.getline(buf, BUF_SZ))
652-
{
653-
line++;
654-
if (buf[0] == 'c')
655-
continue;
656-
if (buf[0] == 'p')
657-
{
658-
if (sscanf(buf, "p cnf %d %d", &nVars, &nCls) < 2)
659-
{
660-
toERROUT("line "<<line<<": failed reading problem line \n");
661-
exit(3);
662-
}
663-
break;
664-
}
665-
else
666-
{
667-
toERROUT("line"<<line<<": problem line expected "<<endl);
668-
}
669-
}
670-
originalVarCount = nVars;
671-
int i, j;
672-
// now read the data
673-
while (inFile.getline(buf, BUF_SZ))
674-
{
675-
line++;
676-
i = 0;
677-
j = 0;
678-
if (buf[0] == 'c')
679-
continue;
680-
while (buf[i] != 0x0)
681-
{
682-
683-
while (buf[i] != 0x0 && buf[i] != '-' && (buf[i] < '0' || buf[i]
684-
> '9'))
685-
i++;
686-
if (buf[i] == 0x0) continue;
687-
while (buf[i] == '-' || buf[i] >= '0' && buf[i] <= '9')
688-
{
689-
token[j] = buf[i];
690-
i++;
691-
j++;
692-
}
693-
token[j] = 0x0;
694-
lit = atoi(token);
695-
j = 0;
696-
if (lit == 0) // end of clause
697-
{
698-
if (clauseLen > 0)
699-
litVec.push_back(0);
700-
clauseLen = 0;
701-
}
702-
else
703-
{
704-
clauseLen++;
705-
litVec.push_back(lit);
706-
}
707-
}
708-
}
709-
710-
if (!inFile.eof())
711-
{
712-
toERROUT(" CNF input: line too long");
713-
}
714-
inFile.close();
715-
/// END FILE input
645+
string line;
646+
string nextWord;
647+
648+
// read the preamble of the cnf file
649+
while(std::getline(inFile, line))
650+
{
651+
nLine++;
652+
if (line.compare(0, 5, "p cnf") == 0)
653+
{
654+
std::stringstream linestream(line);
655+
linestream >> nextWord; // eat 'p'
656+
linestream >> nextWord; // eat 'cnf'
657+
if(!(linestream >> nVars) || !(linestream >> nCls))
658+
{
659+
toERROUT("line "<<nLine<<": failed reading cnf header line, expected p cnf Vars Clauses"<<endl);
660+
exit(3);
661+
}
662+
break;
663+
}
664+
else if (line[0] != 'c')
665+
toERROUT("line " << nLine << ": problem line, expected comment c ... or p cnf Vars Clauses" << endl);
666+
}
667+
668+
originalVarCount = nVars;
669+
char firstChar;
670+
// now read the data
671+
while (inFile >> firstChar)
672+
{
673+
if(isdigit(firstChar) || firstChar == '-')
674+
{
675+
inFile.unget();
676+
// parse clause (begins with (negative) digit)
677+
while((inFile >> lit) && lit != 0)
678+
{
679+
clauseLen++;
680+
litVec.push_back(lit);
681+
}
682+
if (clauseLen > 0)
683+
litVec.push_back(0);
684+
clauseLen = 0;
685+
inFile.ignore(numeric_limits<streamsize>::max(), '\n'); // skip till next line
686+
nLine++;
687+
}
688+
else if (!isspace(firstChar)) // if whitespace, we eat it instead
689+
{
690+
// no digit (or whitespace) so we skip the line
691+
inFile.ignore(numeric_limits<streamsize>::max(),'\n');
692+
nLine++;
693+
}
694+
}
695+
696+
inFile.close();
697+
/// END FILE input
716698

717699

718700
vector<int>::iterator it, jt, itEndCl;

0 commit comments

Comments
 (0)