/src/os/windows/ftk_mmap_win32.c

http://ftk.googlecode.com/ · C · 109 lines · 60 code · 20 blank · 29 comment · 15 complexity · b2be5d0c690836701b51d90b097b6381 MD5 · raw file

  1. /*
  2. * File: ftk_mmap_win32.c
  3. * Author: Li XianJing <xianjimli@hotmail.com>
  4. * Brief: win32 mmap implemention.
  5. *
  6. * Copyright (c) 2009 - 2010 Li XianJing <xianjimli@hotmail.com>
  7. *
  8. * Licensed under the Academic Free License version 2.1
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. /*
  25. * History:
  26. * ================================================================
  27. * 2010-01-06 Li XianJing <xianjimli@hotmail.com> created
  28. *
  29. */
  30. #include "ftk_log.h"
  31. #include "ftk_mmap.h"
  32. #include "ftk_allocator.h"
  33. struct _FtkMmap
  34. {
  35. FILE* fp;
  36. void* data;
  37. size_t length;
  38. };
  39. int ftk_mmap_exist(const char* filename)
  40. {
  41. struct stat st = {0};
  42. return_val_if_fail(filename != NULL, 0);
  43. return stat(filename, &st) == 0;
  44. }
  45. FtkMmap* ftk_mmap_create(const char* filename, size_t offset, size_t size)
  46. {
  47. FtkMmap* thiz = NULL;
  48. struct stat st = {0};
  49. return_val_if_fail(filename != NULL, NULL);
  50. return_val_if_fail(stat(filename, &st) == 0, NULL);
  51. return_val_if_fail(offset < (size_t)st.st_size, NULL);
  52. size = (offset + size) <= (size_t)st.st_size ? size : st.st_size - offset;
  53. thiz = FTK_ZALLOC(sizeof(FtkMmap));
  54. return_val_if_fail(thiz != NULL, NULL);
  55. thiz->fp = fopen(filename, "rb");
  56. if(thiz->fp != NULL)
  57. {
  58. thiz->length = size;
  59. thiz->data = FTK_ZALLOC(size+1);
  60. fseek(thiz->fp, offset, SEEK_SET);
  61. fread(thiz->data, thiz->length, 1, thiz->fp);
  62. fclose(thiz->fp);
  63. }
  64. if(thiz->data == NULL || thiz->data == NULL)
  65. {
  66. FTK_ZFREE(thiz, sizeof(*thiz));
  67. ftk_logd("%s mmap %s failed.\n", __func__, filename);
  68. }
  69. return thiz;
  70. }
  71. void* ftk_mmap_data(FtkMmap* thiz)
  72. {
  73. return_val_if_fail(thiz != NULL, NULL);
  74. return thiz->data;
  75. }
  76. size_t ftk_mmap_length(FtkMmap* thiz)
  77. {
  78. return_val_if_fail(thiz != NULL, 0);
  79. return thiz->length;
  80. }
  81. void ftk_mmap_destroy(FtkMmap* thiz)
  82. {
  83. if(thiz != NULL)
  84. {
  85. FTK_FREE(thiz->data);
  86. FTK_ZFREE(thiz, sizeof(*thiz));
  87. }
  88. return;
  89. }