[GME-commit] GMESRC/Tools/PropertyMerge/PropMerger PropMerger.cpp, NONE, 1.1 PropMerger.sln, NONE, 1.1 PropMerger.vcproj, NONE, 1.1 Traverser.cpp, NONE, 1.1 Traverser.h, NONE, 1.1 stdafx.cpp, NONE, 1.1 stdafx.h, NONE, 1.1

Log messages of CVS commits gme-commit at list.isis.vanderbilt.edu
Tue Jan 8 15:10:22 CST 2008


Update of /project/gme-repository/GMESRC/Tools/PropertyMerge/PropMerger
In directory escher:/tmp/cvs-serv26444/PropertyMerge/PropMerger

Added Files:
	PropMerger.cpp PropMerger.sln PropMerger.vcproj Traverser.cpp 
	Traverser.h stdafx.cpp stdafx.h 
Log Message:
PropMiner & PropMerger checked in.


CVS User: Zoltan Molnar, ISIS (zolmol)

--- NEW FILE: Traverser.cpp ---
#include "StdAfx.h"
#include ".\traverser.h"
#include <fstream>
#include <algorithm>

#define _WIN32_DCOM  // for COInitializeEx

const char * const_default_out_dir = "clip/";

Traverser::Traverser( const std::string& p_path, const std::string& p_dirPath, bool p_hier1d, bool p_hier2d)
	: m_path( p_path)
	, m_inDirPath( p_dirPath)
	, m_hier1d( p_hier1d)
	, m_hier2d( p_hier2d)
{
	setPathPrefix();

	//std::cout << ( ( m_hier1d||m_hier2d)? "Input files will be searched for in subdirectories" : "Input files will be taken from one dir") << std::endl;

	// init COM
	HRESULT hr = CoInitializeEx(0, COINIT_APARTMENTTHREADED);
	ASSERT( SUCCEEDED( hr));
	//BOOL rt = AfxOleInit();
	//ASSERT( rt);
}

Traverser::~Traverser(void)
{
	m_xmlParser.Release();
	m_theGme.Release();

	//m_theProj.Release();
	//m_theTerr.Release();

	// uninit COM
	CoUninitialize();
}

void Traverser::setPathPrefix()
{
	if( !m_inDirPath.empty())
	{
		m_pathPrefix = m_inDirPath;
		if( m_pathPrefix[ m_pathPrefix.length() - 1] != '/'
		 ||	m_pathPrefix[ m_pathPrefix.length() - 1] != '\\')
			m_pathPrefix.append( "/");
	}
	else
	{
		std::string::size_type p = m_path.find_last_of( '\\');
		if( p == std::string::npos)
			p = m_path.find_last_of( '/');

		if( p != std::string::npos)
		{
			m_pathPrefix = m_path.substr( 0, p + 1); // tailing '\\' also
		}

		m_pathPrefix.append( const_default_out_dir);
	}
}

void Traverser::loadIgnorables()
{
	std::string ign_f_name( m_pathPrefix.c_str());
	ign_f_name.append( "objs2ignore.txt");

	std::ifstream f;
	f.open( ign_f_name.c_str());

	if( f)
	{
		char buff[42];
		while( f && f.getline( &buff[0], sizeof( buff) - 1, '\n'))
		{
			m_ignoreList.push_back( std::string( buff));
		}
		
		f.close();
	}
	else
	{
		consoleMsg( "Could not open ignoreable object list file: ", MSG_ERROR);
		consoleMsg( ign_f_name.c_str(), MSG_ERROR);
	}
}

bool Traverser::foundAsIgnoreable( const CComBSTR& p_objId)
{
	char *buff;
	CopyTo( p_objId, &buff);
	std::string o_id( buff);
	if( buff) delete [] buff;

	return std::find( m_ignoreList.begin(), m_ignoreList.end(), o_id) != m_ignoreList.end();
}

