Example #1
0
void ZMessageLooperImpStd::PreRunMessageLoop()
	{
	ZAssertStop(1, fMutex_MessageDispatch.IsLocked());
	ZAssertStop(1, fMessageLoopNesting >= 0);
	fMutex_Structure.Acquire();
	++fMessageLoopNesting;
	ZAssertStop(1, fRunMessageLoop == false);
	fRunMessageLoop = true;
	fMutex_Structure.Release();
	}
Example #2
0
void ZPaneLocator::ReferencingSubPaneRemoved(ZSubPane* inSubPane)
	{
	ZAssertStop(2, fReferencingSubPanes);
	vector<ZSubPane*>::iterator theIter = find(fReferencingSubPanes->begin(), fReferencingSubPanes->end(), inSubPane);
	ZAssertStop(2, theIter != fReferencingSubPanes->end());
	fReferencingSubPanes->erase(theIter);
	if (fReferencingSubPanes->size() == 0)
		{
		delete fReferencingSubPanes;
		fReferencingSubPanes = nil;
		}
	}
Example #3
0
void ZPaneLocator::ReferencingPaneLocatorRemoved(ZPaneLocator* inPaneLocator)
	{
	ZAssertStop(2, fReferencingPaneLocators);
	vector<ZPaneLocator*>::iterator theIter = find(fReferencingPaneLocators->begin(), fReferencingPaneLocators->end(), inPaneLocator);
	ZAssertStop(2, theIter != fReferencingPaneLocators->end());
	fReferencingPaneLocators->erase(theIter);
	if (fReferencingPaneLocators->size() == 0)
		{
		delete fReferencingPaneLocators;
		fReferencingPaneLocators = nil;
		}
	}
Example #4
0
bool ZMessageLooperImpStd::RunNestedMessageLoop(bool* inRunFlag)
	{
	ZAssertStop(1, fMutex_MessageDispatch.IsLocked());
	ZAssertStop(1, fMessageLoopNesting > 0);
	fMutex_Structure.Acquire();
	++fMessageLoopNesting;
	fMutex_Structure.Release();

	int32 oldMutexCount = fMutex_MessageDispatch.FullRelease();
	if (!this->RunMessageLoop(inRunFlag))
		return false;
	fMutex_MessageDispatch.FullAcquire(oldMutexCount);
	return true;
	}
Example #5
0
void ZSubPane::RemoveAdorner(ZRef<ZPaneAdorner> theAdorner)
	{
	if (theAdorner)
		{
		ZAssertStop(2, fAdorners != nil);
		vector<ZRef<ZPaneAdorner> >::iterator theIter = find(fAdorners->begin(), fAdorners->end(), theAdorner);
		ZAssertStop(2, theIter != fAdorners->end());
		fAdorners->erase(theIter);
		if (fAdorners->size() == 0)
			{
			delete fAdorners;
			fAdorners = nil;
			}
		}
	}
Example #6
0
void ZUITextPane_TextEngine::HandleDrop(ZSubPane* inSubPane, ZPoint inPoint, ZDrop& inDrop)
	{
	ASSERTLOCKED();
	ZAssertStop(2, inSubPane == this);
	fTextEngine->HandleDrop(inPoint, inDrop);
	this->BecomeWindowTarget();
	}
Example #7
0
void ZDragClipManager::RegisterMIME(const string& iMIME, bool iIsString)
	{
	ZAssertStop(1, !iMIME.empty());

	if (ZCONFIG_Debug)
		{
		for (vector<MIMENative>::iterator i = fMIMENativeVector.begin(); i != fMIMENativeVector.end(); ++i)
			{
			if (i->fMIME == iMIME)
				{
				ZDebugStopf(1, ("Attempting to register a MIME we already have"));
				return;
				}
			}
		}

	CLIPFORMAT newClipboardFormat;
	if (ZUtil_Win::sUseWAPI())
		newClipboardFormat = ::RegisterClipboardFormatW(ZUnicode::sAsUTF16(iMIME).c_str());
	else
		newClipboardFormat = ::RegisterClipboardFormatA(iMIME.c_str());

	MIMENative theMIMENative;
	theMIMENative.fMIME = iMIME;
	theMIMENative.fCLIPFORMAT = newClipboardFormat;
	theMIMENative.fIsString = iIsString;

	fMIMENativeVector.push_back(theMIMENative);
	}
