#ifndef JES_FILELOADER_H
#define JES_FILELOADER_H

#include "jes/Ref.h"
#include "jes/Text.h"
#include <stdint.h>
#include <vector>

namespace jes
{
    class FileLoader
    {
    public:
        enum
        {
            LINE_ENDING_LF,
            LINE_ENDING_CR,
            LINE_ENDING_CRLF,
            LINE_ENDING_COUNT
        };

        FileLoader();
        ~FileLoader();
        bool load(const char * fname);
        unsigned int num_lines()
        {
            if (m_lines == NULL)
            {
                return 0u;
            }
            else
            {
                return m_lines->size();
            }
        }
        TextRef get_line(unsigned int line_no);
        int get_line_endings() { return m_line_endings; }
    protected:
        typedef std::pair<const uint8_t *, size_t> LineIndexPair;
        typedef std::vector<LineIndexPair> LineIndexPairVector;
        typedef Ref<LineIndexPairVector> LineIndexPairVectorRef;
        void load_buf(size_t size);
        uint8_t * m_buf;
        int m_line_endings;
        LineIndexPairVectorRef m_lines;
    };
    typedef Ref<FileLoader> FileLoaderRef;
}

#endif
