void TemplateManager::SaveRegistry()
{
	int i;
	WORD count = GetCount();
	if (count > 0)
	{
		MemFile filenameFile;
		filenameFile.Write(&count, sizeof(count));
		for (i = 0; i < GetCount(); i++)
		{
			WWhizTemplateGroup* file = GetTemplateGroup(i);
			bool isActive = file->IsActive();
			filenameFile.Write(&isActive, sizeof(bool));
			WORD len = file->GetFilename().GetLength();
			filenameFile.Write(&len, sizeof(WORD));
			filenameFile.Write((LPCTSTR)file->GetFilename(), len);
			unsigned __int64 time = file->GetTimeStamp().GetTime();
			filenameFile.Write(&time, sizeof(unsigned __int64));
		}

		LONG filenameFileSize = filenameFile.GetLength();
		BYTE* filenameFileMem = filenameFile.Detach();
		AfxGetApp()->WriteProfileBinary("Templates", "FileList2",
			filenameFileMem, filenameFileSize);
		free(filenameFileMem);
	}
	else
	{
		AfxGetApp()->WriteProfileBinary("Templates", "FileList2",
			NULL, 0);
	}

	// Write out the tree state.
	LONG treeStateSize = m_treeStateFile.GetLength();
	BYTE* treeStateMem = m_treeStateFile.Detach();
	if (treeStateSize > 0)
	{
		AfxGetApp()->WriteProfileBinary("Templates", "TreeState", treeStateMem, treeStateSize);

		// Reattach the memory.
		m_treeStateFile.SetLength(0);
		m_treeStateFile.Write(treeStateMem, treeStateSize);

		free(treeStateMem);
	}

	// Write out all parameters.
	MemFile paramFile;
	for (i = 0; i < GetCount(); i++)
	{
		WWhizTemplateGroup* file = GetTemplateGroup(i);

		for (int j = 0; j < file->GetCount(); j++)
		{
			Template* code = (Template*)file->Get(j);
			CString menuName = file->GetName() + "|" + code->GetName();

			WORD nameLen = menuName.GetLength();
			paramFile.Write(&nameLen, sizeof(WORD));
			paramFile.Write((LPCTSTR)menuName, nameLen);

			WORD dicCount = code->m_dictionary.GetCount();
			paramFile.Write(&dicCount, sizeof(WORD));
			POSITION pos = code->m_dictionary.GetStartPosition();
			while (pos)
			{
				CString entry;
				CString value;
				code->m_dictionary.GetNextAssoc(pos, entry, value);

				BYTE bLen = entry.GetLength();
				paramFile.Write(&bLen, sizeof(BYTE));
				paramFile.Write((LPCTSTR)entry, bLen);

				WORD wLen;
				wLen = value.GetLength();
				paramFile.Write(&wLen, sizeof(WORD));
				paramFile.Write((LPCTSTR)value, wLen);
			}
		}
	}

	UINT paramFileSize = paramFile.GetLength();
	BYTE* paramFileMem = paramFile.Detach();
	AfxGetApp()->WriteProfileBinary("Templates", "SavedParameters", paramFileMem, paramFileSize);
	free(paramFileMem);
}
void TemplateManager::LoadRegistry()
{
	for (size_t i = 0; i < m_files.GetCount(); i++)
	{
		delete m_files[i];
	}
	m_files.RemoveAll();
	
	UINT size;
	BYTE* mem;
	BYTE* mem2;

	// Read in the filename file.
	if (AfxGetApp()->GetProfileBinary("Templates", "FileList2", &mem, &size)  &&  size != 0)
	{
		MemFile filenameFile;
		filenameFile.Write(mem, size);
		filenameFile.Seek(0, CFile::begin);

		WORD count;
		filenameFile.Read(&count, sizeof(WORD));
		size_t i;
		for (i = 0; i < count; i++)
		{
			CString filename;
			bool isActive;
			filenameFile.Read(&isActive, sizeof(bool));
			ReadStringGT255(filenameFile, filename);
			if (filename.IsEmpty())
				break;		// Error.

			bool oldChanged = m_changed;

			int which = Add(filename);
			if (which == -1)
			{
				m_changed = true;
			}
			else
			{
				WWhizTemplateGroup* file = GetTemplateGroup(which);
				file->SetActive(isActive);

				CTime timeStamp;
				ReadTimeStamp(filenameFile, timeStamp);

				if (timeStamp != file->GetTimeStamp())
					m_changed = true;
				else
					m_changed = oldChanged;
			}
		}
	}
	else if (AfxGetApp()->GetProfileBinary("Templates", "FileList", &mem2, &size))
	{
		MemFile filenameFile;
		filenameFile.Write(mem2, size);
		filenameFile.Seek(0, CFile::begin);
		delete [] mem2;

		WORD count;
		filenameFile.Read(&count, sizeof(WORD));
		size_t i;
		for (i = 0; i < count; i++)
		{
			CString filename;
			ReadStringGT255(filenameFile, filename);
			if (filename.IsEmpty())
				break;		// Error.

			bool oldChanged = m_changed;

			int which = Add(filename);
			if (which == -1)
			{
				m_changed = true;
			}
			else
			{
				WWhizTemplateGroup* file = GetTemplateGroup(which);

				CTime timeStamp;
				ReadTimeStamp(filenameFile, timeStamp);

				if (timeStamp != file->GetTimeStamp())
					m_changed = true;
				else
					m_changed = oldChanged;
			}
		}
	}
		
	delete [] mem;
	mem = NULL;

	// Read in the tree state.
	if (AfxGetApp()->GetProfileBinary("Templates", "TreeState", &mem, &size))
	{
		m_treeStateFile.SetLength(0);
		m_treeStateFile.Write(mem, size);
		m_treeStateFile.Seek(0, CFile::begin);
	}
	delete [] mem;
	mem = NULL;
	
	// Read in the parameters.
	if (AfxGetApp()->GetProfileBinary("Templates", "SavedParameters", &mem, &size))
	{
		MemFile paramFile;
		paramFile.Write(mem, size);
		paramFile.Seek(0, CFile::begin);
		delete [] mem;
		mem = NULL;

		if (size > 0)
		{
			// The saved parameters SHOULD be in the same order.
			for (size_t i = 0; i < (size_t)GetCount(); i++)
			{
				WWhizTemplateGroup* file = GetTemplateGroup(i);

				for (int j = 0; j < file->GetCount(); j++)
				{
					WWhizTemplate* code = file->Get(j);
					CString menuName = file->GetName() + "|" + code->GetName();

					WORD nameLen;
					if (paramFile.Read(&nameLen, sizeof(WORD)) != sizeof(WORD))
						break;				// Not enough info in the file.
					CString newMenuName;
					LPTSTR buf = newMenuName.GetBufferSetLength(nameLen);
					if (paramFile.Read(buf, nameLen) != nameLen)
						break;				// Not enough info in the file.
					newMenuName.ReleaseBuffer(nameLen);

					if (menuName.CompareNoCase(newMenuName) != 0)
					{
//						AfxMessageBox("Doesn't match.\n");
						goto DoneWithParams;
					}

					WORD dicCount;
					paramFile.Read(&dicCount, sizeof(WORD));
					for (int k = 0; k < dicCount; k++)
					{
						CString entry;
						CString value;

						BYTE bLen;
						paramFile.Read(&bLen, sizeof(BYTE));
						LPTSTR buf = entry.GetBufferSetLength(bLen);
						paramFile.Read(buf, bLen);
						entry.ReleaseBuffer(bLen);

						WORD wLen;
						paramFile.Read(&wLen, sizeof(WORD));
						buf = value.GetBufferSetLength(wLen);
						paramFile.Read(buf, wLen);
						value.ReleaseBuffer(wLen);

						code->SetEntry(entry, value);
					}
				}
			}
		}
	}

	delete[] mem;
	
DoneWithParams:
	int hi = 5;
}