简体   繁体   中英

Why wouldn't a search from glGetString(GL_EXTENSIONS) work correctly?

I read this page: http://www.opengl.org/wiki/GlGetString

For example, if the extension GL_EXT_pixel_transform_color_table is listed, doing a simple search for GL_EXT_pixel_transform will return a positive whether or not it is defined.

How is that possible since its space separated? Why dont you just put a space after the keyword you're searching for?

For example:

char *exts = (char *)glGetString(GL_EXTENSIONS);
if(!strstr(exts, "GL_EXT_pixel_transform ")){ // notice the space!
    // not supported
}

I would like to know why this wouldnt work, because for me it does work.

You can tokenise the returned string using space as separator for more reliable search (if you don't want to use the newer API). Eg with Boost.Tokenizer :

typedef boost::tokenizer< boost::char_separator<char> > tokenizer;

boost::char_separator<char> sep(" ");
tokenizer tok(static_cast<const char*>(glGetString(GL_EXTENSIONS)), sep);

if (std::find(tok.begin(), tok.end(), "GL_EXT_pixel_transform") != tok.end()) {
    // extension found
}

What if the extension you are looking for is listed last? Then it will not be followed by a blank.

I know this is an old question but maybe someone else will find it useful. In case you don't want to use any tokenizing library/class here is a function that scans a string for an exact substring (without the mentioned problem). Also, it almost doesn't use any additional memory (string data is not copied):

bool strstrexact(const char *str, const char *substr, const char *delim, const bool isRecursiveCall = 0)
{
    static int substrLen;

    if (!isRecursiveCall)
        substrLen = strlen(substr);

    if (substrLen <= 0)
        return FALSE;

    const char *occurence = strstr(str, substr);

    if (occurence == NULL)
        return FALSE;

    occurence += substrLen;

    if (*occurence == '\0')
        return TRUE;

    const char *nextDelim;
    nextDelim = strstr(occurence, delim);

    if (nextDelim == NULL)
        return FALSE;

    if (nextDelim == occurence)
        return TRUE;

    return strstrexact(nextDelim, substr, delim, TRUE);
}

It returns TRUE if the substring was found or FALSE if it wasn't. In my case Here's how I used it:

if (strstrexact((const char*) glGetString(GL_EXTENSIONS), "WGL_ARB_pixel_format", " ")) {
    // extension is available
} else {
    // extension isn't available
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM