#include "head.h" #ifdef CAN_SLAVE_BOARD int csv_rw_init(CSV_RW* csv, u8 item_space, u16 row, u16 column, char* path) { // u32 size = (row*column*item_space + column*item_space); // memset(csv, 0, sizeof(CSV_RW)); // csv->rw_buf = rt_malloc(size); // if(!csv->rw_buf) // { // return -1; // } strncpy(csv->path, path, strlen(path) > sizeof(csv->path) ? sizeof(csv->path) : strlen(path)); csv->item_space = item_space; csv->row = row; csv->column = column; csv->cur_row = get_flie_rows(csv->path); return 0; } #if 0 int csv_file_update_item(CSV_RW* csv, char* str, u16 row, u16 column) { u32 size; char *buf, *write_buf; // struct file * pfile; int fd; u32 pos = (row*column*csv->item_space + column*csv->item_space); // if(csv->rw_buf) { // size = strlen(csv->rw_buf); // buf = rt_malloc(size); // if(!buf) // { // return -1; // } write_buf = rt_malloc(csv->item_space+1); if(!write_buf) { return -1; } // pfile = rt_file_open(csv->path,O_CREAT|O_RDWR,0); fd = open(csv->path, O_CREAT|O_RDWR); // if(IS_ERR(pfile)) if(IS_ERR(fd)) { // rt_free(buf); rt_free(write_buf); return -3; } // if(rt_file_write(pfile,new_rcd, len, &pos) != len) if(file_write_offset(fd, (u8*)str, csv->item_space, pos) != csv->item_space) { rt_printf("CSV写入失败"); close(fd); return -1; } rt_free(write_buf); close(fd); rt_printf("CSV写入成功"); return 0; } return -1; } #endif int csv_file_update_row(CSV_RW* csv, char* str, u16 row) { struct file * pfile; loff_t pos = strlen(str)*row; rt_printf("pos = %d\r\n", pos); rt_printf("strlen = %d\r\n", strlen(str)); rt_printf("row = %d\r\n", row); pfile = rt_file_open(csv->path,O_CREAT|O_RDWR,0); if(IS_ERR(pfile)) { return -3; } if(rt_file_write(pfile,str, strlen(str), &pos) != strlen(str)) { rt_printf("CSV写入失败\r\n"); rt_file_close(pfile, 0); return -1; } rt_printf("CSV写入成功\r\n"); rt_file_close(pfile, 0); return 0; } #if 0 int csv_write_item(CSV_RW* csv, char* item_str, u16 row, u16 column) { int res; // if(csv->rw_buf) { //strncpy(); res = csv_file_update_item(csv, item_str, row, line); return res; } // return -1; } #endif u32 get_flie_rows(char* path) { struct file * pfile; char read_buf[64] = {0}; int read_len; loff_t pos = 0; u32 i = 0, rows = 0; pfile = rt_file_open(path, O_RDWR,0); if(IS_ERR(pfile)) { rt_printf("打开%s文件失败\r\n", path); return 0; } do { memset(read_buf, 0, sizeof(read_buf)); read_len = rt_file_read(pfile, read_buf, sizeof(read_buf), &pos); pos += read_len; for(i=0; i buf_size) return 0; if(str_len < buf_size) { strncpy(buf, str, str_len); size = buf_size - str_len; for (i=0; i=(data_num-1))); } //rt_printf("格式化内容:%s", buf); } #endif