Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Fullpath or execution dir on widows/linux/mac[SOLVED]  (Read 27475 times)

0 Members and 1 Guest are viewing this topic.

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Fullpath or execution dir on widows/linux/mac
« Reply #45 on: July 30, 2013, 10:29:13 pm »
Quote
folder where the application is started
The working directory ??


To remove the last part of the string, see my previous answer.
SFML / OS X developer

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Fullpath or execution dir on widows/linux/mac
« Reply #46 on: July 30, 2013, 10:36:06 pm »
For Linux-only code, you can use dirname.
Laurent Gomila - SFML developer

wmbuRn

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
    • Email
Re: Fullpath or execution dir on widows/linux/mac
« Reply #47 on: July 31, 2013, 12:09:14 am »
Solved it with:
#ifdef __linux
#include <unistd.h>

std::string getexepath()
  {
  char result[ PATH_MAX ];
  ssize_t count = readlink( "/proc/self/exe", result, PATH_MAX );
  return std::string( result, (count > 0) ? count : 0 );
  }
std::string DirName(std::string source)
{
    source.erase(std::find(source.rbegin(), source.rend(), '/').base(), source.end());
    return source;
}
#endif
 

call it from main();
std::string;
asd = getexepath();
asd = DirName(asd);
 
So string asd now have path to executable without executable name
so sf::Texture texture have become:
sf::Texture texture;
if (!texture.loadFromFile(asd + "Data/again/cb.bmp"))
{
    return 1;
}
 

And it works. Thank you for your help guys. Will work on windows and mac version of #define and will post code here, so if somebody needs it can use it :)

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Fullpath or execution dir on widows/linux/mac
« Reply #48 on: July 31, 2013, 12:32:21 am »
For Linux-only code, you can use dirname.

Good to know! I thought it was only available as a shell command.. I should probably update the resourcePath function to use it. :-)
SFML / OS X developer