Showing the changes to src/core/util_string.cpp (1 of 31 files). Show all
Unified · Split
| @@ -43,6 +43,7 @@ · 42 unmodified lines | |||
| 43 | #endif // #ifndef __CHUNREAL_ENGINE__ | 43 | #endif // #ifndef __CHUNREAL_ENGINE__ |
| 44 | #endif // #ifdef __PLATFORM_WIN32__ | 44 | #endif // #ifdef __PLATFORM_WIN32__ |
| 45 | 45 | ||
| 46 | #include <time.h> | ||
| 46 | #include <limits.h> | 47 | #include <limits.h> |
| 47 | #include <stdio.h> | 48 | #include <stdio.h> |
| 48 | using namespace std; | 49 | using namespace std; |
| @@ -507,7 +508,7 @@ std::string get_full_path( const std::string & fp ) · 458 unmodified lines | |||
| 507 | char * result = realpath(fp.c_str(), buf); | 508 | char * result = realpath(fp.c_str(), buf); |
| 508 | 509 | ||
| 509 | // try with .ck extension | 510 | // try with .ck extension |
| 510 | if(result == NULL && !str_endsin(fp.c_str(), ".ck")) | 511 | if( result == NULL && !extension_matches(fp, ".ck") ) |
| 511 | result = realpath((fp + ".ck").c_str(), buf); | 512 | result = realpath((fp + ".ck").c_str(), buf); |
| 512 | 513 | ||
| 513 | if(result == NULL) | 514 | if(result == NULL) |
| @@ -526,7 +527,7 @@ std::string get_full_path( const std::string & fp ) · 12 unmodified lines | |||
| 526 | #endif | 527 | #endif |
| 527 | 528 | ||
| 528 | // try with .ck extension | 529 | // try with .ck extension |
| 529 | if(result == 0 && !str_endsin(fp.c_str(), ".ck")) | 530 | if( result == 0 && !extension_matches(fp, ".ck") ) |
| 530 | { | 531 | { |
| 531 | #ifndef __CHUNREAL_ENGINE__ | 532 | #ifndef __CHUNREAL_ENGINE__ |
| 532 | result = GetFullPathName((fp + ".ck").c_str(), MAX_PATH, buf, NULL); | 533 | result = GetFullPathName((fp + ".ck").c_str(), MAX_PATH, buf, NULL); |
| @@ -699,21 +700,6 @@ std::string normalize_directory_separator( const std::string & filepath ) · 166 unmodified lines | |||
| 699 | 700 | ||
| 700 | 701 | ||
| 701 | 702 | ||
| 702 | //----------------------------------------------------------------------------- | ||
| 703 | // name: str_endsin() | ||
| 704 | // desc: return true if the first string ends with the second | ||
| 705 | //----------------------------------------------------------------------------- | ||
| 706 | t_CKBOOL str_endsin( const char * str, const char * end ) | ||
| 707 | { | ||
| 708 | size_t len = strlen(str); | ||
| 709 | size_t endlen = strlen(end); | ||
| 710 | |||
| 711 | return strncmp(str+(len-endlen), end, endlen) == 0; | ||
| 712 | } | ||
| 713 | |||
| 714 | |||
| 715 | |||
| 716 | |||
| 717 | //----------------------------------------------------------------------------- | 703 | //----------------------------------------------------------------------------- |
| 718 | // name: is_absolute_path() | 704 | // name: is_absolute_path() |
| 719 | // desc: return true if path names an absolute path on the underlying platform | 705 | // desc: return true if path names an absolute path on the underlying platform |
| @@ -742,21 +728,116 @@ t_CKBOOL is_absolute_path( const std::string & path ) · 22 unmodified lines | |||
| 742 | 728 | ||
| 743 | //----------------------------------------------------------------------------- | 729 | //----------------------------------------------------------------------------- |
| 744 | // name: extension_matches() | 730 | // name: extension_matches() |
| 745 | // desc: check whether a filename matches a particular extension | 731 | // desc: test whether a filename ends in a particular extension |
| 746 | //----------------------------------------------------------------------------- | 732 | //----------------------------------------------------------------------------- |
| 747 | t_CKBOOL extension_matches( const std::string & filename, const std::string & extension ) | 733 | t_CKBOOL extension_matches( const std::string & filename, |
| 734 | const std::string & extension, | ||
| 735 | t_CKBOOL ignoreCase ) | ||
| 748 | { | 736 | { |
| 749 | // lowercase | 737 | // if ignore case, lower case both |
| 750 | string f = tolower( filename ); | 738 | string f = ignoreCase ? tolower(filename) : filename; |
| 751 | string e = tolower( extension ); | 739 | string e = ignoreCase ? tolower(extension) : extension; |
| 752 | // look for extension from the end | 740 | // look for extension from the end |
| 753 | std::size_t pos = f.rfind( e ); | 741 | std::size_t pos = f.rfind( e ); |
| 754 | // check result | 742 | // check result |
| 755 | return (pos != std::string::npos) && (pos == (f.length()-e.length())); | 743 | return (pos != std::string::npos) && (pos == (f.length()-e.length())); |
| 744 | } | ||
| 745 | |||
| 746 | |||
| 747 | |||
| 748 | |||
| 749 | //----------------------------------------------------------------------------- | ||
| 750 | // name: extension_removed() | ||
| 751 | // desc: return filename without extension | ||
| 752 | //----------------------------------------------------------------------------- | ||
| 753 | std::string extension_removed( const std::string & filename, | ||
| 754 | const std::string & extension, | ||
| 755 | t_CKBOOL ignoreCase ) | ||
| 756 | { | ||
| 757 | // if ignore case, lower case both | ||
| 758 | string f = ignoreCase ? tolower(filename) : filename; | ||
| 759 | string e = ignoreCase ? tolower(extension) : extension; | ||
| 760 | // look for extension from the end | ||
| 761 | std::size_t pos = f.rfind( e ); | ||
| 762 | // no match, return original | ||
| 763 | if( pos == std::string::npos ) return filename; | ||
| 764 | // return substring | ||
| 765 | return filename.substr( 0, pos ); | ||
| 766 | } | ||
| 767 | |||
| 768 | |||
| 756 | 769 | ||
| 757 | // t_CKUINT extension_length = strlen( extension ); | 770 | |
| 758 | // t_CKUINT filename_length = strlen( filename ); | 771 | //----------------------------------------------------------------------------- |
| 759 | // | 772 | // name: subdir_ok2recurse() |
| 760 | // return strncmp( extension, filename+(filename_length-extension_length), | 773 | // desc: test if a directory name is OK to recurse, e.g., in search for chugins |
| 761 | // extension_length ) == 0; | 774 | // the reason we have this function is for macOS bundles that are actually |
| 775 | // directories, e.g., if Faust.chug is directory and the actual chugin | ||
| 776 | // is located in side in /Contents/MacOS -- in which case we would not | ||
| 777 | // search for more chugins within the Faust.chug directory; | ||
| 778 | // HOWEVER, something like .chug (a directory name starting with '.') is | ||
| 779 | // okay, as that could be hidden directory | ||
| 780 | //----------------------------------------------------------------------------- | ||
| 781 | t_CKBOOL subdir_ok2recurse( const std::string & dirName, | ||
| 782 | const std::string & extension, | ||
| 783 | t_CKBOOL ignoreCase ) | ||
| 784 | { | ||
| 785 | // if ignore case, lower case both | ||
| 786 | string f = ignoreCase ? tolower(dirName) : dirName; | ||
| 787 | string e = ignoreCase ? tolower(extension) : extension; | ||
| 788 | // if the two are equal, e.g., ".chug". OK | ||
| 789 | if( f == e ) return TRUE; | ||
| 790 | |||
| 791 | // return the opposite of extension matches | ||
| 792 | // i.e., if extension matches, don't recurse | ||
| 793 | return !extension_matches( dirName, extension, ignoreCase ); | ||
| 794 | } | ||
| 795 | |||
| 796 | |||
| 797 | |||
| 798 | |||
| 799 | //----------------------------------------------------------------------------- | ||
| 800 | // name: str_endsin() | ||
| 801 | // desc: return true if the first string ends with the second | ||
| 802 | // *** CURRENTLY NOT USED; see extension_matches() *** | ||
| 803 | //----------------------------------------------------------------------------- | ||
| 804 | t_CKBOOL str_endsin( const char * str, const char * end ) | ||
| 805 | { | ||
| 806 | size_t len = strlen(str); | ||
| 807 | size_t endlen = strlen(end); | ||
| 808 | |||
| 809 | return strncmp(str+(len-endlen), end, endlen) == 0; | ||
| 810 | } | ||
| 811 | |||
| 812 | |||
| 813 | |||
| 814 | |||
| 815 | //----------------------------------------------------------------------------- | ||
| 816 | // name: timestamp_formatted() | 1.5.0.4 (ge) added | ||
| 817 | // desc: return formatted string of current local time; no newline | ||
| 818 | //----------------------------------------------------------------------------- | ||
| 819 | std::string timestamp_formatted() | ||
| 820 | { | ||
| 821 | // timestamp | ||
| 822 | char datetime_buf[32]; | ||
| 823 | // how computer sees time | ||
| 824 | time_t datetime; | ||
| 825 | // get local time | ||
| 826 | time( &datetime ); | ||
| 827 | // format time | ||
| 828 | const char * timestr = ctime(&datetime); | ||
| 829 | // check if result ok | ||
| 830 | if( !timestr ) return "[unable to determine/format time]"; | ||
| 831 | |||
| 832 | // copy formatted date; ctime() always returns fixed length | ||
| 833 | // e.g., "Sat Jun 24 04:18:42 2023" -- 24 characters... okay | ||
| 834 | // what happens when the year becomes 5 or more digits? | ||
| 835 | // in the year 10000... | ||
| 836 | // will there still be computers? or humans? computer music?!? | ||
| 837 | strncpy( datetime_buf, timestr, 24 ); | ||
| 838 | // terminate the string | ||
| 839 | datetime_buf[24] = '\0'; | ||
| 840 | |||
| 841 | // return the formatted timestamp | ||
| 842 | return datetime_buf; | ||
| 762 | } | 843 | } |