Example #8
0
void ZStreamR_ZLibDecode::Imp_Read(void* oDest, size_t iCount, size_t* oCountRead)
	{
	ZAssertStop(kDebug_ZLib, fState.avail_in == 0);
	fState.avail_out = iCount;
	fState.next_out = reinterpret_cast<Bytef*>(oDest);
	for (;;)
		{
		/*int result = */::inflate(&fState, Z_NO_FLUSH);
		if (fState.avail_out == 0)
			{
			break;
			}
		else if (fState.avail_in == 0)
			{
			// Top up our input buffer
			size_t countReadable = fStreamSource.CountReadable();
			if (countReadable == 0)
				countReadable = 1;

			size_t countToRead = min(countReadable, fBufferSize);

			size_t countRead;
			fStreamSource.Read(fBuffer, countToRead, &countRead);

			if (countRead == 0)
				break;

			fState.avail_in = countRead;
			fState.next_in = fBuffer;
			}
		}
	if (oCountRead)
		*oCountRead = iCount - fState.avail_out;
	}
Example #9
0
ZRef<ZUIFactory> ZUIFactory::sGet()
	{
// If sUIFactory is nil then app initialization code has not created a factory, which is now an error. Use
// ZUIUtil::sInitializeUIFactories
	ZAssertStop(2, sUIFactory);
	return sUIFactory;
	}
Example #10
0
HRESULT STDMETHODCALLTYPE ZDragClip_Win_Enum::Next(ULONG iRequestedCount, FORMATETC* oFORMATETC, ULONG* oActualCount)
	{
	ZAssertStop(1, oFORMATETC);

	size_t actualCount = 0;
	while (actualCount < iRequestedCount && fCurrentIter != fTuple.end())
		{
		CLIPFORMAT theCLIPFORMAT;
		string currentName = fTuple.NameOf(fCurrentIter);
		bool isString;
		if (ZDragClipManager::sGet()->LookupMIME(currentName, theCLIPFORMAT, isString))
			{
			oFORMATETC[actualCount].cfFormat = theCLIPFORMAT;
			oFORMATETC[actualCount].ptd = nil;
			oFORMATETC[actualCount].dwAspect = DVASPECT_CONTENT;
			oFORMATETC[actualCount].lindex = -1;
			oFORMATETC[actualCount].tymed = TYMED_HGLOBAL;
			++actualCount;
			}
		++fCurrentIter;
		}
	if (oActualCount)
		*oActualCount = actualCount;
	if (actualCount != iRequestedCount)
		return S_FALSE;
	return S_OK;
	}
Example #11
0
void ZStreamW_ZLibEncode::pFlush()
	{
	ZAssertStop(kDebug_ZLib, fState.avail_in == 0);

	for (;;)
		{
		/*int result = */::deflate(&fState, Z_SYNC_FLUSH);
		if (size_t countToWrite = fBufferSize - fState.avail_out)
			{
			size_t countWritten;
			fStreamSink.Write(fBuffer, countToWrite, &countWritten);
			if (countWritten < countToWrite)
				{
				fBufferSize = 0;
				ZStreamW::sThrowEndOfStream();
				}
			fState.next_out = fBuffer;
			fState.avail_out = fBufferSize;
			}
		else
			{
			break;
			}
		}
	}
Example #12
0
ZSubPane::ZSubPane(ZSuperPane* inSuperPane, ZPaneLocator* inPaneLocator)
:	ZEventHr(inSuperPane), fSuperPane(inSuperPane), fPaneLocator(inPaneLocator), fAdorners(nil)
	{
	ZAssertStop(2, fSuperPane);
	fSuperPane->SubPaneAdded(this);
	if (fPaneLocator)
		fPaneLocator->ReferencingSubPaneAdded(this);
	}
Example #13
0
void ZMessageLooperImpStd::ExitMessageLoop()
	{
	ZAssertLocked(1, fMutex_MessageDispatch);
	ZMutexLocker locker(fMutex_Structure);
	ZAssertStop(1, fRunMessageLoop);
	fRunMessageLoop = false;
	fCondition_Structure.Broadcast();
	}
