Skip to content

Instantly share code, notes, and snippets.

@objectx
Last active December 10, 2015 18:18
Show Gist options
  • Save objectx/4473797 to your computer and use it in GitHub Desktop.
Save objectx/4473797 to your computer and use it in GitHub Desktop.
Path handling utilities.
/*
* paths.cpp:
*
* Author(s): objectx
*/
#include "common.h"
#include "../paths.h"
std::string GetBaseName (const std::string &s) {
auto pos = s.find_last_of ('\\') ;
if (pos == std::string::npos) {
return std::string (s) ;
}
return s.substr (pos + 1) ;
}
std::string GetFullPathName (const std::string &path) {
DWORD sz = ::GetFullPathNameA (path.c_str (), 0, 0, 0) ;
auto buf (std::vector<char> (sz + 0)) ;
::GetFullPathNameA (path.c_str (), buf.size (), &buf [0], 0) ;
return std::string (&buf [0], sz - 1) ;
}
std::string GetUUIDFileName () {
UUID uu ;
UuidCreate (&uu) ;
RPC_CSTR uu_s ;
UuidToStringA (&uu, &uu_s) ;
auto result (std::string ("tmp-")) ;
result += (const char *)uu_s ;
RpcStringFreeA (&uu_s) ;
return result ;
}
std::string GetTempFileNameForOutput (const std::string &output) {
auto outname (GetFullPathName (output)) ;
auto pos = outname.find_last_of ('\\') ;
if (pos == std::string::npos) {
return GetUUIDFileName () ;
}
else {
return outname.substr (0, pos + 1) + GetUUIDFileName () ;
}
}
std::string JoinPath (const std::string &dir, const std::string &file) {
auto dir_full (GetFullPathName (dir)) ;
auto last = *(dir_full.crbegin ()) ;
if (last != '/' && last != '\\') {
dir_full.push_back ('\\') ;
}
dir_full.append (file.begin (), file.end ()) ;
return std::string (dir_full) ;
}
/*
* [END OF FILE]
*/
/*
* paths.h:
*
* Author(s): objectx
*/
#ifndef paths_h__a67fe03643de4e2596f562a7da7bc2c4
#define paths_h__a67fe03643de4e2596f562a7da7bc2c4 1
#if defined (_MSC_VER) && (1300 <= _MSC_VER)
#pragma once
#endif
#include <sys/types.h>
#include <stdint.h>
#include <string>
/**
* Extract basename of supplied path.
*
* @param s PATH
*
* @return basename part of S
*/
std::string GetBaseName (const std::string &s) ;
/**
* Gets fully qualified file name.
*
* @param path
*
* @return Fully qualified file name.
*/
std::string GetFullPathName (const std::string &path) ;
/**
* Gets UUID based file name.
*
* @return UUID file name.
*/
std::string GetUUIDFileName () ;
/**
* Gets UUID based temporal file name.
* Directory part is same as OUTPUT.
*
* @param output Output file name.
*
* @return Constructed file name.
*/
std::string GetTempFileNameForOutput (const std::string &output) ;
/**
* Constructs a path concatenating DIR and FILE.
*
* @param dir
* @param file
*
* @return A constructed path.
*/
std::string JoinPath (const std::string &dir, const std::string &file) ;
#endif /* paths_h__a67fe03643de4e2596f562a7da7bc2c4 */
/*
* [END OF FILE]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment