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 | 368 | /* from http://developer.apple.com/library/mac/#qa/qa1549/_index.html */ |
| 369 | 369 | #if !defined(__PLATFORM_WIN32__) && !defined(__EMSCRIPTEN__) && !defined(__ANDROID__) |
| 370 | −#include <glob.h> | |
| 371 | − | |
| 372 | 370 | |
| 371 | +#include <wordexp.h> | |
| 373 | 372 | //----------------------------------------------------------------------------- |
| 374 | 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 ) | |
| 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 | −//} | |
| 376 | +std::string expandTildePath( const std::string & path ) | |
| 377 | +{ | |
| 378 | + wordexp_t we; | |
| 379 | + // default is the original path | |
| 380 | + std::string result = path; | |
| 381 | + | |
| 382 | + // search for pathnames matching pattern | |
| 383 | + if( wordexp(path.c_str(), &we, 0 ) == 0 ) // success | |
| 384 | + { | |
| 385 | + // check results number | |
| 386 | + if( we.we_wordc > 0 ) | |
| 387 | + { | |
| 388 | + // number of matched pathnames | |
| 389 | + result = we.we_wordv[0]; | |
| 390 | + } | |
| 391 | + // free up | |
| 392 | + wordfree( &we ); | |
| 393 | + } | |
| 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 | 407 | glob_t globbuf; |
| 406 | − char **v; | |
| 407 | − char *expandedPath = NULL, *result = NULL; | |
| 408 | − | |
| 409 | − assert(path != NULL); | |
| 408 | + // default is the original path | |
| 409 | + std::string result = path; | |
| 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 | − expandedPath = v[0]; //number of matched pathnames, gl_pathc == 1 | |
| 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) | |
| 414 | + // number of matched pathnames | |
| 415 | + if( globbuf.gl_pathc > 0 ) | |
| 419 | 416 | { |
| 420 | − //copy the null-termination as well | |
| 421 | − memcpy(result, expandedPath, toCopy); | |
| 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=] | |
| 417 | + // copy | |
| 418 | + result = globbuf.gl_pathv[0]; | |
| 426 | 419 | } |
| 420 | + // free up | |
| 427 | 421 | globfree(&globbuf); |
| 428 | 422 | } |
| 429 | 423 | |
| 424 | + // return result | |
| 430 | 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 | 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 | 469 | #ifndef __PLATFORM_WIN32__ |
| 444 | 470 | |
| 445 | 471 | char buf[PATH_MAX]; |
| 446 | − | |
| 447 | 472 | char * result = realpath(fp.c_str(), buf); |
| 448 | 473 | |
| 449 | 474 | // try with .ck extension |
| @@ -458,7 +483,6 @@ std::string get_full_path( const std::string & fp ) · 8 unmodified lines | ||
| 458 | 483 | #else // |
| 459 | 484 | |
| 460 | 485 | char buf[MAX_PATH]; |
| 461 | − | |
| 462 | 486 | DWORD result = GetFullPathName(fp.c_str(), MAX_PATH, buf, NULL); |
| 463 | 487 | |
| 464 | 488 | // try with .ck extension |
| @@ -480,23 +504,17 @@ std::string get_full_path( const std::string & fp ) · 15 unmodified lines | ||
| 480 | 504 | // name: expand_filepath() |
| 481 | 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 | 509 | #if defined(__WINDOWS_DS__) || defined(__WINDOWS_ASIO__) || defined(__EMSCRIPTEN__) || defined(__ANDROID__) |
| 486 | 510 | // no expansion in Windows systems or Emscripten or Android |
| 487 | 511 | return fp; |
| 488 | 512 | #else |
| 489 | 513 | // expand ~ to full path |
| 490 | − char * expanded_cstr = CreatePathByExpandingTildePath( fp.c_str() ); | |
| 491 | − // check | |
| 492 | − if( expanded_cstr == NULL ) | |
| 493 | − return 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; | |
| 514 | + if( ensurePathExists ) | |
| 515 | + return globTildePath( fp ); | |
| 516 | + else | |
| 517 | + return expandTildePath( fp ); | |
| 500 | 518 | #endif |
| 501 | 519 | } |
| 502 | 520 | |