History

windows chugin path enhancements

Ge Wang committed 3 years ago

Commit f5a227e1 parent bb784ef2 files · +58 −11Browse files

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

Unified · Split

src/core/util_string.cpp+40−4
@@ -494,6 +494,28 @@ std::string globTildePath( const std::string & path ) · 493 unmodified lines
494// path expansion using homemade duct tape on Windows494// path expansion using homemade duct tape on Windows
495//-----------------------------------------------------------------------------495//-----------------------------------------------------------------------------
496#ifdef __PLATFORM_WIN32__496#ifdef __PLATFORM_WIN32__
497
498//-----------------------------------------------------------------------------
499// name: getUserNameWindows()
500// desc: get the user name on windows
501//-----------------------------------------------------------------------------
502std::string getUserNameWindows()
503{
504 // allocate buffer
505 DWORD buflen = 128;
506 char * buf = new char[buflen];
507 // get user name
508 BOOL retval = GetUserNameA( buf, &buflen );
509 // result to return
510 string result;
511 // check if succeeded
512 if( retval ) result = buf;
513 // clean up
514 SAFE_DELETE_ARRAY( buf );
515 // return
516 return result;
517}
518
497//-----------------------------------------------------------------------------519//-----------------------------------------------------------------------------
498// name: expandFilePathWindows()520// name: expandFilePathWindows()
499// desc: expand file path (windows edition)521// desc: expand file path (windows edition)
@@ -512,13 +534,28 @@ std::string expandFilePathWindows( const string & path ) · 12 unmodified lines
512 tokenize( thePath, tokens, "%" );534 tokenize( thePath, tokens, "%" );
513 // every other one is an environment variable535 // every other one is an environment variable
514 t_CKBOOL expand = (thePath.length() && thePath[0] == '%');536 t_CKBOOL expand = (thePath.length() && thePath[0] == '%');
515 // loop537 // loop; flip 'expand'
516 for( t_CKINT i = 0; i < tokens.size(); i++ )538 // HACK: assume path does not contain immediately consecutive env-vars
539 for( t_CKINT i = 0; i < tokens.size(); i++, expand = !expand )
517 {540 {
518 // connstruct expansion541 // connstruct expansion
519 if( !expand ) exp += tokens[i];542 if( !expand ) exp += tokens[i];
520 else543 else
521 {544 {
545 // check for special vars
546 if( tolower( tokens[i] ) == "username" )
547 {
548 // this is supposedly safer than querying the env-var, which is easier to spoof
549 string u = getUserNameWindows();
550 // if got something
551 if( u != "" )
552 {
553 // append
554 exp += u;
555 // move to next token
556 continue;
557 }
558 }
522 // get environment variable559 // get environment variable
523 char * v = getenv( tokens[i].c_str() );560 char * v = getenv( tokens[i].c_str() );
524 // if got something back561 // if got something back
@@ -533,8 +570,6 @@ std::string expandFilePathWindows( const string & path ) · 8 unmodified lines
533 break;570 break;
534 }571 }
535 }572 }
536 // flip | HACK: path does not contain immediately consecutive env-vars
537 expand = !expand;
538 }573 }
539 574
540 // if exp still empty, no change575 // if exp still empty, no change
@@ -610,6 +645,7 @@ std::string expand_filepath( const std::string & fp, t_CKBOOL ensurePathExists ) · 69 unmodified lines
610 // no expansion in Emscripten (webchuck) or Android or iOS645 // no expansion in Emscripten (webchuck) or Android or iOS
611 return fp;646 return fp;
612#elif defined(__PLATFORM_WIN32__)647#elif defined(__PLATFORM_WIN32__)
648 // 1.5.0.4 (ge) added
613 return expandFilePathWindows( fp );649 return expandFilePathWindows( fp );
614#else650#else
615 // expand ~ to full path651 // expand ~ to full path

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