Thread: Cutting up an std::string

  1. #16
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    So I gotta set my std::string in my ms3d structure = to that of the character array in the file def.

    And I'm having issues doing that...
    Then refer to my first post.
    There is a difference between tedious and difficult.

  2. #17
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    So I gotta set my std::string in my ms3d structure = to that of the character array in the file def.

    And I'm having issues doing that...
    Ok, let's break your requirements down:

    1)
    So I gotta set my std::string in my ms3d structure:
    Code:
    string strType;
    2)
    = to
    Code:
    string strType;
    strType =       ;
    3)
    to that of the character array
    Code:
    char charArr[] = "hello";
    string strType;
    
    strType = charArr;
    Last edited by 7stud; 04-02-2006 at 01:36 PM.

  3. #18
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    I think I've said this already, maybe I havn't?

    Every time I do that I get an error: "Cannot convert from const char * to const std::string *

    Code:
    Materials[i].TextureFilename = new char[strlen( pMaterial->m_texture )+1];
    This sets the std::string TextureFilename

    Code:
    	struct Material
    	{
    		float Ambient[4], Diffuse[4], Specular[4], Emissive[4];
    		float Shininess;
    		GLuint Texture;
    		std::string * TextureFilename;
    	};
    
    	int NumMaterials;
    	Material *Materials;
    equal to the char array m_texture

    Code:
    struct MS3DMaterial
    {
        char m_name[32];
        float m_ambient[4];
        float m_diffuse[4];
        float m_specular[4];
        float m_emissive[4];
        float m_shininess;	// 0.0f - 128.0f
        float m_transparency;	// 0.0f - 1.0f
        byte m_mode;	// 0, 1, 2 is unused now
        char m_texture[128];
        char m_alphamap[128];
    } PACK_STRUCT;
    Now, what am I doing wrong now?

    That happens either way, whether or not I take the new char strlen part out or not...

    Bleh..
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  4. #19
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Apparently switching it from a std::string * to just a plain old std::string fixed it..

    Strange..
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  5. #20
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Strange..

    It's not strange. Do you understand what an std::string is? Do you understand what a character array is? Try and understand what each of those two things are to understand why that worked. BTW, you'll almost never need a std::string * in your code.

  6. #21
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Apparently switching it from a std::string * to just a plain old std::string fixed it..

    Strange..
    Yeah, that is weird. string type, pointer to string type, what's the diff?

    By the way, if all you did was change the type of:

    Materials[i].TextureFilename

    then your code is still completely wrong--even though it will compile. Can you see how ridiculous this code is:
    Code:
    string str;
    str = new char[5];
    
    str = "Something much longer than 5 characters. What the..?!";
    cout<<str<<endl;
    
    str="hi";  
    cout<<str<<endl;
    Also, note the memory leak.
    Last edited by 7stud; 04-02-2006 at 09:09 PM.

  7. #22
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    what in the world are you talking about?

    :0

    I just wanna switch it from using a char array to a string object, because I can do all sorts of nifty functions with string objects, such as finding the last of certain characters, erasing parts of the string.

    If the code is blatantly wrong, consult with NeHe over that matter, I'm just working with what I've got.

    Thanks though you've all been alot of help.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  8. #23
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    what in the world are you talking about?
    lol. Heck if I know.

    You've posted so much contradictory stuff in this thread that we're all flying on instruments in a heavy fog.

    However, one thing I am certain of is, if you have this statement:
    Code:
    Materials[i].TextureFilename = new char[strlen( pMaterial->m_texture )+1];
    and materials[i].TextureFilename is a string type, then you have created an instant memory leak: you both dynamically allocated memory and lost a pointer to the memory in the same line--and I didn't even know that was possible. Not to mention the fact that that statement demonstrates a complete ignorance of the string type.
    Last edited by 7stud; 04-03-2006 at 09:10 PM.

  9. #24
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968


    Well, I changed it back to a char array, and did this, this completes my origional goal, deleting everything out of the char array except for the final file name:

    Code:
    void MS3DModel::ReloadTextures()
    {
    	for ( int i = 0; i < NumMaterials; i++ )
    		if ( strlen(Materials[i].TextureFilename) > 0 )
    		{
    			std::string TextureFilename = Materials[i].TextureFilename;
    			int cutoff = TextureFilename.find_last_of('//');
    			TextureFilename = TextureFilename.substr(cutoff + 1);
    			Materials[i].Texture = LoadGLTexture( TextureFilename.c_str() );
    		}
    		else
    			Materials[i].Texture = 0;
    }
    I changed materials.texturefilename back to a char *
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  10. #25
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Just when I thought this whole thread couldn't get any stranger...

    Throughout this whole thread, you've been trying to assign something to Materials[i].TextureFilename, e.g.:
    Code:
    strcpy( Materials[i].TextureFilename, pMaterial->m_texture );
    and
    Code:
    Materials[i].TextureFilename = new char[strlen( pMaterial->m_texture )+1];
    but now you post code that apparently does what you want, and nowhere is Materials[i].TextureFilename assigned any value. What the??

  11. #26
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Uhg, you are slow, it isn't me!

    Code:
    bool MS3DModel::Load(const std::string & name)
    {
    	const char *ptr1 = 0;
    	ptr1= name.data ( );
    
    	ifstream inputFile( ptr1, ios::in | ios::binary);
    	if ( inputFile.fail())
    	{
    		MessageBox(NULL,"Model file not found.","Model Error",MB_OK);
    		return false;	// "Couldn't open the model file."
    	}
    
    	inputFile.seekg( 0, ios::end );
    	long fileSize = inputFile.tellg();
    	inputFile.seekg( 0, ios::beg );
    
    	byte *pBuffer = new byte[fileSize];
    	inputFile.read((char *)pBuffer, fileSize );
    	inputFile.close();
    
    	const byte *pPtr = pBuffer;
    	MS3DHeader *pHeader = ( MS3DHeader* )pPtr;
    	pPtr += sizeof( MS3DHeader );
    
    	if ( strncmp( pHeader->m_ID, "MS3D000000", 10 ) != 0 )
    	{
    		MessageBox(NULL,"Not a valid Milkshape3D model file.", "Model Error",MB_OK);
    		return false; // "Not a valid Milkshape3D model file."
    	}
    
    	if ( pHeader->m_version < 3 || pHeader->m_version > 7 )
    	{
    		MessageBox(NULL,"Not a valid Milkshape3D file version.","Model Error",MB_OK);
    		return false; // "Unhandled file version.  Milkshape3D Version 1.3 through 1.7 is supported." :)
    	}
    
    	int nVertices = *( word* )pPtr; 
    	NumVertices = nVertices;
    	Vertices = new Vertex[nVertices];
    	pPtr += sizeof( word );
    
    	int i;
    	for ( i = 0; i < nVertices; i++ )
    	{
    		MS3DVertex *pVertex = ( MS3DVertex* )pPtr;
    		Vertices[i].BoneID = pVertex->m_boneID;
    		memcpy( Vertices[i].Location, pVertex->m_vertex, sizeof( float )*3 );
    		pPtr += sizeof( MS3DVertex );
    	}
    
    
    	int nTriangles = *( word* )pPtr;
    	NumTriangles = nTriangles;
    	Triangles = new Triangle[nTriangles];
    	pPtr += sizeof( word );
    
    	for ( i = 0; i < nTriangles; i++ )
    	{
    		MS3DTriangle *pTriangle = ( MS3DTriangle* )pPtr;
    		int vertexIndices[3] = { pTriangle->m_vertexIndices[0], pTriangle->m_vertexIndices[1], pTriangle->m_vertexIndices[2] };
    		float t[3] = { 1.0f-pTriangle->m_t[0], 1.0f-pTriangle->m_t[1], 1.0f-pTriangle->m_t[2] };
    		memcpy( Triangles[i].VertexNormals, pTriangle->m_vertexNormals, sizeof( float )*3*3 );
    		memcpy( Triangles[i].Textures1, pTriangle->m_s, sizeof( float )*3 );
    		memcpy( Triangles[i].Textures2, t, sizeof( float )*3 );
    		memcpy( Triangles[i].VertexIndices, vertexIndices, sizeof( int )*3 );
    		pPtr += sizeof( MS3DTriangle );
    	}
    
    	int nGroups = *( word* )pPtr;
    	NumMeshes = nGroups;
    	Meshes = new Mesh[nGroups];
    	pPtr += sizeof( word );
    	for ( i = 0; i < nGroups; i++ )
    	{
    		pPtr += sizeof( byte );	// flags
    		pPtr += 32;				// name
    
    		word nTriangles = *( word* )pPtr;
    		pPtr += sizeof( word );
    		int *pTriangleIndices = new int[nTriangles];
    		for ( int j = 0; j < nTriangles; j++ )
    		{
    			pTriangleIndices[j] = *( word* )pPtr;
    			pPtr += sizeof( word );
    		}
    
    		char materialIndex = *( char* )pPtr;
    		pPtr += sizeof( char );
    	
    		Meshes[i].MaterialIndex = materialIndex;
    		Meshes[i].NumTriangles = nTriangles;
    		Meshes[i].TriangleIndices = pTriangleIndices;
    	}
    
    	int nMaterials = *( word* )pPtr;
    	NumMaterials = nMaterials;
    	Materials = new Material[nMaterials];
    	pPtr += sizeof( word );
    	for ( i = 0; i < nMaterials; i++ )
    	{
    		MS3DMaterial *pMaterial = ( MS3DMaterial* )pPtr;
    		memcpy( Materials[i].Ambient, pMaterial->m_ambient, sizeof( float )*4 );
    		memcpy( Materials[i].Diffuse, pMaterial->m_diffuse, sizeof( float )*4 );
    		memcpy( Materials[i].Specular, pMaterial->m_specular, sizeof( float )*4 );
    		memcpy( Materials[i].Emissive, pMaterial->m_emissive, sizeof( float )*4 );
    		Materials[i].Shininess = pMaterial->m_shininess;
    		Materials[i].TextureFilename = new char[strlen( pMaterial->m_texture )+1];
    		strcpy(Materials[i].TextureFilename, pMaterial->m_texture);
    		pPtr += sizeof( MS3DMaterial );
    	}
    
    	ReloadTextures();
    
    	delete[] pBuffer;
    
    	return true;
    }
    
    void MS3DModel::ReloadTextures()
    {
    	for ( int i = 0; i < NumMaterials; i++ )
    		if ( strlen(Materials[i].TextureFilename) > 0 )
    		{
    			std::string TextureFilename = Materials[i].TextureFilename;
    			int cutoff = TextureFilename.find_last_of('//');
    			TextureFilename = TextureFilename.substr(cutoff + 1);
    			Materials[i].Texture = LoadGLTexture( TextureFilename.c_str() );
    		}
    		else
    			Materials[i].Texture = 0;
    }
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  12. #27
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Code:
    Materials[i].TextureFilename = pMaterial->m_texture;
    If Materials[i].TextureFilename is a std::string, that's all you need. As I said before, I'm not sure why you thought you needed an std::string*, but as long as you make it a plain std::string, that is all you needed. You can do what feels comfortable for you, but using the string is easier than your current code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-14-2007, 03:34 PM
  2. std::string: Has my compiler gone nuts??
    By Andruu75 in forum C++ Programming
    Replies: 9
    Last Post: 09-28-2007, 04:02 AM
  3. Improving my code
    By rwmarsh in forum C++ Programming
    Replies: 14
    Last Post: 07-08-2006, 11:18 AM
  4. Debugging help
    By cuddlez.ini in forum C++ Programming
    Replies: 3
    Last Post: 10-24-2004, 07:08 PM
  5. DLL and std::string woes!
    By Magos in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2004, 12:34 PM