<commit_before>/*
 * DynamicLoadLibrary.cpp  15.08.2006 (mueller)
 *
 * Copyright (C) 2006 by Universitaet Stuttgart (VIS). Alle Rechte vorbehalten.
 */

#ifdef _WIN32
#include <windows.h>
#else /* _WIN32 */
#include <dlfcn.h>
#endif /* _WIN32 */

#include "vislib/DynamicLinkLibrary.h"
#include "vislib/IllegalParamException.h"
#include "vislib/IllegalStateException.h"
#include "vislib/Path.h"
#include "vislib/StringConverter.h"
#ifdef _WIN32
#include "vislib/SystemMessage.h"
#endif /* _WIN32 */
#include "vislib/UnsupportedOperationException.h"


/*
 * vislib::sys::DynamicLinkLibrary::DynamicLinkLibrary
 */
vislib::sys::DynamicLinkLibrary::DynamicLinkLibrary(void) : hModule(NULL),
        loadErrorMsg() {
    // intentionally empty
}


/*
 * vislib::sys::DynamicLinkLibrary::~DynamicLinkLibrary
 */
vislib::sys::DynamicLinkLibrary::~DynamicLinkLibrary(void) {
    this->Free();
}


/*
 * vislib::sys::DynamicLinkLibrary::Free
 */
void vislib::sys::DynamicLinkLibrary::Free(void) {
    if (this->hModule != NULL) {
#ifdef _WIN32
        if (::FreeLibrary(this->hModule) != TRUE) {
            throw DLLException(__FILE__, __LINE__);
        }
#else /* _WIN32 */
        int errorCode = ::dlclose(this->hModule);

        if (errorCode != 0) {
            throw DLLException(::dlerror(), __FILE__, __LINE__);
        }
#endif /* _WIN32 */
        this->hModule = NULL;
    }
}


/*
 * vislib::sys::DynamicLinkLibrary::GetProcAddress(
 */
#ifdef _WIN32
FARPROC vislib::sys::DynamicLinkLibrary::GetProcAddress(
        const CHAR *procName) const{
    return ::GetProcAddress(this->hModule, procName);
}
#else /* _WIN32 */
void *vislib::sys::DynamicLinkLibrary::GetProcAddress(
        const CHAR *procName) const {
    // TODO: Error handling using dlerror
    // Using an exception is probably not useful because of the error-code
    // incompatibility.
    return ::dlsym(this->hModule, procName);
}
#endif /* _WIN32 */


/*
 * vislib::sys::DynamicLinkLibrary::Load
 */
bool vislib::sys::DynamicLinkLibrary::Load(const char *moduleName,
        bool dontResolveReferences, bool alternateSearchPath) {
    this->loadErrorMsg.Clear();
    if (this->IsLoaded()) {
        this->loadErrorMsg = "Call to DynamicLinLibrary::Load() when"
            "a library was already loaded.";
        throw IllegalStateException(this->loadErrorMsg, __FILE__, __LINE__);
    }

#ifdef _WIN32
    DWORD flags = 0;
    UINT oldErrorMode = ::SetErrorMode(SEM_FAILCRITICALERRORS);
    if (dontResolveReferences) flags |= DONT_RESOLVE_DLL_REFERENCES;
    if (alternateSearchPath && vislib::sys::Path::IsAbsolute(moduleName)) {
        flags |= LOAD_WITH_ALTERED_SEARCH_PATH;
    }
    this->hModule = ::LoadLibraryExA(moduleName, NULL, flags);
    ::SetErrorMode(oldErrorMode);
    if (this->hModule == NULL) {
        this->loadErrorMsg = vislib::sys::SystemMessage(::GetLastError());
    }
#else /* _WIN32 */

    this->hModule = ::dlopen(moduleName, RTLD_LAZY/* | RTLD_GLOBAL*/);
    if (this->hModule == NULL) {
        this->loadErrorMsg = ::dlerror();
    }
#endif /* _WIN32 */
    return (this->hModule != NULL);
}

<commit_msg>Does not help, does not harm (I think)<commit_after>/*
 * DynamicLoadLibrary.cpp  15.08.2006 (mueller)
 *
 * Copyright (C) 2006 by Universitaet Stuttgart (VIS). Alle Rechte vorbehalten.
 */

