filerw.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * File : filerw.c
  3. * This file is part of RT-Thread GUI Engine
  4. * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2009-10-16 Bernard first version
  23. */
  24. #include <rtgui/rtgui_system.h>
  25. #include <rtgui/filerw.h>
  26. #ifdef RTGUI_USING_DFS_FILERW
  27. /* standard file read/write */
  28. struct rtgui_filerw_stdio
  29. {
  30. /* inherit from rtgui_filerw */
  31. struct rtgui_filerw parent;
  32. int fd;
  33. rt_bool_t eof;
  34. };
  35. static int stdio_seek(struct rtgui_filerw *context, rt_off_t offset, int whence)
  36. {
  37. struct rtgui_filerw_stdio *stdio_filerw = (struct rtgui_filerw_stdio *)context;
  38. int stdio_whence[3] = {SEEK_SET, SEEK_CUR, SEEK_END};
  39. if (whence < RTGUI_FILE_SEEK_SET || whence > RTGUI_FILE_SEEK_END)
  40. {
  41. return -1;
  42. }
  43. return lseek(stdio_filerw->fd, offset, stdio_whence[whence]);
  44. }
  45. static int stdio_read(struct rtgui_filerw *context, void *ptr, rt_size_t size, rt_size_t maxnum)
  46. {
  47. int result;
  48. struct rtgui_filerw_stdio *stdio_filerw = (struct rtgui_filerw_stdio *)context;
  49. /* end of file */
  50. if (stdio_filerw->eof == RT_TRUE) return -1;
  51. result = read(stdio_filerw->fd, ptr, size * maxnum);
  52. if (result == 0) stdio_filerw->eof = RT_TRUE;
  53. return result;
  54. }
  55. static int stdio_write(struct rtgui_filerw *context, const void *ptr, rt_size_t size, rt_size_t num)
  56. {
  57. struct rtgui_filerw_stdio *stdio_filerw = (struct rtgui_filerw_stdio *)context;
  58. return write(stdio_filerw->fd, (char *)ptr, size * num);
  59. }
  60. static int stdio_tell(struct rtgui_filerw *context)
  61. {
  62. struct rtgui_filerw_stdio *stdio_filerw = (struct rtgui_filerw_stdio *)context;
  63. return lseek(stdio_filerw->fd, 0, SEEK_CUR);
  64. }
  65. static int stdio_eof(struct rtgui_filerw *context)
  66. {
  67. int result;
  68. struct rtgui_filerw_stdio *stdio_filerw = (struct rtgui_filerw_stdio *)context;
  69. if (stdio_filerw->eof == RT_TRUE) result = 1;
  70. else result = -1;
  71. return result;
  72. }
  73. static int stdio_close(struct rtgui_filerw *context)
  74. {
  75. struct rtgui_filerw_stdio *stdio_filerw = (struct rtgui_filerw_stdio *)context;
  76. if (stdio_filerw)
  77. {
  78. close(stdio_filerw->fd);
  79. rtgui_free(stdio_filerw);
  80. return 0;
  81. }
  82. return -1;
  83. }
  84. #endif
  85. /* memory file read/write */
  86. struct rtgui_filerw_mem
  87. {
  88. /* inherit from rtgui_filerw */
  89. struct rtgui_filerw parent;
  90. const rt_uint8_t *mem_base, *mem_position, *mem_end;
  91. };
  92. static int mem_seek(struct rtgui_filerw *context, rt_off_t offset, int whence)
  93. {
  94. const rt_uint8_t *newpos;
  95. struct rtgui_filerw_mem *mem = (struct rtgui_filerw_mem *)context;
  96. RT_ASSERT(mem != RT_NULL);
  97. switch (whence)
  98. {
  99. case RTGUI_FILE_SEEK_SET:
  100. newpos = mem->mem_base + offset;
  101. break;
  102. case RTGUI_FILE_SEEK_CUR:
  103. newpos = mem->mem_position + offset;
  104. break;
  105. case RTGUI_FILE_SEEK_END:
  106. newpos = mem->mem_end + offset;
  107. break;
  108. default:
  109. return -1;
  110. }
  111. if (newpos < mem->mem_base)
  112. newpos = mem->mem_base;
  113. if (newpos > mem->mem_end)
  114. newpos = mem->mem_end;
  115. mem->mem_position = newpos;
  116. return mem->mem_position - mem->mem_base;
  117. }
  118. static int mem_read(struct rtgui_filerw *context, void *ptr, rt_size_t size, rt_size_t maxnum)
  119. {
  120. int total_bytes;
  121. int mem_available;
  122. struct rtgui_filerw_mem *mem = (struct rtgui_filerw_mem *)context;
  123. total_bytes = (maxnum * size);
  124. if ((maxnum <= 0) || (size <= 0) || ((total_bytes / maxnum) != size))
  125. {
  126. return -1;
  127. }
  128. mem_available = mem->mem_end - mem->mem_position;
  129. if (total_bytes > mem_available)
  130. total_bytes = mem_available;
  131. rt_memcpy(ptr, mem->mem_position, total_bytes);
  132. mem->mem_position += total_bytes;
  133. return (total_bytes / size);
  134. }
  135. static int mem_write(struct rtgui_filerw *context, const void *ptr, rt_size_t size, rt_size_t num)
  136. {
  137. return 0; /* not support memory write */
  138. }
  139. static int mem_tell(struct rtgui_filerw *context)
  140. {
  141. struct rtgui_filerw_mem *mem = (struct rtgui_filerw_mem *)context;
  142. return mem->mem_position - mem->mem_base;
  143. }
  144. static int mem_eof(struct rtgui_filerw *context)
  145. {
  146. struct rtgui_filerw_mem *mem = (struct rtgui_filerw_mem *)context;
  147. return mem->mem_position >= mem->mem_end;
  148. }
  149. static int mem_close(struct rtgui_filerw *context)
  150. {
  151. struct rtgui_filerw_mem *mem = (struct rtgui_filerw_mem *)context;
  152. if (mem != RT_NULL)
  153. {
  154. rtgui_free(mem);
  155. return 0;
  156. }
  157. return -1;
  158. }
  159. const rt_uint8_t *rtgui_filerw_mem_getdata(struct rtgui_filerw *context)
  160. {
  161. struct rtgui_filerw_mem *mem = (struct rtgui_filerw_mem *)context;
  162. /* check whether it's a memory filerw */
  163. if (mem->parent.read != mem_read) return RT_NULL;
  164. return mem->mem_base;
  165. }
  166. /* file read/write public interface */
  167. #ifdef RTGUI_USING_DFS_FILERW
  168. static int parse_mode(const char *mode)
  169. {
  170. int f = 0;
  171. for (;;)
  172. {
  173. switch (*mode)
  174. {
  175. case 0:
  176. return f;
  177. case 'b':
  178. f |= O_BINARY;
  179. break;
  180. case 'r':
  181. f = O_RDONLY;
  182. break;
  183. case 'w':
  184. f = O_WRONLY | O_CREAT | O_TRUNC;
  185. break;
  186. case 'a':
  187. f = O_WRONLY | O_CREAT | O_APPEND;
  188. break;
  189. case '+':
  190. f = (f & (~O_WRONLY)) | O_RDWR;
  191. break;
  192. }
  193. ++mode;
  194. }
  195. }
  196. struct rtgui_filerw *rtgui_filerw_create_file(const char *filename, const char *mode)
  197. {
  198. int fd;
  199. struct rtgui_filerw_stdio *rw;
  200. RT_ASSERT(filename != RT_NULL);
  201. rw = RT_NULL;
  202. #ifdef _WIN32_NATIVE
  203. fd = _open(filename, parse_mode(mode), 0);
  204. #else
  205. fd = open(filename, parse_mode(mode), 0);
  206. #endif
  207. if (fd >= 0)
  208. {
  209. rw = (struct rtgui_filerw_stdio *) rtgui_malloc(sizeof(struct rtgui_filerw_stdio));
  210. if (rw != RT_NULL)
  211. {
  212. rw->parent.seek = stdio_seek;
  213. rw->parent.read = stdio_read;
  214. rw->parent.write = stdio_write;
  215. rw->parent.tell = stdio_tell;
  216. rw->parent.close = stdio_close;
  217. rw->parent.eof = stdio_eof;
  218. rw->fd = fd;
  219. rw->eof = RT_FALSE;
  220. return &(rw->parent);
  221. }
  222. close(fd);
  223. }
  224. else
  225. {
  226. rt_kprintf("open:%s failed! errno=%d\n", filename, errno);
  227. }
  228. return RT_NULL;
  229. }
  230. int rtgui_filerw_unlink(const char *filename)
  231. {
  232. #ifdef _WIN32_NATIVE
  233. return _unlink(filename);
  234. #else
  235. #ifndef RT_USING_DFS
  236. /* no unlink function */
  237. return -1;
  238. #else
  239. return unlink(filename);
  240. #endif
  241. #endif
  242. }
  243. #endif
  244. struct rtgui_filerw *rtgui_filerw_create_mem(const rt_uint8_t *mem, rt_size_t size)
  245. {
  246. struct rtgui_filerw_mem *rw;
  247. RT_ASSERT(mem != RT_NULL);
  248. rw = (struct rtgui_filerw_mem *) rtgui_malloc(sizeof(struct rtgui_filerw_mem));
  249. if (rw != RT_NULL)
  250. {
  251. rw->parent.seek = mem_seek;
  252. rw->parent.read = mem_read;
  253. rw->parent.write = mem_write;
  254. rw->parent.tell = mem_tell;
  255. rw->parent.eof = mem_eof;
  256. rw->parent.close = mem_close;
  257. rw->mem_base = mem;
  258. rw->mem_position = mem;
  259. rw->mem_end = mem + size;
  260. }
  261. return &(rw->parent);
  262. }
  263. int rtgui_filerw_seek(struct rtgui_filerw *context, rt_off_t offset, int whence)
  264. {
  265. RT_ASSERT(context != RT_NULL);
  266. return context->seek(context, offset, whence);
  267. }
  268. int rtgui_filerw_read(struct rtgui_filerw *context, void *buffer, rt_size_t size, rt_size_t count)
  269. {
  270. RT_ASSERT(context != RT_NULL);
  271. return context->read(context, buffer, size, count);
  272. }
  273. int rtgui_filerw_write(struct rtgui_filerw *context, const void *buffer, rt_size_t size, rt_size_t count)
  274. {
  275. RT_ASSERT(context != RT_NULL);
  276. return context->write(context, buffer, size, count);
  277. }
  278. int rtgui_filerw_eof(struct rtgui_filerw *context)
  279. {
  280. RT_ASSERT(context != RT_NULL);
  281. return context->eof(context);
  282. }
  283. int rtgui_filerw_tell(struct rtgui_filerw *context)
  284. {
  285. RT_ASSERT(context != RT_NULL);
  286. return context->tell(context);
  287. }
  288. int rtgui_filerw_close(struct rtgui_filerw *context)
  289. {
  290. int result;
  291. RT_ASSERT(context != RT_NULL);
  292. /* close context */
  293. result = context->close(context);
  294. if (result != 0)
  295. {
  296. /* close file failed */
  297. return -1;
  298. }
  299. return 0;
  300. }