filerw.c 9.9 KB
Newer Older
B
bernard.xiong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * File      : filerw.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE
 *
 * Change Logs:
 * Date           Author       Notes
 * 2009-10-16     Bernard      first version
 */

#include <rtgui/filerw.h>
#include <rtgui/rtgui_system.h>

B
bernard.xiong 已提交
18
#ifdef RTGUI_USING_DFS_FILERW
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
#include <dfs_posix.h>

/* standard file read/write */
struct rtgui_filerw_stdio
{
	/* inherit from rtgui_filerw */
	struct rtgui_filerw parent;

	int fd;
	rt_bool_t eof;
};

static int stdio_seek(struct rtgui_filerw *context, rt_off_t offset, int whence)
{
	struct rtgui_filerw_stdio* stdio_filerw = (struct rtgui_filerw_stdio *)context;
	int stdio_whence[3] = {SEEK_SET, SEEK_CUR, SEEK_END};

	if (whence < RTGUI_FILE_SEEK_SET || whence > RTGUI_FILE_SEEK_END)
	{
		return -1;
	}

	return lseek(stdio_filerw->fd, offset, stdio_whence[whence]);
}

static int stdio_read(struct rtgui_filerw *context, void *ptr, rt_size_t size, rt_size_t maxnum)
{
	int result;
	
	struct rtgui_filerw_stdio* stdio_filerw = (struct rtgui_filerw_stdio *)context;

	/* end of file */
	if (stdio_filerw->eof == RT_TRUE) return -1;

	result = read(stdio_filerw->fd, ptr, size * maxnum);
	if (result == 0) stdio_filerw->eof = RT_TRUE;

	return result;
}

static int stdio_write(struct rtgui_filerw *context, const void *ptr, rt_size_t size, rt_size_t num)
{
	struct rtgui_filerw_stdio* stdio_filerw = (struct rtgui_filerw_stdio *)context;

	return write(stdio_filerw->fd, (char*)ptr, size * num);
}

static int stdio_tell(struct rtgui_filerw* context)
{
	struct rtgui_filerw_stdio* stdio_filerw = (struct rtgui_filerw_stdio *)context;

	return lseek(stdio_filerw->fd, 0, SEEK_CUR);
}

static int stdio_eof(struct rtgui_filerw* context)
{
	int result;
	struct rtgui_filerw_stdio* stdio_filerw = (struct rtgui_filerw_stdio *)context;

	if (stdio_filerw->eof == RT_TRUE) result = 1;
	else result = -1;

	return result;
}

static int stdio_close(struct rtgui_filerw *context)
{
	struct rtgui_filerw_stdio* stdio_filerw = (struct rtgui_filerw_stdio *)context;

	if (stdio_filerw)
	{
		close(stdio_filerw->fd);
		rtgui_free(stdio_filerw);

		return 0;
	}

	return -1;
}
B
bernard.xiong 已提交
98
#elif defined(RTGUI_USING_STDIO_FILERW)
B
bernard.xiong 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
#include <stdio.h>

/* standard file read/write */
struct rtgui_filerw_stdio
{
	/* inherit from rtgui_filerw */
	struct rtgui_filerw parent;

	FILE* fp;
};

static int stdio_seek(struct rtgui_filerw *context, rt_off_t offset, int whence)
{
	struct rtgui_filerw_stdio* stdio_filerw = (struct rtgui_filerw_stdio *)context;
	int stdio_whence[3] = {SEEK_SET, SEEK_CUR, SEEK_END};

	if (whence < RTGUI_FILE_SEEK_SET || whence > RTGUI_FILE_SEEK_END)
	{
		return -1;
	}

	if (fseek(stdio_filerw->fp, offset, stdio_whence[whence]) == 0)
	{
		return ftell(stdio_filerw->fp);
	}

	return -1;
}

static int stdio_read(struct rtgui_filerw *context, void *ptr, rt_size_t size, rt_size_t maxnum)
{
	size_t nread;
	struct rtgui_filerw_stdio* stdio_filerw = (struct rtgui_filerw_stdio *)context;

	nread = fread(ptr, size, maxnum, stdio_filerw->fp);
	if (nread == 0 && ferror(stdio_filerw->fp))
	{
		return -1;
	}

	return nread;
}

static int stdio_write(struct rtgui_filerw *context, const void *ptr, rt_size_t size, rt_size_t num)
{
	size_t nwrote;
	struct rtgui_filerw_stdio* stdio_filerw = (struct rtgui_filerw_stdio *)context;

	nwrote = fwrite(ptr, size, num, stdio_filerw->fp);

	if ( nwrote == 0 && ferror(stdio_filerw->fp) )
	{
		return -1;
	}

	return nwrote;
}

157
static int stdio_tell(struct rtgui_filerw* context)
B
bernard.xiong 已提交
158 159 160 161 162 163
{
	struct rtgui_filerw_stdio* stdio_filerw = (struct rtgui_filerw_stdio *)context;

	return ftell(stdio_filerw->fp);
}

164
static int stdio_eof(struct rtgui_filerw* context)
B
bernard.xiong 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
{
	struct rtgui_filerw_stdio* stdio_filerw = (struct rtgui_filerw_stdio *)context;

	return feof(stdio_filerw->fp);
}

static int stdio_close(struct rtgui_filerw *context)
{
	struct rtgui_filerw_stdio* stdio_filerw = (struct rtgui_filerw_stdio *)context;

	if (stdio_filerw)
	{
		fclose(stdio_filerw->fp);
		rtgui_free(stdio_filerw);

		return 0;
	}

	return -1;
}
#endif

/* memory file read/write */
struct rtgui_filerw_mem
{
	/* inherit from rtgui_filerw */
	struct rtgui_filerw parent;

193
	const rt_uint8_t *mem_base, *mem_position, *mem_end;
B
bernard.xiong 已提交
194 195 196 197
};

static int mem_seek(struct rtgui_filerw *context, rt_off_t offset, int whence)
{
198
	const rt_uint8_t* newpos;
B
bernard.xiong 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
	struct rtgui_filerw_mem* mem = (struct rtgui_filerw_mem*)context;

	RT_ASSERT(mem != RT_NULL);

	switch (whence) {
	case RTGUI_FILE_SEEK_SET:
		newpos = mem->mem_base + offset;
		break;

	case RTGUI_FILE_SEEK_CUR:
		newpos = mem->mem_position + offset;
		break;

	case RTGUI_FILE_SEEK_END:
		newpos = mem->mem_end + offset;
		break;

	default:
		return -1;
	}

	if ( newpos < mem->mem_base )
		newpos = mem->mem_base;

	if ( newpos > mem->mem_end )
		newpos = mem->mem_end;

	mem->mem_position = newpos;
	return mem->mem_position- mem->mem_base;
}

static int mem_read(struct rtgui_filerw *context, void *ptr, rt_size_t size, rt_size_t maxnum)
{
	int total_bytes;
	int mem_available;
	struct rtgui_filerw_mem* mem = (struct rtgui_filerw_mem*)context;

	total_bytes = (maxnum * size);
	if ( (maxnum <= 0) || (size <= 0) || ((total_bytes / maxnum) != size) )
	{
		return -1;
	}

	mem_available = mem->mem_end - mem->mem_position;
	if (total_bytes > mem_available)
		total_bytes = mem_available;

	rt_memcpy(ptr, mem->mem_position, total_bytes);
	mem->mem_position += total_bytes;

	return (total_bytes / size);
}

static int mem_write(struct rtgui_filerw *context, const void *ptr, rt_size_t size, rt_size_t num)
253 254
{
#if 0
B
bernard.xiong 已提交
255 256 257 258 259 260 261 262 263 264
	struct rtgui_filerw_mem* mem = (struct rtgui_filerw_mem*)context;

	if ((mem->mem_position + (num * size)) > mem->mem_end)
	{
		num = (mem->mem_end - mem->mem_position)/size;
	}

	rt_memcpy(mem->mem_position, ptr, num*size);
	mem->mem_position += num*size;

265 266 267 268
	return num;
#else
	return 0; /* not support memory write */
#endif
B
bernard.xiong 已提交
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
}

static int mem_tell(struct rtgui_filerw* context)
{
	struct rtgui_filerw_mem* mem = (struct rtgui_filerw_mem*)context;

	return mem->mem_position - mem->mem_base;
}

static int mem_eof(struct rtgui_filerw* context)
{
	struct rtgui_filerw_mem* mem = (struct rtgui_filerw_mem*)context;

	return mem->mem_position >= mem->mem_end;
}

static int mem_close(struct rtgui_filerw *context)
{
	struct rtgui_filerw_mem* mem = (struct rtgui_filerw_mem*)context;

	if (mem != RT_NULL)
	{
		rtgui_free(mem);
		return 0;
	}

	return -1;
}

298
const rt_uint8_t* rtgui_filerw_mem_getdata(struct rtgui_filerw* context)
B
bernard.xiong 已提交
299 300 301 302 303 304 305 306 307 308
{
	struct rtgui_filerw_mem* mem = (struct rtgui_filerw_mem*)context;

	/* check whether it's a memory filerw */
	if (mem->parent.read != mem_read) return RT_NULL;

	return mem->mem_base;
}

/* file read/write public interface */
B
bernard.xiong 已提交
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
#ifdef RTGUI_USING_DFS_FILERW
static int parse_mode(const char *mode)
{
  int f=0;
 
  for (;;)
  {
    switch (*mode)
	{
    case 0: return f;
    case 'b': break;
    case 'r': f=O_RDONLY; break;
    case 'w': f=O_WRONLY|O_CREAT|O_TRUNC; break;
    case 'a': f=O_WRONLY|O_CREAT|O_APPEND; break;
    case '+': f=(f&(~O_WRONLY))|O_RDWR; break;
    }

    ++mode;
  }
}
B
bernard.xiong 已提交
329

330 331 332 333 334 335 336 337
struct rtgui_filerw* rtgui_filerw_create_file(const char* filename, const char* mode)
{
	int fd;
	struct rtgui_filerw_stdio *rw;

	RT_ASSERT(filename != RT_NULL);

	rw = RT_NULL;
B
bernard.xiong 已提交
338
	fd = open(filename, parse_mode(mode), 0);
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358

	if ( fd >= 0 )
	{
		rw = (struct rtgui_filerw_stdio*) rtgui_malloc(sizeof(struct rtgui_filerw_stdio));
		if (rw != RT_NULL)
		{
			rw->parent.seek  = stdio_seek;
			rw->parent.read  = stdio_read;
			rw->parent.write = stdio_write;
			rw->parent.tell  = stdio_tell;
			rw->parent.close = stdio_close;
			rw->parent.eof	 = stdio_eof;

			rw->fd  = fd;
			rw->eof = RT_FALSE;
		}
	}

	return &(rw->parent);
}
B
bernard.xiong 已提交
359
#elif defined(RTGUI_USING_STDIO_FILERW)
B
bernard.xiong 已提交
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
struct rtgui_filerw* rtgui_filerw_create_file(const char* filename, const char* mode)
{
	FILE *fp;
	struct rtgui_filerw_stdio *rw;

	RT_ASSERT(filename != RT_NULL);

	rw = RT_NULL;
	fp = fopen(filename, mode);

	if ( fp != NULL )
	{
		rw = (struct rtgui_filerw_stdio*) rtgui_malloc(sizeof(struct rtgui_filerw_stdio));
		if (rw != RT_NULL)
		{
			rw->parent.seek  = stdio_seek;
			rw->parent.read  = stdio_read;
			rw->parent.write = stdio_write;
			rw->parent.tell  = stdio_tell;
			rw->parent.close = stdio_close;
			rw->parent.eof	 = stdio_eof;

			rw->fp = fp;
		}
	}

	return &(rw->parent);
}
#endif

390
struct rtgui_filerw* rtgui_filerw_create_mem(const rt_uint8_t* mem, rt_size_t size)
B
bernard.xiong 已提交
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
{
	struct rtgui_filerw_mem* rw;
	RT_ASSERT(mem != RT_NULL);

	rw = (struct rtgui_filerw_mem*) rtgui_malloc(sizeof(struct rtgui_filerw_mem));
	if (rw != RT_NULL)
	{
		rw->parent.seek  = mem_seek;
		rw->parent.read  = mem_read;
		rw->parent.write = mem_write;
		rw->parent.tell  = mem_tell;
		rw->parent.eof	 = mem_eof;
		rw->parent.close = mem_close;

		rw->mem_base = mem;
		rw->mem_position = mem;
		rw->mem_end = mem + size;
	}

	return &(rw->parent);
}

int rtgui_filerw_seek(struct rtgui_filerw* context, rt_off_t offset, int whence)
{
	RT_ASSERT(context != RT_NULL);

	return context->seek(context, offset, whence);
}

int rtgui_filerw_read(struct rtgui_filerw* context, void* buffer, rt_size_t size, rt_size_t count)
{
	RT_ASSERT(context != RT_NULL);

	return context->read(context, buffer, size, count);
}

int rtgui_filerw_write(struct rtgui_filerw* context, const void* buffer, rt_size_t size, rt_size_t count)
{
	RT_ASSERT(context != RT_NULL);

	return context->write(context, buffer, size, count);
}

int rtgui_filerw_eof	(struct rtgui_filerw* context)
{
	RT_ASSERT(context != RT_NULL);

	return context->eof(context);
}

int rtgui_filerw_tell(struct rtgui_filerw* context)
{
	RT_ASSERT(context != RT_NULL);

	return context->tell(context);
}

int rtgui_filerw_close(struct rtgui_filerw* context)
{
	int result;

	RT_ASSERT(context != RT_NULL);

	/* close context */
	result = context->close(context);
	if (result != 0)
	{
		/* close file failed */
		return -1;
	}

	return 0;
}