update bitparser:
- now it's a subclass to configBitstreamParser - use ifstream instead of FILE - use a string to store data - simplify header parse
This commit is contained in:
parent
d3da23149c
commit
29a6f63bfe
107
bitparser.cpp
107
bitparser.cpp
|
|
@ -14,30 +14,33 @@ using namespace std;
|
|||
#endif
|
||||
|
||||
BitParser::BitParser(string filename):
|
||||
ConfigBitstreamParser(filename, ConfigBitstreamParser::BIN_MODE),
|
||||
fieldA(), part_name(), date(), hour(),
|
||||
design_name(), userID(), toolVersion(),
|
||||
file_length(0), _filename(filename)
|
||||
design_name(), userID(), toolVersion()
|
||||
{
|
||||
bit_data = NULL;
|
||||
}
|
||||
BitParser::~BitParser()
|
||||
{
|
||||
if (bit_data != NULL)
|
||||
free(bit_data);
|
||||
}
|
||||
|
||||
int BitParser::parseField(unsigned char type, FILE *fd)
|
||||
int BitParser::parseField()
|
||||
{
|
||||
int ret = 1;
|
||||
short length;
|
||||
char tmp[64];
|
||||
int pos, prev_pos;
|
||||
|
||||
/* type */
|
||||
uint8_t type;
|
||||
_fd.read((char *)&type, sizeof(uint8_t));
|
||||
|
||||
if (type != 'e') {
|
||||
fread(&length, sizeof(short), 1, fd);
|
||||
_fd.read((char*)&length, sizeof(uint16_t));
|
||||
length = ntohs(length);
|
||||
} else {
|
||||
length = 4;
|
||||
}
|
||||
fread(tmp, sizeof(unsigned char), length, fd);
|
||||
_fd.read(tmp, sizeof(uint8_t)*length);
|
||||
#ifdef DEBUG
|
||||
for (int i = 0; i < length; i++)
|
||||
printf("%c", tmp[i]);
|
||||
|
|
@ -71,61 +74,39 @@ int BitParser::parseField(unsigned char type, FILE *fd)
|
|||
hour = (tmp);
|
||||
break;
|
||||
case 'e': /* file size */
|
||||
file_length = 0;
|
||||
_bit_length = 0;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
#ifdef DEBUG
|
||||
printf("%x %x\n", 0xff & tmp[i], file_length);
|
||||
printf("%x %x\n", 0xff & tmp[i], _bit_length);
|
||||
#endif
|
||||
file_length <<= 8;
|
||||
file_length |= 0xff & tmp[i];
|
||||
_bit_length <<= 8;
|
||||
_bit_length |= 0xff & tmp[i];
|
||||
}
|
||||
#ifdef DEBUG
|
||||
printf(" %x\n", file_length);
|
||||
printf(" %x\n", _bit_length);
|
||||
#endif
|
||||
ret = 0;
|
||||
|
||||
break;
|
||||
}
|
||||
return length;
|
||||
return ret;
|
||||
|
||||
}
|
||||
int BitParser::parse()
|
||||
{
|
||||
unsigned char *tmp_data;
|
||||
FILE *fd = fopen(_filename.c_str(), "rb");
|
||||
if (fd == NULL) {
|
||||
cerr << "Error: failed to open " + _filename << endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
short length;
|
||||
unsigned char type;
|
||||
uint16_t length;
|
||||
display("parser\n\n");
|
||||
|
||||
/* Field 1 : misc header */
|
||||
fread(&length, sizeof(short), 1, fd);
|
||||
_fd.read((char*)&length, sizeof(uint16_t));
|
||||
length = ntohs(length);
|
||||
display("%d\n", length);
|
||||
fseek(fd, length, SEEK_CUR);
|
||||
fread(&length, sizeof(short), 1, fd);
|
||||
_fd.seekg(length, _fd.cur);
|
||||
|
||||
_fd.read((char*)&length, sizeof(uint16_t));
|
||||
length = ntohs(length);
|
||||
display("%d\n", length);
|
||||
|
||||
/* process all field */
|
||||
fread(&type, sizeof(unsigned char), 1, fd);
|
||||
display("Field 2 %c\n", type);
|
||||
parseField(type, fd);
|
||||
fread(&type, sizeof(unsigned char), 1, fd);
|
||||
display("Field 3 %c\n", type);
|
||||
parseField(type, fd);
|
||||
fread(&type, sizeof(unsigned char), 1, fd);
|
||||
display("Field 4 %c\n", type);
|
||||
parseField(type, fd);
|
||||
fread(&type, sizeof(unsigned char), 1, fd);
|
||||
display("Field 5 %c\n", type);
|
||||
parseField(type, fd);
|
||||
fread(&type, sizeof(unsigned char), 1, fd);
|
||||
display("Field 6 %c\n", type);
|
||||
parseField(type, fd);
|
||||
do {} while (parseField());
|
||||
|
||||
display("results\n\n");
|
||||
|
||||
|
|
@ -134,46 +115,28 @@ int BitParser::parse()
|
|||
cout << "part name : " << part_name << endl;
|
||||
cout << "date : " << date << endl;
|
||||
cout << "hour : " << hour << endl;
|
||||
cout << "file length : " << file_length << endl;
|
||||
cout << "file length : " << _bit_length << endl;
|
||||
|
||||
/* rest of the file is data to send */
|
||||
bit_data = (unsigned char *)malloc(sizeof(unsigned char) * file_length);
|
||||
if (bit_data == NULL) {
|
||||
cerr << "Error: data buffer malloc failed" << endl;
|
||||
return -1;
|
||||
}
|
||||
tmp_data = (unsigned char *)malloc(sizeof(unsigned char) * file_length);
|
||||
if (tmp_data == NULL) {
|
||||
cerr << "Error: data buffer malloc failed" << endl;
|
||||
int pos = _fd.tellg();
|
||||
cout << pos + _bit_length << endl;
|
||||
_fd.read((char *)&_bit_data[0], sizeof(uint8_t) * _bit_length);
|
||||
if (_fd.gcount() != _bit_length) {
|
||||
cerr << "Error: data read different to asked length ";
|
||||
cerr << to_string(_fd.gcount()) << " " << to_string(_bit_length) << endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int pos = ftell(fd);
|
||||
cout << pos + file_length << endl;
|
||||
size_t ret = fread(tmp_data, sizeof(unsigned char), file_length, fd);
|
||||
if (ret != (size_t)file_length) {
|
||||
cerr << "Error: data read different to asked length" << endl;
|
||||
return -1;
|
||||
for (int i = 0; i < _bit_length; i++) {
|
||||
_bit_data[i] = reverseByte(_bit_data[i]);
|
||||
}
|
||||
|
||||
for (int i = 0; i < file_length; i++) {
|
||||
bit_data[i] = reverseByte(tmp_data[i]);
|
||||
}
|
||||
|
||||
fclose(fd);
|
||||
free(tmp_data);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned char *BitParser::getData()
|
||||
uint8_t BitParser::reverseByte(uint8_t src)
|
||||
{
|
||||
return bit_data;
|
||||
}
|
||||
|
||||
unsigned char BitParser::reverseByte(unsigned char src)
|
||||
{
|
||||
unsigned char dst = 0;
|
||||
uint8_t dst = 0;
|
||||
for (int i=0; i < 8; i++) {
|
||||
dst = (dst << 1) | (src & 0x01);
|
||||
src >>= 1;
|
||||
|
|
|
|||
|
|
@ -2,17 +2,18 @@
|
|||
#define BITPARSER_H
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
class BitParser {
|
||||
#include "configBitstreamParser.hpp"
|
||||
|
||||
class BitParser: public ConfigBitstreamParser {
|
||||
public:
|
||||
BitParser(std::string filename);
|
||||
~BitParser();
|
||||
int parse();
|
||||
unsigned char *getData();
|
||||
int getLength() {return file_length;}
|
||||
|
||||
private:
|
||||
int parseField(unsigned char type, FILE *fd);
|
||||
int parseField();
|
||||
unsigned char reverseByte(unsigned char c);
|
||||
std::string fieldA;
|
||||
std::string part_name;
|
||||
|
|
@ -21,9 +22,6 @@ class BitParser {
|
|||
std::string design_name;
|
||||
std::string userID;
|
||||
std::string toolVersion;
|
||||
int file_length;
|
||||
unsigned char *bit_data;
|
||||
std::string _filename;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Reference in New Issue