int __cdecl main()

in blingfiretools/fa_line_format/fa_line_format.cpp [545:693]


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

    --argc, ++argv;

    ::FAIOSetup ();

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

    try {

        std::ofstream ofs;
        if (g_pOutFile) {
            ofs.open (g_pOutFile, std::ios::out);
            pOs = &ofs;
        }
        std::ifstream ifs;
        if (g_pInFile) {
            ifs.open (g_pInFile, std::ios::in);
            FAAssertStream (&ifs, g_pInFile);
            pIs = &ifs;
        }
        // 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);
        }
        // load prefix automaton, if needed
        if (g_pInPrefFsmFile) {
            g_pref_dfa_image.Load (g_pInPrefFsmFile);
            const unsigned char * pImg = g_pref_dfa_image.GetImageDump ();
            DebugLogAssert (pImg);
            g_pref_fsm_dump.SetImage (pImg);
            g_pPrefFsm = &g_pref_fsm_dump;
            g_tr_pref.SetRsDfa (g_pPrefFsm);
        }
        // specify delimiters, if needed
        if (-1 != g_pref_delim) {
            g_tr_pref.SetDelim (g_pref_delim);
            g_tr_pref_rev.SetDelim (g_pref_delim);
        }
        if (-1 != g_redup_delim) {
            g_tr_hyph_redup.SetDelim (g_redup_delim);
            g_tr_hyph_redup_rev.SetDelim (g_redup_delim);
        }
        if (-1 != g_ucf_delim) {
            g_tr_ucf.SetDelim (g_ucf_delim);
            g_tr_ucf_rev.SetDelim (g_ucf_delim);
        }

        DebugLogAssert (pOs && pIs);

        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) {

                // UTF-8 --> UTF-32
                g_ChainSize = \
                    ::FAStrUtf8ToArray (pLine, LineLen, g_Chain, MaxChainSize);

                if (0 > g_ChainSize || MaxChainSize - 1 < g_ChainSize) {
                    throw FAException (FAMsg::IOError, __FILE__, __LINE__);
                }

                if (false == g_no_process) {

                    if (g_wtbt || g_wts) {
                        InitWtbtData ();
                    }
                    if (g_to_lower) {
                        ToLower ();
                    }
                    if (g_to_upper) {
                        ToUpper ();
                    }
                    if (g_to_capitalized) {
                        ToCapitalized ();
                    }
                    if (g_pCharMapFile) {
                        Normalize (); // the WTBT bounaries are lost 
                        /// uncomment if needed
                        /// if (g_wtbt || g_wts) {
                        ///    InitWtbtData ();
                        /// }
                    }
                    if (g_pInTr) {
                        ApplyTransformation ();
                    }
                }

                // UTF-32 --> UTF-8
                const int OutStrSize = \
                  ::FAArrayToStrUtf8 (g_Chain, g_ChainSize, OutStr, MaxStrSize - 1);

                if (0 > OutStrSize || MaxStrSize - 1 < OutStrSize) {
                    throw FAException (FAMsg::IOError, __FILE__, __LINE__);
                }

                OutStr [OutStrSize] = 0;

                if (false == g_no_output) {
                    (*pOs) << OutStr;
                }

            } // of if (0 < LineLen) ...

            if (false == g_no_output) {
                (*pOs) << '\n';
            }

        } // of while (!(pIs->eof ())) ...

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