Example #14
0
void ZMessageLooperImpStd::ExitNestedMessageLoop(bool* inRunFlag)
	{
	fMutex_Structure.Acquire();
	ZAssertStop(1, inRunFlag != nil);
	*inRunFlag = false;
	fCondition_Structure.Broadcast();
	fMutex_Structure.Release();
	}
Example #15
0
HBITMAP ZUtil_Win::sLoadBitmapID(bool iFromApp, int iResourceID)
	{
	ZAssertStop(kDebug_Win, (iResourceID & 0xFFFF0000) == 0);
	HINSTANCE theHINSTANCE = nullptr;
	if (iFromApp)
		theHINSTANCE = spGetAppModuleHandle();

	if (ZUtil_Win::sUseWAPI())
		return ::LoadBitmapW(theHINSTANCE, MAKEINTRESOURCEW(iResourceID));
	else
		return ::LoadBitmapA(theHINSTANCE, MAKEINTRESOURCEA(iResourceID));
	}
Example #16
0
uint64 ZStreamRPos_StreamR::Imp_GetSize()
	{
	// In order to know how much data is available we have
	// to suck it all in from fSource. Another reason to avoid
	// using GetSize if at all possible.
	ZAssertStop(2, !fCommitted);

	uint64 curPosition = fBuffer.GetPosition();
	fBuffer.SetPosition(fBuffer.GetSize());
	fBuffer.CopyAllFrom(fSource, nullptr, nullptr);
	fBuffer.SetPosition(curPosition);
	return fBuffer.GetSize();
	}
Example #17
0
PixelDescRep_Indexed::PixelDescRep_Indexed(const RGBA* iColors, size_t iCount)
:	fCheckedAlpha(false)
	{
	ZAssertStop(kDebug_PixmapNS, iCount <= 256);

	Pixval2RGBA_Indexed::fPixvals = nullptr;
	RGBA2Pixval_Indexed::fPixvals = nullptr;
	fReverseLookup = nullptr;

	fCount = iCount;
	fColors = new RGBA[iCount];
	sMemCopy(fColors, iColors, iCount * sizeof(RGBA));
	}
Example #18
0
void ZStreamRPos_StreamR::Imp_SetPosition(uint64 iPosition)
	{
	ZAssertStop(2, !fCommitted);

	uint64 curSize = fBuffer.GetSize();
	if (iPosition > curSize)
		{
		// Position the buffer at its end
		fBuffer.SetPosition(curSize);

		// And suck in enough data from fSource so its size (and position) is iPosition bytes
		fBuffer.CopyFrom(fSource, iPosition - curSize, nullptr, nullptr);
		}
	fBuffer.SetPosition(iPosition);
	}
Example #19
0
STDMETHODIMP ZDragClip_Win_DataObject::QueryGetData(FORMATETC* iRequestedFORMATETC)
	{
	ZAssertStop(1, iRequestedFORMATETC);
	if (iRequestedFORMATETC->tymed & TYMED_HGLOBAL)
		{
		bool isString;
		string theRequestedMIME;
		if (ZDragClipManager::sGet()->LookupCLIPFORMAT(iRequestedFORMATETC->cfFormat, theRequestedMIME, isString))
			{
			if (fTuple.Has(theRequestedMIME))
				return NOERROR;
			}
		}
	return S_FALSE;
	}
