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