Last active
December 4, 2017 16:42
-
-
Save rightson/c644b9886af80b1e7b16948549239e17 to your computer and use it in GitHub Desktop.
A rough and dirty way to expand string containing path using GNU wordexp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ref: https://linux.die.net/man/3/wordexp | |
#include <iostream> | |
#include <string> | |
#include <wordexp.h> | |
using namespace std; | |
std::string expandPath(const std::string &str) { | |
wordexp_t p; | |
char** w; | |
wordexp(str.c_str(), &p, 0); | |
w = p.we_wordv; | |
std::string ret; | |
if (p.we_wordc > 0) { | |
ret = w[0]; | |
} | |
wordfree(&p); | |
return ret; | |
} | |
int main() { | |
std::string path = expandPath("~rightson/bin"); | |
cout << path << endl; | |
// output: /home/rightson/bin | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment