History

refactor expand_filepath for unix systems

Ge Wang committed 3 years ago

Commit 5d2e58a1 parent 5c6189b4 files · +86 −86Browse files

Showing the changes to src/core/util_string.cpp (1 of 4 files). Show all

Unified · Split

src/core/util_string.cpp+82−16
@@ -388,7 +388,9 @@ done: · 387 unmodified lines
388 388
389 389
390 390
391/* from http://developer.apple.com/library/mac/#qa/qa1549/_index.html */391//-----------------------------------------------------------------------------
392// path expansion using wordexp and glob on UNIX systems
393//-----------------------------------------------------------------------------
392#if !defined(__PLATFORM_WIN32__) && !defined(__EMSCRIPTEN__) && !defined(__ANDROID__) && !defined(__CHIP_MODE__)394#if !defined(__PLATFORM_WIN32__) && !defined(__EMSCRIPTEN__) && !defined(__ANDROID__) && !defined(__CHIP_MODE__)
393 395
394#include <wordexp.h>396#include <wordexp.h>
@@ -398,25 +400,29 @@ done: · 3 unmodified lines
398//-----------------------------------------------------------------------------400//-----------------------------------------------------------------------------
399std::string expandTildePath( const std::string & path )401std::string expandTildePath( const std::string & path )
400{402{
403 // wordexp result
401 wordexp_t we;404 wordexp_t we;
402 // default is the original path405 // the result
403 std::string result = path;406 std::string exp;
404 407
405 // search for pathnames matching pattern408 // "perform shell-style word expansions"
406 if( wordexp(path.c_str(), &we, 0 ) == 0 ) // success409 if( wordexp(path.c_str(), &we, 0 ) == 0 ) // success
407 {410 {
408 // check results number411 // loop word wordc | 1.5.0.4 (ge) updated
409 if( we.we_wordc > 0 )412 for( t_CKINT i = 0; i < we.we_wordc; i++ )
410 {413 {
411 // number of matched pathnames414 // concatenate in case there are spaces in the path
412 result = we.we_wordv[0];415 exp += string(i > 0 ? " " : "") + we.we_wordv[i];
413 }416 }
414 // free up417 // free up
415 wordfree( &we );418 wordfree( &we );
416 }419 }
417 420
421 // if still empty for any reason, default to original
422 if( exp == "" ) exp = path;
423
418 // return result424 // return result
419 return result;425 return exp;
420}426}
421 427
422#include <glob.h>428#include <glob.h>
@@ -449,6 +455,7 @@ std::string globTildePath( const std::string & path ) · 26 unmodified lines
449}455}
450 456
451// original457// original
458// from http://developer.apple.com/library/mac/#qa/qa1549/_index.html
452//char* CreatePathByExpandingTildePath(const char* path)459//char* CreatePathByExpandingTildePath(const char* path)
453//{460//{
454// glob_t globbuf;461// glob_t globbuf;
@@ -483,6 +490,61 @@ std::string globTildePath( const std::string & path ) · 28 unmodified lines
483 490
484 491
485 492
493//-----------------------------------------------------------------------------
494// path expansion using homemade duct tape on Windows
495//-----------------------------------------------------------------------------
496#ifdef __PLATFORM_WIN32__
497//-----------------------------------------------------------------------------
498// name: expandFilePathWindows()
499// desc: expand file path (windows edition)
500//-----------------------------------------------------------------------------
501std::string expandFilePathWindows( const string & path )
502{
503 // expansion
504 string exp;
505
506 // tokens
507 vector<string> tokens;
508 // tokenize
509 tokenize( path, tokens, "%" );
510 // every other one is an environment variable
511 // HACK: this assumes 1) path does not begin with an env-var
512 // and 2) path does not contain immediately consecutive env-vars
513 t_CKBOOL expand = FALSE;
514 // loop
515 for( t_CKINT i = 0; i < tokens.size(); i++ )
516 {
517 // connstruct expansion
518 if( !expand ) exp += tokens[i];
519 else
520 {
521 // get environment variable
522 char * v = getenv( tokens[i].c_str() );
523 if( v ) exp += v;
524 else
525 {
526 EM_log( CK_LOG_SYSTEM, "ERROR expanding %%%s%% in path...", tokens[i].c_str() );
527 // reset
528 exp = "";
529 // outta here
530 break;
531 }
532 }
533 // flip
534 expand = !expand;
535 }
536
537 // if exp still empty, no change
538 if( exp == "" ) exp = path;
539
540 // done
541 return exp;
542}
543#endif // __PLATFORM_WIN32__
544
545
546
547
486//-----------------------------------------------------------------------------548//-----------------------------------------------------------------------------
487// name: get_full_path()549// name: get_full_path()
488// desc: get full path to file550// desc: get full path to file
@@ -539,17 +601,21 @@ std::string get_full_path( const std::string & fp ) · 50 unmodified lines
539// name: expand_filepath()601// name: expand_filepath()
540// desc: if possible return expand path (e.g., from the unix ~)602// desc: if possible return expand path (e.g., from the unix ~)
541//-----------------------------------------------------------------------------603//-----------------------------------------------------------------------------
542std::string expand_filepath( std::string & fp, t_CKBOOL ensurePathExists )604std::string expand_filepath( const std::string & fp, t_CKBOOL ensurePathExists )
543{605{
544#if defined(__PLATFORM_WIN32__) || defined(__EMSCRIPTEN__) || defined(__ANDROID__) || defined(__CHIP_MODE__)606#if defined(__EMSCRIPTEN__) || defined(__ANDROID__) || defined(__CHIP_MODE__)
545 // no expansion in Windows systems or Emscripten or Android or iOS607 // no expansion in Emscripten (webchuck) or Android or iOS
546 return fp;608 return fp;
609#elif defined(__PLATFORM_WIN32__)
610 return expandFilePathWindows( fp );
547#else611#else
548 // expand ~ to full path612 // expand ~ to full path
549 if( ensurePathExists )613 string ep = expandTildePath( fp );
550 return globTildePath( fp );614 // 1.5.0.4 (ge) always expand ~, since glob does not...
551 else615
552 return expandTildePath( fp );616 // if also ensure exists
617 if( ensurePathExists ) { return globTildePath( ep ); }
618 else { return ep; }
553#endif619#endif
554}620}
555 621

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t