Showing the changes to src/core/util_string.cpp (1 of 3 files). Show all
Unified · Split
| @@ -363,13 +363,43 @@ done: · 362 unmodified lines | ||
| 363 | 363 | } |
| 364 | 364 | |
| 365 | 365 | |
| 366 | −/* from http://developer.apple.com/library/mac/#qa/qa1549/_index.html */ | |
| 367 | 366 | |
| 368 | −#if !defined(__PLATFORM_WIN32__) && !defined(__EMSCRIPTEN__) && !defined(__ANDROID__) | |
| 369 | 367 | |
| 368 | +/* from http://developer.apple.com/library/mac/#qa/qa1549/_index.html */ | |
| 369 | +#if !defined(__PLATFORM_WIN32__) && !defined(__EMSCRIPTEN__) && !defined(__ANDROID__) | |
| 370 | 370 | #include <glob.h> |
| 371 | 371 | |
| 372 | 372 | |
| 373 | +//----------------------------------------------------------------------------- | |
| 374 | +// name: expandTildePath() | |
| 375 | +// desc: expand ~ path | |
| 376 | +//----------------------------------------------------------------------------- | |
| 377 | +//std::string expandTildePath( const std::string & path ) | |
| 378 | +//{ | |
| 379 | +// glob_t globbuf; | |
| 380 | +// char ** v; | |
| 381 | +// char * expandedPath = NULL; | |
| 382 | +// // default is the original path | |
| 383 | +// std::string result = path; | |
| 384 | +// | |
| 385 | +// // search for pathnames matching pattern | |
| 386 | +// if( glob(path, GLOB_TILDE, NULL, &globbuf) == 0 ) // success | |
| 387 | +// { | |
| 388 | +// // list of matched pathnames | |
| 389 | +// v = globbuf.gl_pathv; | |
| 390 | +// // number of matched pathnames, gl_pathc == 1 | |
| 391 | +// expandedPath = v[0]; | |
| 392 | +// // copy | |
| 393 | +// result = expandedPath; | |
| 394 | +// // free up | |
| 395 | +// globfree(&globbuf); | |
| 396 | +// } | |
| 397 | +// | |
| 398 | +// // return result | |
| 399 | +// return result; | |
| 400 | +//} | |
| 401 | + | |
| 402 | + | |
| 373 | 403 | char* CreatePathByExpandingTildePath(const char* path) |
| 374 | 404 | { |
| 375 | 405 | glob_t globbuf; |
| @@ -399,11 +429,15 @@ char* CreatePathByExpandingTildePath(const char* path) · 23 unmodified lines | ||
| 399 | 429 | |
| 400 | 430 | return result; |
| 401 | 431 | } |
| 402 | − | |
| 403 | 432 | #endif // __PLATFORM_WIN32__ && __EMSCRIPTEN__ && __ANDROID__ |
| 404 | 433 | |
| 405 | 434 | |
| 406 | −// get full path to file | |
| 435 | + | |
| 436 | + | |
| 437 | +//----------------------------------------------------------------------------- | |
| 438 | +// name: get_full_path() | |
| 439 | +// desc: get full path to file | |
| 440 | +//----------------------------------------------------------------------------- | |
| 407 | 441 | std::string get_full_path( const std::string & fp ) |
| 408 | 442 | { |
| 409 | 443 | #ifndef __PLATFORM_WIN32__ |
| @@ -440,28 +474,39 @@ std::string get_full_path( const std::string & fp ) · 30 unmodified lines | ||
| 440 | 474 | } |
| 441 | 475 | |
| 442 | 476 | |
| 477 | + | |
| 478 | + | |
| 479 | +//----------------------------------------------------------------------------- | |
| 480 | +// name: expand_filepath() | |
| 481 | +// desc: if possible return expand path (e.g., from the unix ~) | |
| 482 | +//----------------------------------------------------------------------------- | |
| 443 | 483 | std::string expand_filepath( std::string & fp ) |
| 444 | 484 | { |
| 445 | 485 | #if defined(__WINDOWS_DS__) || defined(__WINDOWS_ASIO__) || defined(__EMSCRIPTEN__) || defined(__ANDROID__) |
| 446 | 486 | // no expansion in Windows systems or Emscripten or Android |
| 447 | 487 | return fp; |
| 448 | 488 | #else |
| 449 | − | |
| 489 | + // expand ~ to full path | |
| 450 | 490 | char * expanded_cstr = CreatePathByExpandingTildePath( fp.c_str() ); |
| 451 | − | |
| 452 | − if(expanded_cstr == NULL) | |
| 491 | + // check | |
| 492 | + if( expanded_cstr == NULL ) | |
| 453 | 493 | return fp; |
| 454 | − | |
| 494 | + // make c++ string | |
| 455 | 495 | std::string expanded_stdstr = expanded_cstr; |
| 456 | − | |
| 496 | + // free memory | |
| 457 | 497 | free(expanded_cstr); |
| 458 | − | |
| 498 | + // return by value | |
| 459 | 499 | return expanded_stdstr; |
| 460 | − | |
| 461 | 500 | #endif |
| 462 | 501 | } |
| 463 | 502 | |
| 464 | 503 | |
| 504 | + | |
| 505 | + | |
| 506 | +//----------------------------------------------------------------------------- | |
| 507 | +// name: extract_filepath_dir() | |
| 508 | +// desc: return the directory portion of a file path, excluding the filename | |
| 509 | +//----------------------------------------------------------------------------- | |
| 465 | 510 | std::string extract_filepath_dir(std::string &filepath) |
| 466 | 511 | { |
| 467 | 512 | char path_separator = '/'; |
| @@ -474,10 +519,9 @@ std::string extract_filepath_dir(std::string &filepath) · 6 unmodified lines | ||
| 474 | 519 | |
| 475 | 520 | // if the last character is a slash, skip it |
| 476 | 521 | t_CKINT i = filepath.rfind(path_separator); |
| 477 | − | |
| 522 | + // if not separator found, return empty string | |
| 478 | 523 | if(i == std::string::npos) |
| 479 | − return std::string(); | |
| 480 | − | |
| 524 | + return ""; | |
| 481 | 525 | // skip any/all extra trailing slashes |
| 482 | 526 | while( i > 0 && filepath[i-1] == path_separator ) |
| 483 | 527 | i--; |
| @@ -486,7 +530,14 @@ std::string extract_filepath_dir(std::string &filepath) · 2 unmodified lines | ||
| 486 | 530 | return std::string(filepath, 0, i+1); |
| 487 | 531 | } |
| 488 | 532 | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | +//----------------------------------------------------------------------------- | |
| 537 | +// file: dir_go_up() | |
| 538 | +// desc: return directory path 'numUp' levels up the chain | |
| 489 | 539 | // added: ge 1.3.2.0 |
| 540 | +//----------------------------------------------------------------------------- | |
| 490 | 541 | string dir_go_up( const string & dir, t_CKINT numUp ) |
| 491 | 542 | { |
| 492 | 543 | // separator |
| @@ -507,14 +558,13 @@ string dir_go_up( const string & dir, t_CKINT numUp ) · 14 unmodified lines | ||
| 507 | 558 | { |
| 508 | 559 | // find the last slash |
| 509 | 560 | pos = dir.rfind( path_separator, pos-1 ); |
| 561 | + // if no separator found, return empty string | |
| 562 | + if (pos == string::npos) | |
| 563 | + return ""; | |
| 510 | 564 | // skip any extra slashes |
| 511 | 565 | while( pos > 0 && dir[pos-1] == path_separator ) |
| 512 | 566 | pos--; |
| 513 | − | |
| 514 | − // not there | |
| 515 | − if( pos == string::npos ) | |
| 516 | − return ""; | |
| 517 | − else if( pos == 0 ) | |
| 567 | + if( pos == 0 ) | |
| 518 | 568 | return "/"; |
| 519 | 569 | |
| 520 | 570 | // TODO: what about windows? |
| @@ -523,8 +573,7 @@ string dir_go_up( const string & dir, t_CKINT numUp ) · 2 unmodified lines | ||
| 523 | 573 | // decrement |
| 524 | 574 | numUp--; |
| 525 | 575 | } |
| 526 | − | |
| 527 | − | |
| 576 | + | |
| 528 | 577 | // otherwise |
| 529 | 578 | // change spencer 2014-7-17: include trailing slash |
| 530 | 579 | string str = string( dir, 0, pos ); |
| @@ -532,6 +581,9 @@ string dir_go_up( const string & dir, t_CKINT numUp ) · 1 unmodified line | ||
| 532 | 581 | return str; |
| 533 | 582 | } |
| 534 | 583 | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 535 | 587 | //----------------------------------------------------------------------------- |
| 536 | 588 | // name: parse_path_list() |
| 537 | 589 | // desc: split "x:y:z"-style path list into {"x","y","z"} |
| @@ -556,26 +608,66 @@ void parse_path_list( std::string & str, std::list<std::string> & lst ) · 18 unmodified lines | ||
| 556 | 608 | |
| 557 | 609 | |
| 558 | 610 | |
| 559 | −std::string normalize_directory_separator(const std::string &filepath) | |
| 611 | + | |
| 612 | +//----------------------------------------------------------------------------- | |
| 613 | +// name: normalize_directory_separator() | |
| 614 | +// desc: unify directory separator to be consistent across platforms; | |
| 615 | +// inside chuck, we are going with the single forward slash '/' | |
| 616 | +// as the generic directory separator; other separators will | |
| 617 | +// be converted to it | |
| 618 | +//----------------------------------------------------------------------------- | |
| 619 | +std::string normalize_directory_separator( const std::string & filepath ) | |
| 560 | 620 | { |
| 561 | 621 | #ifdef __PLATFORM_WIN32__ |
| 562 | 622 | std::string new_filepath = filepath; |
| 563 | 623 | t_CKINT len = new_filepath.size(); |
| 564 | 624 | for(int i = 0; i < len; i++) |
| 565 | 625 | { |
| 566 | − if(new_filepath[i] == '\\') new_filepath[i] = '/'; | |
| 626 | + if( new_filepath[i] == '\\' ) new_filepath[i] = '/'; | |
| 567 | 627 | } |
| 568 | − | |
| 569 | 628 | return new_filepath; |
| 570 | 629 | #else |
| 571 | − return std::string(filepath); | |
| 630 | + return filepath; | |
| 572 | 631 | #endif // __PLATFORM_WIN32__ |
| 573 | 632 | } |
| 574 | 633 | |
| 575 | −int str_endsin(const char *str, const char *end) | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | +//----------------------------------------------------------------------------- | |
| 638 | +// name: str_endsin() | |
| 639 | +// desc: return true if the first string ends with the second | |
| 640 | +//----------------------------------------------------------------------------- | |
| 641 | +t_CKBOOL str_endsin( const char * str, const char * end ) | |
| 576 | 642 | { |
| 577 | 643 | size_t len = strlen(str); |
| 578 | 644 | size_t endlen = strlen(end); |
| 579 | 645 | |
| 580 | 646 | return strncmp(str+(len-endlen), end, endlen) == 0; |
| 581 | 647 | } |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | +//----------------------------------------------------------------------------- | |
| 653 | +// name: is_absolute_path() | |
| 654 | +// desc: return true if path names an absolute path on the underlying platform | |
| 655 | +// added: 1.4.1.1 (ge) | |
| 656 | +//----------------------------------------------------------------------------- | |
| 657 | +t_CKBOOL is_absolute_path( const std::string & path ) | |
| 658 | +{ | |
| 659 | +#ifdef __PLATFORM_WIN32__ | |
| 660 | + // a little more involved in windows | |
| 661 | + if( path.length() >= 3 && path[1] == ':' && path[2] == '\\' ) | |
| 662 | + { | |
| 663 | + // check drive letter (apparently windows limited to 26 drive letters) | |
| 664 | + if( (path[0] >= 'a' && path[0] <= 'z') || (path[0] >= 'A' && path[0] <= 'Z') ) | |
| 665 | + return true; | |
| 666 | + } | |
| 667 | + // if we got here, then not absolute path | |
| 668 | + return false; | |
| 669 | +#else | |
| 670 | + // does the path begin with '/'? | |
| 671 | + return( workingDir.length() != 0 && workingDir[0] == '/' ); | |
| 672 | +#endif | |
| 673 | +} | |