int __cdecl main()

in blingfiretools/fa_fsm2fsm_iwec/fa_fsm2fsm_iwec.cpp [161:351]


int __cdecl main (int argc, char ** argv)
{
    __PROG__ = argv [0];

    --argc, ++argv;

    ::FAIOSetup ();

    FAAllocator g_alloc;
    FAAutIOTools g_aut_io (&g_alloc);
    FAMapIOTools g_map_io (&g_alloc);

    FARSDfa_ro g_dfa (&g_alloc);
    FARSNfa_ro g_nfa (&g_alloc);
    FAMealyDfa g_dfa_sigma (&g_alloc);
    FAMealyNfa g_nfa_sigma (&g_alloc);
    FACalcIwEqClasses g_IwClassify (&g_alloc);
    FAMap_judy g_output_map;
    FAMultiMap_judy g_output_mmap;
    FARSDfa_wo_ro g_new_dfa (&g_alloc);
    FARSNfa_wo_ro g_new_nfa (&g_alloc);
    FAState2Ow g_state2ow (&g_alloc);
    FAState2Ows g_state2ows (&g_alloc);
    FAMealyDfa g_new_dfa_sigma (&g_alloc);
    FAMealyNfa g_new_nfa_sigma (&g_alloc);

    g_output_mmap.SetAllocator (&g_alloc);

    // parse a command line
    process_args (argc, argv);

    try {

        // select in/out streams
        std::istream * pIs = &std::cin;
        std::ifstream ifs;

        if (NULL != pInFile) {
            ifs.open (pInFile, std::ios::in);
            FAAssertStream (&ifs, pInFile);
            pIs = &ifs;
        }

        if (FAFsmConst::TYPE_RS_DFA == g_fsm_type) {

            g_aut_io.Read (*pIs, &g_dfa);
            g_IwClassify.SetRsDfa (&g_dfa);

            if (-1 == g_iw_max)
                g_iw_max = g_dfa.GetMaxIw ();

        } else if (FAFsmConst::TYPE_RS_NFA == g_fsm_type) {

            g_aut_io.Read (*pIs, &g_nfa);
            g_IwClassify.SetRsNfa (&g_nfa);

            if (-1 == g_iw_max)
                g_iw_max = g_nfa.GetMaxIw ();

        } else if (FAFsmConst::TYPE_MOORE_DFA == g_fsm_type) {

            g_aut_io.Read (*pIs, &g_dfa, &g_state2ow);
            g_IwClassify.SetRsDfa (&g_dfa);

            if (-1 == g_iw_max)
                g_iw_max = g_dfa.GetMaxIw ();

        } else if (FAFsmConst::TYPE_MOORE_MULTI_DFA == g_fsm_type) {

            g_aut_io.Read (*pIs, &g_dfa, &g_state2ows);
            g_IwClassify.SetRsDfa (&g_dfa);

            if (-1 == g_iw_max)
                g_iw_max = g_dfa.GetMaxIw ();

        } else if (FAFsmConst::TYPE_MEALY_DFA == g_fsm_type) {

            g_aut_io.Read (*pIs, &g_dfa, &g_dfa_sigma);
            g_IwClassify.SetRsDfa (&g_dfa);
            g_IwClassify.SetDfaSigma (&g_dfa_sigma);

            if (-1 == g_iw_max)
                g_iw_max = g_dfa.GetMaxIw ();

        } else {
            DebugLogAssert (FAFsmConst::TYPE_MEALY_NFA == g_fsm_type);

            g_aut_io.Read (*pIs, &g_nfa, &g_nfa_sigma);
            g_IwClassify.SetRsNfa (&g_nfa);
            g_IwClassify.SetNfaSigma (&g_nfa_sigma);

            if (-1 == g_iw_max)
                g_iw_max = g_nfa.GetMaxIw ();
        }

        // assign default values

        if (-1 == g_iw_base)
            g_iw_base = 0;

        if (-1 == g_new_iw_base)
            g_new_iw_base = 0;

        g_IwClassify.SetIwBase (g_iw_base);
        g_IwClassify.SetIwMax (g_iw_max);
        g_IwClassify.SetNewIwBase (g_new_iw_base);
        g_IwClassify.SetIw2NewIw (&g_output_map);
        g_IwClassify.Process ();

        if (false == g_no_output) {

            // print out OldIw --> NewIw map, if needed
            if (NULL != pOutMapFile) {
                std::ofstream ofs (pOutMapFile, std::ios::out);
                g_map_io.Print (ofs, &g_output_map);
            }
            // print out NewIw --> OldIw map, if needed
            if (NULL != pOutMMapFile) {

                ::FAReverseMap (&g_output_mmap, &g_output_map);
                g_output_mmap.SortUniq ();

                std::ofstream ofs (pOutMMapFile, std::ios::out);
                g_map_io.Print (ofs, &g_output_mmap);
            }

            std::ostream * pOs = &std::cout;
            std::ofstream ofs;

            if (NULL != pOutFsmFile) {
                ofs.open (pOutFsmFile, std::ios::out);
                pOs = &ofs;
            }

            // print out the resulting automaton, if needed
            if (FAFsmConst::TYPE_RS_DFA == g_fsm_type) {

                ::FARemapRsFsmIws (&g_dfa, &g_new_dfa, &g_output_map);
                g_aut_io.Print (*pOs, &g_new_dfa);

            } else if (FAFsmConst::TYPE_RS_NFA == g_fsm_type) {

                ::FARemapRsFsmIws (&g_nfa, &g_new_nfa, &g_output_map);
                g_aut_io.Print (*pOs, &g_new_nfa);

            } else if (FAFsmConst::TYPE_MOORE_DFA == g_fsm_type) {

                ::FARemapRsFsmIws (&g_dfa, &g_new_dfa, &g_output_map);
                g_aut_io.Print (*pOs, &g_new_dfa, &g_state2ow);

            } else if (FAFsmConst::TYPE_MOORE_MULTI_DFA == g_fsm_type) {

                ::FARemapRsFsmIws (&g_dfa, &g_new_dfa, &g_output_map);
                g_aut_io.Print (*pOs, &g_new_dfa, &g_state2ows);

            } else if (FAFsmConst::TYPE_MEALY_DFA == g_fsm_type) {

                ::FARemapRsFsmIws (&g_dfa, &g_new_dfa, &g_output_map);
                ::FARemapMealySigma2 (&g_dfa, &g_dfa_sigma, \
                    &g_new_dfa_sigma, &g_output_map);
                g_aut_io.Print (*pOs, &g_new_dfa, &g_new_dfa_sigma);

            } else {
                DebugLogAssert (FAFsmConst::TYPE_MEALY_NFA == g_fsm_type);

                ::FARemapRsFsmIws (&g_nfa, &g_new_nfa, &g_output_map);
                ::FARemapMealySigma2 (&g_nfa, &g_nfa_sigma, \
                    &g_new_nfa_sigma, &g_output_map);
                g_aut_io.Print (*pOs, &g_new_nfa, &g_new_nfa_sigma);
            }
        } // of if (false == g_no_output) ...

    } 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;
    }

    return 0;
}