Example #20
0
ZStreamRPos_Win_MultiResource::ZStreamRPos_Win_MultiResource(
    HMODULE iHMODULE, const string& iType, const string& iName)
{
    ZAssertStop(2, iHMODULE);
    fHMODULE = iHMODULE;
    fHGLOBAL_Current = nullptr;
    fLPVOID_Current = nullptr;
    fPosition = 0;

    // Load the descriptor resource
    if (HRSRC descHRSRC = spFindResource(fHMODULE, iName, iType))
    {
        if (HGLOBAL descHGLOBAL = ::LoadResource(fHMODULE, descHRSRC))
        {
            try
            {
                LPVOID descAddress = ::LockResource(descHGLOBAL);
                ZStreamRPos_Memory theSIM(descAddress, SizeofResource(fHMODULE, descHRSRC));

                size_t accumulatedSize = 0;
                size_t resourceCount = theSIM.ReadUInt16();
                string resourceType = spReadZeroTerminatedString(theSIM);
                for (size_t x = 0; x < resourceCount; ++x)
                {
                    string resourceName = spReadZeroTerminatedString(theSIM);
                    if (HRSRC currentHRSRC = spFindResource(fHMODULE, resourceName, resourceType))
                    {
                        fVector_HRSRC.push_back(currentHRSRC);
                        accumulatedSize += ::SizeofResource(fHMODULE, currentHRSRC);
                        fVector_Ends.push_back(accumulatedSize);
                    }
                    else
                    {
                        throw runtime_error(
                            "ZStreamRPos_Win_MultiResource, couldn't load resource");
                    }
                }
            }
            catch (...)
            {
                ::FreeResource(descHGLOBAL);
                throw;
            }
            ::FreeResource(descHGLOBAL);
        }
    }
    fIndex = fVector_Ends.size();
}
Example #21
0
static void spWCToMB(UINT iDestCodePage, const WCHAR* iSource, size_t iSourceCU, size_t& oSourceCU,
			CHAR* oDest, size_t iDestCU, size_t& oDestCU)
	{
	ZAssertStop(1, iSourceCU && iDestCU);
	if (iSourceCU > iDestCU)
		{
		// We've got more UTF-16 than destination bytes, so discover how much space we would need.
		for (;;)
			{
			int result = ::WideCharToMultiByte(
				iDestCodePage, 0, iSource, iSourceCU, oDest, 0, nullptr, nullptr);

			if (result <= iDestCU)
				break;
			// There is insufficient space. Scale the source count to match. This is of course
			// only approximate, but will get us in the right ballpark immediately.
			iSourceCU = size_t(iSourceCU * (double(iDestCU) / double(result)));
			}
		}

	for (;;)
		{
		if (int result = ::WideCharToMultiByte(
			iDestCodePage, 0, iSource, iSourceCU, oDest, iDestCU, nullptr, nullptr))
			{
			oSourceCU = iSourceCU;
			oDestCU = result;
			break;
			}
		else
			{
			DWORD err = ::GetLastError();
			if (err == ERROR_INSUFFICIENT_BUFFER)
				{
				// We don't have enough space, and don't know how much we need.
				// Just halve the source amount.
				iSourceCU /= 2;
				ZAssertStopf(0, iSourceCU, ("iSourceCU went to zero."));
				if (not iSourceCU)
					break;
				}
			else
				{
				ZDebugStopf(0, ("MultiByteToWideChar returned an unexpected error"));
				}
			}
		}
	}
Example #22
0
// From IDataObject
STDMETHODIMP ZDragClip_Win_DataObject::GetData(FORMATETC* iRequestedFORMATETC, STGMEDIUM* oSTGMEDIUM)
	{
	ZAssertStop(1, iRequestedFORMATETC && oSTGMEDIUM);
	if (iRequestedFORMATETC->tymed & TYMED_HGLOBAL)
		{
		bool isString;
		string theRequestedMIME;
		if (ZDragClipManager::sGet()->LookupCLIPFORMAT(iRequestedFORMATETC->cfFormat, theRequestedMIME, isString))
			{
			ZTuple::const_iterator iter = fTuple.IteratorOf(theRequestedMIME);
			if (iter != fTuple.end())
				{
				ZType propertyType = fTuple.TypeOf(iter);
				if (propertyType == eZType_Raw)
					{
					const void* theAddress;
					size_t theSize;
					fTuple.GetRawAttributes(iter, &theAddress, &theSize);
					HGLOBAL theHGLOBAL = ::GlobalAlloc(0, theSize);
					void* globalPtr = ::GlobalLock(theHGLOBAL);
					ZBlockCopy(theAddress, globalPtr, theSize);
					::GlobalUnlock(theHGLOBAL);
					oSTGMEDIUM->tymed = TYMED_HGLOBAL;
					oSTGMEDIUM->hGlobal = theHGLOBAL;
					oSTGMEDIUM->pUnkForRelease = nil;
					return NOERROR;
					}
				else if (propertyType == eZType_String)// && iRequestedFORMATETC->cfFormat == CF_TEXT)
					{
					string theString = fTuple.GetString(iter);
					size_t theSize = theString.size();
					HGLOBAL theHGLOBAL = ::GlobalAlloc(0, theSize + 1);
					void* globalPtr = ::GlobalLock(theHGLOBAL);
					if (theSize)
						ZBlockCopy(theString.data(), globalPtr, theSize);
					reinterpret_cast<char*>(globalPtr)[theSize] = 0;
					::GlobalUnlock(theHGLOBAL);
					oSTGMEDIUM->tymed = TYMED_HGLOBAL;
					oSTGMEDIUM->hGlobal = theHGLOBAL;
					oSTGMEDIUM->pUnkForRelease = nil;
					return NOERROR;
					}
				}
			}
		}
	return E_FAIL;
	}
