/*	Copyright  (c)	Günter Woigk 2010 - 2018
					mailto:kio@little-bat.de

	This file is free software

 	This program is distributed in the hope that it will be useful,
 	but WITHOUT ANY WARRANTY; without even the implied warranty of
 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:

	• Redistributions of source code must retain the above copyright notice,
	  this list of conditions and the following disclaimer.
	• Redistributions in binary form must reproduce the above copyright notice,
	  this list of conditions and the following disclaimer in the documentation
	  and/or other materials provided with the distribution.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
	AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
	THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
	PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
	CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
	PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
	OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
	WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
	OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
	ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


#include "FileInfo.h"
#include "Libraries/unix/files.h"
#include "utilities.h"


// ----------------------------------------------------------------
//	FileInfo
// ----------------------------------------------------------------

FileInfo::FileInfo (const FileInfo& q)
:
	filepath(newcopy(q.filepath)),
	mtime(q.mtime),
	size(q.size)
{}

FileInfo::FileInfo (cstr s, time_t mtime, off_t size)
:
	filepath(newcopy(s)),
	mtime(mtime),
	size(size)
{}

FileInfo::FileInfo (cstr fname) noexcept
:
	FileInfo()
{
	fname = fullpath(fname);

	if (errno == noerror)
	{
		fname = newcopy(fname);
		delete[] filepath;
		filepath = fname;
		mtime = file_mtime(fname);
		size = file_size(fname);
	}
}

FileInfo::FileInfo (FileInfo&& q) noexcept
:
	filepath(q.filepath),
	mtime(q.mtime),
	size(q.size)
{
	q.filepath = nullptr;
}

FileInfo& FileInfo::operator= (FileInfo const& q) noexcept
{
	if(this != &q)
	{
		delete[] filepath;
		new(this) FileInfo(q);
	}
	return *this;
}

FileInfo& FileInfo::operator= (cstr fname) noexcept
{
	delete[] filepath;
	new(this) FileInfo(fname);
	return *this;
}

void FileInfo::updateMtime() noexcept
{
	mtime = file_mtime(filepath);
}

bool FileInfo::isSame() const noexcept
{
	return file_mtime(filepath) == mtime;
}

bool FileInfo::isModified() const noexcept
{
	return file_mtime(filepath) != mtime;
}

bool FileInfo::isNewer() const noexcept
{
	return file_mtime(filepath) > mtime;
}

bool FileInfo::isOlder() const noexcept
{
	return file_mtime(filepath) < mtime;
}

void FileInfo::print(FD& fd, cstr indent) const throws
{
	fd.write_fmt("%s[%6li] %s %s\n", indent, long(size), datetimestr(mtime), filepath);
}

void FileInfo::deserialize(FD& fd) throws
{
	fd.read(mtime);
	fd.read(size);
	filepath = fd.read_new_nstr();
}

void FileInfo::serialize(FD& fd) const throws
{
	fd.write(mtime);
	fd.write(size);
	fd.write_nstr(filepath);
}


// ----------------------------------------------------------------
//	Array FileInfo
// ----------------------------------------------------------------

uint FileInfos::indexof (cstr filepath) const noexcept
{
	for (uint i=cnt; i--;) { if (eq(filepath,data[i].filepath)) return i; }
	return ~0u;
}

void FileInfos::appendifnew (cstr filepath) noexcept
{
	errno = noerror;
	if (contains(filepath)) return;
	FileInfo fi(filepath);
	if (errno==noerror) append(std::move(fi));
}

FileInfo& FileInfos::fileForPos (uint32 spos) const noexcept
{
	assert(cnt > 0);

	uint i = 0;
	while(i+1 < cnt && spos >= data[i].size)
	{
		spos -= data[i++].size;
	}

	return data[i];
}

uint FileInfos::lineForPos (cstr source, uint32 spos) const noexcept
{
	assert(cnt > 0);

	for(uint i = 0; i+1 < cnt && spos >= data[i].size; i++)
	{
		spos   -= data[i].size;
		source += data[i].size;
	}

	return ::lineForPos(source,spos);
}
























