Skip to main content
GameDev.net gamedev.net
🔒 Locked

[c++] vector string to const char**

Started by Defeatist Jun 8, 2010 at 12:01 PM 9 replies 13.9k views
Original Post
Defeatist
Defeatist
Note that the title doesnt display < and >

I want to convert a vector to a const char** without resorting to legacy C memory or file reading.


background info:

An OpenGL function requires me to give it the source code of a shader in the following format:

void glShaderSource(
GLuint shader,
GLsizei count,
const GLchar ** string,
const GLint * length );

const GLchar ** //string
Specifies an array of pointers to strings
containing the source code to be loaded into the
shader.

I use C++ I/O

Thanks in advance!
Zipster
Zipster
Something like this would do the trick I believe:
vector<string> cppStrings = getStrings();vector<const char*> cStrings;for(int i = 0; i < cppStrings.size(); ++i){   cStrings.push_back(cppStrings.c_str());}const char** cStringArray = &cStrings[0];

You can add some error checking for empty vectors, possibly throw in an algorithm for the string assignment, etc. But this is the gist of what you need to do.
fastcall22
fastcall22
#include <string>#include <vector>#include <algorithm>#include <functional>#include <iterator>vector<string> lines;vector<const char*> starts;transform( lines.begin(), lines.end(), back_inserter( starts ), mem_fn( &string::c_str ) );const char** arr = &starts.front();


EDIT:
Ninja'd... And then the server broke down.
Zipster
Zipster
Quote:
Original post by _fastcall
EDIT:
Ninja'd... And then the server broke down.

Your solution is better anyway so I wouldn't consider it a ninja [smile]
Defeatist
Defeatist
Thanks for the help so far! There is one little thing tho, I think getline doesnt add a "\0" terminator to the string because the lines end up not being terminated and the compiler cant compile the shader.. Code so far:


void mSHADER::fromFile(GLhandleARB Handle, string const& Filename){	ifstream file(Filename.c_str());	if(!file.good()){		log_shader.log << "Can't read shader source: " << Filename << endl;		return;	}	string line;	vector<string> lines;	vector<const char*> shader;	while(getline(file,line)){		if((line.at(0) != '/')&&(!line.empty())){			lines.push_back(line);		}	}	shader.reserve( lines.size() );	transform( lines.begin(), lines.end(), back_inserter( shader ), mem_fn( &string::c_str ) );	glShaderSource(Handle, 1, &shader.front(), NULL);		file.close();}


I tried:

line + "\0"
and
line.append("\0");

but neither worked!
Defeatist
Defeatist
Hmm that wasnt it as the shader debugger is reporting strange errors: ERROR: 0:2: 'pre-mature EOF' : syntax error parse error
ERROR: compilation errors. No code generated.

(The 0:2: indicates the line number)



the shaders are just default shaders:

void main()
{
gl_Position = ftransform();
}

and:
void main(void)
{
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}


the code is the same as I posted but with the added + "\n". Screenshot shows the shader code in memory as well. The model is bright white ingame
Antheus
Antheus
Did you read the documentation for glShaderSource?

vector<string> cppStrings = getStrings();vector<const char*> cStrings;vector<GLint> lengths;for(int i = 0; i < cppStrings.size(); ++i){   cStrings.push_back(cppStrings.c_str());   lengths.push_back(cppStrings.length());}glShaderSource(  ??,  cppStrings.size(),  &cStrings[0],  &lengths[0]);
This doesn't need null terminated strings.

Defeatist
Defeatist
Quote:
Original post by Antheus
Did you read the documentation for glShaderSource?

vector<string> cppStrings = getStrings();vector<const char*> cStrings;vector<GLint> lengths;for(int i = 0; i < cppStrings.size(); ++i){   cStrings.push_back(cppStrings.c_str());   lengths.push_back(cppStrings.length());}glShaderSource(  ??,  cppStrings.size(),  &cStrings[0],  &lengths[0]);
This doesn't need null terminated strings.


Yes I did but I thought it was easier to pass null terminated strings and NULL for the lengths. Anyways, its still not compiling correctly but I think this is an OpenGL error:

ERROR: 2:1: 'gl_Position' : undeclared identifier
ERROR: 2:1: 'ftransform' : no matching overloaded function found
ERROR: compilation errors. No code generated.

ERROR: 2:1: 'gl_Position' : undeclared identifier
ERROR: 2:1: 'ftransform' : no matching overloaded function found
ERROR: compilation errors. No code generated.

These are still the simple shaders I got from the opengl wiki
Any ideas before i post this in the OpenGL forums?
Antheus
Antheus
Quote:
Original post by Defeatist

Anyways, its still not compiling correctly but I think this is an OpenGL error:
It is.

Quote:
Any ideas before i post this in the OpenGL forums?
Might be a better place.

Defeatist
Defeatist
Quote:
Original post by Antheus
Quote:
Original post by Defeatist

Anyways, its still not compiling correctly but I think this is an OpenGL error:
It is.

Quote:
Any ideas before i post this in the OpenGL forums?
Might be a better place.


No I mean the shader compiler ;) It didnt compile correctly at first because of the missing lengths, now its saying some glsl internals are undeclared identifiers. Thanks for all the help anyways :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.