History

Merge remote-tracking branch 'upstream/main' into openbsd

Sigrid Solveig Haflínudóttir committed 3 years ago

Commit 642fcfd2 parents 4ed052c 6628be431 files · +852 −387Browse files

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

Unified · Split

src/core/util_string.cpp+108−27
@@ -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>
48using namespace std;49using 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 extension510 // 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#endif527#endif
527 528
528 // try with .ck extension529 // 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//-----------------------------------------------------------------------------
706t_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 platform705// 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 extension731// desc: test whether a filename ends in a particular extension
746//-----------------------------------------------------------------------------732//-----------------------------------------------------------------------------
747t_CKBOOL extension_matches( const std::string & filename, const std::string & extension )733t_CKBOOL extension_matches( const std::string & filename,
734 const std::string & extension,
735 t_CKBOOL ignoreCase )
748{736{
749 // lowercase737 // 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 end740 // look for extension from the end
753 std::size_t pos = f.rfind( e );741 std::size_t pos = f.rfind( e );
754 // check result742 // 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//-----------------------------------------------------------------------------
753std::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//-----------------------------------------------------------------------------
781t_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//-----------------------------------------------------------------------------
804t_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//-----------------------------------------------------------------------------
819std::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}

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