csv_rw.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #include "head.h"
  2. #ifdef CAN_SLAVE_BOARD
  3. int csv_rw_init(CSV_RW* csv,
  4. u8 item_space,
  5. u16 row,
  6. u16 column,
  7. char* path)
  8. {
  9. // u32 size = (row*column*item_space + column*item_space);
  10. // memset(csv, 0, sizeof(CSV_RW));
  11. // csv->rw_buf = rt_malloc(size);
  12. // if(!csv->rw_buf)
  13. // {
  14. // return -1;
  15. // }
  16. strncpy(csv->path, path, strlen(path) > sizeof(csv->path) ? sizeof(csv->path) : strlen(path));
  17. csv->item_space = item_space;
  18. csv->row = row;
  19. csv->column = column;
  20. csv->cur_row = get_flie_rows(csv->path);
  21. return 0;
  22. }
  23. #if 0
  24. int csv_file_update_item(CSV_RW* csv, char* str, u16 row, u16 column)
  25. {
  26. u32 size;
  27. char *buf, *write_buf;
  28. // struct file * pfile;
  29. int fd;
  30. u32 pos = (row*column*csv->item_space + column*csv->item_space);
  31. // if(csv->rw_buf)
  32. {
  33. // size = strlen(csv->rw_buf);
  34. // buf = rt_malloc(size);
  35. // if(!buf)
  36. // {
  37. // return -1;
  38. // }
  39. write_buf = rt_malloc(csv->item_space+1);
  40. if(!write_buf)
  41. {
  42. return -1;
  43. }
  44. // pfile = rt_file_open(csv->path,O_CREAT|O_RDWR,0);
  45. fd = open(csv->path, O_CREAT|O_RDWR);
  46. // if(IS_ERR(pfile))
  47. if(IS_ERR(fd))
  48. {
  49. // rt_free(buf);
  50. rt_free(write_buf);
  51. return -3;
  52. }
  53. // if(rt_file_write(pfile,new_rcd, len, &pos) != len)
  54. if(file_write_offset(fd, (u8*)str, csv->item_space, pos) != csv->item_space)
  55. {
  56. rt_printf("CSV写入失败");
  57. close(fd);
  58. return -1;
  59. }
  60. rt_free(write_buf);
  61. close(fd);
  62. rt_printf("CSV写入成功");
  63. return 0;
  64. }
  65. return -1;
  66. }
  67. #endif
  68. int csv_file_update_row(CSV_RW* csv, char* str, u16 row)
  69. {
  70. struct file * pfile;
  71. loff_t pos = strlen(str)*row;
  72. rt_printf("pos = %d\r\n", pos);
  73. rt_printf("strlen = %d\r\n", strlen(str));
  74. rt_printf("row = %d\r\n", row);
  75. pfile = rt_file_open(csv->path,O_CREAT|O_RDWR,0);
  76. if(IS_ERR(pfile))
  77. {
  78. return -3;
  79. }
  80. if(rt_file_write(pfile,str, strlen(str), &pos) != strlen(str))
  81. {
  82. rt_printf("CSV写入失败\r\n");
  83. rt_file_close(pfile, 0);
  84. return -1;
  85. }
  86. rt_printf("CSV写入成功\r\n");
  87. rt_file_close(pfile, 0);
  88. return 0;
  89. }
  90. #if 0
  91. int csv_write_item(CSV_RW* csv, char* item_str, u16 row, u16 column)
  92. {
  93. int res;
  94. // if(csv->rw_buf)
  95. {
  96. //strncpy();
  97. res = csv_file_update_item(csv, item_str, row, line);
  98. return res;
  99. }
  100. // return -1;
  101. }
  102. #endif
  103. u32 get_flie_rows(char* path)
  104. {
  105. struct file * pfile;
  106. char read_buf[64] = {0};
  107. int read_len;
  108. loff_t pos = 0;
  109. u32 i = 0, rows = 0;
  110. pfile = rt_file_open(path, O_RDWR,0);
  111. if(IS_ERR(pfile))
  112. {
  113. rt_printf("打开%s文件失败\r\n", path);
  114. return 0;
  115. }
  116. do
  117. {
  118. memset(read_buf, 0, sizeof(read_buf));
  119. read_len = rt_file_read(pfile, read_buf, sizeof(read_buf), &pos);
  120. pos += read_len;
  121. for(i=0; i<read_len; i++)
  122. {
  123. if(read_buf[i] == '\n')
  124. {
  125. rows++;
  126. }
  127. }
  128. }while((read_len == sizeof(read_buf)) && read_len);
  129. rt_file_close(pfile, 0);
  130. return rows;
  131. }
  132. //每行的长度一致
  133. int csv_write_row(CSV_RW* csv, char* row_str, u16 row)
  134. {
  135. int res;
  136. res = csv_file_update_row(csv, row_str, row);
  137. return res;
  138. }
  139. int float_format(char* buf, u8 buf_size, char* fmt, float data, u8 end)
  140. {
  141. char str[16] = {0};
  142. u8 size,i;
  143. uint8_t str_len;
  144. sprintf(str, "%4.3f", data);
  145. str_len = strlen(str);
  146. if(str_len > buf_size)
  147. return 0;
  148. if(str_len < buf_size)
  149. {
  150. strncpy(buf, str, str_len);
  151. size = buf_size - str_len;
  152. for (i=0; i<size; i++)
  153. strcat(buf, " ");
  154. }
  155. else
  156. strncpy(buf, str, str_len);
  157. if(!end)
  158. strcat(buf, ",");
  159. return strlen(buf);
  160. }
  161. void float_format_row(char* fmt, u8 item_space, char* buf, float* data, u8 data_num)
  162. {
  163. u8 i;
  164. for(i=0; i<data_num; i++)
  165. {
  166. float_format(buf+i*item_space, item_space, fmt, data[i], (i>=(data_num-1)));
  167. }
  168. //rt_printf("格式化内容:%s", buf);
  169. }
  170. #endif