filerw.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 RTGUI_USING_DFS_FILERW
  17. #include <dfs_posix.h>
  18. /* standard file read/write */
  19. struct rtgui_filerw_stdio
  20. {
  21. /* inherit from rtgui_filerw */
  22. struct rtgui_filerw parent;
  23. int fd;
  24. rt_bool_t eof;
  25. };
  26. static int stdio_seek(struct rtgui_filerw *context, rt_off_t offset, int whence)
  27. {
  28. struct rtgui_filerw_stdio* stdio_filerw = (struct rtgui_filerw_stdio *)context;
  29. int stdio_whence[3] = {SEEK_SET, SEEK_CUR, SEEK_END};
  30. if (whence < RTGUI_FILE_SEEK_SET || whence > RTGUI_FILE_SEEK_END)
  31. {
  32. return -1;
  33. }
  34. return lseek(stdio_filerw->fd, offset, stdio_whence[whence]);
  35. }
  36. static int stdio_read(struct rtgui_filerw *context, void *ptr, rt_size_t size, rt_size_t maxnum)
  37. {
  38. int result;
  39. struct rtgui_filerw_stdio* stdio_filerw = (struct rtgui_filerw_stdio *)context;
  40. /* end of file */
  41. if (stdio_filerw->eof == RT_TRUE) return -1;
  42. result = read(stdio_filerw->fd, ptr, size * maxnum);
  43. if (result == 0) stdio_filerw->eof = RT_TRUE;
  44. return result;
  45. }
  46. static int stdio_write(struct rtgui_filerw *context, const void *ptr, rt_size_t size, rt_size_t num)
  47. {
  48. struct rtgui_filerw_stdio* stdio_filerw = (struct rtgui_filerw_stdio *)context;
  49. return write(stdio_filerw->fd, (char*)ptr, size * num);
  50. }
  51. static int stdio_tell(struct rtgui_filerw* context)
  52. {
  53. struct rtgui_filerw_stdio* stdio_filerw = (struct rtgui_filerw_stdio *)context;
  54. return lseek(stdio_filerw->fd, 0, SEEK_CUR);
  55. }
  56. static int stdio_eof(struct rtgui_filerw* context)
  57. {
  58. int result;
  59. struct rtgui_filerw_stdio* stdio_filerw = (struct rtgui_filerw_stdio *)context;
  60. if (stdio_filerw->eof == RT_TRUE) result = 1;
  61. else result = -1;
  62. return result;
  63. }
  64. static int stdio_close(struct rtgui_filerw *context)
  65. {
  66. struct rtgui_filerw_stdio* stdio_filerw = (struct rtgui_filerw_stdio *)context;
  67. if (stdio_filerw)
  68. {
  69. close(stdio_filerw->fd);
  70. rtgui_free(stdio_filerw);
  71. return 0;
  72. }
  73. return -1;
  74. }
  75. #elif defined(RTGUI_USING_STDIO_FILERW)
  76. #include <stdio.h>
  77. /* standard file read/write */
  78. struct rtgui_filerw_stdio
  79. {
  80. /* inherit from rtgui_filerw */
  81. struct rtgui_filerw parent;
  82. FILE* fp;
  83. };
  84. static int stdio_seek(struct rtgui_filerw *context, rt_off_t offset, int whence)
  85. {
  86. struct rtgui_filerw_stdio* stdio_filerw = (struct rtgui_filerw_stdio *)context;
  87. int stdio_whence[3] = {SEEK_SET, SEEK_CUR, SEEK_END};
  88. if (whence < RTGUI_FILE_SEEK_SET || whence > RTGUI_FILE_SEEK_END)
  89. {
  90. return -1;
  91. }
  92. if (fseek(stdio_filerw->fp, offset, stdio_whence[whence]) == 0)
  93. {
  94. return ftell(stdio_filerw->fp);
  95. }
  96. return -1;
  97. }
  98. static int stdio_read(struct rtgui_filerw *context, void *ptr, rt_size_t size, rt_size_t maxnum)
  99. {
  100. size_t nread;
  101. struct rtgui_filerw_stdio* stdio_filerw = (struct rtgui_filerw_stdio *)context;
  102. nread = fread(ptr, size, maxnum, stdio_filerw->fp);
  103. if (nread == 0 && ferror(stdio_filerw->fp))
  104. {
  105. return -1;
  106. }
  107. return nread;
  108. }
  109. static int stdio_write(struct rtgui_filerw *context, const void *ptr, rt_size_t size, rt_size_t num)
  110. {
  111. size_t nwrote;
  112. struct rtgui_filerw_stdio* stdio_filerw = (struct rtgui_filerw_stdio *)context;
  113. nwrote = fwrite(ptr, size, num, stdio_filerw->fp);
  114. if ( nwrote == 0 && ferror(stdio_filerw->fp) )
  115. {
  116. return -1;
  117. }
  118. return nwrote;
  119. }
  120. static int stdio_tell(struct rtgui_filerw* context)
  121. {
  122. struct rtgui_filerw_stdio* stdio_filerw = (struct rtgui_filerw_stdio *)context;
  123. return ftell(stdio_filerw->fp);
  124. }
  125. static int stdio_eof(struct rtgui_filerw* context)
  126. {
  127. struct rtgui_filerw_stdio* stdio_filerw = (struct rtgui_filerw_stdio *)context;
  128. return feof(stdio_filerw->fp);
  129. }
  130. static int stdio_close(struct rtgui_filerw *context)
  131. {
  132. struct rtgui_filerw_stdio* stdio_filerw = (struct rtgui_filerw_stdio *)context;
  133. if (stdio_filerw)
  134. {
  135. fclose(stdio_filerw->fp);
  136. rtgui_free(stdio_filerw);
  137. return 0;
  138. }
  139. return -1;
  140. }
  141. #endif
  142. /* memory file read/write */
  143. struct rtgui_filerw_mem
  144. {
  145. /* inherit from rtgui_filerw */
  146. struct rtgui_filerw parent;
  147. const rt_uint8_t *mem_base, *mem_position, *mem_end;
  148. };
  149. static int mem_seek(struct rtgui_filerw *context, rt_off_t offset, int whence)
  150. {
  151. const rt_uint8_t* newpos;
  152. struct rtgui_filerw_mem* mem = (struct rtgui_filerw_mem*)context;
  153. RT_ASSERT(mem != RT_NULL);
  154. switch (whence) {
  155. case RTGUI_FILE_SEEK_SET:
  156. newpos = mem->mem_base + offset;
  157. break;
  158. case RTGUI_FILE_SEEK_CUR:
  159. newpos = mem->mem_position + offset;
  160. break;
  161. case RTGUI_FILE_SEEK_END:
  162. newpos = mem->mem_end + offset;
  163. break;
  164. default:
  165. return -1;
  166. }
  167. if ( newpos < mem->mem_base )
  168. newpos = mem->mem_base;
  169. if ( newpos > mem->mem_end )
  170. newpos = mem->mem_end;
  171. mem->mem_position = newpos;
  172. return mem->mem_position- mem->mem_base;
  173. }
  174. static int mem_read(struct rtgui_filerw *context, void *ptr, rt_size_t size, rt_size_t maxnum)
  175. {
  176. int total_bytes;
  177. int mem_available;
  178. struct rtgui_filerw_mem* mem = (struct rtgui_filerw_mem*)context;
  179. total_bytes = (maxnum * size);
  180. if ( (maxnum <= 0) || (size <= 0) || ((total_bytes / maxnum) != size) )
  181. {
  182. return -1;
  183. }
  184. mem_available = mem->mem_end - mem->mem_position;
  185. if (total_bytes > mem_available)
  186. total_bytes = mem_available;
  187. rt_memcpy(ptr, mem->mem_position, total_bytes);
  188. mem->mem_position += total_bytes;
  189. return (total_bytes / size);
  190. }
  191. static int mem_write(struct rtgui_filerw *context, const void *ptr, rt_size_t size, rt_size_t num)
  192. {
  193. return 0; /* not support memory write */
  194. }
  195. static int mem_tell(struct rtgui_filerw* context)
  196. {
  197. struct rtgui_filerw_mem* mem = (struct rtgui_filerw_mem*)context;
  198. return mem->mem_position - mem->mem_base;
  199. }
  200. static int mem_eof(struct rtgui_filerw* context)
  201. {
  202. struct rtgui_filerw_mem* mem = (struct rtgui_filerw_mem*)context;
  203. return mem->mem_position >= mem->mem_end;
  204. }
  205. static int mem_close(struct rtgui_filerw *context)
  206. {
  207. struct rtgui_filerw_mem* mem = (struct rtgui_filerw_mem*)context;
  208. if (mem != RT_NULL)
  209. {
  210. rtgui_free(mem);
  211. return 0;
  212. }
  213. return -1;
  214. }
  215. const rt_uint8_t* rtgui_filerw_mem_getdata(struct rtgui_filerw* context)
  216. {
  217. struct rtgui_filerw_mem* mem = (struct rtgui_filerw_mem*)context;
  218. /* check whether it's a memory filerw */
  219. if (mem->parent.read != mem_read) return RT_NULL;
  220. return mem->mem_base;
  221. }
  222. /* file read/write public interface */
  223. #ifdef RTGUI_USING_DFS_FILERW
  224. static int parse_mode(const char *mode)
  225. {
  226. int f=0;
  227. for (;;)
  228. {
  229. switch (*mode)
  230. {
  231. case 0: return f;
  232. case 'b': break;
  233. case 'r': f=O_RDONLY; break;
  234. case 'w': f=O_WRONLY|O_CREAT|O_TRUNC; break;
  235. case 'a': f=O_WRONLY|O_CREAT|O_APPEND; break;
  236. case '+': f=(f&(~O_WRONLY))|O_RDWR; break;
  237. }
  238. ++mode;
  239. }
  240. }
  241. struct rtgui_filerw* rtgui_filerw_create_file(const char* filename, const char* mode)
  242. {
  243. int fd;
  244. struct rtgui_filerw_stdio *rw;
  245. RT_ASSERT(filename != RT_NULL);
  246. rw = RT_NULL;
  247. fd = open(filename, parse_mode(mode), 0);
  248. if ( fd >= 0 )
  249. {
  250. rw = (struct rtgui_filerw_stdio*) rtgui_malloc(sizeof(struct rtgui_filerw_stdio));
  251. if (rw != RT_NULL)
  252. {
  253. rw->parent.seek = stdio_seek;
  254. rw->parent.read = stdio_read;
  255. rw->parent.write = stdio_write;
  256. rw->parent.tell = stdio_tell;
  257. rw->parent.close = stdio_close;
  258. rw->parent.eof = stdio_eof;
  259. rw->fd = fd;
  260. rw->eof = RT_FALSE;
  261. }
  262. }
  263. return &(rw->parent);
  264. }
  265. #elif defined(RTGUI_USING_STDIO_FILERW)
  266. struct rtgui_filerw* rtgui_filerw_create_file(const char* filename, const char* mode)
  267. {
  268. FILE *fp;
  269. struct rtgui_filerw_stdio *rw;
  270. RT_ASSERT(filename != RT_NULL);
  271. rw = RT_NULL;
  272. fp = fopen(filename, mode);
  273. if ( fp != NULL )
  274. {
  275. rw = (struct rtgui_filerw_stdio*) rtgui_malloc(sizeof(struct rtgui_filerw_stdio));
  276. if (rw != RT_NULL)
  277. {
  278. rw->parent.seek = stdio_seek;
  279. rw->parent.read = stdio_read;
  280. rw->parent.write = stdio_write;
  281. rw->parent.tell = stdio_tell;
  282. rw->parent.close = stdio_close;
  283. rw->parent.eof = stdio_eof;
  284. rw->fp = fp;
  285. }
  286. }
  287. return &(rw->parent);
  288. }
  289. #endif
  290. struct rtgui_filerw* rtgui_filerw_create_mem(const rt_uint8_t* mem, rt_size_t size)
  291. {
  292. struct rtgui_filerw_mem* rw;
  293. RT_ASSERT(mem != RT_NULL);
  294. rw = (struct rtgui_filerw_mem*) rtgui_malloc(sizeof(struct rtgui_filerw_mem));
  295. if (rw != RT_NULL)
  296. {
  297. rw->parent.seek = mem_seek;
  298. rw->parent.read = mem_read;
  299. rw->parent.write = mem_write;
  300. rw->parent.tell = mem_tell;
  301. rw->parent.eof = mem_eof;
  302. rw->parent.close = mem_close;
  303. rw->mem_base = mem;
  304. rw->mem_position = mem;
  305. rw->mem_end = mem + size;
  306. }
  307. return &(rw->parent);
  308. }
  309. int rtgui_filerw_seek(struct rtgui_filerw* context, rt_off_t offset, int whence)
  310. {
  311. RT_ASSERT(context != RT_NULL);
  312. return context->seek(context, offset, whence);
  313. }
  314. int rtgui_filerw_read(struct rtgui_filerw* context, void* buffer, rt_size_t size, rt_size_t count)
  315. {
  316. RT_ASSERT(context != RT_NULL);
  317. return context->read(context, buffer, size, count);
  318. }
  319. int rtgui_filerw_write(struct rtgui_filerw* context, const void* buffer, rt_size_t size, rt_size_t count)
  320. {
  321. RT_ASSERT(context != RT_NULL);
  322. return context->write(context, buffer, size, count);
  323. }
  324. int rtgui_filerw_eof (struct rtgui_filerw* context)
  325. {
  326. RT_ASSERT(context != RT_NULL);
  327. return context->eof(context);
  328. }
  329. int rtgui_filerw_tell(struct rtgui_filerw* context)
  330. {
  331. RT_ASSERT(context != RT_NULL);
  332. return context->tell(context);
  333. }
  334. int rtgui_filerw_close(struct rtgui_filerw* context)
  335. {
  336. int result;
  337. RT_ASSERT(context != RT_NULL);
  338. /* close context */
  339. result = context->close(context);
  340. if (result != 0)
  341. {
  342. /* close file failed */
  343. return -1;
  344. }
  345. return 0;
  346. }