in blingfiretools/fa_pats_select/fa_pats_select.cpp [312:526]
int __cdecl main (int argc, char ** argv)
{
__PROG__ = argv [0];
--argc, ++argv;
::FAIOSetup ();
process_args (argc, argv);
try {
FAAssert (pFsmFile && pK2IFile && pI2InfoFile, FAMsg::IOError);
FAAutIOTools aut_io (&g_alloc);
FAMapIOTools map_io (&g_alloc);
// patterns storage
FAImageDump FsmImg;
FAImageDump K2IImg;
FAImageDump I2InfoImg;
FARSDfa_pack_triv PatRsDfa;
FAMealyDfa_pack_triv PatMealyDfa;
FAOw2Iw_pack_triv PatRevMealy;
FAArray_pack K2I;
FAMultiMap_pack OwsId2Ows;
FARSDfa_ro PatRsDfa_txt (&g_alloc);
FAMealyDfa_ro PatMealyDfa_txt (&g_alloc);
FAArray_p2ca K2I_txt;
FAMultiMap_ar OwsId2Ows_txt;
OwsId2Ows_txt.SetAllocator (&g_alloc);
PatMealyDfa_txt.SetRsDfa (&PatRsDfa_txt);
// load automata
if (FAFsmConst::FORMAT_DUMP == g_format_type) {
FsmImg.Load (pFsmFile);
K2IImg.Load (pK2IFile);
I2InfoImg.Load (pI2InfoFile);
PatRsDfa.SetImage (FsmImg.GetImageDump ());
PatMealyDfa.SetImage (FsmImg.GetImageDump ());
PatRevMealy.SetImage (FsmImg.GetImageDump ());
K2I.SetImage (K2IImg.GetImageDump ());
OwsId2Ows.SetImage (I2InfoImg.GetImageDump ());
g_pDfa = &PatRsDfa;
g_pMealy = &PatMealyDfa;
g_pOw2Iw = &PatRevMealy;
g_pK2I = &K2I;
g_pI2Info = &OwsId2Ows;
} else {
DebugLogAssert (FAFsmConst::FORMAT_TXT == g_format_type);
std::ifstream fsm_ifs (pFsmFile, std::ios::in);
FAAssertStream (&fsm_ifs, pFsmFile);
aut_io.Read (fsm_ifs, &PatRsDfa_txt, &PatMealyDfa_txt);
std::ifstream ows_ifs (pI2InfoFile, std::ios::in);
FAAssertStream (&ows_ifs, pI2InfoFile);
map_io.Read (ows_ifs, &OwsId2Ows_txt);
const int * pA;
int Count;
std::ifstream k2i_ifs (pK2IFile, std::ios::in);
FAAssertStream (&k2i_ifs, pK2IFile);
map_io.Read (k2i_ifs, &pA, &Count);
K2I_txt.SetArray (pA, Count);
g_pDfa = &PatRsDfa_txt;
g_pMealy = &PatMealyDfa_txt;
g_pOw2Iw = &PatMealyDfa_txt;
g_pK2I = &K2I_txt;
g_pI2Info = &OwsId2Ows_txt;
}
// load normalization map, if needed
if (g_pCharMapFile) {
g_charmap_image.Load (g_pCharMapFile);
const unsigned char * pImg = g_charmap_image.GetImageDump ();
DebugLogAssert (pImg);
g_charmap.SetImage (pImg);
}
// adjust input/output
std::istream * pIs = &std::cin;
std::ifstream ifs;
std::ostream * pOs = &std::cout;
std::ofstream ofs;
std::ostream * pUnOs = NULL;
std::ofstream un_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;
}
if (NULL != pUnOutFile) {
un_ofs.open (pUnOutFile, std::ios::out);
pUnOs = &un_ofs;
}
FAAssert (pIs && pOs, FAMsg::IOError);
FASelectTrPatterns_print Pat2Pat (&g_alloc, pOs, pUnOs);
// see if a subset should be calculated
if (false == g_TakeAll) {
Pat2Pat.SetNoEmpty (g_NoEmpty);
Pat2Pat.SetFsm (g_pDfa, g_pMealy, g_pOw2Iw);
Pat2Pat.SetK2I (g_pK2I);
Pat2Pat.SetI2Info (g_pI2Info);
/// add left anchor
g_Iws [0] = g_spec_l_anchor;
g_Ows [0] = FAFsmConst::HYPH_NO_HYPH;
// 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) {
/// str -> chains
g_Count = FATrWordIOTools_utf8::Str2IwOw (pLine, LineLen, \
g_Iws + 1, g_Ows + 1, FALimits::MaxWordLen - 2,
g_ignore_case, g_pMap);
/// add right anchor
if (g_Count + 1 < FALimits::MaxWordLen) {
g_Iws [g_Count + 1] = g_spec_r_anchor;
g_Ows [g_Count + 1] = FAFsmConst::HYPH_NO_HYPH;
}
g_Count += 2;
Pat2Pat.AddIwsOws (g_Iws, g_Ows, g_Count);
}
} // of while (!(pIs->eof ())) ...
Pat2Pat.Process ();
} else { // if (g_TakeAll)
FAMphInterpretTools_t < int > mph;
mph.SetMealy (g_pMealy);
mph.SetRsDfa (g_pDfa);
mph.SetOw2Iw (g_pOw2Iw);
const int Count = g_pK2I->GetCount ();
for (int K = 0; K < Count; ++K) {
// get left side of a pattern
const int LeftSize = \
mph.GetChain (K, g_Iws, FALimits::MaxWordLen);
FAAssert (0 < LeftSize && LeftSize <= FALimits::MaxWordLen,
FAMsg::InternalError);
// get right side of a pattern
const int I = g_pK2I->GetAt (K);
const int RightSize = \
g_pI2Info->Get (I, g_Ows, FALimits::MaxWordLen);
FAAssert (RightSize - 1 == LeftSize, FAMsg::InternalError);
// return the pattern without the frequency
Pat2Pat.PutPattern (g_Iws, g_Ows + 1, LeftSize);
}
} // of if (false == g_TakeAll) ...
} 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;
}