【VC开源代码栏目提醒】:网学会员VC开源代码为您提供ibitstr.cpp参考,解决您在ibitstr.cpp学习中工作中的难题,参考学习。
/* ibitstr.cpp
Input bitstream class implementation
Changes by Jeff Tsay:
01/26/96 : Modified for Windows file handles.
11/29/96 : Added syncword detection for compatibility with streams produced
by DALET. Changed at the request of Wilfried Solbach, thanks for the
donation in advance! However Layer I MPP files playback jerkily.
03/09/97 : Added a routine to read layer III side info. Also not mentioned
previously are several routines that allow seeking in a stream.
04/14/97 : Moved the side info routine to the layer III decoder object.
Revamped the seeking mechanism and also included better syncword detection.
Moved the original file reading mechanisms back for better
portability, and used John Fehr's implementations of the seeking routines
for non-Win32 file reading. */
/*
* @(#) ibitstream.cc 1.8, last edit: 6/15/94 16:51:45
* @(#) Copyright (C) 1993, 1994 Tobias Bading (bading@cs.tu-berlin.de)
* @(#) Berlin University of Technology
*
* 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, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* Changes from version 1.1 to 1.2:
* - third argument in open syscall added
* - minor bug in get_header() fixed
*/
#define __WIN32__
#ifdef __WIN32__
#include <windows.h>
#else
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#endif // __WIN32__
#ifndef GUI
#include <iostream.h>
#endif // GUI
#include "all.h"
#include "ibitstr.h"
#include "header.h"
#ifdef DAMN_INTEL_BYTE_ORDER
#define swap_int32(int_32) (((int_32) << 24) | (((int_32) << 8) & 0x00ff0000) | \
(((int_32) >> 8) & 0x0000ff00) | ((int_32) >> 24))
#endif // DAMN_INTEL_BYTE_ORDER
Ibitstream::Ibitstream(const char *filename)
{
#ifdef __WIN32__
SECURITY_ATTRIBUTES security;
security.nLength = sizeof(SECURITY_ATTRIBUTES);
security.lpSecurityDescriptor = NULL;
security.bInheritHandle = FALSE;
fd = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ,
&security, OPEN_EXISTING, NULL, NULL);
if (fd == INVALID_HANDLE_VALUE) {
char bad_file_msg[256];
lstrcpy(bad_file_msg, "Error opening file: ");
#ifdef WIN32GUI
MessageBox(NULL, lstrcat(bad_file_msg, filename) , "Invalid handle", MB_OK);
#else
cerr << "Error opening file: " << filename << endl;
#endif // WIN32GUI
return;
}
#else
// copy any arguments you need, open the file, and check for error
if ((fd = open(filename, O_RDONLY)) == -1)
{
#ifndef GUI
cerr << "Error opening file: " << filename << endl;
#endif
return;
}
#endif // __WIN32__
wordpointer = buffer;
bitindex = 0;
// Seeking variables
current_frame_number = -1;
#ifdef SEEK_STOP
last_frame_number = -1;
#endif
copy = false;
}
Ibitstream::Ibitstream(const Ibitstream &s0)
{
int32 i;
fd = s0.fd;
for (i=0; i<bufferintsize; i++)
buffer[i] = s0.buffer[i];
framesize = s0.framesize;
wordpointer = buffer + (s0.wordpointer - s0.buffer);
bitindex = s0.bitindex;
syncword = s0.syncword;
single_ch_mode = s0.single_ch_mode;
current_frame_number = s0.current_frame_number;
#ifdef SEEK_STOP
last_frame_number = s0.last_frame_number;
#endif
copy = true;
}
// Dummy constructor
Ibitstream::Ibitstream()
{
fd = 0;
wordpointer = buffer;
bitindex = 0;
current_frame_number = -1;
#ifdef SEEK_STOP
last_frame_number = -1;
#endif
copy = false;
}
void Ibitstream::reset()
{
SetFilePointer(fd, 0, NULL, FILE_BEGIN);
wordpointer = buffer;
bitindex = 0;
current_frame_number = -1;
#ifdef SEEK_STOP
last_frame_number = -1;
#endif
copy = false;
}
Ibitstream::~Ibitstream()
{
// close the file
#ifdef __WIN32__
if (fd && !copy) CloseHandle(fd);
#else
if (fd && !copy) close(fd);
#endif
}
Ibitstream &Ibitstream::operator = (const Ibitstream &s0)
{
int32 i;
fd = s0.fd;
for (i=0; i<bufferintsize; i++)
buffer[i] = s0.buffer[i];
framesize = s0.framesize;
wordpointer = buffer + (s0.wordpointer - s0.buffer);
bitindex = s0.bitindex;
syncword = s0.syncword