myxlib/src/myx/filesystem/whereami.hpp

122 lines
3.1 KiB
C++
Raw Normal View History

#ifndef WHEREAMIPP_HPP_
#define WHEREAMIPP_HPP_
#ifdef __cplusplus
extern "C" {
#endif
/**
* Returns the path to the current executable.
*
* Usage:
* - first call `int length = wai_getExecutablePath(NULL, 0, NULL);` to
* retrieve the length of the path
* - allocate the destination buffer with `path = (char*)malloc(length + 1);`
* - call `wai_getExecutablePath(path, length, NULL)` again to retrieve the
* path
* - add a terminal NUL character with `path[length] = '\0';`
*
* @param out destination buffer, optional
* @param capacity destination buffer capacity
* @param dirname_length optional recipient for the length of the dirname part
* of the path.
*
* @return the length of the executable path on success (without a terminal NUL
* character), otherwise `-1`
*/
int getExecutablePath( char* out, int capacity, int* dirname_length );
/**
* Returns the path to the current module.
*
* Usage:
* - first call `int length = wai_getModulePath(NULL, 0, NULL);` to retrieve
* the length of the path
* - allocate the destination buffer with `path = (char*)malloc(length + 1);`
* - call `wai_getModulePath(path, length, NULL)` again to retrieve the path
* - add a terminal NUL character with `path[length] = '\0';`
*
* @param out destination buffer, optional
* @param capacity destination buffer capacity
* @param dirname_length optional recipient for the length of the dirname part
* of the path.
*
* @return the length of the module path on success (without a terminal NUL
* character), otherwise `-1`
*/
int getModulePath( char* out, int capacity, int* dirname_length );
#ifdef __cplusplus
}
#endif
#if !defined( WHEREAMI_STRING_T )
#include <string>
typedef std::string whereami_string_t;
#else
typedef WHEREAMI_STRING_T whereami_string_t;
#endif
#if !defined( WHEREAMI_DISABLE_OSTREAM )
#include <ostream>
#endif
#if ( defined ( __cplusplus ) && ( __cplusplus > 199711L ) ) || ( defined( _MSC_FULL_VER ) && ( _MSC_FULL_VER >= 150020706 ) )
#define WHEREAMI_CXX11
#endif
#if defined( __GNUC__ )
#if !( ( __GNUC__ * 100 + __GNUC_MINOR__ ) < 408 )
#undef WHEREAMI_CXX11
#endif
#endif
namespace whereami {
class whereami_path_t
{
public:
#if defined( WHEREAMI_CXX11 )
operator whereami_string_t() &&;
operator whereami_string_t() const &;
#else
operator const whereami_string_t&() const;
#endif
whereami_string_t dirname() const;
whereami_string_t basename() const;
private:
whereami_path_t();
#if defined( WHEREAMI_CXX11 )
whereami_path_t( whereami_string_t&& path, int dirname_length ) noexcept;
#else
whereami_path_t( whereami_string_t& m_path, int m_dirnameLength );
#endif
friend whereami_path_t getExecutablePath();
friend whereami_path_t getModulePath();
#if !defined( WHEREAMI_DISABLE_OSTREAM )
friend std::ostream& operator<<( std::ostream& os, const whereami_path_t& m_path );
#endif
whereami_string_t m_path;
int m_dirnameLength;
}; // class whereami_path_t
/**
* Returns the path to the current executable.
*/
whereami_path_t getExecutablePath();
/**
* Returns the path to the current module.
*/
whereami_path_t getModulePath();
} // namespace whereami
#endif // #ifndef WHEREAMIPP_H