CComPtr<IGMEOLEApp> Traverser::getGME( IMgaProject *p_project)
{
	CComPtr<IGMEOLEApp> ret;
	if( !p_project) return ret;

	CComBSTR bstrName("GME.Application");
	CComQIPtr<IMgaClient> pClient;
	HRESULT hr = p_project->GetClientByName( bstrName, &pClient);
	if( FAILED( hr)) 
	{
		//ASSERT(0);
		//throw "Can't retrieve GME Application object";
	}
	if( SUCCEEDED( hr) && pClient) {
		CComQIPtr<IDispatch> pDispatch;
		COMTHROW( pClient->get_OLEServer( &pDispatch));
		if( pDispatch) {
			COMTHROW( pDispatch.QueryInterface( &ret));
		}
	}

	return ret;
}

void Traverser::stdConsoleMsg( const char *    p_msg, int p_type, bool p_prefixed)
{
	std::cout << std::endl;

	if( p_prefixed)
	{
		switch( p_type) {	
			case MSG_ERROR:    std::cout << "E: ";break;
			case MSG_INFO:     std::cout << "I: ";break;
			case MSG_WARNING:  std::cout << "W: ";break;
			case MSG_NORMAL:
			default:           std::cout << "N: ";break;
		}
	} 

	std::cout << (p_msg ? p_msg:"<null>");
}

void Traverser::stdConsoleMsg( const CComBSTR& p_msg, int p_type, bool p_prefixed)
{
	std::cout << std::endl;

	if( p_prefixed)
	{
		switch( p_type) {	
			case MSG_ERROR:    std::cout << "E: ";break;
			case MSG_INFO:     std::cout << "I: ";break;
			case MSG_WARNING:  std::cout << "W: ";break;
			case MSG_NORMAL:
			default:           std::cout << "N: ";break;
		}
	} 

	char * msg = 0;
	CopyTo( p_msg, &msg); // msg will be freed by us
	std::cout << (msg ? msg:"<null>");
	if( msg) 
		delete [] msg;
}

void Traverser::consoleMsg( const CComBSTR& p_msg, int p_type, bool p_prefixed)
{
	if( !m_theGme) 
	{
		stdConsoleMsg( p_msg, p_type, p_prefixed); 
		return;
	}
	m_theGme->ConsoleMessage( p_msg, (msgtype_enum) p_type);
}

void Traverser::consoleMsg( const char *    p_msg, int p_type, bool p_prefixed)
{
	if( !m_theGme)
	{
		stdConsoleMsg( p_msg, p_type, p_prefixed); 
		return;
	}
	m_theGme->ConsoleMessage( CComBSTR( p_msg), (msgtype_enum) p_type);
}

bool Traverser::fileFound( const char * p_file)
{
	FILE *f = fopen( p_file, "r");
	if( f) // file present
	{
		fclose( f);
		return true; 
	}

	return false;
}

bool Traverser::fileFound( const CComBSTR& p_file)
{
	char *buff = 0;
	CopyTo( p_file, &buff);

	bool rv = fileFound( buff);

	if( buff) delete [] buff;

	return rv;
}

void Traverser::createMgaDtd()
{
	std::string dtd_f_name( m_pathPrefix.c_str());
	dtd_f_name.append( "mga.dtd");

	if( fileFound( dtd_f_name.c_str())) return;

	//consoleMsg( "Could not find mga.dtd file in the directory. Please copy it there.", MSG_ERROR);
	//throw -1;

	char * data = 0;
	DWORD  len  = 0;
	bool   sc   = true;

	HMODULE hm = 0;//GetModuleHandle("PropMerge.dll");
	if( !hm) sc = false;
	else {
		HRSRC res1 = FindResource(hm, "IDR_MGA.DTD", "DTD"); // mga.dtd is copied into this resource
		if( !res1) sc = false;
		else {
			HGLOBAL res2 = LoadResource(hm, res1);
			if( !res2) sc = false;
			else {
				data = ( char* )LockResource( res2);
				if( !data) sc = false;
				else {
					len = SizeofResource(hm, res1);
				}
			}
		}
	}

	FILE *f;
	// create, if we have the content
	if( sc)
		f = fopen( dtd_f_name.c_str(), "w");

	if( !sc || !f || !data || !len)
	{
		consoleMsg( "Could not create mga.dtd file in clip directory. Please copy it there.", MSG_ERROR);
		throw -1;
	}

	fwrite( data, len, 1, f);
	fclose( f);
}

