visual c++ - c++ libgen.h (mingw) missing for vc140 x64 project - helper functions -


i have these helper functions:

std::string pathname_directory(const std::string &pathname) {   char buffer[pathname.size() + 1];   memset(buffer, 0, sizeof(buffer));   std::copy(pathname.begin(), pathname.end(), buffer);   return std::string(dirname(buffer)); }  std::string pathname_sans_directory(const std::string &pathname) {   char buffer[pathname.size() + 1];   memset(buffer, 0, sizeof(buffer));   std::copy(pathname.begin(), pathname.end(), buffer);   return std::string(basename(buffer)); } 

which rely on libgen.h, mingw header.

1) why vs give error "expression must have constant value" lines containing "char buffer[pathname.size() + 1];"?

2) there pre-defined functions handle vs 2015?

libgen.h posix header, question has no particular connection mingw.

the compiler complains about:

char buffer[pathname.size() + 1]; 

because standard c++ (any standard) prohibits variable length arrays (although c99 permits them).

you avoid using vlas rewriting functions:

#include <string> #include <memory> #include <algorithm> #include <libgen.h>  std::string pathname_directory(const std::string &pathname) {     char const *cs = pathname.c_str();     std::size_t cslen = pathname.size() + 1;     std::unique_ptr<char[]> pbuf(new char[cslen]);     std::copy(cs,cs + cslen,pbuf.get());     return dirname(pbuf.get()); }  std::string pathname_sans_directory(const std::string &pathname) {     char const *cs = pathname.c_str();     std::size_t cslen = pathname.size() + 1;     std::unique_ptr<char[]> pbuf(new char[cslen]);     std::copy(cs,cs + cslen,pbuf.get());     return basename(pbuf.get()); } 

however, dirname , basename perhaps not worth rigmarole of getting (or indeed in way trying it).

especially if want of pathname_directory(pathname) of pathname not including last path-separator, , want of pathname_sans_directory(pathname) of pathname after last path-separator. because dirname , basename surprise returning "." in cases expect empty string. see dirname , basename documentation.

if that's so, course of least bother yourself:

#include <string>  std::string pathname_directory(const std::string &pathname) {     std::size_t len = pathname.find_last_of("/\\");     return len == std::string::npos ? "": pathname.substr(0,len); }  std::string pathname_sans_directory(const std::string &pathname) {     std::size_t len = pathname.find_last_of("/\\");     return len == std::string::npos ? pathname : pathname.substr(len + 1); } 

Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -