config.txt文件的内容
output_address0:0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88
output_address1:0x55 0x66 0x77 0x88 0x99 0xAA 0xBB 0xCC
output_address2:0x99 0xAA 0xBB 0xCC 0xDD 0xEE 0xFF 0x00
output_address3:0xDD 0xEE 0xFF 0x00 0x11 0x22 0x33 0x44
output_length0:0x11 0x22 0x33 0x44
output_length1:0x55 0x66 0x77 0x88
output_length2:0x99 0xAA 0xBB 0xCC
output_length3:0xDD 0xEE 0xFF 0x00
input_address:0xAA 0x55 0xAA 0x55 0xAA 0x55 0xAA 0x55
pad:0x11 0x22 0x33 0x44
row:0x12 0x34 0x56 0x78
col:0x9A 0xBC 0xDE 0xF0
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#define DATA_FILE_COUNT 2
#define DATA_FILE_OUTPUT_COUNT (32*1024*1024)
#define DATA_FILE_CONFIG 256
typedef unsigned char uint8_t;
typedef unsigned short int uint16_t;
typedef unsigned int uint32_t;
typedef signed int int32_t;
typedef unsigned long long int uint64_t;
struct WR_File
{
uint64_t out_addr[4];
uint32_t out_length[4];
uint64_t input_addr;
uint32_t pad;
uint32_t row;
uint32_t col;
uint32_t CRC_check_code;
uint32_t file_offset[DATA_FILE_COUNT+1];
};
int32_t ReadFile(char *FileAddress);
int32_t ModifyFile(void);
uint32_t CalCrc(const char *buf, uint64_t len);
uint32_t WriteFile(char *DataAddress);
int ReadConfigFile(void);
void StructToArray(void);
void StringConvertNumber(char **StringArray);
#define CONFIGPATH "../Config/config.txt"
#define OUTPUTFILE "../OutputFile/data_output.bin"
char data_file[] = "../Data/data.bin";
char data_inst_file[] = "../Data/data_inst.bin";
char *data_file_p[DATA_FILE_COUNT] = {data_file,data_inst_file};
uint8_t output_file[DATA_FILE_OUTPUT_COUNT];
char *data_buff = NULL;
uint64_t data_file_size = 0;
int32_t data_file_fd = 0;
uint64_t data_file_all_size = 0;
uint8_t StringArrayNum[8]; //string convert data
struct WR_File FileConfig;
/*
*description:this function is read data file, and store data of the pointer data_buff
*/
int32_t ReadFile(char *FileAddress){
int32_t lseek_return_begin = 0;
int32_t read_return_value = 0;
data_file_fd = open(FileAddress, O_RDONLY);
if(-1 == data_file_fd)
{
printf("open %s file fail!\r\n",FileAddress);
close(data_file_fd);
return -1;
}
data_file_size = lseek(data_file_fd, 0, SEEK_END);
if(-1 == data_file_size)
{
printf("get %s file size error!\r\n",FileAddress);
close(data_file_fd);
return -1;
}
lseek_return_begin = lseek(data_file_fd, 0, SEEK_SET);
if(-1 == lseek_return_begin)
{
printf("lseek %s file pointer error!\r\n",FileAddress);
close(data_file_fd);
return -1;
}
data_buff = (char *)malloc(data_file_size * sizeof(char));
if(NULL == data_buff)
{
printf("malloc %s file data space fail!\r\n",FileAddress);
close(data_file_fd);
return -1;
}
read_return_value = read(data_file_fd, data_buff, data_file_size);
if(-1 == read_return_value)
{
printf("read %s file error!\r\n",FileAddress);
close(data_file_fd);
free(data_buff);
data_buff = NULL;
return -1;
}
return 0;
}
/*
*description:this function is modify data file
*/
int32_t ModifyFile(void){
uint32_t count = 0;
uint32_t status = 0;
FileConfig.file_offset[0] = DATA_FILE_CONFIG;
data_file_all_size += DATA_FILE_CONFIG;
memset(output_file,0,DATA_FILE_OUTPUT_COUNT);
for(count = 0;count < DATA_FILE_COUNT;count++){
status = ReadFile(data_file_p[count]);
if(-1 == status){
printf("read %s file error!\r\n",data_file_p[count]);
close(data_file_fd);
free(data_buff);
data_buff = NULL;
return -1;
}
FileConfig.file_offset[count+1] = FileConfig.file_offset[count] + data_file_size;
// printf("FileConfig.file_offset[%d] = %08x\n",count,FileConfig.file_offset[count]);
memcpy(&output_file[data_file_all_size],data_buff,data_file_size);
data_file_all_size += data_file_size;
close(data_file_fd);
free(data_buff);
data_buff = NULL;
data_file_size = 0;
}
// printf("FileConfig.file_offset[%d] = %08x\n",count,FileConfig.file_offset[count]);
FileConfig.CRC_check_code = CalCrc(&output_file[DATA_FILE_CONFIG],data_file_all_size);
// printf("FileConfig.CRC_check_code = %08x\n",FileConfig.CRC_check_code);
StructToArray();
status = WriteFile(output_file);
if(-1 == status){
printf("write file error!\r\n");
return -1;
}
return 0;
}
/*
*description:CRC check code
*/
uint32_t CalCrc(const char *buf, uint64_t len)
{
uint64_t byte;
uint8_t k;
uint16_t ACC,TOPBIT;
uint32_t remainder = 0x0000;
TOPBIT = 0x8000;
for (byte = 0; byte < len; ++byte)
{
ACC = buf[byte];
remainder ^= (ACC <<8);
for (k = 8; k > 0; --k)
{
if (remainder & TOPBIT)
{
remainder = (remainder << 1) ^0x8005;
}
else
{
remainder = (remainder << 1);
}
}
}
remainder=remainder^0x0000;
return remainder;
}
/*
*description:write data to file
*/
uint32_t WriteFile(char *DataAddress){
uint32_t DO_fd = 0;
uint64_t WriteCount = 0,WriteOffset = 0;
DO_fd = open(OUTPUTFILE, O_WRONLY | O_CREAT | O_TRUNC, 0777); //write and creat need limits to rights
if(-1 == DO_fd)
{
printf("open %s file fail!\r\n",OUTPUTFILE);
close(DO_fd);
return -1;
}
while(1){
if(0 == (WriteCount == write(DO_fd, (DataAddress+WriteOffset), data_file_all_size - WriteOffset))){
printf("write data to file success!\r\n");
break;
}
WriteOffset += WriteCount;
WriteCount = 0;
}
close(DO_fd);
return 0;
}
/*
*description:read configuration of file
*/
int ReadConfigFile(void){
uint32_t config_file_fd = 0,config_cmd_count = 0,temp_int = 0,struct_array = 0,odd_num = 1;
uint64_t config_file_size = 0,lseek_return_begin = 0,read_return_value = 0;
char *config_buff = NULL;
char *p = NULL;
char *s = NULL;
char *CmdAndConfigData[DATA_FILE_CONFIG];
config_file_fd = open(CONFIGPATH, O_RDONLY);
if(-1 == config_file_fd)
{
printf("open %s file fail!\r\n",CONFIGPATH);
close(config_file_fd);
return -1;
}
config_file_size = lseek(config_file_fd, 0, SEEK_END);
if(-1 == config_file_size)
{
printf("get %s file size error!\r\n",CONFIGPATH);
close(config_file_fd);
return -1;
}
lseek_return_begin = lseek(config_file_fd, 0, SEEK_SET);
if(-1 == lseek_return_begin)
{
printf("lseek %s file pointer error!\r\n",CONFIGPATH);
close(config_file_fd);
return -1;
}
config_buff = (char *)malloc(config_file_size * sizeof(char));
if(NULL == config_buff)
{
printf("malloc %s file data space fail!\r\n",CONFIGPATH);
close(config_file_fd);
return -1;
}
read_return_value = read(config_file_fd, config_buff, config_file_size);
if(-1 == read_return_value)
{
printf("read %s file error!\r\n",CONFIGPATH);
close(config_file_fd);
free(config_buff);
config_buff = NULL;
return -1;
}
memset(CmdAndConfigData, 0, DATA_FILE_CONFIG);
s = config_buff;
p = strsep(&s, ":\n");
while(p != NULL)
{
CmdAndConfigData[config_cmd_count] = p;
// printf("CmdAndConfigData[%d] = %s\n",config_cmd_count,CmdAndConfigData[config_cmd_count]);
// printf("%s\n",s);
p = strsep(&s, ":\n");
config_cmd_count++;
// printf("*p = %s\r\n",p);
// printf("config_cmd_count = %d\r\n",config_cmd_count);
}
for(struct_array = 0;struct_array < 4;struct_array++){
StringConvertNumber(CmdAndConfigData + odd_num);
for(temp_int = 0;temp_int < 8;temp_int++){
// printf("StringArrayNum[%d] = %02x\n",temp_int,StringArrayNum[temp_int]);
FileConfig.out_addr[struct_array] += ((uint64_t)StringArrayNum[temp_int] << ((7 - temp_int) * 8));
}
odd_num += 2;
// printf("FileConfig.out_addr[%d] = %016llx\n",struct_array,FileConfig.out_addr[struct_array]);
}
for(struct_array = 0;struct_array < 4;struct_array++){
StringConvertNumber(CmdAndConfigData + odd_num);
for(temp_int = 0;temp_int < 4;temp_int++){
// printf("StringArrayNum[%d] = %02x\n",temp_int,StringArrayNum[temp_int]);
FileConfig.out_length[struct_array] += ((uint32_t)StringArrayNum[temp_int] << ((3 - temp_int) * 8));
}
odd_num += 2;
// printf("FileConfig.out_length[%d] = %08x\n",struct_array,FileConfig.out_length[struct_array]);
}
StringConvertNumber(CmdAndConfigData + odd_num);
for(temp_int = 0;temp_int < 8;temp_int++){
// printf("StringArrayNum[%d] = %02x\n",temp_int,StringArrayNum[temp_int]);
FileConfig.input_addr += ((uint64_t)StringArrayNum[temp_int] << ((7 - temp_int) * 8));
}
odd_num += 2;
// printf("FileConfig.input_addr = %016llx\n",FileConfig.input_addr);
StringConvertNumber(CmdAndConfigData + odd_num);
for(temp_int = 0;temp_int < 4;temp_int++){
// printf("StringArrayNum[%d] = %02x\n",temp_int,StringArrayNum[temp_int]);
FileConfig.pad += ((uint32_t)StringArrayNum[temp_int] << ((3 - temp_int) * 8));
}
odd_num += 2;
// printf("FileConfig.pad = %08x\n",FileConfig.pad);
StringConvertNumber(CmdAndConfigData + odd_num);
for(temp_int = 0;temp_int < 4;temp_int++){
// printf("StringArrayNum[%d] = %02x\n",temp_int,StringArrayNum[temp_int]);
FileConfig.row += ((uint32_t)StringArrayNum[temp_int] << ((3 - temp_int) * 8));
}
odd_num += 2;
// printf("FileConfig.row = %08x\n",FileConfig.row);
StringConvertNumber(CmdAndConfigData + odd_num);
for(temp_int = 0;temp_int < 4;temp_int++){
// printf("StringArrayNum[%d] = %02x\n",temp_int,StringArrayNum[temp_int]);
FileConfig.col += ((uint32_t)StringArrayNum[temp_int] << ((3 - temp_int) * 8));
}
odd_num += 2;
// printf("FileConfig.col = %08x\n",FileConfig.col);
close(config_file_fd);
free(config_buff);
config_buff = NULL;
return 0;
}
/*
*description:this function is assign the value of the structure to the array
*/
void StructToArray(void){
uint32_t temp_array = 0,output_file_offset = 0,temp_int_count = 1;
uint8_t temp_int = 0;
for(temp_array = 0;temp_array < 4;temp_array++){
// printf("FileConfig.out_addr[%d] = %016llx\n",temp_array,FileConfig.out_addr[temp_array]);
temp_int_count = 1;
for(temp_int = output_file_offset;temp_int < output_file_offset + 8;temp_int++){
output_file[temp_int] = 0xFF & (FileConfig.out_addr[temp_array] >> (64 - temp_int_count * 8));
// printf("output_file[%d] = %02x\n",temp_int,output_file[temp_int]);
temp_int_count++;
}
output_file_offset += 8;
}
for(temp_array = 0;temp_array < 4;temp_array++){
// printf("FileConfig.out_length[%d] = %08x\n",temp_array,FileConfig.out_length[temp_array]);
temp_int_count = 1;
for(temp_int = output_file_offset;temp_int < output_file_offset + 4;temp_int++){
output_file[temp_int] = 0xFF & (FileConfig.out_length[temp_array] >> (32 - temp_int_count * 8));
// printf("output_file[%d] = %02x\n",temp_int,output_file[temp_int]);
temp_int_count++;
}
output_file_offset += 4;
}
// printf("FileConfig.input_addr = %016llx\n",FileConfig.input_addr);
temp_int_count = 1;
for(temp_int = output_file_offset;temp_int < output_file_offset + 8;temp_int++){
output_file[temp_int] = 0xFF & (FileConfig.input_addr >> (64 - temp_int_count * 8));
// printf("output_file[%d] = %02x\n",temp_int,output_file[temp_int]);
temp_int_count++;
}
output_file_offset += 8;
// printf("FileConfig.pad = %08x\n",FileConfig.pad);
temp_int_count = 1;
for(temp_int = output_file_offset;temp_int < output_file_offset + 4;temp_int++){
output_file[temp_int] = 0xFF & (FileConfig.pad >> (32 - temp_int_count * 8));
// printf("output_file[%d] = %02x\n",temp_int,output_file[temp_int]);
temp_int_count++;
}
output_file_offset += 4;
// printf("FileConfig.row = %08x\n",FileConfig.row);
temp_int_count = 1;
for(temp_int = output_file_offset;temp_int < output_file_offset + 4;temp_int++){
output_file[temp_int] = 0xFF & (FileConfig.row >> (32 - temp_int_count * 8));
// printf("output_file[%d] = %02x\n",temp_int,output_file[temp_int]);
temp_int_count++;
}
output_file_offset += 4;
// printf("FileConfig.col = %08x\n",FileConfig.col);
temp_int_count = 1;
for(temp_int = output_file_offset;temp_int < output_file_offset + 4;temp_int++){
output_file[temp_int] = 0xFF & (FileConfig.col >> (32 - temp_int_count * 8));
// printf("output_file[%d] = %02x\n",temp_int,output_file[temp_int]);
temp_int_count++;
}
output_file_offset += 4;
// printf("FileConfig.CRC_check_code = %08x\n",FileConfig.CRC_check_code);
temp_int_count = 1;
for(temp_int = output_file_offset;temp_int < output_file_offset + 4;temp_int++){
output_file[temp_int] = 0xFF & (FileConfig.CRC_check_code >> (32 - temp_int_count * 8));
// printf("output_file[%d] = %02x\n",temp_int,output_file[temp_int]);
temp_int_count++;
}
output_file_offset += 4;
for(temp_array = 0;temp_array < DATA_FILE_COUNT+1;temp_array++){
// printf("FileConfig.file_offset[%d] = %08x\n",temp_array,FileConfig.file_offset[temp_array]);
temp_int_count = 1;
for(temp_int = output_file_offset;temp_int < output_file_offset + 4;temp_int++){
output_file[temp_int] = 0xFF & (FileConfig.file_offset[temp_array] >> (32 - temp_int_count * 8));
// printf("output_file[%d] = %02x\n",temp_int,output_file[temp_int]);
temp_int_count++;
}
output_file_offset += 4;
}
}
/*
*description:This function converts a string to a corresponding number
*parameter:
*/
void StringConvertNumber(char **StringArray){
char *point = NULL;
uint8_t config_count = 0;
char *string_one_data[8];
memset(&StringArrayNum,0,8);
memset(string_one_data,0,8);
point = strsep(StringArray, " \n");
while(point != NULL)
{
string_one_data[config_count] = point;
// printf("string_one_data[%d] = %s\n",config_count,string_one_data[config_count]);
StringArrayNum[config_count] = strtol(point,NULL,16);;
// printf("StringArrayNum[%d] = %d\n",config_count,StringArrayNum[config_count]);
point = strsep(StringArray, " \n");
config_count++;
}
}
int main(){
int status = 0;
status = ReadConfigFile();
if(-1 == status){
printf("open the config file fail!\r\n");
return -1;
}
status = ModifyFile();
if(-1 == status){
printf("modify file fail!\r\n");
return -1;
}
return 0;
}
这里面有读取字符串转换为数字的函数,也有指针传递的内容