in blingfiretools/fa_wrec/fa_wrec.cpp [195:403]
int __cdecl main (int argc, char ** argv)
{
__PROG__ = argv [0];
--argc, ++argv;
::FAIOSetup ();
process_args (argc, argv);
try {
if ((!g_pInTagSet2File && g_pInLdbFile) ||
(g_pInTagSet2File && !g_pInLdbFile)) {
throw FAException ("--tagset2= and --ldb= should both be specified",
__FILE__, __LINE__);
}
// Input Tokens
tokens.SetAllocator (&g_alloc);
tokens.SetCopyChains (true);
// CNF
cnf.SetAllocator (&g_alloc);
// Type2Ows map
type2ows.SetAllocator (&g_alloc);
/// adjust IO
std::istream * pIs = &std::cin;
if (NULL != g_pInFile) {
std::ifstream ifs;
ifs.open (g_pInFile, std::ios::in);
FAAssertStream (&ifs, g_pInFile);
pIs = &ifs;
}
if (NULL != g_pInTagSetFile) {
std::ifstream tagset_ifs (g_pInTagSetFile, std::ios::in);
FAAssertStream (&tagset_ifs, g_pInTagSetFile);
map_io.Read (tagset_ifs, &tagset);
}
if (NULL != g_pInTagSet2File) {
std::ifstream tagset2_ifs (g_pInTagSet2File, std::ios::in);
FAAssertStream (&tagset2_ifs, g_pInTagSet2File);
map_io.Read (tagset2_ifs, &tagset2);
}
if (NULL != g_pInLdbFile) {
ldb_img.Load (g_pInLdbFile);
ldb.SetImage (ldb_img.GetImageDump ());
tag_dict.SetConf (ldb.GetTagDictConf (), ldb.GetInTr ());
}
if (NULL != g_pInExtDigFile) {
std::ifstream ifs (g_pInExtDigFile, std::ios::in);
FAAssertStream (&ifs, g_pInExtDigFile);
map_io.Read (ifs, &tokens);
map_io.Read (ifs, &cnf);
map_io.Read (ifs, &type2ows);
FAAssert (!::FAIsEmpty (&cnf) && !::FAIsEmpty (&type2ows), \
FAMsg::IOError);
}
/// do processing
wrecc.SetType (g_Type);
wrecc.SetEncodingName (g_pInputEnc);
wrecc.SetDictRoot (g_pDictRoot);
if (NULL != g_pInTagSetFile) {
wrecc.SetTagSet (&tagset);
}
if (NULL != g_pInTagSet2File) {
wrecc.SetTagSet2 (&tagset2);
}
if (NULL != g_pInLdbFile) {
wrecc.SetTagDict (&tag_dict);
}
if (NULL != g_pInExtDigFile) {
wrecc.SetTokens (&tokens);
wrecc.SetCNF (&cnf);
wrecc.SetType2Ows (&type2ows);
}
std::string line;
const char * pLine;
int LineLen;
while (!(pIs->eof ())) {
std::string Re;
// read until the empty line is met
if (!std::getline (*pIs, line))
break;
pLine = line.c_str ();
LineLen = (const int) line.length ();
if (0 < LineLen) {
DebugLogAssert (pLine);
if (0x0D == (unsigned char) pLine [LineLen - 1])
LineLen--;
}
while (0 < LineLen) {
Re = Re + std::string (pLine, LineLen) + "\n";
if (pIs->eof ())
break;
if (!std::getline (*pIs, line))
break;
pLine = line.c_str ();
LineLen = (const int) line.length ();
if (0 < LineLen) {
DebugLogAssert (pLine);
if (0x0D == (unsigned char) pLine [LineLen - 1])
LineLen--;
}
}
const char * pRe = Re.c_str ();
int ReLength = (const int) Re.length ();
if (0 < ReLength) {
DebugLogAssert (pRe);
wrecc.AddRule (pRe, ReLength);
} else {
// break the outer loop, if RE is empty
break;
}
} // of while (!(pIs->eof ())) ...
// make compilation
wrecc.Process ();
/// save the results, only if not empty
if (wrecc.GetDfa1 ()) {
std::ostream * pOs = & std::cout ;
std::ofstream ofs;
if (g_pOutFile) {
if (g_build_dump) {
ofs.open (g_pOutFile, std::ios::out | std::ios::binary);
} else {
ofs.open (g_pOutFile, std::ios::out);
}
pOs = & ofs;
}
if (!g_build_dump) {
::FAPrintWre (*pOs, &wrecc);
} else {
::FASaveWre (*pOs, &wrecc);
}
}
/// save external digitzer, if needed
if (g_pOutExtDigFile) {
const FAChain2NumA * pTokens = wrecc.GetTokens ();
FAAssert (pTokens, FAMsg::InternalError);
const FAMultiMapA * pCnf = wrecc.GetCNF ();
FAAssert (pCnf && !::FAIsEmpty (pCnf), FAMsg::InternalError);
const FAMultiMapA * pOwsMap = wrecc.GetType2Ows ();
FAAssert (pOwsMap && !::FAIsEmpty (pOwsMap), FAMsg::InternalError);
std::ofstream ofs (g_pOutExtDigFile, std::ios::out);
map_io.Print (ofs, pTokens);
map_io.Print (ofs, pCnf);
map_io.Print (ofs, pOwsMap);
}
} 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;
}