Example #23
0
static void spMBToWC(UINT iSourceCodePage, const CHAR* iSource, size_t iSourceCU, size_t& oSourceCU,
			WCHAR* oDest, size_t iDestCU, size_t& oDestCU)
	{
	ZAssertStop(1, iSourceCU && iDestCU);

	if (iSourceCU >= 2 * iDestCU)
		{
		// We've got at least twice as many source bytes as space for destination code units.
		// The odds are higher that we'd overflow the destination buffer. So discover how
		// much space we would need.
		for (;;)
			{
			int result = ::MultiByteToWideChar(iSourceCodePage, 0, iSource, iSourceCU, oDest, 0);
			if (result <= iDestCU)
				break;
			// There is insufficient space. Scale the source count to match. This is of course
			// only approximate, but will get us in the right ballpark immediately.
			iSourceCU = size_t(iSourceCU * (size_t(double(iDestCU) / double(result))));
			}
		}

	for (;;)
		{
		if (int result = ::MultiByteToWideChar(
			iSourceCodePage, 0, iSource, iSourceCU, oDest, iDestCU))
			{
			oSourceCU = iSourceCU;
			oDestCU = result;
			break;
			}
		else
			{
			DWORD err = ::GetLastError();
			if (err == ERROR_INSUFFICIENT_BUFFER)
				{
				// We don't have enough space, and don't know how much we need.
				// Just halve the source amount.
				iSourceCU /= 2;
				ZAssertStopf(0, iSourceCU, ("iSourceCU went to zero."));
				}
			else
				{
				ZDebugStopf(0, ("MultiByteToWideChar returned an unexpected error"));
				}
			}
		}
	}
Example #24
0
ZDragClipManager::ZDragClipManager()
	{
	ZAssertStop(1, sDragClipManager == nil);
	sDragClipManager = this;

// Get the initial MIME type mappings installed

	#if ZCONFIG(OS, MacOS7) || ZCONFIG(OS, Carbon) || ZCONFIG(OS, MacOSX)
		this->RegisterMIME("text/plain", 'TEXT', true);
		this->RegisterMIME("image/x-PICT", 'PICT', false);
	#endif

	#if ZCONFIG(OS, Win32)
		this->RegisterMIME("text/plain", CF_TEXT, true);
		this->RegisterMIME("image/x-BMP", CF_DIB, false);
	#endif
	}
Example #25
0
PixelDescRep_Indexed::PixelDescRep_Indexed(const RGBA* iColors,
	uint32* iPixvals, size_t iCount, bool iStripAlpha)
:	fCheckedAlpha(true),
	fHasAlpha(false)
	{
	ZAssertStop(kDebug_PixmapNS, iCount <= 256);

	ZAssertStopf(1, iStripAlpha, ("This constructor is only used when stripping alpha"));

	Pixval2RGBA_Indexed::fPixvals = nullptr;
	RGBA2Pixval_Indexed::fPixvals = nullptr;
	fReverseLookup = nullptr;

	vector<RGBA> vectorColors;
	vector<uint32> vectorPixvals;
	for (size_t x = 0; x < iCount; ++x)
		{
		vector<uint32>::iterator insertIter =
			lower_bound(vectorPixvals.begin(), vectorPixvals.end(), iPixvals[x]);

		insertIter = vectorPixvals.insert(insertIter, iPixvals[x]);
		vectorColors.insert(
			vectorColors.begin() + (insertIter - vectorPixvals.begin()), iColors[x]);
		}

	fCount = iCount;
	fColors = new RGBA[iCount];

	try
		{
		Pixval2RGBA_Indexed::fPixvals = new uint32[iCount];
		RGBA2Pixval_Indexed::fPixvals = Pixval2RGBA_Indexed::fPixvals;
		}
	catch (...)
		{
		delete fColors;
		throw;
		}

	sMemCopy(fColors, &vectorColors[0], iCount * sizeof(RGBA));
	sMemCopy(Pixval2RGBA_Indexed::fPixvals, &vectorPixvals[0], iCount * sizeof(uint32));

	// Set the alpha value to be completely opaque.
	for (size_t x = 0; x < iCount; ++x)
		sAlpha(fColors[x]) = 1.0;
	}
