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
367367
368368 /* from http://developer.apple.com/library/mac/#qa/qa1549/_index.html */
369369 #if !defined(__PLATFORM_WIN32__) && !defined(__EMSCRIPTEN__) && !defined(__ANDROID__)
370−#include <glob.h>
371−
372370
371+#include <wordexp.h>
373372 //-----------------------------------------------------------------------------
374373 // name: expandTildePath()
375−// desc: expand ~ path
374+// desc: expand ~ path, does not care whether path is valid or not
376375 //-----------------------------------------------------------------------------
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+ }
401394
395+ // return result
396+ return result;
397+}
402398
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 )
404406 {
405407 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;
410410
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
412413 {
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 )
419416 {
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];
426419 }
420+ // free up
427421 globfree(&globbuf);
428422 }
429423
424+ // return result
430425 return result;
431426 }
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+//}
432458 #endif // __PLATFORM_WIN32__ && __EMSCRIPTEN__ && __ANDROID__
433459
434460
@@ -443,7 +469,6 @@ std::string get_full_path( const std::string & fp ) · 8 unmodified lines
443469 #ifndef __PLATFORM_WIN32__
444470
445471 char buf[PATH_MAX];
446−
447472 char * result = realpath(fp.c_str(), buf);
448473
449474 // try with .ck extension
@@ -458,7 +483,6 @@ std::string get_full_path( const std::string & fp ) · 8 unmodified lines
458483 #else //
459484
460485 char buf[MAX_PATH];
461−
462486 DWORD result = GetFullPathName(fp.c_str(), MAX_PATH, buf, NULL);
463487
464488 // try with .ck extension
@@ -480,23 +504,17 @@ std::string get_full_path( const std::string & fp ) · 15 unmodified lines
480504 // name: expand_filepath()
481505 // desc: if possible return expand path (e.g., from the unix ~)
482506 //-----------------------------------------------------------------------------
483−std::string expand_filepath( std::string & fp )
507+std::string expand_filepath( std::string & fp, t_CKBOOL ensurePathExists )
484508 {
485509 #if defined(__WINDOWS_DS__) || defined(__WINDOWS_ASIO__) || defined(__EMSCRIPTEN__) || defined(__ANDROID__)
486510 // no expansion in Windows systems or Emscripten or Android
487511 return fp;
488512 #else
489513 // 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 );
500518 #endif
501519 }
502520

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