Showing the changes to src/core/util_string.cpp (1 of 4 files). Show all
Unified · Split
| @@ -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 | 394 | #if !defined(__PLATFORM_WIN32__) && !defined(__EMSCRIPTEN__) && !defined(__ANDROID__) && !defined(__CHIP_MODE__) |
| 393 | 395 | |
| 394 | 396 | #include <wordexp.h> |
| @@ -398,25 +400,29 @@ done: · 3 unmodified lines | ||
| 398 | 400 | //----------------------------------------------------------------------------- |
| 399 | 401 | std::string expandTildePath( const std::string & path ) |
| 400 | 402 | { |
| 403 | + // wordexp result | |
| 401 | 404 | wordexp_t we; |
| 402 | − // default is the original path | |
| 403 | − std::string result = path; | |
| 405 | + // the result | |
| 406 | + std::string exp; | |
| 404 | 407 | |
| 405 | − // search for pathnames matching pattern | |
| 408 | + // "perform shell-style word expansions" | |
| 406 | 409 | if( wordexp(path.c_str(), &we, 0 ) == 0 ) // success |
| 407 | 410 | { |
| 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++ ) | |
| 410 | 413 | { |
| 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]; | |
| 413 | 416 | } |
| 414 | 417 | // free up |
| 415 | 418 | wordfree( &we ); |
| 416 | 419 | } |
| 417 | 420 | |
| 421 | + // if still empty for any reason, default to original | |
| 422 | + if( exp == "" ) exp = path; | |
| 423 | + | |
| 418 | 424 | // return result |
| 419 | − return result; | |
| 425 | + return exp; | |
| 420 | 426 | } |
| 421 | 427 | |
| 422 | 428 | #include <glob.h> |
| @@ -449,6 +455,7 @@ std::string globTildePath( const std::string & path ) · 26 unmodified lines | ||
| 449 | 455 | } |
| 450 | 456 | |
| 451 | 457 | // original |
| 458 | +// from http://developer.apple.com/library/mac/#qa/qa1549/_index.html | |
| 452 | 459 | //char* CreatePathByExpandingTildePath(const char* path) |
| 453 | 460 | //{ |
| 454 | 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 | +//----------------------------------------------------------------------------- | |
| 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 | + | |
| 486 | 548 | //----------------------------------------------------------------------------- |
| 487 | 549 | // name: get_full_path() |
| 488 | 550 | // desc: get full path to file |
| @@ -539,17 +601,21 @@ std::string get_full_path( const std::string & fp ) · 50 unmodified lines | ||
| 539 | 601 | // name: expand_filepath() |
| 540 | 602 | // desc: if possible return expand path (e.g., from the unix ~) |
| 541 | 603 | //----------------------------------------------------------------------------- |
| 542 | −std::string expand_filepath( std::string & fp, t_CKBOOL ensurePathExists ) | |
| 604 | +std::string expand_filepath( const std::string & fp, t_CKBOOL ensurePathExists ) | |
| 543 | 605 | { |
| 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 | |
| 546 | 608 | return fp; |
| 609 | +#elif defined(__PLATFORM_WIN32__) | |
| 610 | + return expandFilePathWindows( fp ); | |
| 547 | 611 | #else |
| 548 | 612 | // 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; } | |
| 553 | 619 | #endif |
| 554 | 620 | } |
| 555 | 621 | |