CComBSTR Traverser::makeFileName( const CComBSTR& p_gd, bool *p_fileExists)
{
	CComBSTR ret;
	ret.Append( m_pathPrefix.c_str());
	if( m_hier1d || m_hier2d)
	{
		char *bf = 0;
		CopyTo( p_gd, &bf);
		if( bf)
		{
			ret.Append( std::string( 1, bf[1]).c_str()); // the 2nd char is the first digit since it starts with '{'
			if( m_hier2d)
				ret.Append( std::string( 1, bf[2]).c_str()); // the two digit option
			ret.Append( "/");
			delete [] bf;
		}
	}
	ret.AppendBSTR( p_gd);

	*p_fileExists = fileFound( ret);
	return ret;
}

template <typename T>
void Traverser::dealWith( IMgaProject *p_project, IMgaTerritory *p_terr, T   *p_item)
{
	if( !m_xmlParser) return;
	if( !p_item) 
	{
		ASSERT(0);
		return;
	}

	CComBSTR gd;
	COMTHROW( p_item->GetGuidDisp( &gd));

	bool file_exists = false;
	CComBSTR fname = makeFileName( gd, &file_exists);

	try
	{
		COMTHROW( p_project->CommitTransaction());
	}
	catch(...)
	{
		COMTHROW( p_project->AbortTransaction());
		consoleMsg( "Error before commiting to ", MSG_ERROR);
		consoleMsg( gd, MSG_ERROR);
	}

	try
	{
		if( file_exists)
			COMTHROW( m_xmlParser->ParseFCOs( p_item, fname));
		else if( !foundAsIgnoreable( gd))
		{
			fname.Append( " not found for object below ");
			consoleMsg( fname, MSG_ERROR);//("No such file found as " + fname).c_str(), MSG_ERROR);
			consoleMsg( gd, MSG_ERROR, false);
		}
	}
	catch(...)
	{
		consoleMsg( "Error during parsing into ", MSG_ERROR);
		consoleMsg( gd, MSG_ERROR);
	}
	
	try 
	{
		COMTHROW( p_project->BeginTransaction( p_terr));
	}
	catch(...)
	{
		consoleMsg( "Could not restart transaction after parsing into ", MSG_ERROR);
		consoleMsg( gd, MSG_ERROR);
		throw;
	}
}

void Traverser::traverse( IMgaProject *p_project, IMgaTerritory *p_terr, IMgaFolder *p_curr)
{
	if( !p_curr) return;

	CComPtr<IMgaFolders> subs;
	CComPtr<IMgaFCOs>    objs;
	long                 len = 0;

	COMTHROW( p_curr->get_ChildFolders( &subs));
	if( subs) COMTHROW( subs->get_Count( &len));
	for( long i = 1; i <= len; ++i)
	{
		CComPtr<IMgaFolder> item_i;
		COMTHROW( subs->get_Item( i, &item_i));

		if( !item_i)
		{
			ASSERT(0);
			continue;
		}

		dealWith( p_project, p_terr, item_i.p);

		traverse( p_project, p_terr, item_i);
	}

	len = 0; // will be reused
	COMTHROW( p_curr->get_ChildFCOs( &objs));
	if( objs) COMTHROW( objs->get_Count( &len));
	for( long i = 1; i <= len; ++i)
	{
		CComPtr<IMgaFCO> item_i;
		COMTHROW( objs->get_Item( i, &item_i));

		if( !item_i)
		{
			ASSERT(0);
			continue;
		}

		dealWith( p_project, p_terr, item_i.p);

		objtype_enum ot;
		COMTHROW( item_i->get_ObjType( &ot));
		if( ot == OBJTYPE_MODEL)
		{
			CComQIPtr<IMgaModel> item_m( item_i);
			ASSERT( item_m);
			traverse( p_project, p_terr, item_m);
		}
	}
}

