int __cdecl main()

in blingfiretools/fa_align/fa_align.cpp [333:546]


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

    --argc, ++argv;

    ::FAIOSetup ();

    // process command line
    process_args (argc, argv);

    int LineNum = -1;
    std::string line;

    try {

        if (g_pInFile) {
            g_ifs.open (g_pInFile, std::ios::in);
            FAAssertStream (&g_ifs, g_pInFile);
            g_pIs = &g_ifs;
        }
        if (g_pOutFile) {
            g_ofs.open (g_pOutFile, std::ios::out);
            g_pOs = &g_ofs;
        }
        // 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);
        }

        FAStringTokenizer tokenizer;
        tokenizer.SetSpaces ("\t");

        while (!(g_pIs->eof ())) {

            if (!std::getline (*g_pIs, line))
                break;

            LineNum++;

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

                /// read the input

                tokenizer.SetString (pLine, LineLen);

                const char * pStr1 = NULL;
                int Len1 = 0;
                tokenizer.GetNextStr (&pStr1, &Len1);

                const char * pStr2 = NULL;
                int Len2 = 0;
                tokenizer.GetNextStr (&pStr2, &Len2);

                FAAssert (pStr1 && 0 < Len1 && pStr2 && 0 < Len2, \
                    FAMsg::IOError);

                g_Count1 = \
                    ::FAStrUtf8ToArray (pStr1, Len1, g_Buff1, MaxBuffSize);
                // normalize the case, if needed
                if (g_ignore_case) {
                    ::FAUtf32StrLower (g_Buff1, g_Count1);
                }
                // normalize a word
                if (g_pCharMapFile) {
                    g_Count1 = ::FANormalizeWord (g_Buff1, g_Count1, \
                        g_Buff1, MaxBuffSize, &g_charmap);
                }
                FAAssert (0 < g_Count1 && MaxBuffSize > g_Count1, \
                    FAMsg::IOError);

                g_Count2 = \
                    ::FAStrUtf8ToArray (pStr2, Len2, g_Buff2, MaxBuffSize);
                // normalize the case, if needed
                if (g_ignore_case) {
                    ::FAUtf32StrLower (g_Buff2, g_Count2);
                }
                // normalize a word
                if (g_pCharMapFile) {
                    g_Count2 = ::FANormalizeWord (g_Buff2, g_Count2, \
                        g_Buff2, MaxBuffSize, &g_charmap);
                }
                FAAssert (0 < g_Count2 && MaxBuffSize > g_Count2, \
                    FAMsg::IOError);

                if (g_no_process)
                    continue;

                /// make the alignment

                Process ();

                if (g_no_output)
                    continue;

                /// print the results

                if (false == g_no_epsilon) {

                    const int OutLen = ::FAArrayToStrUtf8 (g_OutBuff, \
                        g_OutCount, OutStr, MaxOutStr);
                    FAAssert (0 < OutLen && OutLen < MaxOutStr, FAMsg::IOError);
                    OutStr [OutLen] = 0;

                } else {

                    char * pOut = OutStr;

                    if (-1 != g_spec_l_anchor) {
                        pOut = ::FAIntToUtf8 (g_spec_l_anchor, pOut, \
                                                FAUtf8Const::MAX_CHAR_SIZE);
                        FAAssert (pOut, FAMsg::IOError);
                        *pOut++ = ' ';
                        pOut = ::FAIntToUtf8 (g_Filler2, pOut, \
                                                FAUtf8Const::MAX_CHAR_SIZE);
                        FAAssert (pOut, FAMsg::IOError);
                    }

                    bool fStart = true;
                    int PrevIw = -1;

                    for (int i = 0; i < g_OutCount; ++i) {

                        int C = g_OutBuff [i];

                        // a character from string 1
                        if (0 == i % 2) {

                            if (g_Filler == C) {
                                continue;
                            }

                            if (!fStart) {
                                *pOut++ = ' ';
                            }
                            fStart = false;

                            PrevIw = C;
                            pOut = ::FAIntToUtf8 (C, pOut, \
                                                  FAUtf8Const::MAX_CHAR_SIZE);
                            FAAssert (pOut, FAMsg::IOError);
                            *pOut++ = ' ';

                        // a character from string 2
                        } else {

                            // if the output is g_Filler and there will be
                            // something attached to the left, then don't
                            // print g_Filler2
                            if (g_Filler == C && i + 1 < g_OutCount && \
                                g_Filler == g_OutBuff [i + 1]) {
                                continue;
                            }

                            if (C == PrevIw) {
                                C = g_Filler2;
                            }

                            pOut = ::FAIntToUtf8 (C, pOut, \
                                                  FAUtf8Const::MAX_CHAR_SIZE);
                            FAAssert (pOut, FAMsg::IOError);
                            fStart = false;
                        }

                    } // of for (int i = 0; ...

                    *pOut = 0;
                }

                (*g_pOs) << OutStr << '\n';
            }

        } // of while (!(g_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';

        std::cerr << "ERROR: in data at line: " << LineNum << " in \"" \
            << line << "\"\n";

        return 2;

    } catch (...) {

        std::cerr << "ERROR: Unknown error in program " << __PROG__ << '\n';

        std::cerr << "ERROR: in data at line: " << LineNum << " in \"" \
            << line << "\"\n";

        return 1;
    }

    // print out memory leaks, if any
    FAPrintLeaks(&g_alloc, std::cerr);

    return 0;
}