Exemplo n.º 1
0
MPQArchive::MPQArchive(const char* filename)
{
    int result = libmpq__archive_open(&mpq_a, filename, -1);
    printf("Opening %s\n", filename);
    if (result) {
        switch(result) {
            case LIBMPQ_ERROR_OPEN :
                printf("Error opening archive '%s': Does file really exist?\n", filename);
                break;
            case LIBMPQ_ERROR_FORMAT :            /* bad file format */
                printf("Error opening archive '%s': Bad file format\n", filename);
                break;
            case LIBMPQ_ERROR_SEEK :         /* seeking in file failed */
                printf("Error opening archive '%s': Seeking in file failed\n", filename);
                break;
            case LIBMPQ_ERROR_READ :              /* Read error in archive */
                printf("Error opening archive '%s': Read error in archive\n", filename);
                break;
            case LIBMPQ_ERROR_MALLOC :               /* maybe not enough memory? :) */
                printf("Error opening archive '%s': Maybe not enough memory\n", filename);
                break;
            default:
                printf("Error opening archive '%s': Unknown error\n", filename);
                break;
        }
        return;
    }
    gOpenArchives.push_front(this);
}
Exemplo n.º 2
0
bool MPQArchiveImpl::load( const MPQArchive* self, const std::string& filename )
{
  filename_ = filename;
  
  int32_t good = libmpq__archive_open( &archive_, filename.c_str(), -1 );
  
  // Sanity check
  if ( good < 0 )
    return false;

  return true;
}
Exemplo n.º 3
0
//------------------------------------------------------------------------------
libmpq__off_t MpqHandler::getListFile( const std::string &filename,
                                       mpq_archive_s **mpq_arc,
                                       uint8_t **buffer ) const {
  int32_t err = 0;
  std::string mpq_file = _dataDirectory + std::string( "/" ) + filename;

  try {
    // open mpq archive
    err = libmpq__archive_open( mpq_arc, mpq_file.c_str(), -1 );
    if ( err != LIBMPQ_SUCCESS ) {
      throw( std::string( "libmpq__archive_open failed" ) );
    }

    // get file number of the list file which contains all names in the archive
    uint32_t file_num = 0;
    err = libmpq__file_number( *mpq_arc, LIBMPQ_LISTFILE_NAME, &file_num );
    if ( err != LIBMPQ_SUCCESS ) {
      throw( std::string( "libmpq__file_number failed" ) );
    }

    // read file from archive...get file size and reserve space
    libmpq__off_t file_size;
    err = libmpq__file_unpacked_size( *mpq_arc, file_num, &file_size );
    if ( err != LIBMPQ_SUCCESS ) {
      throw( std::string( "libmpq__file_unpacked_size failed" ) );
    }

    // read file from archive to buffer
    *buffer = new uint8_t[file_size];
    err = libmpq__file_read( *mpq_arc, file_num, *buffer, file_size, NULL );
    if ( err != LIBMPQ_SUCCESS ) {
      throw( std::string( "libmpq__file_read failed" ) );
    }

    return file_size;
  } catch( std::string err_msg ) {
    // if an archive has been opened close it
    if ( *mpq_arc ) {
      libmpq__archive_close( *mpq_arc );
    }

    delete [] *buffer;

    // quit application
    std::stringstream ss;
    ss << "Cannot open " << mpq_file << " " << err_msg;
    quitApp( ss.str() );
  }

  return -1;
}
Exemplo n.º 4
0
/* this function shows some archive information. */
int mpq_info__archive_info(char *program_name, char *mpq_filename, unsigned int number, unsigned int count) {

	/* some common variables. */
	int result;
	off_t size_packed    = 0;
	off_t size_unpacked  = 0;
	off_t offset         = 0;
	unsigned int version = 0;
	unsigned int files   = 0;
	mpq_archive_s *mpq_archive;

	/* open the mpq-archive. */
	if ((result = libmpq__archive_open(&mpq_archive, mpq_filename, -1)) < 0) {

		/* open archive failed. */
		NOTICE("archive number:			%i/%i\n", number, count);
		NOTICE("archive name:			%s\n", mpq_filename);
		NOTICE("archive type:			no mpq archive\n");

	} else {

		/* fetch some required information. */
		libmpq__archive_version(mpq_archive, &version);
		libmpq__archive_offset(mpq_archive, &offset);
		libmpq__archive_files(mpq_archive, &files);
		libmpq__archive_size_packed(mpq_archive, &size_packed);
		libmpq__archive_size_unpacked(mpq_archive, &size_unpacked);

		/* open archive was successful, show information. */
		NOTICE("archive number:			%i/%i\n", number, count);
		NOTICE("archive name:			%s\n", mpq_filename);
		NOTICE("archive version:		%i\n", version);
		NOTICE("archive offset:			%" OFFTSTR "\n", offset);
		NOTICE("archive files:			%i\n", files);
		NOTICE("archive packed size:		%" OFFTSTR "\n", size_packed);
		NOTICE("archive unpacked size:		%" OFFTSTR "\n", size_unpacked);
		NOTICE("archive compression ratio:	%.2f\n", (100 - ((float)size_packed / (float)size_unpacked * 100)));

		libmpq__archive_close(mpq_archive);
	}

	/* if multiple archives were given, continue with next one. */
	if (number < count) {
		NOTICE("\n-- next archive --\n\n");
	}

	/* if no error was found, return zero. */
	return 0;
}