/*	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.
*/


#ifndef SOURCEFILEINFO_H
#define SOURCEFILEINFO_H

#include "Templates/Array.h"


class FileInfo
{
public:
	cstr	filepath;
	time_t	mtime;
	off_t	size;

	~FileInfo ()						noexcept { delete[] filepath; }
	FileInfo ()							noexcept :filepath(nullptr),mtime(0),size(0){}
	FileInfo (FileInfo&& q)				noexcept;
	FileInfo (FileInfo const& q);
	FileInfo (cstr filepath, time_t mtime, off_t size);
	FileInfo (cstr filepath)			noexcept;	// sets errno

	FileInfo& operator= (FileInfo&& q)		noexcept { std::swap(filepath,q.filepath); return *this; }
	FileInfo& operator= (FileInfo const&)	noexcept;
	FileInfo& operator= (cstr filepath)		noexcept; // sets errno

	void updateMtime ()					noexcept;
	bool isOlder	 () const			noexcept; // is actual file mtime older than stored?
	bool isSame		 () const			noexcept; // is actual file mtime exactly as stored?
	bool isNewer	 () const			noexcept; // is actual file mtime newer than stored?
	bool isModified  () const			noexcept; // is actual file mtime != stored mtime?

	void print		 (FD&, cstr indent) const throws;
	void deserialize (FD&)				throws;
	void serialize	 (FD&) const		throws;
};


inline bool eq(FileInfo const& a, FileInfo const& b) noexcept
{
	return eq(a.filepath,b.filepath);
}


// ----------------------------------------------------------------


class FileInfos : public Array<FileInfo>
{
public:
	uint indexof (cstr filepath)		const	noexcept;
	uint indexof (FileInfo const& q)	const	noexcept { return indexof(q.filepath); }
	bool contains (cstr filepath)		const	noexcept { return indexof(filepath) != ~0u; }
	void appendifnew (FileInfo const& q)		noexcept { if(indexof(q) == ~0u) append(q); }
	void appendifnew (cstr filepath)			noexcept; // also not appended if errno

	uint lineForPos (cstr source, uint32 spos)	const noexcept;	// for __line__
	FileInfo& fileForPos (uint32 spos)			const noexcept;	// for __file__
};


#endif

























