filerw.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. #if 0
  194. struct rtgui_filerw_mem* mem = (struct rtgui_filerw_mem*)context;
  195. if ((mem->mem_position + (num * size)) > mem->mem_end)
  196. {
  197. num = (mem->mem_end - mem->mem_position)/size;
  198. }
  199. rt_memcpy(mem->mem_position, ptr, num*size);
  200. mem->mem_position += num*size;
  201. return num;
  202. #else
  203. return 0; /* not support memory write */
  204. #endif
  205. }
  206. static int mem_tell(struct rtgui_filerw* context)
  207. {
  208. struct rtgui_filerw_mem* mem = (struct rtgui_filerw_mem*)context;
  209. return mem->mem_position - mem->mem_base;
  210. }
  211. static int mem_eof(struct rtgui_filerw* context)
  212. {
  213. struct rtgui_filerw_mem* mem = (struct rtgui_filerw_mem*)context;
  214. return mem->mem_position >= mem->mem_end;
  215. }
  216. static int mem_close(struct rtgui_filerw *context)
  217. {
  218. struct rtgui_filerw_mem* mem = (struct rtgui_filerw_mem*)context;
  219. if (mem != RT_NULL)
  220. {
  221. rtgui_free(mem);
  222. return 0;
  223. }
  224. return -1;
  225. }
  226. const rt_uint8_t* rtgui_filerw_mem_getdata(struct rtgui_filerw* context)
  227. {
  228. struct rtgui_filerw_mem* mem = (struct rtgui_filerw_mem*)context;
  229. /* check whether it's a memory filerw */
  230. if (mem->parent.read != mem_read) return RT_NULL;
  231. return mem->mem_base;
  232. }
  233. /* file read/write public interface */
  234. #ifdef RTGUI_USING_DFS_FILERW
  235. static int parse_mode(const char *mode)
  236. {
  237. int f=0;
  238. for (;;)
  239. {
  240. switch (*mode)
  241. {
  242. case 0: return f;
  243. case 'b': break;
  244. case 'r': f=O_RDONLY; break;
  245. case 'w': f=O_WRONLY|O_CREAT|O_TRUNC; break;
  246. case 'a': f=O_WRONLY|O_CREAT|O_APPEND; break;
  247. case '+': f=(f&(~O_WRONLY))|O_RDWR; break;
  248. }
  249. ++mode;
  250. }
  251. }
  252. struct rtgui_filerw* rtgui_filerw_create_file(const char* filename, const char* mode)
  253. {
  254. int fd;
  255. struct rtgui_filerw_stdio *rw;
  256. RT_ASSERT(filename != RT_NULL);
  257. rw = RT_NULL;
  258. fd = open(filename, parse_mode(mode), 0);
  259. if ( fd >= 0 )
  260. {
  261. rw = (struct rtgui_filerw_stdio*) rtgui_malloc(sizeof(struct rtgui_filerw_stdio));
  262. if (rw != RT_NULL)
  263. {
  264. rw->parent.seek = stdio_seek;
  265. rw->parent.read = stdio_read;
  266. rw->parent.write = stdio_write;
  267. rw->parent.tell = stdio_tell;
  268. rw->parent.close = stdio_close;
  269. rw->parent.eof = stdio_eof;
  270. rw->fd = fd;
  271. rw->eof = RT_FALSE;
  272. }
  273. }
  274. return &(rw->parent);
  275. }
  276. #elif defined(RTGUI_USING_STDIO_FILERW)
  277. struct rtgui_filerw* rtgui_filerw_create_file(const char* filename, const char* mode)
  278. {
  279. FILE *fp;
  280. struct rtgui_filerw_stdio *rw;
  281. RT_ASSERT(filename != RT_NULL);
  282. rw = RT_NULL;
  283. fp = fopen(filename, mode);
  284. if ( fp != NULL )
  285. {
  286. rw = (struct rtgui_filerw_stdio*) rtgui_malloc(sizeof(struct rtgui_filerw_stdio));
  287. if (rw != RT_NULL)
  288. {
  289. rw->parent.seek = stdio_seek;
  290. rw->parent.read = stdio_read;
  291. rw->parent.write = stdio_write;
  292. rw->parent.tell = stdio_tell;
  293. rw->parent.close = stdio_close;
  294. rw->parent.eof = stdio_eof;
  295. rw->fp = fp;
  296. }
  297. }
  298. return &(rw->parent);
  299. }
  300. #endif
  301. struct rtgui_filerw* rtgui_filerw_create_mem(const rt_uint8_t* mem, rt_size_t size)
  302. {
  303. struct rtgui_filerw_mem* rw;
  304. RT_ASSERT(mem != RT_NULL);
  305. rw = (struct rtgui_filerw_mem*) rtgui_malloc(sizeof(struct rtgui_filerw_mem));
  306. if (rw != RT_NULL)
  307. {
  308. rw->parent.seek = mem_seek;
  309. rw->parent.read = mem_read;
  310. rw->parent.write = mem_write;
  311. rw->parent.tell = mem_tell;
  312. rw->parent.eof = mem_eof;
  313. rw->parent.close = mem_close;
  314. rw->mem_base = mem;
  315. rw->mem_position = mem;
  316. rw->mem_end = mem + size;
  317. }
  318. return &(rw->parent);
  319. }
  320. int rtgui_filerw_seek(struct rtgui_filerw* context, rt_off_t offset, int whence)
  321. {
  322. RT_ASSERT(context != RT_NULL);
  323. return context->seek(context, offset, whence);
  324. }
  325. int rtgui_filerw_read(struct rtgui_filerw* context, void* buffer, rt_size_t size, rt_size_t count)
  326. {
  327. RT_ASSERT(context != RT_NULL);
  328. return context->read(context, buffer, size, count);
  329. }
  330. int rtgui_filerw_write(struct rtgui_filerw* context, const void* buffer, rt_size_t size, rt_size_t count)
  331. {
  332. RT_ASSERT(context != RT_NULL);
  333. return context->write(context, buffer, size, count);
  334. }
  335. int rtgui_filerw_eof (struct rtgui_filerw* context)
  336. {
  337. RT_ASSERT(context != RT_NULL);
  338. return context->eof(context);
  339. }
  340. int rtgui_filerw_tell(struct rtgui_filerw* context)
  341. {
  342. RT_ASSERT(context != RT_NULL);
  343. return context->tell(context);
  344. }
  345. int rtgui_filerw_close(struct rtgui_filerw* context)
  346. {
  347. int result;
  348. RT_ASSERT(context != RT_NULL);
  349. /* close context */
  350. result = context->close(context);
  351. if (result != 0)
  352. {
  353. /* close file failed */
  354. return -1;
  355. }
  356. return 0;
  357. }