void Traverser::traverse( IMgaProject *p_project, IMgaTerritory *p_terr, IMgaModel  *p_curr)
{
	if( !p_curr) return;

	CComPtr<IMgaFolders> subs;
	CComPtr<IMgaFCOs>    objs;
	long                 len = 0;

	COMTHROW( p_curr->get_ChildFCOs( &objs));
	if( objs) COMTHROW( objs->get_Count( &len));
	for( long i = 1; i <= len; ++i)
	{
		CComPtr<IMgaFCO> item_i;
		COMTHROW( objs->get_Item( i, &item_i));

		if( !item_i)
		{
			ASSERT(0);
			continue;
		}

		dealWith( p_project, p_terr, item_i.p);

		objtype_enum ot;
		COMTHROW( item_i->get_ObjType( &ot));
		if( ot == OBJTYPE_MODEL)
		{
			CComQIPtr<IMgaModel> item_m( item_i);
			ASSERT( item_m);
			traverse( p_project, p_terr, item_m);
		}
	}
}


// This is the main component method for interpereters and plugins. 
// May als be used in case of invokeable addons
long Traverser::InvokeEx( IMgaProject *project,  IMgaFCO *currentobj,  
									IMgaFCOs *selectedobjs,  long param) {
	COMTRY {
		CComPtr<IMgaTerritory> terr;

		consoleMsg( "Import started from: ", MSG_INFO);
		consoleMsg( m_pathPrefix.c_str(), MSG_INFO);

		//try {
		//	createMgaDtd();
		//} catch(...) {
		//}
		
		try {
			m_theGme = getGME( project);
		} catch(...) {
			consoleMsg( "Executed outside of the GME application", MSG_INFO);
		}

		loadIgnorables();

		m_xmlParser.CoCreateInstance( L"Mga.MgaParser");
		//m_theProj = CComPtr<IMgaProject>( project);


		COMTHROW( project->CreateTerritory( NULL, &terr));
		COMTHROW( project->BeginTransaction( terr));

		try 
		{
			CComPtr<IMgaFolder> rf;
			COMTHROW( project->get_RootFolder( &rf));

			dealWith( project, terr, rf.p);
			traverse( project, terr, rf);

			COMTHROW( project->CommitTransaction());
		}
		catch( hresult_exception& hr)
		{
			consoleMsg( "HResult Exception during traversal:", MSG_ERROR);
			consoleMsg( hr.what(), MSG_ERROR);

			project->AbortTransaction(); 
			throw; 
		}
		catch(...)
		{
			project->AbortTransaction(); 
			throw; 
		}
	} COMCATCH(;);
}


void Traverser::exec()
{
	CComPtr<IMgaProject> the_proj;
	the_proj.CoCreateInstance( L"Mga.MgaProject");
	if( !the_proj)
	{
		consoleMsg( "Could not create Mga.MgaProject COM class!", MSG_ERROR);
		return;
	}

	if( m_path.empty())
	{
		consoleMsg( "Input file not provided!", MSG_ERROR);
		return;
	}

	CComBSTR b_path( "MGA=");
	b_path.Append( m_path.c_str());
	
	// Open
	try
	{
		COMTHROW( the_proj->Open( b_path));
	}
	catch( hresult_exception& hr)
	{
		consoleMsg( "Could not open project file:", MSG_ERROR);
		consoleMsg( b_path, MSG_ERROR);
		consoleMsg( "HResult Exception during execution:", MSG_ERROR);
		consoleMsg( hr.what(), MSG_ERROR);
		return;
	}

	// Invoke traversal
	try
	{
		COMTHROW( InvokeEx( the_proj, 0, 0, 0));
	}
	catch( hresult_exception& hr)
	{
		consoleMsg( "HResult Exception during execution:", MSG_ERROR);
		consoleMsg( hr.what(), MSG_ERROR);
	}
	catch( ...)
	{
		consoleMsg( "Unknown exception during execution", MSG_ERROR);
	}

	// Save
	try
	{
		COMTHROW( the_proj->Save( b_path, VARIANT_TRUE)); // ?keepoldname?
	}
	catch( hresult_exception& hr)
	{
		consoleMsg( "HResult Exception during save", MSG_ERROR);
		consoleMsg( hr.what(), MSG_ERROR);
	}
	catch( ...)
	{
		consoleMsg( "Unknown exception during save", MSG_ERROR);
	}
	
	// Close
	try
	{
		COMTHROW( the_proj->Close( VARIANT_FALSE)); // ?abort?
	}
	catch( hresult_exception& hr)
	{
		consoleMsg( "HResult Exception during close", MSG_ERROR);
		consoleMsg( hr.what(), MSG_ERROR);
	}
	catch( ...)
	{
		consoleMsg( "Unknown exception during close", MSG_ERROR);
	}
}


