filerw.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. #ifdef _WIN32
  166. case 'b': f|=O_BINARY;break;
  167. #else
  168. case 'b': break;
  169. #endif
  170. case 'r': f=O_RDONLY; break;
  171. case 'w': f=O_WRONLY|O_CREAT|O_TRUNC; break;
  172. case 'a': f=O_WRONLY|O_CREAT|O_APPEND; break;
  173. case '+': f=(f&(~O_WRONLY))|O_RDWR; break;
  174. }
  175. ++mode;
  176. }
  177. }
  178. struct rtgui_filerw* rtgui_filerw_create_file(const char* filename, const char* mode)
  179. {
  180. int fd;
  181. struct rtgui_filerw_stdio *rw;
  182. RT_ASSERT(filename != RT_NULL);
  183. rw = RT_NULL;
  184. #ifdef _WIN32
  185. fd = _open(filename, parse_mode(mode), 0);
  186. #else
  187. fd = open(filename, parse_mode(mode), 0);
  188. #endif
  189. if ( fd >= 0 )
  190. {
  191. rw = (struct rtgui_filerw_stdio*) rtgui_malloc(sizeof(struct rtgui_filerw_stdio));
  192. if (rw != RT_NULL)
  193. {
  194. rw->parent.seek = stdio_seek;
  195. rw->parent.read = stdio_read;
  196. rw->parent.write = stdio_write;
  197. rw->parent.tell = stdio_tell;
  198. rw->parent.close = stdio_close;
  199. rw->parent.eof = stdio_eof;
  200. rw->fd = fd;
  201. rw->eof = RT_FALSE;
  202. }
  203. }
  204. return &(rw->parent);
  205. }
  206. int rtgui_filerw_unlink(const char *filename)
  207. {
  208. #ifdef _WIN32
  209. return _unlink(filename);
  210. #else
  211. return unlink(filename);
  212. #endif
  213. }
  214. #endif
  215. struct rtgui_filerw* rtgui_filerw_create_mem(const rt_uint8_t* mem, rt_size_t size)
  216. {
  217. struct rtgui_filerw_mem* rw;
  218. RT_ASSERT(mem != RT_NULL);
  219. rw = (struct rtgui_filerw_mem*) rtgui_malloc(sizeof(struct rtgui_filerw_mem));
  220. if (rw != RT_NULL)
  221. {
  222. rw->parent.seek = mem_seek;
  223. rw->parent.read = mem_read;
  224. rw->parent.write = mem_write;
  225. rw->parent.tell = mem_tell;
  226. rw->parent.eof = mem_eof;
  227. rw->parent.close = mem_close;
  228. rw->mem_base = mem;
  229. rw->mem_position = mem;
  230. rw->mem_end = mem + size;
  231. }
  232. return &(rw->parent);
  233. }
  234. int rtgui_filerw_seek(struct rtgui_filerw* context, rt_off_t offset, int whence)
  235. {
  236. RT_ASSERT(context != RT_NULL);
  237. return context->seek(context, offset, whence);
  238. }
  239. int rtgui_filerw_read(struct rtgui_filerw* context, void* buffer, rt_size_t size, rt_size_t count)
  240. {
  241. RT_ASSERT(context != RT_NULL);
  242. return context->read(context, buffer, size, count);
  243. }
  244. int rtgui_filerw_write(struct rtgui_filerw* context, const void* buffer, rt_size_t size, rt_size_t count)
  245. {
  246. RT_ASSERT(context != RT_NULL);
  247. return context->write(context, buffer, size, count);
  248. }
  249. int rtgui_filerw_eof (struct rtgui_filerw* context)
  250. {
  251. RT_ASSERT(context != RT_NULL);
  252. return context->eof(context);
  253. }
  254. int rtgui_filerw_tell(struct rtgui_filerw* context)
  255. {
  256. RT_ASSERT(context != RT_NULL);
  257. return context->tell(context);
  258. }
  259. int rtgui_filerw_close(struct rtgui_filerw* context)
  260. {
  261. int result;
  262. RT_ASSERT(context != RT_NULL);
  263. /* close context */
  264. result = context->close(context);
  265. if (result != 0)
  266. {
  267. /* close file failed */
  268. return -1;
  269. }
  270. return 0;
  271. }