filerw.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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/rtgui_system.h>
  15. #include <rtgui/filerw.h>
  16. #ifdef RTGUI_USING_DFS_FILERW
  17. /* standard file read/write */
  18. struct rtgui_filerw_stdio
  19. {
  20. /* inherit from rtgui_filerw */
  21. struct rtgui_filerw parent;
  22. int fd;
  23. rt_bool_t eof;
  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. return lseek(stdio_filerw->fd, offset, stdio_whence[whence]);
  34. }
  35. static int stdio_read(struct rtgui_filerw *context, void *ptr, rt_size_t size, rt_size_t maxnum)
  36. {
  37. int result;
  38. struct rtgui_filerw_stdio *stdio_filerw = (struct rtgui_filerw_stdio *)context;
  39. /* end of file */
  40. if (stdio_filerw->eof == RT_TRUE) return -1;
  41. result = read(stdio_filerw->fd, ptr, size * maxnum);
  42. if (result == 0) stdio_filerw->eof = RT_TRUE;
  43. return result;
  44. }
  45. static int stdio_write(struct rtgui_filerw *context, const void *ptr, rt_size_t size, rt_size_t num)
  46. {
  47. struct rtgui_filerw_stdio *stdio_filerw = (struct rtgui_filerw_stdio *)context;
  48. return write(stdio_filerw->fd, (char *)ptr, size * num);
  49. }
  50. static int stdio_tell(struct rtgui_filerw *context)
  51. {
  52. struct rtgui_filerw_stdio *stdio_filerw = (struct rtgui_filerw_stdio *)context;
  53. return lseek(stdio_filerw->fd, 0, SEEK_CUR);
  54. }
  55. static int stdio_eof(struct rtgui_filerw *context)
  56. {
  57. int result;
  58. struct rtgui_filerw_stdio *stdio_filerw = (struct rtgui_filerw_stdio *)context;
  59. if (stdio_filerw->eof == RT_TRUE) result = 1;
  60. else result = -1;
  61. return result;
  62. }
  63. static int stdio_close(struct rtgui_filerw *context)
  64. {
  65. struct rtgui_filerw_stdio *stdio_filerw = (struct rtgui_filerw_stdio *)context;
  66. if (stdio_filerw)
  67. {
  68. close(stdio_filerw->fd);
  69. rtgui_free(stdio_filerw);
  70. return 0;
  71. }
  72. return -1;
  73. }
  74. #endif
  75. /* memory file read/write */
  76. struct rtgui_filerw_mem
  77. {
  78. /* inherit from rtgui_filerw */
  79. struct rtgui_filerw parent;
  80. const rt_uint8_t *mem_base, *mem_position, *mem_end;
  81. };
  82. static int mem_seek(struct rtgui_filerw *context, rt_off_t offset, int whence)
  83. {
  84. const rt_uint8_t *newpos;
  85. struct rtgui_filerw_mem *mem = (struct rtgui_filerw_mem *)context;
  86. RT_ASSERT(mem != RT_NULL);
  87. switch (whence)
  88. {
  89. case RTGUI_FILE_SEEK_SET:
  90. newpos = mem->mem_base + offset;
  91. break;
  92. case RTGUI_FILE_SEEK_CUR:
  93. newpos = mem->mem_position + offset;
  94. break;
  95. case RTGUI_FILE_SEEK_END:
  96. newpos = mem->mem_end + offset;
  97. break;
  98. default:
  99. return -1;
  100. }
  101. if (newpos < mem->mem_base)
  102. newpos = mem->mem_base;
  103. if (newpos > mem->mem_end)
  104. newpos = mem->mem_end;
  105. mem->mem_position = newpos;
  106. return mem->mem_position - mem->mem_base;
  107. }
  108. static int mem_read(struct rtgui_filerw *context, void *ptr, rt_size_t size, rt_size_t maxnum)
  109. {
  110. int total_bytes;
  111. int mem_available;
  112. struct rtgui_filerw_mem *mem = (struct rtgui_filerw_mem *)context;
  113. total_bytes = (maxnum * size);
  114. if ((maxnum <= 0) || (size <= 0) || ((total_bytes / maxnum) != size))
  115. {
  116. return -1;
  117. }
  118. mem_available = mem->mem_end - mem->mem_position;
  119. if (total_bytes > mem_available)
  120. total_bytes = mem_available;
  121. rt_memcpy(ptr, mem->mem_position, total_bytes);
  122. mem->mem_position += total_bytes;
  123. return (total_bytes / size);
  124. }
  125. static int mem_write(struct rtgui_filerw *context, const void *ptr, rt_size_t size, rt_size_t num)
  126. {
  127. return 0; /* not support memory write */
  128. }
  129. static int mem_tell(struct rtgui_filerw *context)
  130. {
  131. struct rtgui_filerw_mem *mem = (struct rtgui_filerw_mem *)context;
  132. return mem->mem_position - mem->mem_base;
  133. }
  134. static int mem_eof(struct rtgui_filerw *context)
  135. {
  136. struct rtgui_filerw_mem *mem = (struct rtgui_filerw_mem *)context;
  137. return mem->mem_position >= mem->mem_end;
  138. }
  139. static int mem_close(struct rtgui_filerw *context)
  140. {
  141. struct rtgui_filerw_mem *mem = (struct rtgui_filerw_mem *)context;
  142. if (mem != RT_NULL)
  143. {
  144. rtgui_free(mem);
  145. return 0;
  146. }
  147. return -1;
  148. }
  149. const rt_uint8_t *rtgui_filerw_mem_getdata(struct rtgui_filerw *context)
  150. {
  151. struct rtgui_filerw_mem *mem = (struct rtgui_filerw_mem *)context;
  152. /* check whether it's a memory filerw */
  153. if (mem->parent.read != mem_read) return RT_NULL;
  154. return mem->mem_base;
  155. }
  156. /* file read/write public interface */
  157. #ifdef RTGUI_USING_DFS_FILERW
  158. static int parse_mode(const char *mode)
  159. {
  160. int f = 0;
  161. for (;;)
  162. {
  163. switch (*mode)
  164. {
  165. case 0:
  166. return f;
  167. case 'b':
  168. f |= O_BINARY;
  169. break;
  170. case 'r':
  171. f = O_RDONLY;
  172. break;
  173. case 'w':
  174. f = O_WRONLY | O_CREAT | O_TRUNC;
  175. break;
  176. case 'a':
  177. f = O_WRONLY | O_CREAT | O_APPEND;
  178. break;
  179. case '+':
  180. f = (f & (~O_WRONLY)) | O_RDWR;
  181. break;
  182. }
  183. ++mode;
  184. }
  185. }
  186. struct rtgui_filerw *rtgui_filerw_create_file(const char *filename, const char *mode)
  187. {
  188. int fd;
  189. struct rtgui_filerw_stdio *rw;
  190. RT_ASSERT(filename != RT_NULL);
  191. rw = RT_NULL;
  192. #ifdef _WIN32_NATIVE
  193. fd = _open(filename, parse_mode(mode), 0);
  194. #else
  195. fd = open(filename, parse_mode(mode), 0);
  196. #endif
  197. if (fd >= 0)
  198. {
  199. rw = (struct rtgui_filerw_stdio *) rtgui_malloc(sizeof(struct rtgui_filerw_stdio));
  200. if (rw != RT_NULL)
  201. {
  202. rw->parent.seek = stdio_seek;
  203. rw->parent.read = stdio_read;
  204. rw->parent.write = stdio_write;
  205. rw->parent.tell = stdio_tell;
  206. rw->parent.close = stdio_close;
  207. rw->parent.eof = stdio_eof;
  208. rw->fd = fd;
  209. rw->eof = RT_FALSE;
  210. }
  211. }
  212. return &(rw->parent);
  213. }
  214. int rtgui_filerw_unlink(const char *filename)
  215. {
  216. #ifdef _WIN32_NATIVE
  217. return _unlink(filename);
  218. #else
  219. #ifndef RT_USING_DFS
  220. /* no unlink function */
  221. return -1;
  222. #else
  223. return unlink(filename);
  224. #endif
  225. #endif
  226. }
  227. #endif
  228. struct rtgui_filerw *rtgui_filerw_create_mem(const rt_uint8_t *mem, rt_size_t size)
  229. {
  230. struct rtgui_filerw_mem *rw;
  231. RT_ASSERT(mem != RT_NULL);
  232. rw = (struct rtgui_filerw_mem *) rtgui_malloc(sizeof(struct rtgui_filerw_mem));
  233. if (rw != RT_NULL)
  234. {
  235. rw->parent.seek = mem_seek;
  236. rw->parent.read = mem_read;
  237. rw->parent.write = mem_write;
  238. rw->parent.tell = mem_tell;
  239. rw->parent.eof = mem_eof;
  240. rw->parent.close = mem_close;
  241. rw->mem_base = mem;
  242. rw->mem_position = mem;
  243. rw->mem_end = mem + size;
  244. }
  245. return &(rw->parent);
  246. }
  247. int rtgui_filerw_seek(struct rtgui_filerw *context, rt_off_t offset, int whence)
  248. {
  249. RT_ASSERT(context != RT_NULL);
  250. return context->seek(context, offset, whence);
  251. }
  252. int rtgui_filerw_read(struct rtgui_filerw *context, void *buffer, rt_size_t size, rt_size_t count)
  253. {
  254. RT_ASSERT(context != RT_NULL);
  255. return context->read(context, buffer, size, count);
  256. }
  257. int rtgui_filerw_write(struct rtgui_filerw *context, const void *buffer, rt_size_t size, rt_size_t count)
  258. {
  259. RT_ASSERT(context != RT_NULL);
  260. return context->write(context, buffer, size, count);
  261. }
  262. int rtgui_filerw_eof(struct rtgui_filerw *context)
  263. {
  264. RT_ASSERT(context != RT_NULL);
  265. return context->eof(context);
  266. }
  267. int rtgui_filerw_tell(struct rtgui_filerw *context)
  268. {
  269. RT_ASSERT(context != RT_NULL);
  270. return context->tell(context);
  271. }
  272. int rtgui_filerw_close(struct rtgui_filerw *context)
  273. {
  274. int result;
  275. RT_ASSERT(context != RT_NULL);
  276. /* close context */
  277. result = context->close(context);
  278. if (result != 0)
  279. {
  280. /* close file failed */
  281. return -1;
  282. }
  283. return 0;
  284. }