// ファイル名から拡張子を獲得して、 // 指定の拡張子と同じかどうかを調べます(大文字/小文字の区別なし) #include <iostream> #include <string> #include <cctype> using namespace std; ////////////////////////////////////////////////////////////////////// // string compare no-case // Return // == 0 : equal // != 0 : different ////////////////////////////////////////////////////////////////////// int cmp_nocase( const string &s1, const string &s2 ) { string::size_type len1 = s1.size(); string::size_type len2 = s2.size(); if ( len1 != len2 ) return 1; for ( string::size_type i=0; i < len1 ; i++ ) { if ( toupper( s1.at(i) ) != toupper( s2.at(i) ) ) return 1; } return 0; } string & get_file_ext( const string& str, string &ext ) { ext = ""; cout << "str = " << str << endl; string::size_type pos = str.find_last_of( "." ); if ( pos == str.npos ) { return ext; // not found, return default. } if ( pos == str.size() ) { return ext; // terminated `.' } ext = str.substr( pos+1 ); return ext; } bool cmp_file_ext( const string & filename, const string & ext2 ) { string ext1; get_file_ext( filename, ext1 ); return cmp_nocase( ext1, ext2 ); } int main() { const string s1 = "cpp"; string s2 = "foobar.asm"; string s3 = "foobar.CPP"; cout << s1 << " vs " << s2 << " : " << cmp_file_ext( s2, s1 ) << endl; cout << s1 << " vs " << s3 << " : " << cmp_file_ext( s3, s1 ) << endl; return 0; } /* /* 後方から、"."をサーチして、あればそこから文字列最後までの /* 文字列を抽出します。Perl みたいでよいです。 /**/