www.pudn.com > TidyWin32-src.zip > TidyObject.cpp


// TidyObject.cpp : Implementation of CTidyObject 
 
#include "stdafx.h" 
#include "TidyCOM.h" 
#include "TidyObject.h" 
 
#include  
#include  
 
//------------------------------------------------------------------- 
// CTidyObject 
 
HRESULT CTidyObject::FinalConstruct() 
{ 
	HRESULT hr = CComObject::CreateInstance(&m_pOptions); 
	if (SUCCEEDED(hr)) { 
		m_pOptions->AddRef(); 
		m_pOptions->SetPtr(reinterpret_cast(&m_TidyProxy)); 
	} 
	m_TidyProxy.ResetConfig(); 
 
	return hr; 
} 
 
void CTidyObject::FinalRelease() 
{ 
	m_pOptions->Release(); 
} 
 
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// ISupportsErrorInfo 
STDMETHODIMP CTidyObject::InterfaceSupportsErrorInfo(REFIID riid) 
{ 
	static const IID* arr[] =  
	{ 
		&IID_ITidyObject 
	}; 
	for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++) 
	{ 
		if (InlineIsEqualGUID(*arr[i],riid)) 
			return S_OK; 
	} 
	return S_FALSE; 
} 
 
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// ITidyObject 
 
STDMETHODIMP CTidyObject::TidyToMem(BSTR sourceFile, BSTR *result) 
{ 
	USES_CONVERSION; 
	m_TidyProxy.Reset(); 
	try { 
		m_TidyProxy.DoTidy(OLE2T(sourceFile)); 
	} 
	catch (...) { 
		return Error("HTML Tidy exception"); 
	} 
	ParseTidyMsgs(); 
	*result = T2BSTR(m_TidyProxy.GetTidyOutput().c_str()); 
 
	return S_OK; 
} 
 
STDMETHODIMP CTidyObject::TidyToFile(BSTR sourceFile, BSTR destFile) 
{ 
	USES_CONVERSION; 
	m_TidyProxy.Reset(); 
	try { 
		m_TidyProxy.DoTidy(OLE2T(sourceFile), OLE2T(destFile)); 
	} 
	catch (...) { 
		return Error("HTML Tidy exception"); 
	} 
	ParseTidyMsgs(); 
 
	return S_OK; 
} 
 
STDMETHODIMP CTidyObject::TidyMemToMem(BSTR sourceStr, BSTR* result) 
{ 
	int pid = getpid(); 
	char tempPath[MAX_PATH]; 
	GetTempPath(MAX_PATH, tempPath); 
	char tempFileName[MAX_PATH]; 
	GetTempFileName(tempPath, "tdy", pid, tempFileName); 
 
	FILE* fout = fopen(tempFileName, "wb"); 
	if (fout) { 
		unsigned strLen = SysStringLen(sourceStr); 
		unsigned short* p = sourceStr; 
		unsigned charsWritten = 0; 
		char buffer[1024]; 
		while (charsWritten < strLen) { 
			unsigned charsToWrite = min(1024, strLen - charsWritten); 
			char* q = buffer; 
			for (int i = 0; i < charsToWrite; ++i) 
				*q++ = *p++; 
			if (fwrite(buffer, sizeof(char), charsToWrite, fout) != charsToWrite) { 
				unlink(tempFileName); 
				return Error("Problem writing to temporary file"); 
			} 
			charsWritten += charsToWrite; 
		} 
		fclose(fout); 
	} 
	else return Error("Can't create temporary file"); 
 
	m_TidyProxy.Reset(); 
	try { 
		m_TidyProxy.DoTidy(tempFileName); 
	} 
	catch (...) { 
		unlink(tempFileName); 
		return Error("HTML Tidy exception"); 
	} 
	unlink(tempFileName); 
	ParseTidyMsgs(); 
	*result = T2BSTR(m_TidyProxy.GetTidyOutput().c_str()); 
 
	return S_OK; 
} 
 
STDMETHODIMP CTidyObject::get_TotalWarnings(long *pVal) 
{ 
	*pVal = m_Warnings.size(); 
 
	return S_OK; 
} 
 
STDMETHODIMP CTidyObject::get_TotalErrors(long *pVal) 
{ 
	*pVal = m_Errors.size(); 
 
	return S_OK; 
} 
 
STDMETHODIMP CTidyObject::get_Warning(long i, BSTR *pVal) 
{ 
	USES_CONVERSION; 
	if (m_Warnings.empty() || i < 0 || i > m_Warnings.size() - 1) 
		return Error("Parameter out of range"); 
	*pVal = T2BSTR(m_Warnings[i].c_str()); 
 
	return S_OK; 
} 
 
STDMETHODIMP CTidyObject::get_Error(long i, BSTR *pVal) 
{ 
	USES_CONVERSION; 
	if (m_Errors.empty() || i < 0 || i > m_Errors.size() - 1) 
		return Error("Parameter out of range"); 
	*pVal = T2BSTR(m_Errors[i].c_str()); 
 
	return S_OK; 
} 
 
STDMETHODIMP CTidyObject::get_Comments(BSTR *pVal) 
{ 
	*pVal = T2BSTR(m_Comments.c_str()); 
 
	return S_OK; 
} 
 
STDMETHODIMP CTidyObject::get_Options(ITidyOptions** pVal) 
{ 
	HRESULT hr = m_pOptions->QueryInterface(IID_ITidyOptions, 
		reinterpret_cast(pVal)); 
 
	return hr; 
} 
 
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
void CTidyObject::ParseTidyMsgs() 
{ 
	m_Warnings.erase(m_Warnings.begin(), m_Warnings.end()); 
	m_Errors.erase(m_Errors.begin(), m_Errors.end()); 
	m_Comments = ""; 
 
	string extraInfo; 
	for (list::const_iterator it = m_TidyProxy.GetTidyMsgs().begin(); 
		it != m_TidyProxy.GetTidyMsgs().end(); ++it) { 
		const string& msg = *it; 
		if (msg.find("Warning:") != string::npos) 
			m_Warnings.push_back(msg); 
		else if (msg.find("Error:") != string::npos) 
			m_Errors.push_back(msg); 
		else { 
			m_Comments += msg; 
			m_Comments += "\r\n"; 
		} 
	} 
}