Simple std::string to Vector3df

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
wildrj
Posts: 301
Joined: Thu Mar 23, 2006 12:49 am
Location: Texas/ Cyberspace
Contact:

Simple std::string to Vector3df

Post by wildrj »

Alright not much to say title says it all. Basically this function takes a string formatted like so "0.0,0.0,0.0" and turns it into a irrlicht vector3df this code is somewhat ugly but comments should clear it up. Did i mention i give you the doxygen info to :D Hope it helps someone.

Code: Select all

		//! GetVector3dfFromStr
		/*!
			This is a internal function that takes a string and converts it
			into a vector3df.
			\param val is the string to turn into a vector3df
			\warning this function only takes strings formated as follows
			value,value,value it will not take anything but that format.
		*/
		irr::core::vector3df GetVector3dfFromStr(std::string val)
		{
			irr::core::vector3df vec;
			std::string temp;
			std::istringstream temp2;
			std::istringstream temp3;
			std::istringstream temp4;
			size_t found;
			size_t found2;
			
			//First we find the first instance of ',' in the given string
			found = val.find(",");

			//Then we cut the given string from the beginning up to the first instance of ','
			temp = val.substr(0,found);

			//Then we put the newly cut string into a istream
			temp2.str(temp);

			//Then we convert the istream to a float and place it in our vector as X
			temp2 >> vec.X;
			
			//Now we cut from the first instance of ',' on down
			temp = val.substr(found+1);

			//Now we find the first instance of ',' in our newly cut string
			found2 = temp.find(",");
			//Now we substring up to the first instance of ','
			temp = temp.substr(0,found2);

			//Then we turn the cut string into a istream 
			temp3.str(temp);

			//Then we covert it into a float and place it in our vector as Y
			temp3 >> vec.Y;

			//Now we find the second instance of ',' in our val string
			found2 = val.find(",",found+1);
			//Then we cut the string from the last instnace till the
			//end of the string
			temp = val.substr(found2 + 1);
			
			//Then we turn the cut string into a istream
			temp4.str(temp);

			//Then we convert it into a float and place it in our vector as Z
			temp4 >> vec.Z;

			//Finally we return the cut vector.
			return vec;
		
		}

//Example usage:
//std::string test = "2.0,4.55550,-32432.00";
//irr::core::vector3df ourvec;
//ourvec = GetVector3dfFromStr(test);
//Thats it.

http://wild.deathtouchstudios.com << My gamedev blog
<Programming is a way of life>
If at first you don't succeed press the delete key till you do :)
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Your code assumes that the string is properly formatted and has no way to indicate that it isn't. I see that you document this, but that doesn't make your code very usable. It is also going to be pretty inefficient. What about something like this...

Code: Select all

#include <stdio.h>

// return 0 on success, 1 on failure to read 3 components
int getVectorFromStr(const char* s, core::vector3df& v)
{
  return 3 != sscanf ("%f, %f, %f", &v.X, &v.Y, &v.Z);
}
If you are going to parse the string using iostreams, you should be able to simplify you code greatly to something like this (note: untested)...

Code: Select all

#include <iostream>

int getVectorFromStr(const char* s, core::vector3df& v)
{
  std::istringstream ss(s);
  char comma;

  ss >> v.X;
  if (! (ss >> comma) || comma != ',')
    return 1;
  ss >> v.Y;
  if (! (ss >> comma) || comma != ',')
    return 1;
  ss >> v.Z;

  return ss.good() ? 0 : 1;
}
Travis
Post Reply