History

updated tilde expansion; added to working dir param setting

Ge Wang committed 4 years ago

Commit 35554971 parent 46860633 files · +81 −60Browse files

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

Unified · Split

src/core/util_string.cpp+76−58
@@ -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 ~ path374// desc: expand ~ path, does not care whether path is valid or not
376//-----------------------------------------------------------------------------375//-----------------------------------------------------------------------------
377//std::string expandTildePath( const std::string & path )376std::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 path381
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 pattern384 {
386// if( glob(path, GLOB_TILDE, NULL, &globbuf) == 0 ) // success385 // check results number
387// {386 if( we.we_wordc > 0 )
388// // list of matched pathnames387 {
389// v = globbuf.gl_pathv;388 // number of matched pathnames
390// // number of matched pathnames, gl_pathc == 1389 result = we.we_wordv[0];
391// expandedPath = v[0];390 }
392// // copy391 // free up
393// result = expandedPath;392 wordfree( &we );
394// // free up393 }
395// globfree(&globbuf);
396// }
397//
398// // return result
399// return result;
400//}
401 394
395 // return result
396 return result;
397}
402 398
403char* 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//-----------------------------------------------------------------------------
405std::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) //success411 // 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 pathnames414 // number of matched pathnames
414 expandedPath = v[0]; //number of matched pathnames, gl_pathc == 1415 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 well417 // 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 extension474 // 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 extension488 // 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//-----------------------------------------------------------------------------
483std::string expand_filepath( std::string & fp )507std::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 Android510 // no expansion in Windows systems or Emscripten or Android
487 return fp;511 return fp;
488#else512#else
489 // expand ~ to full path513 // expand ~ to full path
490 char * expanded_cstr = CreatePathByExpandingTildePath( fp.c_str() );514 if( ensurePathExists )
491 // check515 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#endif518#endif
501}519}
502 520

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