in blingfiretools/fa_iwowsuff2pats/fa_iwowsuff2pats.cpp [208:412]
int __cdecl main (int argc, char ** argv)
{
__PROG__ = argv [0];
--argc, ++argv;
::FAIOSetup ();
// parse a command line
process_args (argc, argv);
try {
// adjust input/output
std::istream * pIs = &std::cin;
std::ifstream ifs;
std::ostream * pOs = &std::cout;
std::ofstream ofs;
if (NULL != pInFile) {
ifs.open (pInFile, std::ios::in);
FAAssertStream (&ifs, pInFile);
pIs = &ifs;
}
if (NULL != pOutFile) {
ofs.open (pOutFile, std::ios::out);
pOs = &ofs;
}
DebugLogAssert (pIs);
DebugLogAssert (pOs);
FATmpSuff2Pats suff2pat (&g_alloc);
suff2pat.SetMinPatLen (g_MinPatLen);
suff2pat.SetMinPatPrec (g_MinPatPrec);
suff2pat.SetOutStream (pOs);
FATmpSuff2Pats ** ppSuff2Pats = NULL;
if (g_DontCarePats) {
ppSuff2Pats = NEW FATmpSuff2Pats* [g_MaxLeftCx];
FAAssert (ppSuff2Pats, FAMsg::OutOfMemory);
for (int i = 0; i < g_MaxLeftCx; ++i) {
FATmpSuff2Pats * pSuff2Pats = NEW FATmpSuff2Pats (&g_alloc);
FAAssert (pSuff2Pats, FAMsg::OutOfMemory);
ppSuff2Pats [i] = pSuff2Pats;
if (i + 1 > g_MinPatLen) {
pSuff2Pats->SetMinPatLen (i + 1);
} else {
pSuff2Pats->SetMinPatLen (g_MinPatLen);
}
pSuff2Pats->SetMinPatPrec (g_MinPatPrec);
pSuff2Pats->SetOutStream (pOs);
}
}
// process the input
std::string line;
while (!(pIs->eof ())) {
if (!std::getline (*pIs, line))
break;
const char * pLine = line.c_str ();
int LineLen = (const int) line.length ();
if (0 < LineLen) {
DebugLogAssert (pLine);
if (0x0D == (unsigned char) pLine [LineLen - 1])
LineLen--;
}
if (0 < LineLen) {
const int Padding1 = (int) strspn (pLine, " \t");
pLine += Padding1;
LineLen -= Padding1;
FAAssert (0 <= Padding1 && 0 < LineLen, FAMsg::IOError);
// read the frequency
const int Freq = strtol (pLine, NULL, 10);
const int FreqStrLen = (int) strcspn (pLine, " \t");
pLine += FreqStrLen;
LineLen -= FreqStrLen;
FAAssert (0 < FreqStrLen && 0 < LineLen, FAMsg::IOError);
const int Padding2 = (int) strspn (pLine, " \t");
pLine += Padding2;
LineLen -= Padding2;
FAAssert (0 <= Padding2 && 0 < LineLen, FAMsg::IOError);
const char * pChainStr = pLine;
const char * pChainStrEnd = pLine + LineLen;
g_ChainSize = 0;
while (pChainStr < pChainStrEnd) {
FAAssert (g_ChainSize < 2 * FALimits::MaxWordLen, \
FAMsg::IOError);
g_Chain [g_ChainSize] = strtol (pChainStr, NULL, 16);
g_ChainSize++;
pChainStr = strchr (pChainStr, ' ');
if (NULL == pChainStr)
break;
pChainStr++;
}
if (g_DontCarePats) {
const int ChainSize_2 = g_ChainSize / 2;
for (int i = 0; i < ChainSize_2; ++i) {
const int Iw = g_Chain [i];
g_Chain2 [i << 1] = Iw;
g_Chain2 [(i << 1) + 1] = FAFsmConst::HYPH_DONT_CARE;
}
for (int Pos = 0; Pos < g_MaxLeftCx && Pos < ChainSize_2; ++Pos) {
FATmpSuff2Pats * pSuff2Pats = ppSuff2Pats [Pos];
DebugLogAssert (pSuff2Pats);
// store the actual Ow for the given position
const int Ow = g_Chain [Pos + ChainSize_2];
g_Chain2 [(Pos << 1) + 1] = Ow;
pSuff2Pats->AddChain (g_Chain2, g_ChainSize, Freq);
// restore the Ow value back
g_Chain2 [(Pos << 1) + 1] = FAFsmConst::HYPH_DONT_CARE;
}
} else {
const int ChainSize_2 = g_ChainSize / 2;
for (int i = 0; i < ChainSize_2; ++i) {
const int Iw = g_Chain [i];
const int Ow = g_Chain [i + ChainSize_2];
g_Chain2 [i << 1] = Iw;
g_Chain2 [(i << 1) + 1] = Ow;
}
suff2pat.AddChain (g_Chain2, g_ChainSize, Freq);
} // of if (if (m_DontCarePats)) ...
} // of if (0 < LineLen) ...
} // of while ...
// finish up the processing
if (g_DontCarePats) {
for (int i = 0; i < g_MaxLeftCx; ++i) {
FATmpSuff2Pats * pSuff2Pats = ppSuff2Pats [i];
pSuff2Pats->Process ();
delete pSuff2Pats ;
}
delete[] ppSuff2Pats;
} else {
suff2pat.Process ();
}
} catch (const FAException & e) {
const char * const pErrMsg = e.GetErrMsg ();
const char * const pFile = e.GetSourceName ();
const int Line = e.GetSourceLine ();
std::cerr << "ERROR: " << pErrMsg << " in " << pFile \
<< " at line " << Line << " in program " << __PROG__ << '\n';
return 2;
} catch (...) {
std::cerr << "ERROR: Unknown error in program " << __PROG__ << '\n';
return 1;
}
// print out memory leaks, if any
FAPrintLeaks(&g_alloc, std::cerr);
return 0;
}