U
uche
can someone please look at this code and decipher it for me....i've
been stressed by it!
=============================================
//disk.cpp
#include "disk.h"
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <fstream>
using namespace std;
bool Cdisk:
pen(const char* filename, std::ifstream& file_in)
{
file_in.open(filename, ios::binary);
if (!file_in)
{
printf("Fail to Open\n");
return false;
}//if
return true;
}//open
bool Cdisk::eof(ifstream& file_in) const
{
if (file_in.eof())
return 1;
return 0;
}//eof
unsigned char Cdisk::read(unsigned char* buffer, std::ifstream&
file_in, int& current_buff_size, int& file_index)
{
if(current_buff_size <= 0)
{
file_index = 0;
file_in.read((char*)buffer, 512);
current_buff_size =file_in.gcount();
}
else if(current_buff_size==0)
{
exit(0); //terminates the whole program!!
}
current_buff_size--;
file_index++;
return (buffer[file_index]);
}//read
int Cdisk::seek(int position, std::ifstream& file_in, Cdisk& disk)
{
int start(0), end(0);
start =disk.tell(file_in);
file_in.seekg(0, ios::end);
end =disk.tell(file_in);
int size_of_file = end - start;
file_in.seekg(0, ios::beg);
return size_of_file;
/*
long pos;
long file_beg, file_end;
long file_size;
pos = fseek(file_in, position, 0);
if (pos == 0 || pos <= 511)
return true;
return false;*/
}//seek
int Cdisk::tell(std::ifstream& file_in) const
{
int start = file_in.tellg();
return start;
}//tell
void Cdisk::close(std::ifstream& file_in)
{
file_in.close();
}//close
=========================
#include <iostream> // use this to write to the console
#include <string> // you may or may not need this...
#include "disk.h" // see disk.h for a Cdisk class template and
comments
#include "disk.cpp"
#define length 512;
using namespace std;
// Write any functions or function prototypes you need here.
// Note that you may NOT call any Linux library functions to open,
read,
// close or position the disk file -- use your Cdisk class for that.
void output(unsigned char, int, bool&);
unsigned char retrieve_bytes(unsigned char* , ifstream& , int& ,
int& , Cdisk& );
int main(int argc, const char **argv)
{
// See if there's a "filename" parameter passed to this function
// NOTE: argv[0] is the full path name of this program
// argv[1] is the first parameter, argv[2] the second, etc.
// argc is the number of parameters passed, plus 1
std::ifstream file_in;
long Size_of_file(0), start(0), finish(0), address(0);
int remainder_bytes(0), position(0);
int file_index(0), sector_index(0), current_buff_size(0);
Cdisk disk;
unsigned char buffer[512];
if (argc != 2) {
cout << "No filename parameter" << endl;
return -1;
}
const char *filename= argv[1]; // this should be the filename
// write your hex dump program here -- it MUST use your Cdisk method
// functions to open, read, seek and close the binary file, for
example,
// bool opened= disk.open(filename); (returns true or false)
if(!disk.open(filename, file_in))
{
return 1;
}//opens file
start = disk.tell(file_in);
Size_of_file= disk.seek(position, file_in, disk);
bool endoffile = disk.eof(file_in);
if( !(Size_of_file > 0) )
{
exit(0);
}
while(!endoffile || current_buff_size != 0)
{
output(disk.read(buffer, file_in, current_buff_size, file_index),
current_buff_size, endoffile);
Size_of_file--;
endoffile = disk.eof(file_in);
}
return 0;
}//end main
void output(unsigned char ret_buffer, int curr_buffer_size, bool&
endoffile)
{
int index2=0;
int addr=0;
unsigned char outBuff[16];
outBuff[curr_buffer_size] = ret_buffer;
cout<<setw(4)<<setfill('0')<<uppercase<<hex<<addr<<"0: "; //prints
mem location
for (int index = 0; index <16; index++)
{
index2++;
if (index2 <= curr_buffer_size)
cout<<hex<<setw(2)<<setfill('0')<<(int)outBuff[index];
else
cout<<" ";
cout<<" ";
}//end for
cout<<setfill(' ');
//cout<< " ");
index2 = 0;
for (int index = 0; index < 16; index ++)
{
index2++;
if (index2 <= curr_buffer_size)
{
if (outBuff[index] < 32 || outBuff[index] > 126)
printf(".");
else
cout<<outBuff[index];
}//end if
}//end for
============================================
#ifndef DISK_H
#define DISK_H
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <fstream>
class Cdisk
{
public:
// DO NOT change or add to any of these public functions, except to
make
// minor syntax or type corrections.
// You will notice that these mimic the Linux binary file utilities,
// except that these only support file reading
// open a file for reading
// returns true if successful, false otherwise
bool open(const char* filename, std::ifstream& file_in);
// return true if read head is at the end of the file
// Can also tell from the bytes returned from 'read' whether at end
of file
bool eof(std::ifstream& file_in) const;
// read into the buffer, up to 'length' bytes, return number of
bytes
// actually read
unsigned char read(unsigned char* buffer, std::ifstream& file_in,
int& current_buff_size, int& file_index); // get_byte
// position the read/write head to 'position' relative
// to the file origin -- success returns true, failure returns
false.
// You will get a failure if position is negative or >= length of
the file.
// Positions are measured with 0 at the file origin.
int seek(int position, std::ifstream& file_in, Cdisk& disk);
// return the current position of the read/write head
// 0 marks the file origin.
int tell(std::ifstream& file_in) const;
// close the file
void close(std::ifstream& file_in);
};
been stressed by it!
=============================================
//disk.cpp
#include "disk.h"
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <fstream>
using namespace std;
bool Cdisk:
{
file_in.open(filename, ios::binary);
if (!file_in)
{
printf("Fail to Open\n");
return false;
}//if
return true;
}//open
bool Cdisk::eof(ifstream& file_in) const
{
if (file_in.eof())
return 1;
return 0;
}//eof
unsigned char Cdisk::read(unsigned char* buffer, std::ifstream&
file_in, int& current_buff_size, int& file_index)
{
if(current_buff_size <= 0)
{
file_index = 0;
file_in.read((char*)buffer, 512);
current_buff_size =file_in.gcount();
}
else if(current_buff_size==0)
{
exit(0); //terminates the whole program!!
}
current_buff_size--;
file_index++;
return (buffer[file_index]);
}//read
int Cdisk::seek(int position, std::ifstream& file_in, Cdisk& disk)
{
int start(0), end(0);
start =disk.tell(file_in);
file_in.seekg(0, ios::end);
end =disk.tell(file_in);
int size_of_file = end - start;
file_in.seekg(0, ios::beg);
return size_of_file;
/*
long pos;
long file_beg, file_end;
long file_size;
pos = fseek(file_in, position, 0);
if (pos == 0 || pos <= 511)
return true;
return false;*/
}//seek
int Cdisk::tell(std::ifstream& file_in) const
{
int start = file_in.tellg();
return start;
}//tell
void Cdisk::close(std::ifstream& file_in)
{
file_in.close();
}//close
=========================
#include <iostream> // use this to write to the console
#include <string> // you may or may not need this...
#include "disk.h" // see disk.h for a Cdisk class template and
comments
#include "disk.cpp"
#define length 512;
using namespace std;
// Write any functions or function prototypes you need here.
// Note that you may NOT call any Linux library functions to open,
read,
// close or position the disk file -- use your Cdisk class for that.
void output(unsigned char, int, bool&);
unsigned char retrieve_bytes(unsigned char* , ifstream& , int& ,
int& , Cdisk& );
int main(int argc, const char **argv)
{
// See if there's a "filename" parameter passed to this function
// NOTE: argv[0] is the full path name of this program
// argv[1] is the first parameter, argv[2] the second, etc.
// argc is the number of parameters passed, plus 1
std::ifstream file_in;
long Size_of_file(0), start(0), finish(0), address(0);
int remainder_bytes(0), position(0);
int file_index(0), sector_index(0), current_buff_size(0);
Cdisk disk;
unsigned char buffer[512];
if (argc != 2) {
cout << "No filename parameter" << endl;
return -1;
}
const char *filename= argv[1]; // this should be the filename
// write your hex dump program here -- it MUST use your Cdisk method
// functions to open, read, seek and close the binary file, for
example,
// bool opened= disk.open(filename); (returns true or false)
if(!disk.open(filename, file_in))
{
return 1;
}//opens file
start = disk.tell(file_in);
Size_of_file= disk.seek(position, file_in, disk);
bool endoffile = disk.eof(file_in);
if( !(Size_of_file > 0) )
{
exit(0);
}
while(!endoffile || current_buff_size != 0)
{
output(disk.read(buffer, file_in, current_buff_size, file_index),
current_buff_size, endoffile);
Size_of_file--;
endoffile = disk.eof(file_in);
}
return 0;
}//end main
void output(unsigned char ret_buffer, int curr_buffer_size, bool&
endoffile)
{
int index2=0;
int addr=0;
unsigned char outBuff[16];
outBuff[curr_buffer_size] = ret_buffer;
cout<<setw(4)<<setfill('0')<<uppercase<<hex<<addr<<"0: "; //prints
mem location
for (int index = 0; index <16; index++)
{
index2++;
if (index2 <= curr_buffer_size)
cout<<hex<<setw(2)<<setfill('0')<<(int)outBuff[index];
else
cout<<" ";
cout<<" ";
}//end for
cout<<setfill(' ');
//cout<< " ");
index2 = 0;
for (int index = 0; index < 16; index ++)
{
index2++;
if (index2 <= curr_buffer_size)
{
if (outBuff[index] < 32 || outBuff[index] > 126)
printf(".");
else
cout<<outBuff[index];
}//end if
}//end for
============================================
#ifndef DISK_H
#define DISK_H
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <fstream>
class Cdisk
{
public:
// DO NOT change or add to any of these public functions, except to
make
// minor syntax or type corrections.
// You will notice that these mimic the Linux binary file utilities,
// except that these only support file reading
// open a file for reading
// returns true if successful, false otherwise
bool open(const char* filename, std::ifstream& file_in);
// return true if read head is at the end of the file
// Can also tell from the bytes returned from 'read' whether at end
of file
bool eof(std::ifstream& file_in) const;
// read into the buffer, up to 'length' bytes, return number of
bytes
// actually read
unsigned char read(unsigned char* buffer, std::ifstream& file_in,
int& current_buff_size, int& file_index); // get_byte
// position the read/write head to 'position' relative
// to the file origin -- success returns true, failure returns
false.
// You will get a failure if position is negative or >= length of
the file.
// Positions are measured with 0 at the file origin.
int seek(int position, std::ifstream& file_in, Cdisk& disk);
// return the current position of the read/write head
// 0 marks the file origin.
int tell(std::ifstream& file_in) const;
// close the file
void close(std::ifstream& file_in);
};