--- NEW FILE: PropMerger.cpp ---
// PropMerger.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Traverser.h"

void exec( const std::string& p_inoutFilePath, const std::string& p_inDirRoot, bool p_h1, bool p_h2)
{
	try
	{
		Traverser * e = new Traverser( p_inoutFilePath, p_inDirRoot, p_h1, p_h2);
		e->exec();
		delete e;
	}
	catch(...)
	{
		std::cout << "Error: Exception caught" << std::endl;
	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	if( argc < 2)
	{
		std::cout << "Usage: " << argv[0] << " <file.mga> [-1|-2|-f] [-d inDir]" << std::endl;
		std::cout << "       -1: look for input files in one digit hashed subdirectories" << std::endl;
		std::cout << "       -2: look for input files in two digit hashed subdirectories" << std::endl;
		std::cout << "       -f: look for flat input files in one directory" << std::endl;
		std::cout << "       -d: directory root for the input files. Default: <input file's dir>/clip/" << std::endl;
	}
	else
	{
		std::string inout_file;
		std::string in_dir;

		bool one_dig_subdirs       = false;
		bool two_dig_subdirs       = false;
		bool inout_file_collected  = false;
		bool in_dir_collect_mode   = false;

		for( int i = 1; i < argc; ++i)
		{
			std::string curr( argv[i]);
			if( curr.size() <= 0) continue;

			if( in_dir_collect_mode)
			{
				in_dir = curr;
				in_dir_collect_mode = false;
			}
			else if( curr[0] == '-' || curr[0] == '/')
			{
				if( curr.substr(1) == "1")
					one_dig_subdirs = true;
				else if( curr.substr(1) == "2")
					two_dig_subdirs = true;
				else if( curr.substr(1, 1) == "d" || curr.substr(1, 1) == "D")
					in_dir_collect_mode = true;
			}
			else
			{
				if( !inout_file_collected)
				{
					inout_file = curr;
					inout_file_collected = true;
				}
			}
		}

		// some validation:
		if( one_dig_subdirs && two_dig_subdirs)
		{
			std::cout << "-1 and -2 can't be used in the same time.Defaulting to -1." << std::endl;
			two_dig_subdirs = false;
		}

		//
		exec( inout_file, in_dir, one_dig_subdirs, two_dig_subdirs);
	}

	return 0;
}


--- NEW FILE: PropMerger.vcproj ---
<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioProject
	ProjectType="Visual C++"
	Version="7.10"
	Name="PropMerger"
	ProjectGUID="{32BFBADC-C521-4B8F-9653-618F4F7D9CF7}"
	Keyword="Win32Proj">
	<Platforms>
		<Platform
			Name="Win32"/>
	</Platforms>
	<Configurations>
		<Configuration
			Name="Debug|Win32"
			OutputDirectory="Debug"
			IntermediateDirectory="Debug"
			ConfigurationType="1"
			UseOfMFC="2"
			UseOfATL="0"
			CharacterSet="2">
			<Tool
				Name="VCCLCompilerTool"
				Optimization="0"
				PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
				MinimalRebuild="TRUE"
				BasicRuntimeChecks="3"
				RuntimeLibrary="3"
				UsePrecompiledHeader="3"
				WarningLevel="3"
				Detect64BitPortabilityProblems="TRUE"
				DebugInformationFormat="4"/>
			<Tool
				Name="VCCustomBuildTool"/>
			<Tool
				Name="VCLinkerTool"
				AdditionalOptions="/VERBOSE:lib"
				OutputFile="$(OutDir)/PropMerger.exe"
				LinkIncremental="2"
				IgnoreDefaultLibraryNames=""
				GenerateDebugInformation="TRUE"
				ProgramDatabaseFile="$(OutDir)/PropMerger.pdb"
				SubSystem="1"
				TargetMachine="1"/>
			<Tool
				Name="VCMIDLTool"/>
			<Tool
				Name="VCPostBuildEventTool"/>
			<Tool
				Name="VCPreBuildEventTool"/>
			<Tool
				Name="VCPreLinkEventTool"/>
			<Tool
				Name="VCResourceCompilerTool"/>
			<Tool
				Name="VCWebServiceProxyGeneratorTool"/>
			<Tool
				Name="VCXMLDataGeneratorTool"/>
			<Tool
				Name="VCWebDeploymentTool"/>
			<Tool
				Name="VCManagedWrapperGeneratorTool"/>
			<Tool
				Name="VCAuxiliaryManagedWrapperGeneratorTool"/>
		</Configuration>
		<Configuration
			Name="Release|Win32"
			OutputDirectory="Release"
			IntermediateDirectory="Release"
			ConfigurationType="1"
			UseOfMFC="2"
			UseOfATL="1"
			CharacterSet="2">
			<Tool
				Name="VCCLCompilerTool"
				PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
				RuntimeLibrary="2"
				UsePrecompiledHeader="3"
				WarningLevel="3"
				Detect64BitPortabilityProblems="TRUE"
				DebugInformationFormat="3"/>
			<Tool
				Name="VCCustomBuildTool"/>
			<Tool
				Name="VCLinkerTool"
				OutputFile="$(OutDir)/PropMerger.exe"
				LinkIncremental="1"
				IgnoreDefaultLibraryNames=""
				GenerateDebugInformation="TRUE"
				SubSystem="1"
				OptimizeReferences="2"
				EnableCOMDATFolding="2"
				TargetMachine="1"/>
			<Tool
				Name="VCMIDLTool"/>
			<Tool
				Name="VCPostBuildEventTool"/>
			<Tool
				Name="VCPreBuildEventTool"/>
			<Tool
				Name="VCPreLinkEventTool"/>
			<Tool
				Name="VCResourceCompilerTool"/>
			<Tool
				Name="VCWebServiceProxyGeneratorTool"/>
			<Tool
				Name="VCXMLDataGeneratorTool"/>
			<Tool
				Name="VCWebDeploymentTool"/>
			<Tool
				Name="VCManagedWrapperGeneratorTool"/>
			<Tool
				Name="VCAuxiliaryManagedWrapperGeneratorTool"/>
		</Configuration>
	</Configurations>
	<References>
	</References>
	<Files>
		<Filter
			Name="Source Files"
			Filter="cpp;c;cxx;def;odl;idl;hpj;bat;asm;asmx"
			UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}">
			<File
				RelativePath=".\PropMerger.cpp">
			</File>
			<File
				RelativePath=".\stdafx.cpp">
				<FileConfiguration
					Name="Debug|Win32">
					<Tool
						Name="VCCLCompilerTool"
						UsePrecompiledHeader="1"/>
				</FileConfiguration>
				<FileConfiguration
					Name="Release|Win32">
					<Tool
						Name="VCCLCompilerTool"
						UsePrecompiledHeader="1"/>
				</FileConfiguration>
			</File>
			<File
				RelativePath=".\Traverser.cpp">
			</File>
		</Filter>
		<Filter
			Name="Header Files"
			Filter="h;hpp;hxx;hm;inl;inc;xsd"
			UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}">
			<File
				RelativePath=".\stdafx.h">
			</File>
			<File
				RelativePath=".\Traverser.h">
			</File>
		</Filter>
		<Filter
			Name="Resource Files"
			Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx"
			UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}">
		</Filter>
		<File
			RelativePath=".\ReadMe.txt">
		</File>
	</Files>
	<Globals>
	</Globals>
