Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

157 строки
3.5 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. * Keep in mind that buffers are not strings. When loading a C string, the
  32. * terminating '\0' must not be part of the buffer, hence \e size should
  33. * be computed with strlen(). Conversely, the '\0' is not appended to
  34. * exported buffers even when they could be parsed as strings.
  35. *
  36. * \param data The memory area to load.
  37. * \param size The size of the memory area.
  38. * \return A \e libcucul buffer pointing to the memory area, or NULL
  39. * if an error occurred.
  40. */
  41. cucul_buffer_t *cucul_load_memory(void *data, unsigned long int size)
  42. {
  43. cucul_buffer_t *buf;
  44. buf = malloc(sizeof(cucul_buffer_t));
  45. if(!buf)
  46. return NULL;
  47. buf->data = data;
  48. buf->size = size;
  49. buf->user_data = 1;
  50. return buf;
  51. }
  52. /** \brief Load a file into a buffer.
  53. *
  54. * Load a file into memory and returns a \e libcucul buffer for use with
  55. * other functions.
  56. *
  57. * \param file The filename
  58. * \return A \e libcucul buffer containing the file's contents, or NULL
  59. * if an error occurred.
  60. */
  61. #if !defined(__KERNEL__)
  62. cucul_buffer_t *cucul_load_file(char const *file)
  63. {
  64. cucul_buffer_t *buf;
  65. FILE *fp;
  66. long int size;
  67. fp = fopen(file, "rb");
  68. if(!fp)
  69. return NULL;
  70. buf = malloc(sizeof(cucul_buffer_t));
  71. if(!buf)
  72. {
  73. fclose(fp);
  74. return NULL;
  75. }
  76. fseek(fp, 0, SEEK_END);
  77. size = ftell(fp);
  78. buf->data = malloc(size);
  79. if(!buf->data)
  80. {
  81. free(buf);
  82. fclose(fp);
  83. return NULL;
  84. }
  85. buf->size = size;
  86. buf->user_data = 0;
  87. fseek(fp, 0, SEEK_SET);
  88. fread(buf->data, buf->size, 1, fp);
  89. fclose(fp);
  90. return buf;
  91. }
  92. #endif
  93. /** \brief Get the buffer size.
  94. *
  95. * Return the length (in bytes) of the memory area stored in the given
  96. * \e libcucul buffer.
  97. *
  98. * This function never fails.
  99. *
  100. * \param buf A \e libcucul buffer
  101. * \return The buffer data length.
  102. */
  103. unsigned long int cucul_get_buffer_size(cucul_buffer_t *buf)
  104. {
  105. return buf->size;
  106. }
  107. /** \brief Get the buffer data.
  108. *
  109. * Get a pointer to the memory area stored in the given
  110. * \e libcucul buffer.
  111. *
  112. * This function never fails.
  113. *
  114. * \param buf A \e libcucul buffer
  115. * \return A pointer to the buffer memory area.
  116. */
  117. void * cucul_get_buffer_data(cucul_buffer_t *buf)
  118. {
  119. return buf->data;
  120. }
  121. /** \brief Free a buffer.
  122. *
  123. * Free the structures associated with the given \e libcucul buffer.
  124. *
  125. * This function never fails.
  126. *
  127. * \param buf A \e libcucul buffer
  128. * \return This function always returns 0.
  129. */
  130. int cucul_free_buffer(cucul_buffer_t *buf)
  131. {
  132. if(!buf->user_data)
  133. free(buf->data);
  134. free(buf);
  135. return 0;
  136. }