#include "stdafx.h"
#include "LKFLineBuffer.h"

using namespace System::Runtime::InteropServices;
using namespace System::IO;

namespace lkfxdok {
	namespace c {
		LKFLineBuffer::LKFLineBuffer()
		{
			_lineExtend = 10000;
			_lineAlloc = 0;
			_lineCount = 0;
			_lines = nullptr;
		}

		LKFLineBuffer::LKFLineBuffer(String ^fileName) {
			StreamReader ^r = gcnew StreamReader(fileName);
			while (!r->EndOfStream) {
				AddLine(r->ReadLine());
			}
			r->Close();
		}

		void LKFLineBuffer::AddLine(String ^line) {

			if (_lineAlloc == _lineCount) 
			{
				array<String^> ^old = _lines;
				_lines = gcnew array<String^>(_lineAlloc += _lineExtend);
				
				if (_lineCount > 0) {
					for (unsigned int i = 0; i < _lineCount; i++)
						_lines[i] = old[i];
				}
			}

			_lines[_lineCount++] = line;
		}

		unsigned int LKFLineBuffer::LineExtend::get(void) {
			return _lineExtend;
		}

		void LKFLineBuffer::LineExtend::set(unsigned int lineExtend) {
			_lineExtend = lineExtend;
		}
		
		unsigned int LKFLineBuffer::LineCount::get(void) {
			return _lineCount;
		}

		String ^LKFLineBuffer::LineAtIndex(unsigned int index) {
			if (index < _lineCount)
				return _lines[index];
			return nullptr;
		}

	}
}