</VisualStudioProject>

--- NEW FILE: stdafx.h ---
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once

#define WINVER 0x0501

#include <iostream>
#include <tchar.h>
#include <Afx.h>
//#include <afxdisp.h>
#include <Atlbase.h>
#include <Objbase.h>


#if !defined(ASSERT) && defined(ATLASSERT)
#define ASSERT ATLASSERT
#endif

// --------------------------- hresult_exception

class hresult_exception : public exception
{
public:
	hresult_exception() throw();
	hresult_exception(const hresult_exception &e) throw();
	hresult_exception(HRESULT hr) throw();
	hresult_exception &operator=(const hresult_exception &e) throw();
	hresult_exception &operator=(HRESULT hr) throw();
	~hresult_exception() throw();
	virtual const char *what() const throw();

public:
	HRESULT hr;
};



#define HR_THROW(_hr) \
do { \
	ASSERT(("HR_THROW: Throwing HRESULT exception. Press IGNORE", false)); \
	throw hresult_exception(_hr); \
} while(false)


#define COMTHROW(FUNC) \
do { \
	HRESULT _hr = (FUNC); \
	if( FAILED(_hr) ) { \
		ASSERT(("COMTHROW: Throwing HRESULT exception. Press IGNORE", false)); \
		throw hresult_exception(_hr); \
	} \
} while(false)

