2015-10-19 19 views
5

Esiste una funzionalità in boost::filesystem per espandere i percorsi che iniziano con un simbolo di directory home dell'utente (~ su Unix), simile alla funzione os.path.expanduser fornita in Python?Espansione percorso utente con boost :: filesystem

+0

Hai provato a utilizzare http://www.boost.org/doc/libs/1_48_0/libs/filesystem/v3/doc/reference.html#canonical? – Hamdor

+0

@Hamdor Ho provato qualcosa come 'canonical (path (" ~/test.txt "))', ma non ha funzionato. Utilizzo errato? – Daniel

+0

Dubito che ci sia. Ma vedi anche http://stackoverflow.com/questions/4891006/how-to-create-a-folder-in-the-home-directory – WhiteViking

risposta

4

No.

Ma si può attuarla facendo qualcosa di simile a questo:

namespace bfs = boost::filesystem; 
    using std; 

    bfs::path expand (bfs::path in) { 
    if (in.size() < 1) return in; 

    const char * home = getenv ("HOME"); 
    if (home == NULL) { 
     cerr << "error: HOME variable not set." << endl; 
     throw std::invalid_argument ("error: HOME environment variable not set."); 
    } 

    string s = in.c_str(); 
    if (s[0] == '~') { 
     s = string(home) + s.substr (1, s.size() - 1); 
     return bfs::path (s); 
    } else { 
     return in; 
    } 
    } 

Inoltre, uno sguardo alla similar question suggerito da @WhiteViking.

Problemi correlati