"Fossies" - the Fresh Open Source Software Archive

Member "zutils-1.10/zcatgrep.cc" (5 Jan 2021, 1938 Bytes) of package /linux/privat/zutils-1.10.tar.lz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "zcatgrep.cc" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes reports: 1.10-rc1_vs_1.10 or 1.9_vs_1.10.

    1 /* Common code for zcat and zgrep
    2    Copyright (C) 2010-2021 Antonio Diaz Diaz.
    3 
    4    This program is free software: you can redistribute it and/or modify
    5    it under the terms of the GNU General Public License as published by
    6    the Free Software Foundation, either version 2 of the License, or
    7    (at your option) any later version.
    8 
    9    This program is distributed in the hope that it will be useful,
   10    but WITHOUT ANY WARRANTY; without even the implied warranty of
   11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12    GNU General Public License for more details.
   13 
   14    You should have received a copy of the GNU General Public License
   15    along with this program.  If not, see <http://www.gnu.org/licenses/>.
   16 */
   17 
   18 #ifndef O_BINARY
   19 #define O_BINARY 0
   20 #endif
   21 
   22 
   23 int simple_extension_index( const std::string & name )
   24   {
   25   for( int i = 0; i < num_formats; ++i )
   26     {
   27     const std::string ext( simple_extensions[i] );
   28     if( name.size() > ext.size() &&
   29         name.compare( name.size() - ext.size(), ext.size(), ext ) == 0 )
   30       return i;
   31     }
   32   return -1;
   33   }
   34 
   35 
   36 int open_instream( std::string & input_filename, const bool search,
   37                    const bool no_messages = false )
   38   {
   39   int infd = open( input_filename.c_str(), O_RDONLY | O_BINARY );
   40   if( infd < 0 )
   41     {
   42     const int saved_errno = errno;
   43     if( search && simple_extension_index( input_filename ) < 0 )
   44       {
   45       for( int i = 0; i < num_formats; ++i )
   46         if( enabled_format( format_order[i] ) )
   47           {
   48           const std::string name( input_filename +
   49                                   simple_extensions[format_order[i]] );
   50           infd = open( name.c_str(), O_RDONLY | O_BINARY );
   51           if( infd >= 0 ) { input_filename = name; break; }
   52           }
   53       }
   54     if( infd < 0 && !no_messages )
   55       show_file_error( input_filename.c_str(), "Can't open input file",
   56                        saved_errno );
   57     }
   58   return infd;
   59   }