// FileNode.cpp: implementation of the CFileNode class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "FileNode.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

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

CFileNode::CFileNode(CString filename)
{
	strName = filename;
	pFile = new Stuff::NotationFile(filename);
	ReadBuildFile(pFile);
	pParent = NULL;
}

CFileNode::~CFileNode()
{
	pFile->IgnoreChanges();
	delete pFile;
	POSITION pos = listChildren.GetHeadPosition();
	while (pos)
	{
		CFileNode *pChild = (CFileNode *)listChildren.GetNext(pos);
		delete pChild;
	}
}

/*******************************************************************************
/* function name: ReadBuildFile
/* description: Read a build file, and build a file hierarchy
/*******************************************************************************/
int CFileNode::ReadBuildFile(Stuff::NotationFile *notefile)
{
	int count = 0;
	Stuff::NotationFile::PageIterator *pages = notefile->MakePageIterator();
	Register_Object(pages);
	Stuff::Page *page;
	while (NULL != (page = pages->ReadAndNext()))
	{
		CString pagename = page->GetName();
		if (pagename.GetLength() ==0)
		{
			Stuff::Page::NoteIterator *notes = page->MakeNoteIterator();
			Stuff::Note *note;
			while (NULL != (note = notes->ReadAndNext()))
			{
				count++;
				CString notename = note->GetName();

				notename = "content\\"+notename;
				CFileNode *pNode = new CFileNode(notename);
				listChildren.AddHead(pNode);
				pNode->pParent = this;

				{
					Stuff::NotationFile entryfile;
					note->GetEntry(&entryfile);
					{
						CString entryfilename = entryfile.GetFileName();
						count += pNode->ReadBuildFile(&entryfile);
					}
				}
				
			}
			delete notes;
		}
	}
	Unregister_Object(pages);
	delete pages;
	return count;
}