diff options
author | José Fonseca <jfonseca@vmware.com> | 2015-03-26 15:20:36 +0000 |
---|---|---|
committer | José Fonseca <jfonseca@vmware.com> | 2015-03-26 15:22:59 +0000 |
commit | 2eef9ee231a89a9aab74da01dce93584d5f482ee (patch) | |
tree | 971e20c52c721f766eb20d8b9187f4ef7b6e99fc /wrappers | |
parent | d95bb26b86b0310255e34113d494bb91d43eee60 (diff) |
gltrace: Use std::string for string values.
To avoid leaks and simplify the code.
Diffstat (limited to 'wrappers')
-rw-r--r-- | wrappers/config.cpp | 72 | ||||
-rw-r--r-- | wrappers/config.hpp | 25 |
2 files changed, 48 insertions, 49 deletions
diff --git a/wrappers/config.cpp b/wrappers/config.cpp index 1c84ee69..67ec6995 100644 --- a/wrappers/config.cpp +++ b/wrappers/config.cpp @@ -64,7 +64,7 @@ static void create_extensions_list(configuration *conf) { // count extensions - const char *ext = conf->extensions; + const char *ext = conf->extensions.c_str(); conf->numExtensions = 0; while (1) { ext = strstr(ext, "GL_"); @@ -82,7 +82,7 @@ create_extensions_list(configuration *conf) // extract individual extension names int count = 0; - const char *extStart = conf->extensions, *extEnd; + const char *extStart = conf->extensions.c_str(), *extEnd; while (1) { extStart = strstr(extStart, "GL_"); if (!extStart) @@ -153,12 +153,10 @@ getChar(FILE *f, const char **buf) // Scan 'buf' for a string value of the form: "string" (double-quoted string) // Return the string in newly malloc'd memory -static char * +static std::string stringValue(FILE *f, const char *buf) { - int resultLen = 100; - char *result = (char *) malloc(resultLen); - int outLen = 0; + std::string result; char c; // look for = @@ -167,7 +165,7 @@ stringValue(FILE *f, const char *buf) } while (c && c != '='); if (!c) - return 0; + return result; // look for opening " do { @@ -175,14 +173,14 @@ stringValue(FILE *f, const char *buf) } while (c && c != '\"'); if (!c) - return NULL; + return result; // scan characters in the string while (1) { c = getChar(f, &buf); if (!c) { std::cerr << "Error: didn't find closing \" in config file!\n"; - return NULL; + return result; } if (c == '\n') { @@ -192,17 +190,10 @@ stringValue(FILE *f, const char *buf) if (c == '\"') { // end of string - result[outLen] = 0; return result; } - else if (outLen >= resultLen) { - // grow output buffer - resultLen *= 2; - result = (char *) realloc(result, resultLen); - result[outLen++] = c; - } else { - result[outLen++] = c; + result.push_back(c); } } } @@ -283,13 +274,12 @@ parse_file(FILE *f, configuration *conf) conf->versionMinor = intValue(f, b + 17); } else if (matchKeyword(b, "GL_CONTEXT_PROFILE_MASK")) { - char *maskStr = stringValue(f, b + 24); + std::string maskStr = stringValue(f, b + 24); conf->profileMask = 0x0; - if (strstr(maskStr, "GL_CONTEXT_CORE_PROFILE_BIT")) + if (maskStr.find("GL_CONTEXT_CORE_PROFILE_BIT") != std::string::npos) conf->profileMask |= GL_CONTEXT_CORE_PROFILE_BIT; - if (strstr(maskStr, "GL_CONTEXT_COMPATIBILITY_PROFILE_BIT")) + if (maskStr.find("GL_CONTEXT_COMPATIBILITY_PROFILE_BIT") != std::string::npos) conf->profileMask |= GL_CONTEXT_COMPATIBILITY_PROFILE_BIT; - free(maskStr); } else { std::cerr << "Unexpected config variable: " << b << ".\n"; @@ -297,7 +287,7 @@ parse_file(FILE *f, configuration *conf) } } while (!feof(f)); - if (conf->version) { + if (!conf->version.empty()) { // String version was specified, compute integer major/minor versions conf->versionMajor = conf->version[0] - '0'; conf->versionMinor = conf->version[2] - '0'; @@ -306,23 +296,21 @@ parse_file(FILE *f, configuration *conf) } else if (conf->versionMajor) { // Numeric version was specified, update the string - if (conf->version) { + if (!conf->version.empty()) { // if version string was specified too, override it conf->version[0] = '0' + conf->versionMajor; conf->version[2] = '0' + conf->versionMinor; } else { // allocate new version string - conf->version = (char *) malloc(4); - assert(conf->version); - conf->version[0] = '0' + conf->versionMajor; - conf->version[1] = '.'; - conf->version[2] = '0' + conf->versionMinor; - conf->version[3] = 0; + conf->version.clear(); + conf->version.push_back('0' + conf->versionMajor); + conf->version.push_back('.'); + conf->version.push_back('0' + conf->versionMinor); } } - if (conf->extensions) { + if (!conf->extensions.empty()) { create_extensions_list(conf); } } @@ -345,20 +333,18 @@ readConfigFile(const char *filename) configuration *conf = new configuration; - memset(conf, 0, sizeof(*conf)); - parse_file(f, conf); fclose(f); if (1) { // debug - os::log("apitrace: config GL_VENDOR = %s\n", conf->vendor); - os::log("apitrace: config GL_VERSION = %s\n", conf->version); - os::log("apitrace: config GL_EXTENSIONS = %s\n", conf->extensions); + os::log("apitrace: config GL_VENDOR = %s\n", conf->vendor.c_str()); + os::log("apitrace: config GL_VERSION = %s\n", conf->version.c_str()); + os::log("apitrace: config GL_EXTENSIONS = %s\n", conf->extensions.c_str()); os::log("apitrace: config GL_NUM_EXTENSIONS = %d\n", conf->numExtensions); - os::log("apitrace: config GL_RENDERER = %s\n", conf->renderer); - os::log("apitrace: config GL_SHADING_LANGUAGE_VERSION = %s\n", conf->glslVersion); + os::log("apitrace: config GL_RENDERER = %s\n", conf->renderer.c_str()); + os::log("apitrace: config GL_SHADING_LANGUAGE_VERSION = %s\n", conf->glslVersion.c_str()); os::log("apitrace: config GL_MAX_TEXTURE_SIZE = %d\n", conf->maxTextureSize); os::log("apitrace: config GL_MAJOR_VERSION = %d\n", conf->versionMajor); os::log("apitrace: config GL_MINOR_VERSION = %d\n", conf->versionMinor); @@ -403,15 +389,15 @@ getConfigString(const configuration *config, GLenum pname) switch (pname) { case GL_VERSION: - return (const GLubyte *) config->version; + return (const GLubyte *) config->version.c_str(); case GL_VENDOR: - return (const GLubyte *) config->vendor; + return (const GLubyte *) config->vendor.c_str(); case GL_EXTENSIONS: - return (const GLubyte *) config->extensions; + return (const GLubyte *) config->extensions.c_str(); case GL_RENDERER: - return (const GLubyte *) config->renderer; + return (const GLubyte *) config->renderer.c_str(); case GL_SHADING_LANGUAGE_VERSION: - return (const GLubyte *) config->glslVersion; + return (const GLubyte *) config->glslVersion.c_str(); default: return NULL; } @@ -424,7 +410,7 @@ getConfigStringi(const configuration *config, GLenum pname, GLuint index) { if (!config || pname != GL_EXTENSIONS || - !config->extensions || + config->extensions.empty() || index >= config->numExtensions) { return NULL; } diff --git a/wrappers/config.hpp b/wrappers/config.hpp index e5c7b0cf..519462cc 100644 --- a/wrappers/config.hpp +++ b/wrappers/config.hpp @@ -26,22 +26,35 @@ #include "glimports.hpp" +#include <string> + namespace gltrace { struct configuration { - int versionMajor, versionMinor; - char *version; - const char *vendor; - const char *extensions; - const char *renderer; - const char *glslVersion; + int versionMajor; + int versionMinor; + std::string version; + std::string vendor; + std::string extensions; + std::string renderer; + std::string glslVersion; GLbitfield profileMask; int maxTextureSize; // 2D texture size int numExtensions; char **extensionsList; + + inline + configuration() : + versionMajor(0), + versionMinor(0), + profileMask(0), + maxTextureSize(0), + numExtensions(0), + extensionsList(0) + {} }; |