expand file names that have environment variables in their path

后端 未结 8 2073
礼貌的吻别
礼貌的吻别 2020-12-01 08:06

What\'s the best way to expand

${MyPath}/filename.txt to /home/user/filename.txt

or

%MyPath%/filename.txt to c:\\Document         


        
相关标签:
8条回答
  • 2020-12-01 08:46

    As the question is tagged "wxWidgets", you can use wxExpandEnvVars() function used by wxConfig for its environment variable expansion. The function itself is unfortunately not documented but it basically does what you think it should and expands any occurrences of $VAR, $(VAR) or ${VAR} on all platforms and also of %VAR% under Windows only.

    0 讨论(0)
  • 2020-12-01 08:49

    For UNIX (or at least POSIX) systems, have a look at wordexp:

    #include <iostream>
    #include <wordexp.h>
    using namespace std;
    int main() {
      wordexp_t p;
      char** w;
      wordexp( "$HOME/bin", &p, 0 );
      w = p.we_wordv;
      for (size_t i=0; i<p.we_wordc;i++ ) cout << w[i] << endl;
      wordfree( &p );
      return 0;
    }
    

    It seems it will even do glob-like expansions (which may or may not be useful for your particular situation).

    0 讨论(0)
提交回复
热议问题