| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- #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<read_len; i++)
- {
- if(read_buf[i] == '\n')
- {
- rows++;
- }
- }
- }while((read_len == sizeof(read_buf)) && read_len);
-
- rt_file_close(pfile, 0);
- return rows;
- }
- //每行的长度一致
- int csv_write_row(CSV_RW* csv, char* row_str, u16 row)
- {
- int res;
-
- res = csv_file_update_row(csv, row_str, row);
- return res;
- }
- int float_format(char* buf, u8 buf_size, char* fmt, float data, u8 end)
- {
- char str[16] = {0};
- u8 size,i;
- uint8_t str_len;
-
- sprintf(str, "%4.3f", data);
- str_len = strlen(str);
- if(str_len > buf_size)
- return 0;
-
- if(str_len < buf_size)
- {
- strncpy(buf, str, str_len);
- size = buf_size - str_len;
- for (i=0; i<size; i++)
- strcat(buf, " ");
- }
- else
- strncpy(buf, str, str_len);
- if(!end)
- strcat(buf, ",");
- return strlen(buf);
- }
- void float_format_row(char* fmt, u8 item_space, char* buf, float* data, u8 data_num)
- {
- u8 i;
-
- for(i=0; i<data_num; i++)
- {
- float_format(buf+i*item_space, item_space, fmt, data[i], (i>=(data_num-1)));
- }
- //rt_printf("格式化内容:%s", buf);
- }
- #endif
|