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
388388
389389
390390
391−/* from http://developer.apple.com/library/mac/#qa/qa1549/_index.html */
391+//-----------------------------------------------------------------------------
392+// path expansion using wordexp and glob on UNIX systems
393+//-----------------------------------------------------------------------------
392394 #if !defined(__PLATFORM_WIN32__) && !defined(__EMSCRIPTEN__) && !defined(__ANDROID__) && !defined(__CHIP_MODE__)
393395
394396 #include <wordexp.h>
@@ -398,25 +400,29 @@ done: · 3 unmodified lines
398400 //-----------------------------------------------------------------------------
399401 std::string expandTildePath( const std::string & path )
400402 {
403+ // wordexp result
401404 wordexp_t we;
402− // default is the original path
403− std::string result = path;
405+ // the result
406+ std::string exp;
404407
405− // search for pathnames matching pattern
408+ // "perform shell-style word expansions"
406409 if( wordexp(path.c_str(), &we, 0 ) == 0 ) // success
407410 {
408− // check results number
409− if( we.we_wordc > 0 )
411+ // loop word wordc | 1.5.0.4 (ge) updated
412+ for( t_CKINT i = 0; i < we.we_wordc; i++ )
410413 {
411− // number of matched pathnames
412− result = we.we_wordv[0];
414+ // concatenate in case there are spaces in the path
415+ exp += string(i > 0 ? " " : "") + we.we_wordv[i];
413416 }
414417 // free up
415418 wordfree( &we );
416419 }
417420
421+ // if still empty for any reason, default to original
422+ if( exp == "" ) exp = path;
423+
418424 // return result
419− return result;
425+ return exp;
420426 }
421427
422428 #include <glob.h>
@@ -449,6 +455,7 @@ std::string globTildePath( const std::string & path ) · 26 unmodified lines
449455 }
450456
451457 // original
458+// from http://developer.apple.com/library/mac/#qa/qa1549/_index.html
452459 //char* CreatePathByExpandingTildePath(const char* path)
453460 //{
454461 // glob_t globbuf;
@@ -483,6 +490,61 @@ std::string globTildePath( const std::string & path ) · 28 unmodified lines
483490
484491
485492
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+//-----------------------------------------------------------------------------
501+std::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+
486548 //-----------------------------------------------------------------------------
487549 // name: get_full_path()
488550 // desc: get full path to file
@@ -539,17 +601,21 @@ std::string get_full_path( const std::string & fp ) · 50 unmodified lines
539601 // name: expand_filepath()
540602 // desc: if possible return expand path (e.g., from the unix ~)
541603 //-----------------------------------------------------------------------------
542−std::string expand_filepath( std::string & fp, t_CKBOOL ensurePathExists )
604+std::string expand_filepath( const std::string & fp, t_CKBOOL ensurePathExists )
543605 {
544−#if defined(__PLATFORM_WIN32__) || defined(__EMSCRIPTEN__) || defined(__ANDROID__) || defined(__CHIP_MODE__)
545− // no expansion in Windows systems or Emscripten or Android or iOS
606+#if defined(__EMSCRIPTEN__) || defined(__ANDROID__) || defined(__CHIP_MODE__)
607+ // no expansion in Emscripten (webchuck) or Android or iOS
546608 return fp;
609+#elif defined(__PLATFORM_WIN32__)
610+ return expandFilePathWindows( fp );
547611 #else
548612 // expand ~ to full path
549− if( ensurePathExists )
550− return globTildePath( fp );
551− else
552− return expandTildePath( fp );
613+ string ep = expandTildePath( fp );
614+ // 1.5.0.4 (ge) always expand ~, since glob does not...
615+
616+ // if also ensure exists
617+ if( ensurePathExists ) { return globTildePath( ep ); }
618+ else { return ep; }
553619 #endif
554620 }
555621

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