|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| #include <corecrt_internal.h>
|
| #include <memory.h>
|
| #include <search.h>
|
| #include <cfguard.h>
|
|
|
| #ifdef _M_CEE
|
| #define __fileDECL __clrcall
|
| #else
|
| #define __fileDECL __cdecl
|
| #endif
|
|
|
|
|
|
|
| #ifdef __USE_CONTEXT
|
| #define __COMPARE(context, p1, p2) (*compare)(context, p1, p2)
|
| #else
|
| #define __COMPARE(context, p1, p2) (*compare)(p1, p2)
|
| #endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| #ifndef _M_CEE
|
| extern "C"
|
| DECLSPEC_GUARDNOCF
|
| #endif
|
| #ifdef __USE_CONTEXT
|
| void* __fileDECL _lsearch_s(
|
| void const* const key,
|
| void* const base,
|
| unsigned int* const num,
|
| size_t const width,
|
| int (__fileDECL* compare)(void*, void const*, void const*),
|
| void* const context
|
| )
|
| #else
|
| void* __fileDECL _lsearch(
|
| const void* const key,
|
| void* const base,
|
| unsigned int* const num,
|
| unsigned int const width,
|
| int (__fileDECL* compare)(void const*, void const*)
|
| )
|
| #endif
|
| {
|
| _VALIDATE_RETURN(key != nullptr, EINVAL, nullptr);
|
| _VALIDATE_RETURN(num != nullptr, EINVAL, nullptr);
|
| _VALIDATE_RETURN(base != nullptr, EINVAL, nullptr);
|
| _VALIDATE_RETURN(width > 0, EINVAL, nullptr);
|
| _VALIDATE_RETURN(compare != nullptr, EINVAL, nullptr);
|
|
|
| _GUARD_CHECK_ICALL(compare);
|
|
|
| char* const first = static_cast<char*>(base);
|
| char* const last = first + *num * width;
|
|
|
|
|
| __crt_state_management::scoped_global_state_reset saved_state;
|
|
|
| for (char* p = first; p != last; p += width)
|
| {
|
| if (__COMPARE(context, key, p) == 0)
|
| {
|
| return p;
|
| }
|
| }
|
|
|
| memcpy(last, key, width);
|
| ++(*num);
|
| return last;
|
| }
|
|
|
| #undef __COMPARE
|
|
|