Source

gpalign-cpp / src / io.cpp

//--------------------------------------------------------------------------//
// io.cpp
// Lars Yencken <lars.yencken@gmail.com>
// vim: ts=4 sw=4 sts=4 expandtab:
// Sun Oct  7 22:03:57 EST 2007
//--------------------------------------------------------------------------//

#include "io.hpp"
#include "platform.hpp"

#include <fstream>
#include <iostream>
#include <locale>
#include <sstream>

using namespace std;

//--------------------------------------------------------------------------//

void parseEntries(const char* filename, vector<Segment>& entries)
{
    wcout << L"Reading from " << filename << endl;
    wifstream iStream(filename);

#ifndef BSD
    iStream.imbue(locale(g_locale));
#endif

    if (!iStream) {
        wcout << L"--> Error: couldn't open file for reading" << endl;
        exit(1);
    }

    wchar_t buffer[BUFFER_SIZE + 1];
    buffer[BUFFER_SIZE] = L'\0';

    entries.clear();
    int lineNo = 1;
    wstring line, graphemes, phonemes;
    while (getline(iStream, line)) {
        wistringstream wss(line);
        wss >> graphemes >> phonemes;
        if (graphemes.size() == 0 || phonemes.size() == 0 || 
                wchar_t(wss.peek()) != EOF) {
            wcout << L"--> Format error on line " << lineNo << endl;
            wcout << L"    Expected two space-separated entries: grapheme phoneme" << endl;
            exit(1);
        }
        entries.push_back(Segment(wstring(graphemes), wstring(phonemes)));
        lineNo++;
    }
    iStream.close();
    if (entries.size() > 0) {
        wcout << L"--> Read " << entries.size() << L" entries" << endl;
    } else {
        wcout << L"--> Error: no entries read" << endl;
        exit(1);
    }
    return;
}

//--------------------------------------------------------------------------//

void dumpAlignments(const char* filename,
        const vector<BaseAlignment>& alignments)
{
    wcout << L"Writing to " << filename << endl;
    wcout << L"--> " << alignments.size() << L" records" << endl;
    wofstream oStream(filename);
#ifndef BSD
    oStream.imbue(locale("en_US.UTF-8"));
#endif
    if (!oStream) {
        wcerr << L"--> Error: can't open file for writing" << endl;
        exit(1);
    }
    for (vector<BaseAlignment>::const_iterator iter = alignments.begin();
            iter != alignments.end(); iter++) {
        oStream << iter->first << L":" << iter->second << endl;
    }
    oStream.close();
    return;
}

//--------------------------------------------------------------------------//