Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

152 rindas
3.2 KiB

  1. /*
  2. * libcucul Canvas for ultrafast compositing of Unicode letters
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the Do What The Fuck You Want To
  10. * Public License, Version 2, as published by Sam Hocevar. See
  11. * http://sam.zoy.org/wtfpl/COPYING for more details.
  12. */
  13. /*
  14. * This file contains buffer handling functions.
  15. */
  16. #include "config.h"
  17. #include "common.h"
  18. #if !defined(__KERNEL__)
  19. # include <stdio.h>
  20. # include <stdlib.h>
  21. # include <string.h>
  22. #endif
  23. #include "cucul.h"
  24. #include "cucul_internals.h"
  25. /** \brief Load a memory area into a buffer.
  26. *
  27. * Create a \e libcucul buffer that points to the given memory area. The
  28. * data is not duplicated and any changes made to the original memory area
  29. * will appear in the buffer.
  30. *
  31. * \param data The memory area to load.
  32. * \param size The size of the memory area.
  33. * \return A \e libcucul buffer pointing to the memory area, or NULL
  34. * if an error occurred.
  35. */
  36. cucul_buffer_t *cucul_load_memory(void *data, unsigned long int size)
  37. {
  38. cucul_buffer_t *buf;
  39. buf = malloc(sizeof(cucul_buffer_t));
  40. if(!buf)
  41. return NULL;
  42. buf->data = data;
  43. buf->size = size;
  44. buf->user_data = 1;
  45. return buf;
  46. }
  47. /** \brief Load a file into a buffer.
  48. *
  49. * Load a file into memory and returns a \e libcucul buffer for use with
  50. * other functions.
  51. *
  52. * \param file The filename
  53. * \return A \e libcucul buffer containing the file's contents, or NULL
  54. * if an error occurred.
  55. */
  56. #if !defined(__KERNEL__)
  57. cucul_buffer_t *cucul_load_file(char const *file)
  58. {
  59. cucul_buffer_t *buf;
  60. FILE *fp;
  61. long int size;
  62. fp = fopen(file, "rb");
  63. if(!fp)
  64. return NULL;
  65. buf = malloc(sizeof(cucul_buffer_t));
  66. if(!buf)
  67. {
  68. fclose(fp);
  69. return NULL;
  70. }
  71. fseek(fp, 0, SEEK_END);
  72. size = ftell(fp);
  73. buf->data = malloc(size);
  74. if(!buf->data)
  75. {
  76. free(buf);
  77. fclose(fp);
  78. return NULL;
  79. }
  80. buf->size = size;
  81. buf->user_data = 0;
  82. fseek(fp, 0, SEEK_SET);
  83. fread(buf->data, buf->size, 1, fp);
  84. fclose(fp);
  85. return buf;
  86. }
  87. #endif
  88. /** \brief Get the buffer size.
  89. *
  90. * Return the length (in bytes) of the memory area stored in the given
  91. * \e libcucul buffer.
  92. *
  93. * This function never fails.
  94. *
  95. * \param buf A \e libcucul buffer
  96. * \return The buffer data length.
  97. */
  98. unsigned long int cucul_get_buffer_size(cucul_buffer_t *buf)
  99. {
  100. return buf->size;
  101. }
  102. /** \brief Get the buffer data.
  103. *
  104. * Get a pointer to the memory area stored in the given
  105. * \e libcucul buffer.
  106. *
  107. * This function never fails.
  108. *
  109. * \param buf A \e libcucul buffer
  110. * \return A pointer to the buffer memory area.
  111. */
  112. void * cucul_get_buffer_data(cucul_buffer_t *buf)
  113. {
  114. return buf->data;
  115. }
  116. /** \brief Free a buffer.
  117. *
  118. * Free the structures associated with the given \e libcucul buffer.
  119. *
  120. * This function never fails.
  121. *
  122. * \param buf A \e libcucul buffer
  123. * \return This function always returns 0.
  124. */
  125. int cucul_free_buffer(cucul_buffer_t *buf)
  126. {
  127. if(!buf->user_data)
  128. free(buf->data);
  129. free(buf);
  130. return 0;
  131. }