#define XERR
#include "zipcheck.ih"

namespace
{
    Pattern fileName{ "^[\\w_]+(\\.\\w+)?$" };
}

bool ZipCheck::isTextFile(string const &entry)
{
    if (not (fileName << entry))
    {
        err() << entry << 
                ": incorrect filename (cf. submitinfo.txt item 4)\n.";
        return false;
    }

                                // check whether 'entry' is a text file
    d_coutExtractor.execute("/usr/local/bin/istxt " + entry);

    string info;
    getline(d_coutExtractor, info);

    if (info.find("text") == string::npos)
    {
        err() << "non-text file: " << d_path << entry << '\n';
        return false;
    }
                                // it's a text file: if not ending in '\n'
                                // add a final '\n'
    fstream io{ entry, ios::in | ios::out };
    io.seekg(-1, ios::end);
    if (io.peek() != '\n')      // if the last char != '\n'
    {
        io.seekp(0, ios::end);
        io.put('\n');           // then add a '\n' to the end
    }

    return true;
}
