// FileRev.cpp: implementation of the FileRev class.
//
//////////////////////////////////////////////////////////////////////

#include "FileHead.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

FileRev::FileRev(StrBuf newrev, FileHead* parent, StrBuf newchange, short revtype, bool text)
{
	rev = newrev;
	change = newchange;
	file = parent;
	type = revtype;
	prev = NULL;
	next = NULL;
	from = NULL;
	fromcheck = false;
	intocheck = false;
	fromrev = StrBuf();
	fromfile = StrBuf();
	intofiles = ListList();
	intorevs = ListList();
	ent = NULL;
	istext = text;

	fromarrows = NULL;
	fromtexts = NULL;
	intotexts = NULL;
}

void FileRev::AddFromArrow(FileRev* rev, ArrowType atype)
{
	if (
		(type == TYPE_EDIT || type == TYPE_ADD) 
		&& (atype != edit) ) atype = impure; //sanity check for "impure" integs
	FileRevArrow* newarrow = new FileRevArrow;
	newarrow->ptr = rev;
	newarrow->type = atype;
	switch (atype)
	{
	case edit: 
	case branch:
	case copy:
		newarrow->contrib = all; break;
	case ignore:
		newarrow->contrib = none; break;
	case impure:
	case merge:
		newarrow->contrib = some; break;
	}
	newarrow->next = fromarrows;
	fromarrows = newarrow;

	/* Downgrade "contrib" of other arrows as needed. */

	FileRevArrow* scan = fromarrows->next;
	while (scan)
	{
		switch (fromarrows->contrib)
		{
		case all:
			scan->contrib = none; break;
		case some:
			if (scan->contrib == all) scan->contrib = some; break;
		}
		scan = scan->next;
	}
}

void FileRev::AddFromText(StrBuf& infile, StrBuf& inrev, ArrowType atype)
{
	FileTextArrow* newtext = new FileTextArrow;
	newtext->file.Set(infile);
	newtext->rev.Set(inrev);
	newtext->type = atype;
	newtext->next = fromtexts;
	fromtexts = newtext;
}

void FileRev::AddIntoText(StrBuf& infile, StrBuf& inrev, ArrowType atype)
{
	FileTextArrow* newtext = new FileTextArrow;
	newtext->file.Set(infile);
	newtext->rev.Set(inrev);
	newtext->type = atype;
	newtext->next = intotexts;
	intotexts = newtext;
}

FileRev::~FileRev()
{
	FileRevArrow* rmg;
	while (fromarrows)
	{
		rmg = fromarrows->next;
		delete fromarrows;
		fromarrows = rmg;
	}

	if (next != NULL) delete next;
}
