| 839 | **********************************************************************/ |
| 840 | |
| 841 | void CPLErrorSetState(CPLErr eErrClass, CPLErrorNum err_no, const char *pszMsg, |
| 842 | const GUInt32 *pnErrorCounter) |
| 843 | { |
| 844 | CPLErrorContext *psCtx = CPLGetErrorContext(); |
| 845 | if (psCtx == nullptr) |
| 846 | return; |
| 847 | if (IS_PREFEFINED_ERROR_CTX(psCtx)) |
| 848 | { |
| 849 | int bMemoryError = FALSE; |
| 850 | if (eErrClass == CE_None) |
| 851 | CPLSetTLSWithFreeFuncEx( |
| 852 | CTLS_ERRORCONTEXT, |
| 853 | reinterpret_cast<void *>( |
| 854 | const_cast<CPLErrorContext *>(&sNoErrorContext)), |
| 855 | nullptr, &bMemoryError); |
| 856 | else if (eErrClass == CE_Warning) |
| 857 | CPLSetTLSWithFreeFuncEx( |
| 858 | CTLS_ERRORCONTEXT, |
| 859 | reinterpret_cast<void *>( |
| 860 | const_cast<CPLErrorContext *>(&sWarningContext)), |
| 861 | nullptr, &bMemoryError); |
| 862 | else if (eErrClass == CE_Failure) |
| 863 | CPLSetTLSWithFreeFuncEx( |
| 864 | CTLS_ERRORCONTEXT, |
| 865 | reinterpret_cast<void *>( |
| 866 | const_cast<CPLErrorContext *>(&sFailureContext)), |
| 867 | nullptr, &bMemoryError); |
| 868 | return; |
| 869 | } |
| 870 | |
| 871 | psCtx->nLastErrNo = err_no; |
| 872 | const size_t size = std::min(static_cast<size_t>(psCtx->nLastErrMsgMax - 1), |
| 873 | strlen(pszMsg)); |
| 874 | char *pszLastErrMsg = CPLErrorContextGetString(psCtx); |
| 875 | memcpy(pszLastErrMsg, pszMsg, size); |
| 876 | pszLastErrMsg[size] = '\0'; |
| 877 | psCtx->eLastErrType = eErrClass; |
| 878 | if (pnErrorCounter) |
| 879 | psCtx->nErrorCounter = *pnErrorCounter; |
| 880 | } |
| 881 | |
| 882 | /** |
| 883 | * Restore an error state, without emitting an error. |