PageRenderTime 467ms CodeModel.GetById 227ms app.highlight 7ms RepoModel.GetById 144ms app.codeStats 0ms

/src/os/wince/ftk_mmap_wince.c

http://ftk.googlecode.com/
C | 101 lines | 84 code | 16 blank | 1 comment | 19 complexity | 8be04065b6b6409f587dad270ff5f896 MD5 | raw file
  1
  2#include "ftk_log.h"
  3#include "ftk_mmap.h"
  4#include "ftk_allocator.h"
  5
  6struct _FtkMmap
  7{
  8	FILE* fp;
  9	void* data;
 10	size_t length;
 11};
 12
 13int ftk_mmap_exist(const char* filename)
 14{
 15	wchar_t buf[MAX_PATH];
 16	HANDLE handle;
 17	WIN32_FIND_DATAW find_data;
 18	if (MultiByteToWideChar(CP_ACP, 0, filename, -1, buf, MAX_PATH) == 0)
 19	{
 20		return 0;
 21	}
 22	handle = FindFirstFileW(buf, &find_data);
 23	if (handle == INVALID_HANDLE_VALUE)
 24	{
 25		return 0;
 26	}
 27	FindClose(handle);
 28	return 1;
 29}
 30
 31FtkMmap* ftk_mmap_create(const char* filename, size_t offset, size_t size)
 32{
 33	size_t n;
 34	wchar_t buf[MAX_PATH];
 35	FtkMmap* thiz = NULL;
 36	LARGE_INTEGER li;
 37	WIN32_FILE_ATTRIBUTE_DATA attr;
 38	return_val_if_fail(filename != NULL, NULL);
 39
 40	if (MultiByteToWideChar(CP_ACP, 0, filename, -1, buf, MAX_PATH) == 0)
 41	{
 42		return NULL;
 43	}
 44	if (!GetFileAttributesExW(buf, GetFileExInfoStandard, &attr))
 45	{
 46		return NULL;
 47	}
 48	li.LowPart  = attr.nFileSizeLow;
 49	li.HighPart = attr.nFileSizeHigh;
 50	n = (size_t) li.QuadPart;
 51	/*ftk_logd("%s: %d\n", filename, n);*/
 52	return_val_if_fail(offset < n, NULL);
 53
 54	size = (offset + size) <= n ? size : n - offset;
 55
 56	thiz = FTK_ZALLOC(sizeof(FtkMmap));
 57	return_val_if_fail(thiz != NULL, NULL);
 58
 59	thiz->fp = fopen(filename, "rb");
 60	if(thiz->fp != NULL)
 61	{
 62		thiz->length = size;
 63		thiz->data = FTK_ZALLOC(size+1);
 64		fseek(thiz->fp, offset, SEEK_SET);
 65		fread(thiz->data, thiz->length, 1, thiz->fp);
 66		fclose(thiz->fp);
 67	}
 68
 69	if(thiz->data == NULL || thiz->data == NULL)
 70	{
 71		FTK_ZFREE(thiz, sizeof(*thiz));
 72		ftk_logd("%s mmap %s failed.\n", __func__, filename);
 73	}
 74
 75	return thiz;
 76}
 77
 78void* ftk_mmap_data(FtkMmap* thiz)
 79{
 80	return_val_if_fail(thiz != NULL, NULL);
 81
 82	return thiz->data;
 83}
 84
 85size_t ftk_mmap_length(FtkMmap* thiz)
 86{
 87	return_val_if_fail(thiz != NULL, 0);
 88
 89	return thiz->length;
 90}
 91
 92void ftk_mmap_destroy(FtkMmap* thiz)
 93{
 94	if(thiz != NULL)
 95	{
 96		FTK_FREE(thiz->data);
 97		FTK_ZFREE(thiz, sizeof(*thiz));
 98	}
 99
100	return;
101}