#define COMTRY try

#define COMCATCH(CLEANUP) \
	catch(hresult_exception &e) \
	{ \
		ASSERT( FAILED(e.hr) ); \
		{ CLEANUP; } \
		return e.hr; \
	} \
	return S_OK;


void CopyTo(const CComBSTR b, char* *p_ptrResult); // caller's job to free
void CopyTo(const OLECHAR *p, int olelen, char *s, int charlen);
void CopyTo(const char *p, int len, BSTR *b);

--- NEW FILE: stdafx.cpp ---
// stdafx.cpp : source file that includes just the standard includes
// PropMerger.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information

#include "stdafx.h"

// --------------------------- hresult_exception

hresult_exception::hresult_exception()
{
	hr = 0;
}

hresult_exception::hresult_exception(const hresult_exception &e)
{
	hr = e.hr;
}

hresult_exception::hresult_exception(HRESULT a)
{
	hr = a;
}

hresult_exception &hresult_exception::operator=(const hresult_exception &e)
{
	hr = e.hr;
	return *this;
}

hresult_exception &hresult_exception::operator=(HRESULT a)
{
	hr = a;
	return *this;
}

hresult_exception::~hresult_exception()
{
}

const char *hresult_exception::what() const
{
	static char message[80];
	sprintf(message, "HRESULT (0x%08lx) exception", hr);
	return message;
}

void CopyTo( CComBSTR p_src, char **p_ptrResult)
{
	if( !p_ptrResult) return;

	*p_ptrResult = 0;

	unsigned int len = p_src.Length();
	if( !len) return;

	char *buff = new char[ len + 1]; 

	CopyTo( p_src, len, buff, len);
	buff[ len] = '\0';

	*p_ptrResult = buff;
}

void CopyTo(const OLECHAR *p, int olelen, char *s, int charlen)
{
	ASSERT( olelen >= -1 && charlen >= 0 );
	ASSERT( charlen == 0 || p != NULL );

	if( charlen <= 0 )
		return;

	UINT acp = GetACP();

	int len = WideCharToMultiByte(acp, 0, p, olelen, 
		s, charlen, NULL, NULL);

	// zero if failed
	ASSERT( len > 0 );

	ASSERT( len == charlen );
}		

void CopyTo(const char *p, int len, BSTR *b)
{
	ASSERT( len >= 0 );
	ASSERT( b != NULL );

	if(*b)
	{
		SysFreeString(*b);
		*b = NULL;
	}

	if( len <= 0 )
		return;

	UINT acp = GetACP();
	int blen = MultiByteToWideChar(acp, 0, p, len, NULL, 0);

	if( blen <= 0 )
		HR_THROW(-1);

	*b = SysAllocStringLen(NULL, blen);
	if( *b == NULL )
		HR_THROW(-1);

	int tlen = MultiByteToWideChar(acp, 0, p, len, *b, blen);

	if( tlen <= 0 )
		HR_THROW(-1);
	
	ASSERT( tlen == blen );

	(*b)[blen] = '\0';
}


