History

working dir acknowledges windows absolute path

gewang committed 4 years ago

Commit 546b4ed1 parent 1d4c06b3 files · +153 −50Browse files

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

Unified · Split

src/core/util_string.cpp+118−26
@@ -363,13 +363,43 @@ done: · 362 unmodified lines
363363 }
364364
365365
366−/* from http://developer.apple.com/library/mac/#qa/qa1549/_index.html */
367366
368−#if !defined(__PLATFORM_WIN32__) && !defined(__EMSCRIPTEN__) && !defined(__ANDROID__)
369367
368+/* from http://developer.apple.com/library/mac/#qa/qa1549/_index.html */
369+#if !defined(__PLATFORM_WIN32__) && !defined(__EMSCRIPTEN__) && !defined(__ANDROID__)
370370 #include <glob.h>
371371
372372
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+
373403 char* CreatePathByExpandingTildePath(const char* path)
374404 {
375405 glob_t globbuf;
@@ -399,11 +429,15 @@ char* CreatePathByExpandingTildePath(const char* path) · 23 unmodified lines
399429
400430 return result;
401431 }
402−
403432 #endif // __PLATFORM_WIN32__ && __EMSCRIPTEN__ && __ANDROID__
404433
405434
406−// get full path to file
435+
436+
437+//-----------------------------------------------------------------------------
438+// name: get_full_path()
439+// desc: get full path to file
440+//-----------------------------------------------------------------------------
407441 std::string get_full_path( const std::string & fp )
408442 {
409443 #ifndef __PLATFORM_WIN32__
@@ -440,28 +474,39 @@ std::string get_full_path( const std::string & fp ) · 30 unmodified lines
440474 }
441475
442476
477+
478+
479+//-----------------------------------------------------------------------------
480+// name: expand_filepath()
481+// desc: if possible return expand path (e.g., from the unix ~)
482+//-----------------------------------------------------------------------------
443483 std::string expand_filepath( std::string & fp )
444484 {
445485 #if defined(__WINDOWS_DS__) || defined(__WINDOWS_ASIO__) || defined(__EMSCRIPTEN__) || defined(__ANDROID__)
446486 // no expansion in Windows systems or Emscripten or Android
447487 return fp;
448488 #else
449−
489+ // expand ~ to full path
450490 char * expanded_cstr = CreatePathByExpandingTildePath( fp.c_str() );
451−
452− if(expanded_cstr == NULL)
491+ // check
492+ if( expanded_cstr == NULL )
453493 return fp;
454−
494+ // make c++ string
455495 std::string expanded_stdstr = expanded_cstr;
456−
496+ // free memory
457497 free(expanded_cstr);
458−
498+ // return by value
459499 return expanded_stdstr;
460−
461500 #endif
462501 }
463502
464503
504+
505+
506+//-----------------------------------------------------------------------------
507+// name: extract_filepath_dir()
508+// desc: return the directory portion of a file path, excluding the filename
509+//-----------------------------------------------------------------------------
465510 std::string extract_filepath_dir(std::string &filepath)
466511 {
467512 char path_separator = '/';
@@ -474,10 +519,9 @@ std::string extract_filepath_dir(std::string &filepath) · 6 unmodified lines
474519
475520 // if the last character is a slash, skip it
476521 t_CKINT i = filepath.rfind(path_separator);
477−
522+ // if not separator found, return empty string
478523 if(i == std::string::npos)
479− return std::string();
480−
524+ return "";
481525 // skip any/all extra trailing slashes
482526 while( i > 0 && filepath[i-1] == path_separator )
483527 i--;
@@ -486,7 +530,14 @@ std::string extract_filepath_dir(std::string &filepath) · 2 unmodified lines
486530 return std::string(filepath, 0, i+1);
487531 }
488532
533+
534+
535+
536+//-----------------------------------------------------------------------------
537+// file: dir_go_up()
538+// desc: return directory path 'numUp' levels up the chain
489539 // added: ge 1.3.2.0
540+//-----------------------------------------------------------------------------
490541 string dir_go_up( const string & dir, t_CKINT numUp )
491542 {
492543 // separator
@@ -507,14 +558,13 @@ string dir_go_up( const string & dir, t_CKINT numUp ) · 14 unmodified lines
507558 {
508559 // find the last slash
509560 pos = dir.rfind( path_separator, pos-1 );
561+ // if no separator found, return empty string
562+ if (pos == string::npos)
563+ return "";
510564 // skip any extra slashes
511565 while( pos > 0 && dir[pos-1] == path_separator )
512566 pos--;
513−
514− // not there
515− if( pos == string::npos )
516− return "";
517− else if( pos == 0 )
567+ if( pos == 0 )
518568 return "/";
519569
520570 // TODO: what about windows?
@@ -523,8 +573,7 @@ string dir_go_up( const string & dir, t_CKINT numUp ) · 2 unmodified lines
523573 // decrement
524574 numUp--;
525575 }
526−
527−
576+
528577 // otherwise
529578 // change spencer 2014-7-17: include trailing slash
530579 string str = string( dir, 0, pos );
@@ -532,6 +581,9 @@ string dir_go_up( const string & dir, t_CKINT numUp ) · 1 unmodified line
532581 return str;
533582 }
534583
584+
585+
586+
535587 //-----------------------------------------------------------------------------
536588 // name: parse_path_list()
537589 // 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
556608
557609
558610
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 )
560620 {
561621 #ifdef __PLATFORM_WIN32__
562622 std::string new_filepath = filepath;
563623 t_CKINT len = new_filepath.size();
564624 for(int i = 0; i < len; i++)
565625 {
566− if(new_filepath[i] == '\\') new_filepath[i] = '/';
626+ if( new_filepath[i] == '\\' ) new_filepath[i] = '/';
567627 }
568−
569628 return new_filepath;
570629 #else
571− return std::string(filepath);
630+ return filepath;
572631 #endif // __PLATFORM_WIN32__
573632 }
574633
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 )
576642 {
577643 size_t len = strlen(str);
578644 size_t endlen = strlen(end);
579645
580646 return strncmp(str+(len-endlen), end, endlen) == 0;
581647 }
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+}

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