#define XERR
#include "ordertxt.ih"

namespace 
{
    Pattern s_inline{ R"(\[(\..*)\])" };        // e.g., [.i]
}

bool OrderTxt::fill(ElementVect &vect)
{
    ifstream in{ "order.txt" };
    
    bool ret = true;

    string filename;                       // element in order.txt
    size_t lineIdx = 0;
    while (getline(in, filename))
    {
        Element element;
                                            // cf. V 1.21.01 
                                            //     handler/picksources.cc:

                                            // remove CRs from Windows
                                            // skip empty lines
        if (not (istringstream{ String::trim(filename) } >> element.name))
        {
            ++lineIdx;
            continue;
        }
                                            // at [.*] change the default
        if (lineIdx == 0 and (s_inline << element.name))        // inline ext.
        {
            setExtensionField(INL, s_inline[1]);
            ++lineIdx;
            continue;
        }
            
                                            // a name starting with a path
        if (element.name.find_first_of("/.") == 0)
        {
            errIdx(lineIdx) << element.name << 
                              ": relative or absolute paths not supported\n";
            element.type = ERR;
        }
        else 
        {
            size_t first = element.name.find_first_of("+-");

                                            // keep the +/-, otherwise 0
            first = first == 0 ? element.name.front() : 0;

            if (first != 0)                 // rm the initial +/-
                element.name.erase(element.name.begin());

            if (not fs::exists(element.name))
            {
                errIdx(lineIdx) << element.name << " does not exist\n";
                element.type = ERR;
            }
            else
            {
                switch (first)
                {
                    case '+':
                        element.type = CODE;
                    break;
                    
                    case '-':
                        element.type = SKIP;
                    break;

                    default:
                        setType(element);
                    break;
                }
            }


//            bool skip = element.name.find('-') == 0;
//
//            if (skip)                       // rm the initial -
//                element.name.erase(element.name.begin());
//
//            if (fs::exists(element.name))
//                setType(element);
//            else
//            {
//                errIdx(lineIdx) << element.name << " does not exist\n";
//                element.type = ERR;
//            }
//
//            if (skip)
//                element.type = SKIP;
        }

        if (element.type == ERR)
            ret = false;
        else 
        {
            element.idx = lineIdx;
            vect.push_back(move(element));
        }

        ++lineIdx;
    }
    return ret;
}


