CRTF_HTMLConverter::CRTF_HTMLConverter(TMode enMode)
{
	ASSERT(enMode == c_modRTF2HTML); //vice versa unsupported at the moment
	m_enMode = enMode;
	m_RTFTree = new CRTFTree;
	m_strTitle = _T("RTF2HTML Generated Document");
	ResetMetaData();
}
            void ArbitrarySizeCirclebuffer::SetupVariables()
            {
                m_alreadyInitialized = true;
                m_rawBufferPointerEnd = PointerArithmetic::Addition(m_rawBufferPointerStart, m_rawBufferSize);
                ComputeAdjustments();
                m_data = static_cast<ArbitraryStaticData*>(m_rawBufferPointerStart);

                if(m_metaDataMutex != nullptr)
                {
                    if(m_metaDataMutex->try_lock())
                    {
                        printf("This process is the owner of the metadata lock for the circlebuffer.\n");
                        ResetMetaData();
                    }
                }
                else
                {
                    ResetMetaData();
                }
            }
CRTF_HTMLConverter::~CRTF_HTMLConverter()
{
	delete m_RTFTree;
	ResetMetaData();
}
bool CRTF_HTMLConverter::ConvertRTF2HTML(BOOL bWantHeaderFooter)
{
	//Initializing
	m_strHTML = _T("");

	//Check_Valid_RTF
	m_strRTF.TrimRight(_T("\n"));
	m_strRTF.TrimRight(_T("\r"));
	CString strEndChar = m_strRTF.Right(1);
	strEndChar;
	if (!((m_strRTF.GetLength() >= 7) && (m_strRTF.Left(6) == _T("{\\rtf1"))))
	{
		//Invalid RTF file. Must start with "{RTF1" and end with "}"
		ASSERT(FALSE);
		return false;
	}

	// make sure it ends in a '}'
	int nLen = m_strRTF.GetLength();

	while (m_strRTF[nLen - 1] != _T('}'))
	{
		nLen--;
	}

	if (nLen < m_strRTF.GetLength())
	{
		m_strRTF = m_strRTF.Left(nLen);
	}

	ASSERT(m_strRTF[nLen - 1] == _T('}'));

	// remove pictures (for now)
	int nPicStart = m_strRTF.Find(_T("{\\pict"));

	while (nPicStart != -1)
	{
		// look for closing tag and remove
		int nLen = m_strRTF.GetLength();
		int nPicEnd = nPicStart + 6;

		while (nPicEnd < nLen)
		{
			if (m_strRTF[nPicEnd] == _T('}'))
			{
				break;
			}

			nPicEnd++;
		}

		if (nPicEnd > nLen)
		{
			return false;   // no end to picture
		}

		// remove picture
		if (nPicEnd == nLen)
		{
			m_strRTF = m_strRTF.Left(nPicStart);
		}
		else
		{
			m_strRTF = m_strRTF.Left(nPicStart) + m_strRTF.Mid(nPicEnd + 1);
		}

		// next picture
		nPicStart = m_strRTF.Find(_T("{\\pict"));
	}

	//Clear internal members
	ResetMetaData();

	//Build up RTF Tree. Each tree in a node coresponds to a {} section in the RTF file (code)
	//and has a designator (name), like \RTF or \FONTTBL. We start with the whole RTF file
	//(Section \RTF1)
	delete m_RTFTree;
	m_RTFTree = new CRTFTree;
	CRTFNode NodeRoot = R2H_BuildTree(m_strRTF);
	CString strRTFCode = (NodeRoot)->m_strThisCode;

	//Fill internal meta data members
	R2H_SetMetaData(NodeRoot);

	//Create html main body
	R2H_CreateHTMLElements(strRTFCode);

	//HTML Header
	R2H_GetHTMLElements(m_strHTML);

	if (bWantHeaderFooter)
	{
		R2H_GetHTMLElements(m_strHTML);
		m_strHTML = R2H_GetHTMLHeader() + m_strHTML + _T("\r\n") + R2H_GetHTMLFooter();
	}

	return true;
}