filerw.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * File : filerw.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2009-10-16 Bernard first version
  13. */
  14. #include <rtgui/filerw.h>
  15. #include <rtgui/rtgui_system.h>
  16. #ifdef RT_USING_STDIO_FILERW
  17. #include <stdio.h>
  18. /* standard file read/write */
  19. struct rtgui_filerw_stdio
  20. {
  21. /* inherit from rtgui_filerw */
  22. struct rtgui_filerw parent;
  23. FILE* fp;
  24. };
  25. static int stdio_seek(struct rtgui_filerw *context, rt_off_t offset, int whence)
  26. {
  27. struct rtgui_filerw_stdio* stdio_filerw = (struct rtgui_filerw_stdio *)context;
  28. int stdio_whence[3] = {SEEK_SET, SEEK_CUR, SEEK_END};
  29. if (whence < RTGUI_FILE_SEEK_SET || whence > RTGUI_FILE_SEEK_END)
  30. {
  31. return -1;
  32. }
  33. if (fseek(stdio_filerw->fp, offset, stdio_whence[whence]) == 0)
  34. {
  35. return ftell(stdio_filerw->fp);
  36. }
  37. return -1;
  38. }
  39. static int stdio_read(struct rtgui_filerw *context, void *ptr, rt_size_t size, rt_size_t maxnum)
  40. {
  41. size_t nread;
  42. struct rtgui_filerw_stdio* stdio_filerw = (struct rtgui_filerw_stdio *)context;
  43. nread = fread(ptr, size, maxnum, stdio_filerw->fp);
  44. if (nread == 0 && ferror(stdio_filerw->fp))
  45. {
  46. return -1;
  47. }
  48. return nread;
  49. }
  50. static int stdio_write(struct rtgui_filerw *context, const void *ptr, rt_size_t size, rt_size_t num)
  51. {
  52. size_t nwrote;
  53. struct rtgui_filerw_stdio* stdio_filerw = (struct rtgui_filerw_stdio *)context;
  54. nwrote = fwrite(ptr, size, num, stdio_filerw->fp);
  55. if ( nwrote == 0 && ferror(stdio_filerw->fp) )
  56. {
  57. return -1;
  58. }
  59. return nwrote;
  60. }
  61. int stdio_tell(struct rtgui_filerw* context)
  62. {
  63. struct rtgui_filerw_stdio* stdio_filerw = (struct rtgui_filerw_stdio *)context;
  64. return ftell(stdio_filerw->fp);
  65. }
  66. int stdio_eof(struct rtgui_filerw* context)
  67. {
  68. struct rtgui_filerw_stdio* stdio_filerw = (struct rtgui_filerw_stdio *)context;
  69. return feof(stdio_filerw->fp);
  70. }
  71. static int stdio_close(struct rtgui_filerw *context)
  72. {
  73. struct rtgui_filerw_stdio* stdio_filerw = (struct rtgui_filerw_stdio *)context;
  74. if (stdio_filerw)
  75. {
  76. fclose(stdio_filerw->fp);
  77. rtgui_free(stdio_filerw);
  78. return 0;
  79. }
  80. return -1;
  81. }
  82. #endif
  83. /* memory file read/write */
  84. struct rtgui_filerw_mem
  85. {
  86. /* inherit from rtgui_filerw */
  87. struct rtgui_filerw parent;
  88. rt_uint8_t *mem_base, *mem_position, *mem_end;
  89. };
  90. static int mem_seek(struct rtgui_filerw *context, rt_off_t offset, int whence)
  91. {
  92. rt_uint8_t* newpos;
  93. struct rtgui_filerw_mem* mem = (struct rtgui_filerw_mem*)context;
  94. RT_ASSERT(mem != RT_NULL);
  95. switch (whence) {
  96. case RTGUI_FILE_SEEK_SET:
  97. newpos = mem->mem_base + offset;
  98. break;
  99. case RTGUI_FILE_SEEK_CUR:
  100. newpos = mem->mem_position + offset;
  101. break;
  102. case RTGUI_FILE_SEEK_END:
  103. newpos = mem->mem_end + offset;
  104. break;
  105. default:
  106. return -1;
  107. }
  108. if ( newpos < mem->mem_base )
  109. newpos = mem->mem_base;
  110. if ( newpos > mem->mem_end )
  111. newpos = mem->mem_end;
  112. mem->mem_position = newpos;
  113. return mem->mem_position- mem->mem_base;
  114. }
  115. static int mem_read(struct rtgui_filerw *context, void *ptr, rt_size_t size, rt_size_t maxnum)
  116. {
  117. int total_bytes;
  118. int mem_available;
  119. struct rtgui_filerw_mem* mem = (struct rtgui_filerw_mem*)context;
  120. total_bytes = (maxnum * size);
  121. if ( (maxnum <= 0) || (size <= 0) || ((total_bytes / maxnum) != size) )
  122. {
  123. return -1;
  124. }
  125. mem_available = mem->mem_end - mem->mem_position;
  126. if (total_bytes > mem_available)
  127. total_bytes = mem_available;
  128. rt_memcpy(ptr, mem->mem_position, total_bytes);
  129. mem->mem_position += total_bytes;
  130. return (total_bytes / size);
  131. }
  132. static int mem_write(struct rtgui_filerw *context, const void *ptr, rt_size_t size, rt_size_t num)
  133. {
  134. struct rtgui_filerw_mem* mem = (struct rtgui_filerw_mem*)context;
  135. if ((mem->mem_position + (num * size)) > mem->mem_end)
  136. {
  137. num = (mem->mem_end - mem->mem_position)/size;
  138. }
  139. rt_memcpy(mem->mem_position, ptr, num*size);
  140. mem->mem_position += num*size;
  141. return num;
  142. }
  143. static int mem_tell(struct rtgui_filerw* context)
  144. {
  145. struct rtgui_filerw_mem* mem = (struct rtgui_filerw_mem*)context;
  146. return mem->mem_position - mem->mem_base;
  147. }
  148. static int mem_eof(struct rtgui_filerw* context)
  149. {
  150. struct rtgui_filerw_mem* mem = (struct rtgui_filerw_mem*)context;
  151. return mem->mem_position >= mem->mem_end;
  152. }
  153. static int mem_close(struct rtgui_filerw *context)
  154. {
  155. struct rtgui_filerw_mem* mem = (struct rtgui_filerw_mem*)context;
  156. if (mem != RT_NULL)
  157. {
  158. rtgui_free(mem);
  159. return 0;
  160. }
  161. return -1;
  162. }
  163. rt_uint8_t* rtgui_filerw_mem_getdata(struct rtgui_filerw* context)
  164. {
  165. struct rtgui_filerw_mem* mem = (struct rtgui_filerw_mem*)context;
  166. /* check whether it's a memory filerw */
  167. if (mem->parent.read != mem_read) return RT_NULL;
  168. return mem->mem_base;
  169. }
  170. /* file read/write public interface */
  171. #ifdef RT_USING_STDIO_FILERW
  172. struct rtgui_filerw* rtgui_filerw_create_file(const char* filename, const char* mode)
  173. {
  174. FILE *fp;
  175. struct rtgui_filerw_stdio *rw;
  176. RT_ASSERT(filename != RT_NULL);
  177. rw = RT_NULL;
  178. fp = fopen(filename, mode);
  179. if ( fp != NULL )
  180. {
  181. rw = (struct rtgui_filerw_stdio*) rtgui_malloc(sizeof(struct rtgui_filerw_stdio));
  182. if (rw != RT_NULL)
  183. {
  184. rw->parent.seek = stdio_seek;
  185. rw->parent.read = stdio_read;
  186. rw->parent.write = stdio_write;
  187. rw->parent.tell = stdio_tell;
  188. rw->parent.close = stdio_close;
  189. rw->parent.eof = stdio_eof;
  190. rw->fp = fp;
  191. }
  192. }
  193. return &(rw->parent);
  194. }
  195. #endif
  196. struct rtgui_filerw* rtgui_filerw_create_mem(rt_uint8_t* mem, rt_size_t size)
  197. {
  198. struct rtgui_filerw_mem* rw;
  199. RT_ASSERT(mem != RT_NULL);
  200. rw = (struct rtgui_filerw_mem*) rtgui_malloc(sizeof(struct rtgui_filerw_mem));
  201. if (rw != RT_NULL)
  202. {
  203. rw->parent.seek = mem_seek;
  204. rw->parent.read = mem_read;
  205. rw->parent.write = mem_write;
  206. rw->parent.tell = mem_tell;
  207. rw->parent.eof = mem_eof;
  208. rw->parent.close = mem_close;
  209. rw->mem_base = mem;
  210. rw->mem_position = mem;
  211. rw->mem_end = mem + size;
  212. }
  213. return &(rw->parent);
  214. }
  215. int rtgui_filerw_seek(struct rtgui_filerw* context, rt_off_t offset, int whence)
  216. {
  217. RT_ASSERT(context != RT_NULL);
  218. return context->seek(context, offset, whence);
  219. }
  220. int rtgui_filerw_read(struct rtgui_filerw* context, void* buffer, rt_size_t size, rt_size_t count)
  221. {
  222. RT_ASSERT(context != RT_NULL);
  223. return context->read(context, buffer, size, count);
  224. }
  225. int rtgui_filerw_write(struct rtgui_filerw* context, const void* buffer, rt_size_t size, rt_size_t count)
  226. {
  227. RT_ASSERT(context != RT_NULL);
  228. return context->write(context, buffer, size, count);
  229. }
  230. int rtgui_filerw_eof (struct rtgui_filerw* context)
  231. {
  232. RT_ASSERT(context != RT_NULL);
  233. return context->eof(context);
  234. }
  235. int rtgui_filerw_tell(struct rtgui_filerw* context)
  236. {
  237. RT_ASSERT(context != RT_NULL);
  238. return context->tell(context);
  239. }
  240. int rtgui_filerw_close(struct rtgui_filerw* context)
  241. {
  242. int result;
  243. RT_ASSERT(context != RT_NULL);
  244. /* close context */
  245. result = context->close(context);
  246. if (result != 0)
  247. {
  248. /* close file failed */
  249. return -1;
  250. }
  251. return 0;
  252. }