Skip to content

Instantly share code, notes, and snippets.

@gszauer
Created July 25, 2014 21:37
Show Gist options
  • Save gszauer/a80bce810d7a185d2662 to your computer and use it in GitHub Desktop.
Save gszauer/a80bce810d7a185d2662 to your computer and use it in GitHub Desktop.
Simple tokenizer sample
#include <iostream>
#include <assert.h>
#include <fstream>
#include <stdio.h>
#include <iostream>
#include <cstring>
int main(int argc, const char * argv[]) {
std::ifstream shaderFile("glslTest", std::ifstream::in);
assert (shaderFile);
std::string shaderString;
shaderFile.seekg(0, std::ios::end);
shaderString.reserve(shaderFile.tellg());
shaderFile.seekg(0, std::ios::beg);
shaderString.assign((std::istreambuf_iterator<char>(shaderFile)),
std::istreambuf_iterator<char>());
shaderFile.close();
char* shader = new char[shaderString.length() + 1];
memset(shader, 0, shaderString.length());
strcpy(shader, shaderString.c_str());
char* token = strtok(shader, ", \n\t");
while (token != 0) {
if (token[0] == '@') {
token = token + 1;
std::cout << "Directive: " << token;
if (strcmp(token, "blendfunc") == 0) {
token = strtok(0, ", \n\t");
std::cout << ", values: " << token << ", ";
token = strtok(0, ", \n\t");
std::cout << token;
}
else {
token = strtok(0, ", \n\t");
std::cout << ", value: " << token;
}
std::cout << "\n";
}
token = strtok(0, ", \n\t");
}
// insert code here...
delete[] shader;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment