filerw.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. case RTGUI_FILE_SEEK_SET:
  89. newpos = mem->mem_base + offset;
  90. break;
  91. case RTGUI_FILE_SEEK_CUR:
  92. newpos = mem->mem_position + offset;
  93. break;
  94. case RTGUI_FILE_SEEK_END:
  95. newpos = mem->mem_end + offset;
  96. break;
  97. default:
  98. return -1;
  99. }
  100. if ( newpos < mem->mem_base )
  101. newpos = mem->mem_base;
  102. if ( newpos > mem->mem_end )
  103. newpos = mem->mem_end;
  104. mem->mem_position = newpos;
  105. return mem->mem_position- mem->mem_base;
  106. }
  107. static int mem_read(struct rtgui_filerw *context, void *ptr, rt_size_t size, rt_size_t maxnum)
  108. {
  109. int total_bytes;
  110. int mem_available;
  111. struct rtgui_filerw_mem* mem = (struct rtgui_filerw_mem*)context;
  112. total_bytes = (maxnum * size);
  113. if ( (maxnum <= 0) || (size <= 0) || ((total_bytes / maxnum) != size) )
  114. {
  115. return -1;
  116. }
  117. mem_available = mem->mem_end - mem->mem_position;
  118. if (total_bytes > mem_available)
  119. total_bytes = mem_available;
  120. rt_memcpy(ptr, mem->mem_position, total_bytes);
  121. mem->mem_position += total_bytes;
  122. return (total_bytes / size);
  123. }
  124. static int mem_write(struct rtgui_filerw *context, const void *ptr, rt_size_t size, rt_size_t num)
  125. {
  126. return 0; /* not support memory write */
  127. }
  128. static int mem_tell(struct rtgui_filerw* context)
  129. {
  130. struct rtgui_filerw_mem* mem = (struct rtgui_filerw_mem*)context;
  131. return mem->mem_position - mem->mem_base;
  132. }
  133. static int mem_eof(struct rtgui_filerw* context)
  134. {
  135. struct rtgui_filerw_mem* mem = (struct rtgui_filerw_mem*)context;
  136. return mem->mem_position >= mem->mem_end;
  137. }
  138. static int mem_close(struct rtgui_filerw *context)
  139. {
  140. struct rtgui_filerw_mem* mem = (struct rtgui_filerw_mem*)context;
  141. if (mem != RT_NULL)
  142. {
  143. rtgui_free(mem);
  144. return 0;
  145. }
  146. return -1;
  147. }
  148. const rt_uint8_t* rtgui_filerw_mem_getdata(struct rtgui_filerw* context)
  149. {
  150. struct rtgui_filerw_mem* mem = (struct rtgui_filerw_mem*)context;
  151. /* check whether it's a memory filerw */
  152. if (mem->parent.read != mem_read) return RT_NULL;
  153. return mem->mem_base;
  154. }
  155. /* file read/write public interface */
  156. #ifdef RTGUI_USING_DFS_FILERW
  157. static int parse_mode(const char *mode)
  158. {
  159. int f=0;
  160. for (;;)
  161. {
  162. switch (*mode)
  163. {
  164. case 0: return f;
  165. case 'b': f|=O_BINARY;break;
  166. case 'r': f=O_RDONLY; break;
  167. case 'w': f=O_WRONLY|O_CREAT|O_TRUNC; break;
  168. case 'a': f=O_WRONLY|O_CREAT|O_APPEND; break;
  169. case '+': f=(f&(~O_WRONLY))|O_RDWR; break;
  170. }
  171. ++mode;
  172. }
  173. }
  174. struct rtgui_filerw* rtgui_filerw_create_file(const char* filename, const char* mode)
  175. {
  176. int fd;
  177. struct rtgui_filerw_stdio *rw;
  178. RT_ASSERT(filename != RT_NULL);
  179. rw = RT_NULL;
  180. #ifdef _WIN32
  181. fd = _open(filename, parse_mode(mode), 0);
  182. #else
  183. fd = open(filename, parse_mode(mode), 0);
  184. #endif
  185. if ( fd >= 0 )
  186. {
  187. rw = (struct rtgui_filerw_stdio*) rtgui_malloc(sizeof(struct rtgui_filerw_stdio));
  188. if (rw != RT_NULL)
  189. {
  190. rw->parent.seek = stdio_seek;
  191. rw->parent.read = stdio_read;
  192. rw->parent.write = stdio_write;
  193. rw->parent.tell = stdio_tell;
  194. rw->parent.close = stdio_close;
  195. rw->parent.eof = stdio_eof;
  196. rw->fd = fd;
  197. rw->eof = RT_FALSE;
  198. }
  199. }
  200. return &(rw->parent);
  201. }
  202. int rtgui_filerw_unlink(const char *filename)
  203. {
  204. #ifdef _WIN32
  205. return _unlink(filename);
  206. #else
  207. return unlink(filename);
  208. #endif
  209. }
  210. #endif
  211. struct rtgui_filerw* rtgui_filerw_create_mem(const rt_uint8_t* mem, rt_size_t size)
  212. {
  213. struct rtgui_filerw_mem* rw;
  214. RT_ASSERT(mem != RT_NULL);
  215. rw = (struct rtgui_filerw_mem*) rtgui_malloc(sizeof(struct rtgui_filerw_mem));
  216. if (rw != RT_NULL)
  217. {
  218. rw->parent.seek = mem_seek;
  219. rw->parent.read = mem_read;
  220. rw->parent.write = mem_write;
  221. rw->parent.tell = mem_tell;
  222. rw->parent.eof = mem_eof;
  223. rw->parent.close = mem_close;
  224. rw->mem_base = mem;
  225. rw->mem_position = mem;
  226. rw->mem_end = mem + size;
  227. }
  228. return &(rw->parent);
  229. }
  230. int rtgui_filerw_seek(struct rtgui_filerw* context, rt_off_t offset, int whence)
  231. {
  232. RT_ASSERT(context != RT_NULL);
  233. return context->seek(context, offset, whence);
  234. }
  235. int rtgui_filerw_read(struct rtgui_filerw* context, void* buffer, rt_size_t size, rt_size_t count)
  236. {
  237. RT_ASSERT(context != RT_NULL);
  238. return context->read(context, buffer, size, count);
  239. }
  240. int rtgui_filerw_write(struct rtgui_filerw* context, const void* buffer, rt_size_t size, rt_size_t count)
  241. {
  242. RT_ASSERT(context != RT_NULL);
  243. return context->write(context, buffer, size, count);
  244. }
  245. int rtgui_filerw_eof (struct rtgui_filerw* context)
  246. {
  247. RT_ASSERT(context != RT_NULL);
  248. return context->eof(context);
  249. }
  250. int rtgui_filerw_tell(struct rtgui_filerw* context)
  251. {
  252. RT_ASSERT(context != RT_NULL);
  253. return context->tell(context);
  254. }
  255. int rtgui_filerw_close(struct rtgui_filerw* context)
  256. {
  257. int result;
  258. RT_ASSERT(context != RT_NULL);
  259. /* close context */
  260. result = context->close(context);
  261. if (result != 0)
  262. {
  263. /* close file failed */
  264. return -1;
  265. }
  266. return 0;
  267. }