/* Common code for zcmp and zdiff
   Copyright (C) 2010-2026 Antonio Diaz Diaz.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef O_BINARY
#define O_BINARY 0
#endif


int open_instream( const std::string & input_filename )
  {
  const int infd = open( input_filename.c_str(), O_RDONLY | O_BINARY );
  if( infd < 0 )
    show_file_error( input_filename.c_str(), "Can't open input file", errno );
  return infd;
  }


int open_other_instream( std::string & name )
  {
  const int eindex = extension_index( name );	// search extension
  if( eindex >= 0 && enabled_format( -1 ) )	// open uncompressed version
    {
    std::string s( name, 0, name.size() - std::strlen( extension_from( eindex ) ) );
    s += extension_to( eindex );
    const int infd = open( s.c_str(), O_RDONLY | O_BINARY );
    if( infd >= 0 ) { name = s; return infd; }
    }
  const int eformat = extension_format( eindex );
  for( int i = 0; i < num_formats; ++i )	// search compressed version
    {
    const int format_index = format_order[i];
    if( eformat != format_index && enabled_format( format_index ) )
      {
      std::string s( name, 0, name.size() - std::strlen( extension_from( eindex ) ) );
      s += simple_extensions[format_index];
      const int infd = open( s.c_str(), O_RDONLY | O_BINARY );
      if( infd >= 0 ) { name = s; return infd; }
      }
    }
  return -1;
  }


void parse_format_types2( const std::string & arg, const char * const pn,
                          int format_index[2] )
  {
  const unsigned i = std::min( arg.find( ',' ), arg.size() );
  if( i != std::min( arg.rfind( ',' ), arg.size() ) )
    { show_option_error( arg.c_str(), "Too many formats in", pn );
      std::exit( 1 ); }
  format_index[0] =
    ( i > 0 ) ? parse_format_type( arg.substr( 0, i ), pn ) : -1;
  format_index[1] =
    ( i + 1 < arg.size() ) ? parse_format_type( arg.substr( i + 1 ), pn ) : -1;
  }


bool check_identical( const std::string & name1, const std::string & name2 )
  {
  if( name1 == name2 ) return true;
  struct stat st1, st2;
  if( stat( name1.c_str(), &st1 ) || stat( name2.c_str(), &st2 ) ) return false;
  return ( st1.st_ino == st2.st_ino && st1.st_dev == st2.st_dev );
  }