#ifdef _WIN32
#include <windows.h>
#else /* _WIN32 */
#include <dlfcn.h>
#endif /* _WIN32 */

#include "vislib/DynamicLinkLibrary.h"
#include "vislib/IllegalParamException.h"
#include "vislib/IllegalStateException.h"
#include "vislib/Path.h"
#include "vislib/StringConverter.h"
#ifdef _WIN32
#include "vislib/SystemMessage.h"
#endif /* _WIN32 */
#include "vislib/UnsupportedOperationException.h"


/*
 * vislib::sys::DynamicLinkLibrary::DynamicLinkLibrary
 */
vislib::sys::DynamicLinkLibrary::DynamicLinkLibrary(void) : hModule(NULL),
        loadErrorMsg() {
    // intentionally empty
}


/*
 * vislib::sys::DynamicLinkLibrary::~DynamicLinkLibrary
 */
vislib::sys::DynamicLinkLibrary::~DynamicLinkLibrary(void) {
    this->Free();
}


/*
 * vislib::sys::DynamicLinkLibrary::Free
 */
void vislib::sys::DynamicLinkLibrary::Free(void) {
    if (this->hModule != NULL) {
#ifdef _WIN32
        if (::FreeLibrary(this->hModule) != TRUE) {
            throw DLLException(__FILE__, __LINE__);
        }
#else /* _WIN32 */
        int errorCode = ::dlclose(this->hModule);

        if (errorCode != 0) {
            throw DLLException(::dlerror(), __FILE__, __LINE__);
        }
#endif /* _WIN32 */
        this->hModule = NULL;
    }
}


/*
 * vislib::sys::DynamicLinkLibrary::GetProcAddress(
 */
#ifdef _WIN32
FARPROC vislib::sys::DynamicLinkLibrary::GetProcAddress(
        const CHAR *procName) const{
    return ::GetProcAddress(this->hModule, procName);
}
#else /* _WIN32 */
void *vislib::sys::DynamicLinkLibrary::GetProcAddress(
        const CHAR *procName) const {
    // TODO: Error handling using dlerror
    // Using an exception is probably not useful because of the error-code
    // incompatibility.
    return ::dlsym(this->hModule, procName);
}
#endif /* _WIN32 */


/*
 * vislib::sys::DynamicLinkLibrary::Load
 */
bool vislib::sys::DynamicLinkLibrary::Load(const char *moduleName,
        bool dontResolveReferences, bool alternateSearchPath) {
    this->loadErrorMsg.Clear();
    if (this->IsLoaded()) {
        this->loadErrorMsg = "Call to DynamicLinLibrary::Load() when"
            "a library was already loaded.";
        throw IllegalStateException(this->loadErrorMsg, __FILE__, __LINE__);
    }

#ifdef _WIN32
    DWORD flags = 0;
    UINT oldErrorMode = ::SetErrorMode(SEM_FAILCRITICALERRORS);
    if (dontResolveReferences) flags |= DONT_RESOLVE_DLL_REFERENCES;
    if (alternateSearchPath && vislib::sys::Path::IsAbsolute(moduleName)) {
        flags |= LOAD_WITH_ALTERED_SEARCH_PATH;
    }
    this->hModule = ::LoadLibraryExA(moduleName, NULL, flags);
    ::SetErrorMode(oldErrorMode);
    if (this->hModule == NULL) {
        this->loadErrorMsg = vislib::sys::SystemMessage(::GetLastError());
    }
#else /* _WIN32 */

    this->hModule = ::dlopen(moduleName, RTLD_LAZY | RTLD_GLOBAL);
    if (this->hModule == NULL) {
        this->loadErrorMsg = ::dlerror();
    }
#endif /* _WIN32 */
    return (this->hModule != NULL);
}


{'fraction_non_alphanumeric': 0.14833147054127416, 'fraction_numerical': 0.013412102826121666, 'mean_word_length': 3.8110599078341014, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5192465', 'n_tokens_mistral': 2093, 'n_tokens_neox': 2020, 'n_words': 391}