home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c      Meh, in C you gotta define EVERYTHING      243,242 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 242,375 of 243,242   
   Bonita Montero to All   
   Re: is_binary_file()   
   08 Dec 25 20:50:06   
   
   From: Bonita.Montero@gmail.com   
      
   And if you like it fast:   
      
   bool binary( path pth )   
   {   
      
        static vector valid = []()   
            {   
                vector ret( numeric_limits::max() );   
                for( size_t c = ret.size(); c--; )   
                    ret[c] = c >= 0x20 || c == '\r' || c == '\n' || c ==   
   '\t';   
                return ret;   
            }();   
        ifstream ifs;   
        ifs.exceptions( ios_base::failbit | ios_base::badbit );   
        ifs.open( pth, ios_base::binary | ios_base::ate );   
        streampos pos = ifs.tellg();   
        if( pos > (size_t)-1 )   
            throw ios_base::failure( "file too large", error_code(   
   (int)errc::file_too_large, generic_category() ) );   
        string buf( (size_t)pos, 0 );   
        ifs.seekg( 0 );   
        ifs.read( buf.data(), buf.size() );   
        return find_if( buf.begin(), buf.end(), []( unsigned char c ) {   
   return !valid[c]; } ) == buf.end();   
   }   
      
   The cool thing about that is that the array valid is initialized only   
   once and threads-sfe.   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca