Showing the changes to src/core/util_string.cpp (1 of 3 files). Show all
Unified · Split
| @@ -367,68 +367,94 @@ done: · 366 unmodified lines | |||
| 367 | 367 | ||
| 368 | /* from http://developer.apple.com/library/mac/#qa/qa1549/_index.html */ | 368 | /* from http://developer.apple.com/library/mac/#qa/qa1549/_index.html */ |
| 369 | #if !defined(__PLATFORM_WIN32__) && !defined(__EMSCRIPTEN__) && !defined(__ANDROID__) | 369 | #if !defined(__PLATFORM_WIN32__) && !defined(__EMSCRIPTEN__) && !defined(__ANDROID__) |
| 370 | #include <glob.h> | ||
| 371 | |||
| 372 | 370 | ||
| 371 | #include <wordexp.h> | ||
| 373 | //----------------------------------------------------------------------------- | 372 | //----------------------------------------------------------------------------- |
| 374 | // name: expandTildePath() | 373 | // name: expandTildePath() |
| 375 | // desc: expand ~ path | 374 | // desc: expand ~ path, does not care whether path is valid or not |
| 376 | //----------------------------------------------------------------------------- | 375 | //----------------------------------------------------------------------------- |
| 377 | //std::string expandTildePath( const std::string & path ) | 376 | std::string expandTildePath( const std::string & path ) |
| 378 | //{ | 377 | { |
| 379 | // glob_t globbuf; | 378 | wordexp_t we; |
| 380 | // char ** v; | 379 | // default is the original path |
| 381 | // char * expandedPath = NULL; | 380 | std::string result = path; |
| 382 | // // default is the original path | 381 | |
| 383 | // std::string result = path; | 382 | // search for pathnames matching pattern |
| 384 | // | 383 | if( wordexp(path.c_str(), &we, 0 ) == 0 ) // success |
| 385 | // // search for pathnames matching pattern | 384 | { |
| 386 | // if( glob(path, GLOB_TILDE, NULL, &globbuf) == 0 ) // success | 385 | // check results number |
| 387 | // { | 386 | if( we.we_wordc > 0 ) |
| 388 | // // list of matched pathnames | 387 | { |
| 389 | // v = globbuf.gl_pathv; | 388 | // number of matched pathnames |
| 390 | // // number of matched pathnames, gl_pathc == 1 | 389 | result = we.we_wordv[0]; |
| 391 | // expandedPath = v[0]; | 390 | } |
| 392 | // // copy | 391 | // free up |
| 393 | // result = expandedPath; | 392 | wordfree( &we ); |
| 394 | // // free up | 393 | } |
| 395 | // globfree(&globbuf); | ||
| 396 | // } | ||
| 397 | // | ||
| 398 | // // return result | ||
| 399 | // return result; | ||
| 400 | //} | ||
| 401 | 394 | ||
| 395 | // return result | ||
| 396 | return result; | ||
| 397 | } | ||
| 402 | 398 | ||
| 403 | char* CreatePathByExpandingTildePath(const char* path) | 399 | #include <glob.h> |
| 400 | //----------------------------------------------------------------------------- | ||
| 401 | // name: globTildePath() | ||
| 402 | // desc: expand ~ path, making sure the path actually exists | ||
| 403 | // adapted from CreatePathByExpandingTildePath below | ||
| 404 | //----------------------------------------------------------------------------- | ||
| 405 | std::string globTildePath( const std::string & path ) | ||
| 404 | { | 406 | { |
| 405 | glob_t globbuf; | 407 | glob_t globbuf; |
| 406 | char **v; | 408 | // default is the original path |
| 407 | char *expandedPath = NULL, *result = NULL; | 409 | std::string result = path; |
| 408 | |||
| 409 | assert(path != NULL); | ||
| 410 | 410 | ||
| 411 | if (glob(path, GLOB_TILDE, NULL, &globbuf) == 0) //success | 411 | // search for pathnames matching pattern |
| 412 | if( glob(path.c_str(), GLOB_TILDE, NULL, &globbuf) == 0 ) // success | ||
| 412 | { | 413 | { |
| 413 | v = globbuf.gl_pathv; //list of matched pathnames | 414 | // number of matched pathnames |
| 414 | expandedPath = v[0]; //number of matched pathnames, gl_pathc == 1 | 415 | if( globbuf.gl_pathc > 0 ) |
| 415 | |||
| 416 | size_t toCopy = strlen(expandedPath) + 1; //the extra char is for the null-termination | ||
| 417 | result = (char*)calloc(1, toCopy); | ||
| 418 | if(result) | ||
| 419 | { | 416 | { |
| 420 | //copy the null-termination as well | 417 | // copy |
| 421 | memcpy(result, expandedPath, toCopy); | 418 | result = globbuf.gl_pathv[0]; |
| 422 | // was: strncpy(result, expandedPath,strlen(expandedPath) + 1); | ||
| 423 | // which was generating a warning on linux/gcc | ||
| 424 | // warning: ‘char* strncpy(char*, const char*, size_t)’ specified | ||
| 425 | // bound depends on the length of the source argument [-Wstringop-overflow=] | ||
| 426 | } | 419 | } |
| 420 | // free up | ||
| 427 | globfree(&globbuf); | 421 | globfree(&globbuf); |
| 428 | } | 422 | } |
| 429 | 423 | ||
| 424 | // return result | ||
| 430 | return result; | 425 | return result; |
| 431 | } | 426 | } |
| 427 | |||
| 428 | // original | ||
| 429 | //char* CreatePathByExpandingTildePath(const char* path) | ||
| 430 | //{ | ||
| 431 | // glob_t globbuf; | ||
| 432 | // char **v; | ||
| 433 | // char *expandedPath = NULL, *result = NULL; | ||
| 434 | // | ||
| 435 | // assert(path != NULL); | ||
| 436 | // | ||
| 437 | // if (glob(path, GLOB_TILDE, NULL, &globbuf) == 0) //success | ||
| 438 | // { | ||
| 439 | // v = globbuf.gl_pathv; //list of matched pathnames | ||
| 440 | // expandedPath = v[0]; //number of matched pathnames, gl_pathc == 1 | ||
| 441 | // | ||
| 442 | // size_t toCopy = strlen(expandedPath) + 1; //the extra char is for the null-termination | ||
| 443 | // result = (char*)calloc(1, toCopy); | ||
| 444 | // if(result) | ||
| 445 | // { | ||
| 446 | // //copy the null-termination as well | ||
| 447 | // memcpy(result, expandedPath, toCopy); | ||
| 448 | // // was: strncpy(result, expandedPath,strlen(expandedPath) + 1); | ||
| 449 | // // which was generating a warning on linux/gcc | ||
| 450 | // // warning: ‘char* strncpy(char*, const char*, size_t)’ specified | ||
| 451 | // // bound depends on the length of the source argument [-Wstringop-overflow=] | ||
| 452 | // } | ||
| 453 | // globfree(&globbuf); | ||
| 454 | // } | ||
| 455 | // | ||
| 456 | // return result; | ||
| 457 | //} | ||
| 432 | #endif // __PLATFORM_WIN32__ && __EMSCRIPTEN__ && __ANDROID__ | 458 | #endif // __PLATFORM_WIN32__ && __EMSCRIPTEN__ && __ANDROID__ |
| 433 | 459 | ||
| 434 | 460 | ||
| @@ -443,7 +469,6 @@ std::string get_full_path( const std::string & fp ) · 8 unmodified lines | |||
| 443 | #ifndef __PLATFORM_WIN32__ | 469 | #ifndef __PLATFORM_WIN32__ |
| 444 | 470 | ||
| 445 | char buf[PATH_MAX]; | 471 | char buf[PATH_MAX]; |
| 446 | |||
| 447 | char * result = realpath(fp.c_str(), buf); | 472 | char * result = realpath(fp.c_str(), buf); |
| 448 | 473 | ||
| 449 | // try with .ck extension | 474 | // try with .ck extension |
| @@ -458,7 +483,6 @@ std::string get_full_path( const std::string & fp ) · 8 unmodified lines | |||
| 458 | #else // | 483 | #else // |
| 459 | 484 | ||
| 460 | char buf[MAX_PATH]; | 485 | char buf[MAX_PATH]; |
| 461 | |||
| 462 | DWORD result = GetFullPathName(fp.c_str(), MAX_PATH, buf, NULL); | 486 | DWORD result = GetFullPathName(fp.c_str(), MAX_PATH, buf, NULL); |
| 463 | 487 | ||
| 464 | // try with .ck extension | 488 | // try with .ck extension |
| @@ -480,23 +504,17 @@ std::string get_full_path( const std::string & fp ) · 15 unmodified lines | |||
| 480 | // name: expand_filepath() | 504 | // name: expand_filepath() |
| 481 | // desc: if possible return expand path (e.g., from the unix ~) | 505 | // desc: if possible return expand path (e.g., from the unix ~) |
| 482 | //----------------------------------------------------------------------------- | 506 | //----------------------------------------------------------------------------- |
| 483 | std::string expand_filepath( std::string & fp ) | 507 | std::string expand_filepath( std::string & fp, t_CKBOOL ensurePathExists ) |
| 484 | { | 508 | { |
| 485 | #if defined(__WINDOWS_DS__) || defined(__WINDOWS_ASIO__) || defined(__EMSCRIPTEN__) || defined(__ANDROID__) | 509 | #if defined(__WINDOWS_DS__) || defined(__WINDOWS_ASIO__) || defined(__EMSCRIPTEN__) || defined(__ANDROID__) |
| 486 | // no expansion in Windows systems or Emscripten or Android | 510 | // no expansion in Windows systems or Emscripten or Android |
| 487 | return fp; | 511 | return fp; |
| 488 | #else | 512 | #else |
| 489 | // expand ~ to full path | 513 | // expand ~ to full path |
| 490 | char * expanded_cstr = CreatePathByExpandingTildePath( fp.c_str() ); | 514 | if( ensurePathExists ) |
| 491 | // check | 515 | return globTildePath( fp ); |
| 492 | if( expanded_cstr == NULL ) | 516 | else |
| 493 | return fp; | 517 | return expandTildePath( fp ); |
| 494 | // make c++ string | ||
| 495 | std::string expanded_stdstr = expanded_cstr; | ||
| 496 | // free memory | ||
| 497 | free(expanded_cstr); | ||
| 498 | // return by value | ||
| 499 | return expanded_stdstr; | ||
| 500 | #endif | 518 | #endif |
| 501 | } | 519 | } |
| 502 | 520 | ||