static short sGetUnusedMenuID(bool hierarchical) { if (hierarchical) { short startValue = sNextMenuHierMenuID; while (true) { short currentValue = sNextMenuHierMenuID; MenuHandle existingMenu = ::GetMenuHandle(currentValue); ++sNextMenuHierMenuID; // wrap it around if (sNextMenuHierMenuID >= 256) sNextMenuHierMenuID = 1; // If we didn't find a menu, then this ID is available if (existingMenu == nil) return currentValue; // If we end up back where we started then we can't find a free ID if (sNextMenuHierMenuID == startValue) { ZDebugStopf(1, ("sGetUnusedMenuID, couldn't find a free hierarchical ID")); break; } } } else { short startValue = sNextMenuID; while (true) { short currentValue = sNextMenuID; MenuHandle existingMenu = ::GetMenuHandle(currentValue); ++sNextMenuID; // wrap it around if (sNextMenuID >= 2000) sNextMenuID = 1000; // If we didn't find a menu, then this ID is available if (existingMenu == nil) return currentValue; // If we end up back where we started then we can't find a free ID if (sNextMenuID == startValue) { ZDebugStopf(1, ("sGetUnusedMenuID, couldn't find a free non-hierarchical ID!!")); break; } } } return 0; }
ZSuperPane* ZSubPane::GetOutermostSuperPane() { if (fSuperPane) return fSuperPane->GetOutermostSuperPane(); ZDebugStopf(1, ("ZSubPane::GetOutermostSuperPane, should have been overridden somewher")); return nil; }
size_t ZGRgnRep_HRGN::Decompose(DecomposeProc iProc, void* iRefcon) { RECT bounds; int regionType = ::GetRgnBox(fHRGN, &bounds); if (regionType == NULLREGION) return 0 ; if (regionType == SIMPLEREGION) { iProc(sRectPOD(bounds.left, bounds.top, bounds.right, bounds.bottom), iRefcon); return 1; } if (regionType == COMPLEXREGION) { size_t regionLength = ::GetRegionData(fHRGN, 0, nullptr); std::vector<char> rawStorage(regionLength); RGNDATA* regionData = reinterpret_cast<RGNDATA*>(&rawStorage[0]); ::GetRegionData(fHRGN, regionLength, regionData); RECT* rectArray = (RECT*)(regionData->Buffer); size_t callbacksMade = 0; for (size_t x = 0; x < regionData->rdh.nCount; ++x) { ++callbacksMade; if (iProc(sRectPOD(rectArray[x].left, rectArray[x].top, rectArray[x].right, rectArray[x].bottom), iRefcon)) break; } return callbacksMade; } ZDebugStopf(1, ("ZGRgnRep_HRGN::Decompose, unknown region type")); return 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); }
ZPoint ZSubPane::ToSuper(const ZPoint& inPoint) { if (fSuperPane) return inPoint + this->GetLocation() - fSuperPane->GetTranslation(); ZDebugStopf(1, ("Somebody's gotta overide ToSuper(const ZPoint& inPoint)!")); return inPoint + this->GetLocation(); }
int ZCompare::sCompare(const char* iTypeName, const void* iL, const void* iR) { if (ZQ<ZCompare*> theQ = ZUtil_STL::sQGet(sSingleton<CompareMap>(), iTypeName)) return (*theQ)->Compare(iL, iR); ZDebugStopf(0, "ZCompare::sCompare called on unsupported type '%s'", iTypeName); return iL < iR ? -1 : iL > iR ? 1 : 0; }
ZPoint ZSubPane::ToGlobal(const ZPoint& inPoint) { ZPoint newPoint = this->ToSuper(inPoint); if (fSuperPane) return fSuperPane->ToGlobal(newPoint); ZDebugStopf(1, ("Somebody's gotta overide ToGlobal(const ZPoint& inPoint)!")); return newPoint; }
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); }
bool ZTextDecoder_iconv::Decode( const void* iSource, size_t iSourceBytes, size_t* oSourceBytes, size_t* oSourceBytesSkipped, UTF32* iDest, size_t iDestCU, size_t* oDestCU) { const char* localSource = static_cast<const char*>(iSource); size_t localSourceBytes = iSourceBytes; char* localDest = reinterpret_cast<char*>(iDest); size_t localDestBytes = iDestCU * sizeof(UTF32); size_t sourceBytesSkipped = 0; bool sourceComplete = true; while (localSourceBytes && localDestBytes) { if (size_t(-1) == sIconv( fConverter, &localSource, &localSourceBytes, &localDest, &localDestBytes)) { int err = errno; if (err == EINVAL) { sourceComplete = false; break; } else if (err == E2BIG) { break; } else if (err == EILSEQ) { // Illegal source sequence. Advance by a byte and try again. ++localSource; --localSourceBytes; ++sourceBytesSkipped; } else { ZDebugStopf(1, ("Unexpected error %d", err)); } } } if (oSourceBytes) *oSourceBytes = localSource - static_cast<char*>(const_cast<void*>(iSource)); if (oSourceBytesSkipped) *oSourceBytesSkipped = sourceBytesSkipped; if (oDestCU) *oDestCU = reinterpret_cast<UTF32*>(localDest) - iDest; return sourceComplete; }
ZTxn::~ZTxn() { ZAcqMtxR locker(fMutex); if (!fTargets.empty()) { try { this->pAbort(); } catch (...) { ZDebugStopf(0, ("Exception thrown when calling pAbort")); } } }
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")); } } } }
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")); } } } }
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")); } } } }
void ZMainNS::sDaemonize(bool iForceFDClose) { #if ZCONFIG_SPI_Enabled(POSIX) switch (::fork()) { case -1: { ZDebugStopf(0,("cannot fork")); break; } case 0: { // stdin if (iForceFDClose || ::isatty(0)) { close(0); ::open("/dev/null",O_RDONLY); } // stdout if (iForceFDClose || ::isatty(1)) { close(1); ::open("/dev/null",O_WRONLY); } // stderr if (iForceFDClose || ::isatty(2)) { close(2); ::open("/dev/null",O_WRONLY); } // Previously we'd used setpgrp. Changed per Chris Teplov's suggestion. ::setsid(); break; } default: { exit(0); } } #endif // ZCONFIG_SPI_Enabled(POSIX) }
void ZDragClipManager::RegisterMIME(const string& iMIME, ScrapFlavorType iScrapFlavorType, 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; } } } MIMENative theMIMENative; theMIMENative.fMIME = iMIME; theMIMENative.fScrapFlavorType = iScrapFlavorType; theMIMENative.fIsString = iIsString; fMIMENativeVector.push_back(theMIMENative); }
bool ZMessageLooperImpStd::RunMessageLoop(bool* inRunFlag) { ZAssertStop(1, !fMutex_MessageDispatch.IsLocked()); ZMessageLooperImpStd* priorLooperForThread = reinterpret_cast<ZMessageLooperImpStd*>(ZThread::sTLSGet(sTLSKey_LooperForCurrentThread)); ZAssertStop(1, priorLooperForThread == nil || priorLooperForThread == this); ZThread::sTLSSet(sTLSKey_LooperForCurrentThread, reinterpret_cast<ZThread::TLSData_t>(this)); try { fMutex_Structure.Acquire(); ZAssertStop(1, fMessageLoopNesting > 0); bool* priorLooperExistsAddress = fLooperExistsAddress; bool looperExists = true; fLooperExistsAddress = &looperExists; while ((inRunFlag == nil || *inRunFlag) && fRunMessageLoop) { while (fMessageQueue.size() == 0 && (inRunFlag == nil || *inRunFlag) && fRunMessageLoop) { fCondition_Structure.Wait(fMutex_Structure); } if (fMessageQueue.size() > 0 && (inRunFlag == nil || *inRunFlag) && fRunMessageLoop) { fDispatchingMessage = true; fTimeLastMessageDispatched = ZTicks::sNow(); fMutex_Structure.Release(); fMutex_MessageDispatch.Acquire(); fMutex_Structure.Acquire(); if (fMessageQueue.size() > 0 && (inRunFlag == nil || *inRunFlag) && fRunMessageLoop) { ZMessage theMessage = fMessageQueue.front(); fMessageQueue.pop_front(); fMutex_Structure.Release(); try { this->DispatchMessageImp(theMessage); } catch (...) {} if (!looperExists) { if (priorLooperExistsAddress) *priorLooperExistsAddress = false; return false; } fMutex_Structure.Acquire(); fDispatchingMessage = false; } fMutex_MessageDispatch.Release(); } } fLooperExistsAddress = priorLooperExistsAddress; --fMessageLoopNesting; fMutex_Structure.Release(); } catch (...) { ZDebugStopf(0, ("Serious problem ... uncaught exception")); } fMutex_MessageDispatch.Acquire(); fCondition_MessageDispatch.Broadcast(); fMutex_MessageDispatch.Release(); if (priorLooperForThread == nil) ZThread::sTLSSet(sTLSKey_LooperForCurrentThread, nil); return true; }
int ZCompare::Compare(const void* iL, const void* iR) { ZDebugStopf(0, "ZCompare::Compare not overridden type '%s'", fTypeName); return 0; }
void ZRefCounted::sCheckAccess(ZRefCounted* iObject) { if (!iObject || ZThreadSafe_Get(iObject->fRefCount) <= 0) ZDebugStopf(0, ("ZRef accessed with nil object or invalid reference count")); }
bool ZTextDecoder_Mac::Decode( const void* iSource, size_t iSourceBytes, size_t* oSourceBytes, size_t* oSourceBytesSkipped, UTF32* iDest, size_t iDestCU, size_t* oDestCU) { // When we're working with a destination buffer that can't hold all the source material // we have ConvertFromTextToUnicode write into the local array 'offsets' the byte // offsets that correspond to each UTF-16 intermediate code unit generated. // sSourceOffsets is used to tell ConvertFromTextToUnicode which offsets are // 'significant', in our case that means all of them. We populate sSourceOffsets // the first time we need to use it, it's then available for use in all // subsequent invocations. ByteOffset offsets[kBufSize]; ByteCount countOffsets; // utf16Buffer is the target for calls to ConvertFromTextToUnicode, // we use ZUnicode::sUTF16ToUTF32 to further transcribe the generated UTF-16 // into the UTF-32 we need. UniChar utf16Buffer[kBufSize]; const uint8* localSource = static_cast<const uint8*>(iSource); UTF32* localDest = iDest; bool sourceComplete = true; size_t sourceBytesSkipped = 0; OptionBits flags = 0; if (fIsReset) fIsReset = false; else flags |= kUnicodeKeepInfoMask; while (iSourceBytes && iDestCU) { ByteCount sourceConsumed; ByteCount utf16Generated; OSStatus err; if (iDestCU < iSourceBytes && iDestCU < kBufSize) { // We have space for fewer UTF-32 code units than we have source code units, // so it's at least feasible that we'd overflow the destination. We also // have space for fewer code units than we have for intermediate UTF-16 // code units and so this iteration may be the one where we have to // bail out early. So we pass the offset array to ConvertFromTextToUnicode in // order to discover where any truncation in the source occurred. if (!sInitedSourceOffsets) { // This is thread safe, even if two threads execute this at the same time // the end result will be the same -- an array of kBufSize elements containing // values from 0 to kBufSize - 1, and sInitedSourceOffsets being true. for (size_t x = 0; x < kBufSize; ++x) sSourceOffsets[x] = x; sInitedSourceOffsets = true; } size_t countToConvert = min(iSourceBytes, kBufSize); err = ::ConvertFromTextToUnicode(fInfo, countToConvert, localSource, flags, countToConvert, sSourceOffsets, &countOffsets, offsets, kBufSize * sizeof(UniChar), &sourceConsumed, &utf16Generated, utf16Buffer); } else { err = ::ConvertFromTextToUnicode(fInfo, iSourceBytes, localSource, flags, 0, nil, nil, nil, // Offset array stuff. kBufSize * sizeof(UniChar), &sourceConsumed, &utf16Generated, utf16Buffer); } if (sourceConsumed == 0) { // ConvertFromTextToUnicode was unable to consume any source data. if (err == noErr) { // This is actually an unlikely return value. Let's flag it for now and see. ZDebugStopf(1, ("ConvertFromTextToUnicode returned noErr")); break; } else if (err == kTECPartialCharErr) { // An incomplete sequence was all that was present in the source. sourceComplete = false; break; } else if (err == kTextMalformedInputErr) { // Bad source data. Skip a byte and try again. ++localSource; --iSourceBytes; ++sourceBytesSkipped; } else if (err == kTECUsedFallbacksStatus) { // Ignore. } else if (err == kTECBufferBelowMinimumSizeErr) { // This shouldn't ever happen as we have a fair sized // intermediate buffer, and we're not decomposing source characters // into multiple Unicode characters. sourceComplete = false; break; } else { ZDebugStopf(1, ("ConvertFromTextToUnicode returned err %d", err)); } } else { ZAssertStopf(1, (utf16Generated & 1) == 0, ("utf16Generated should be even")); utf16Generated /= 2; size_t utf16Consumed; size_t utf32Generated; ZUnicode::sUTF16ToUTF32( reinterpret_cast<const UTF16*>(utf16Buffer), utf16Generated, &utf16Consumed, localDest, iDestCU, &utf32Generated); if (utf16Generated > utf16Consumed) { // We were not able to consume all the utf16 data generated by // ConvertFromTextToUnicode. So the number of source code units // consumed is *not* sourceConsumed, but some lesser number. ZAssertStop(1, iDestCU < iSourceBytes && iDestCU < kBufSize); sourceConsumed = offsets[utf16Consumed - 1]; } localSource += sourceConsumed; iSourceBytes -= sourceConsumed; localDest += utf32Generated; iDestCU -= utf32Generated; } } if (oSourceBytes) *oSourceBytes = localSource - static_cast<const uint8*>(iSource); if (oSourceBytesSkipped) *oSourceBytesSkipped = sourceBytesSkipped; if (oDestCU) *oDestCU = localDest - iDest; return true; }