Example #26
0
ZTuple ZDragClip_Win_DataObject::sAsTuple(IDataObject* iIDataObject)
	{
	ZTuple theTuple;
	ZDragClip_Win_DataObject* theDataObject;
	if (SUCCEEDED(iIDataObject->QueryInterface(ZDragClip_Win_DataObject::sIID, (void**)&theDataObject)))
		{
		theTuple = *theDataObject->GetTuple();
		theDataObject->Release();
		}
	else
		{
		vector<FORMATETC> filteredVector;
		::sFilterNonHGLOBAL(iIDataObject, filteredVector);

		for (vector<FORMATETC>::const_iterator i = filteredVector.begin();i != filteredVector.end(); ++i)
			{
			bool isString;
			string thePropertyName;
			if (ZDragClipManager::sGet()->LookupCLIPFORMAT((*i).cfFormat, thePropertyName, isString))
				{
				ZAssertStop(1, !thePropertyName.empty());
				FORMATETC theFORMATETC = *i;
				STGMEDIUM theSTGMEDIUM;
				HRESULT theHRESULT = iIDataObject->GetData(&theFORMATETC, &theSTGMEDIUM);
				if (SUCCEEDED(theHRESULT))
					{
					if (theSTGMEDIUM.tymed & TYMED_HGLOBAL)
						{
						void* globalPtr = ::GlobalLock(theSTGMEDIUM.hGlobal);
						// Special case text, to find and ignore the zero terminator, and to store a string.
						if (theFORMATETC.cfFormat == CF_TEXT)
							theTuple.SetString(thePropertyName, string(reinterpret_cast<char*>(globalPtr)));
						else
							theTuple.SetRaw(thePropertyName, globalPtr, ::GlobalSize(theSTGMEDIUM.hGlobal));

						::GlobalUnlock(theSTGMEDIUM.hGlobal);
						}
					::ReleaseStgMedium(&theSTGMEDIUM);
					}
				}
			}
		}
	return theTuple;
	}
Example #27
0
ZMessageLooperImpStd::~ZMessageLooperImpStd()
	{
	ZAssertLocked(1, fMutex_MessageDispatch);

	if (sLooperForCurrentThread() == this)
		{
		/* If we're being called by ZMessageLooperImpStd::RunMessageLoop, potentially
		indirectly and from multiple nestings, then each caller of RunMessageLoop will
		have passed in the address of a bool, the last of which is now stored in
		fLooperExistsAddress. We set that false, and simply return from this method,
		which will return through each invoking RunMessageLoop, which will see that its
		bool is true, will set true the prior bool, and return without touching the
		this variable. In general, deleting ourselves is *not* a safe operation. ZooLib
		does so under very constrained circumstances, and you should also. However,
		sometime, somewhere, someone's going to PoseModal on some window, and then
		PoseModal on another window, and then delete the calling window before the
		calls to PoseModal have returned. And this all will handle that, although the
		rest of their code might not :) */
		ZAssertStop(1, fLooperExistsAddress);
		*fLooperExistsAddress = false;
		fLooperExistsAddress = nil;
		}
	else
		{
		// In the other case we're being deleted by some other thread, and can thus just
		// force the main message loop to exit by setting fRunMessageLoop false, broadcasting
		// fCondition_Structure and waiting for fMessageLoopNesting to hit zero.
		fMutex_Structure.Acquire();
		fRunMessageLoop = false;
		fCondition_Structure.Broadcast();

		while (fMessageLoopNesting > 0)
			{
			fMutex_Structure.Release();
			fCondition_MessageDispatch.Wait(fMutex_MessageDispatch);
			fMutex_Structure.Acquire();
			}
		fMutex_Structure.Release();
		}
	// The two branches above mean that ZMessageLooperImpStd is robust in the face
	// of destruction by calls from any source -- your own code may not be so
	// robust, so be careful how you trash message loopers.
	}
