// PathManager.cpp: implementation of the CPathManager class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Peeper.h"
#include "PathManager.h"

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

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

CPathManager::CPathManager()
{
	


	
}

void CPathManager::ScanRegistry()
{
	HKEY dirkey;
	if(ERROR_SUCCESS==RegOpenKey(HKEY_CURRENT_USER,"Software\\Microsoft\\Devstudio\\6.0\\Build System\\Components\\Platforms\\Win32 (x86)\\Directories",&dirkey))
	{
		char cstr[8096];
		DWORD size=8096;		
		if(ERROR_SUCCESS==RegQueryValueEx(dirkey,"Include Dirs",NULL,NULL,(unsigned char *)cstr,&size))
		{
			CString str=cstr;
			int start=0,end;
			
			end=str.Find(';');
			while(end>=0)
			{
				AddPath(str.Mid(start,end-start));
				start=end+1;
				end=str.Find(';',start);
			}
			
			AddPath(str.Mid(start));			
		}
		RegCloseKey(dirkey);
	}
	
}


bool SysGetFile(const char *fname,const CString &path,CString &outbuf)
{
	
	OFSTRUCT ofs;
	HFILE file;
	

	file=OpenFile(path+fname,&ofs,OF_READ);
	if(file==HFILE_ERROR) return false;
		DWORD rbts,fsize=GetFileSize((HANDLE)file,NULL);
		char *buf=new char[fsize];
		ReadFile((HANDLE)file,buf,fsize,&rbts, NULL);
		outbuf = buf;
		delete buf;
	CloseHandle((HANDLE)file);
//	outbuf.Replace("\x0D\x0A","\n");

	return true;
}

bool CPathManager::DoesFileExist(const char *fname,const CString &path)
{
	OFSTRUCT ofs;
	HFILE file;

	file=OpenFile(path+fname,&ofs,OF_READ);
	if(file==HFILE_ERROR) return false;
	CloseHandle((HANDLE)file);
	return true;
}

bool CPathManager::GetPath(const char *fname,CString &path)
{
	for(int i=0;i<PathList.GetSize();i++)
	{
		if(DoesFileExist(fname,PathList[i]))
		{
		path=PathList[i];
		return true;
		}
	}
	
return false;
}

bool CPathManager::GetFile(const char *fname,CString &path,CString &outfile)
{
	for(int i=0;i<PathList.GetSize();i++)
	{
		if(SysGetFile(fname,PathList[i],outfile))
		{
		path=PathList[i];
		return true;
		}
	}
	
return false;
}

void CPathManager::ScanProject(CString dspfname)
{
PathList.RemoveAll();
	
	CString dsppath,dspbase;
	dsppath=dspfname.Left(dspfname.ReverseFind('\\'));
	dspbase=dspfname.Mid(dspfname.ReverseFind('\\'));
	CString file_data;
	if(SysGetFile(dspbase,dsppath,file_data))
	{
	
	int startcpp=0,endl;
	
	startcpp=file_data.Find("# ADD CPP",startcpp);
	while(startcpp>=0)
	{
		endl=file_data.Find('\n',startcpp);
		if(endl<0) endl=file_data.GetLength();
		
		int startinc=0;
		
		startinc=file_data.Find("/I",startinc);
		while(startinc>=0 && startinc<endl)
		{
			int sst,sed;
			CString subpath;
			sst=file_data.Find('\"',startinc);
			ASSERT(sst<=endl);
			sed=file_data.Find('\"',sst+1);
			ASSERT(sed<=endl);
			subpath=dsppath+"\\"+file_data.Mid(sst+1,(sed-sst)-1);
			AddPath(subpath);
			startinc=file_data.Find("/I",sed);
		}
		
		
		startcpp=file_data.Find("# ADD CPP",startcpp+1);
	}
	}
	
	ScanRegistry();
}



void CPathManager::AddPath(CString str)
{
	for(int i=0;i<PathList.GetSize();i++)
	{
		if(!PathList[i].CompareNoCase(str)) 
			return;
	}
	PathList.Add(str+'\\');
}

CPathManager::~CPathManager()
{
	
}
