//
//	Copyright © 2007 Nemetschek North America 
//	All Rights Reserved.
//
// Created by Vladislav Stanev


#include "StdAfx.h"

//#include "STRResLoader.h"


using namespace VWFC::Tools;

// ---------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------
CSTRResLoader::CSTRResLoader()
{
}

// ---------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------
CSTRResLoader::~CSTRResLoader()
{
}

// ---------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------
/*static*/ CSTRResLoader& CSTRResLoader::Instance()
{
	static CSTRResLoader	instance;
	return instance;
}

// ---------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------
bool CSTRResLoader::Load(short id, short index, TXString& outString, bool useOnlyLocalResources)
{
	if (&outString != &fInternalStringForLoad) {
		TLoadedTXStringMap::iterator lsi = fLoadedStrings.find(TShortPair(id, index));
		if (lsi != fLoadedStrings.end()) {
			outString = lsi->second;
			return true;
		}
	}

	TStringsMap::iterator	it	= fmapStrings.find( id );
	if ( it == fmapStrings.end() ) {
		// add this string table
		GSHandle	hResource	= ::GS_GetResource( gCBP, 'STR#', id, useOnlyLocalResources );
		if ( hResource && *hResource ) {
			Uint16		numStrings		= **(Uint16**) hResource;
			size_t		resourceSize;

			GS_GetHandleSize(gCBP, hResource, resourceSize);

			TStrResData &data = fmapStrings[id];
			data.fData.resize(resourceSize);
			data.fIndex.resize(numStrings+1);

			memcpy(&data.fData[0], *hResource, resourceSize);

			// skip items count and position on the first string
			const unsigned char *pStrings = &data.fData[2];
			const unsigned char *pResEnd = pStrings + resourceSize - 2;

			// read the strings
			if ( pStrings && numStrings ) {
				for(Uint16 i=1; i<=numStrings && pStrings < pResEnd; i++ ) {
					data.fIndex[i] = pStrings;
					pStrings += pStrings[ 0 ] + 1;
				}
			}
			
			// release resource
			::GS_ReleaseResource( gCBP, (GSHandle) hResource );
		}
	}

	// read the string
	bool	ok		= false;
	it				= fmapStrings.find( id );
	VERIFYN( kVStanev, it != fmapStrings.end() );
	if ( it != fmapStrings.end() ) {
		const TStrResData&	resData	= it->second;
		if ( index >= 1 && (size_t) index < resData.fIndex.size() ) {
			outString				= resData.fIndex[index];

			outString.AnsiToMac();

			typedef std::pair<TShortPair, TXString> MapPair;
			fLoadedStrings.insert(MapPair(TShortPair(id, index), outString));

			ok						= true;
		}
	}

	return ok;
}

// ---------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------
const TXString& CSTRResLoader::Load(short id, short index, bool useOnlyLocalResources)
{
	TLoadedTXStringMap::iterator lsi = fLoadedStrings.find(TShortPair(id, index));

	if (lsi != fLoadedStrings.end()) 
		return lsi->second;

	else if (!VERIFYN( kVStanev, this->Load( id, index, fInternalStringForLoad, useOnlyLocalResources) ))
		return fErrorReturnString;

	lsi = fLoadedStrings.find(TShortPair(id, index));
	if (lsi == fLoadedStrings.end()) 
		return fErrorReturnString;

	return fLoadedStrings[TShortPair(id, index)];
}