Example #28
0
void ZTextEncoder_iconv::Encode(const UTF32* iSource, size_t iSourceCU, size_t* oSourceCU,
					void* iDest, size_t iDestBytes, size_t* oDestBytes)
	{
	const char* localSource = static_cast<const char*>(static_cast<const void*>(iSource));
	size_t localSourceBytes = iSourceCU * sizeof(UTF32);

	char* localDest = static_cast<char*>(iDest);
	size_t localDestBytes = iDestBytes;

	while (localSourceBytes && localDestBytes)
		{
		if (size_t(-1) == sIconv(
			fConverter, &localSource, &localSourceBytes, &localDest, &localDestBytes))
			{
			int err = errno;
			if (err == EINVAL)
				{
				// This shouldn't happen.
				ZDebugStopf(1, ("A truncated source UTF32 sequence shouldn't be possible"));
				break;
				}
			else if (err == E2BIG)
				{
				break;
				}
			else if (err == EILSEQ)
				{
				// Illegal source sequence. Advance by one UTF32 CU and try again?
				localSource += 4;
				ZAssertStop(1, localSourceBytes >= 4);
				localSourceBytes -= 4;
				}
			else
				{
				ZDebugStopf(1, ("Unexpected error %d", err));
				}
			}
		}
	if (oSourceCU)
		*oSourceCU = reinterpret_cast<const UTF32*>(localSource) - iSource;
	if (oDestBytes)
		*oDestBytes = localDest - static_cast<char*>(iDest);
	}
Example #29
0
static void spMBToWC_CanFail(
	UINT iSourceCodePage, const CHAR* iSource, size_t iSourceCU, size_t& oSourceCU,
	WCHAR* oDest, size_t iDestCU, size_t& oDestCU)
	{
	ZAssertStop(1, iSourceCU && iDestCU);

	DWORD theFlags = MB_ERR_INVALID_CHARS;
	for (;;)
		{
		if (int result = ::MultiByteToWideChar(
			iSourceCodePage, theFlags, iSource, iSourceCU, oDest, iDestCU))
			{
			oSourceCU = iSourceCU;
			oDestCU = result;
			break;
			}
		else
			{
			DWORD err = ::GetLastError();
			if (err == ERROR_INSUFFICIENT_BUFFER || err == ERROR_NO_UNICODE_TRANSLATION)
				{
				// Either we don't have enough destination space or there's bad data in the source.
				// Halve the source amount we're going to try to decode.
				iSourceCU /= 2;
				if (not iSourceCU)
					{
					// iSourceCU went to zero.
					oSourceCU = 0;
					oDestCU = 0;
					break;
					}
				}
			else if (err == ERROR_INVALID_FLAGS)
				{
				theFlags = 0;
				}
			else
				{
				ZDebugStopf(0, ("MultiByteToWideChar returned an unexpected error"));
				}
			}
		}
	}
Example #30
0
PixelDescRep_Indexed::PixelDescRep_Indexed(const RGBA* iColors,
	size_t iCount, bool iStripAlpha)
:	fCheckedAlpha(true),
	fHasAlpha(false)
	{
	ZAssertStop(kDebug_PixmapNS, iCount <= 256);

	ZAssertStopf(1, iStripAlpha, "This constructor is only used when stripping alpha");

	Pixval2RGBA_Indexed::fPixvals = nullptr;
	RGBA2Pixval_Indexed::fPixvals = nullptr;
	fReverseLookup = nullptr;

	fCount = iCount;
	fColors = new RGBA[iCount];
	sMemCopy(fColors, iColors, iCount * sizeof(RGBA));

	// Set the alpha value to be completely opaque.
	for (size_t x = 0; x < iCount; ++x)
		sAlpha(fColors[x]) = 1.0;
	}