--- NEW FILE: PropMerger.sln ---
Microsoft Visual Studio Solution File, Format Version 8.00
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "PropMerger", "PropMerger.vcproj", "{32BFBADC-C521-4B8F-9653-618F4F7D9CF7}"
	ProjectSection(ProjectDependencies) = postProject
	EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "IDLComp", "IDLComp\IDLComp.vcproj", "{0ED4C2EA-5EA6-4BAC-A152-489BC3420043}"
	ProjectSection(ProjectDependencies) = postProject
	EndProjectSection
EndProject
Global
	GlobalSection(SolutionConfiguration) = preSolution
		Debug = Debug
		Release = Release
	EndGlobalSection
	GlobalSection(ProjectConfiguration) = postSolution
		{32BFBADC-C521-4B8F-9653-618F4F7D9CF7}.Debug.ActiveCfg = Debug|Win32
		{32BFBADC-C521-4B8F-9653-618F4F7D9CF7}.Debug.Build.0 = Debug|Win32
		{32BFBADC-C521-4B8F-9653-618F4F7D9CF7}.Release.ActiveCfg = Release|Win32
		{32BFBADC-C521-4B8F-9653-618F4F7D9CF7}.Release.Build.0 = Release|Win32
		{0ED4C2EA-5EA6-4BAC-A152-489BC3420043}.Debug.ActiveCfg = Debug|Win32
		{0ED4C2EA-5EA6-4BAC-A152-489BC3420043}.Debug.Build.0 = Debug|Win32
		{0ED4C2EA-5EA6-4BAC-A152-489BC3420043}.Release.ActiveCfg = Release|Win32
		{0ED4C2EA-5EA6-4BAC-A152-489BC3420043}.Release.Build.0 = Release|Win32
	EndGlobalSection
	GlobalSection(ExtensibilityGlobals) = postSolution
	EndGlobalSection
	GlobalSection(ExtensibilityAddIns) = postSolution
	EndGlobalSection
EndGlobal

--- NEW FILE: Traverser.h ---
#pragma once

#include "IDLComp\GMEIDLs_h.h"
#include <string>
#include <vector>

class Traverser
{
public:
	Traverser(void);
	Traverser( const std::string& p_path, const std::string& p_dirRoot, bool p_hier1d, bool p_hier2d);
	~Traverser(void);
	void exec();

	long InvokeEx( IMgaProject *project,  IMgaFCO *currentobj,  IMgaFCOs *selectedobjs,  long param);

protected:
	const bool                m_hier1d;
	const bool                m_hier2d;
	const std::string         m_path;
	const std::string         m_inDirPath;
	std::string               m_pathPrefix; // calculated

	std::vector< std::string> m_ignoreList;

	CComPtr<IMgaParser> m_xmlParser;
	CComPtr<IGMEOLEApp> m_theGme;

	//CComPtr<IMgaProject>     m_theProj;
	//CComPtr<IMgaTerritory>   m_theTerr;

	CComPtr<IGMEOLEApp> getGME( IMgaProject *p_project);

	void stdConsoleMsg( const char *    p_msg, int p_type, bool p_prefixed);
	void stdConsoleMsg( const CComBSTR& p_msg, int p_type, bool p_prefixed);

	void consoleMsg( const CComBSTR& p_msg, int p_type, bool p_prefixed = true);
	void consoleMsg( const char *    p_msg, int p_type, bool p_prefixed = true);

	void         setPathPrefix();
	CComBSTR     makeFileName( const CComBSTR& p_gd, bool *p_fileExists);
	bool         fileFound( const char *      p_file);
	bool         fileFound( const CComBSTR&   p_file);

	void         createMgaDtd();
	bool         foundAsIgnoreable( const CComBSTR& p_objId);
	void         loadIgnorables();


	void traverse( IMgaProject *p_project, IMgaTerritory *p_terr, IMgaFolder *p_curr);
	void traverse( IMgaProject *p_project, IMgaTerritory *p_terr, IMgaModel  *p_curr);

	template <typename T> // for IMgaFCO* and IMgaFolder*
	void dealWith( IMgaProject *p_project, IMgaTerritory *p_terr, T      *p_item);
};



More information about the GME-commit mailing list