Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

153 Zeilen
3.3 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. * This function creates a \e libcucul buffer that points to the given
  28. * memory area. The data is not duplicated and any changes made to the
  29. * original memory area 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. * This function loads a file into memory and returns a \e libcucul buffer
  50. * for use with 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. * This function returns the length (in bytes) of the memory area stored
  91. * in the given \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. * This function returns 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. * This function frees the structures associated with the given
  119. * \e libcucul buffer.
  120. *
  121. * This function never fails.
  122. *
  123. * \param buf A \e libcucul buffer
  124. * \return This function always returns 0.
  125. */
  126. int cucul_free_buffer(cucul_buffer_t *buf)
  127. {
  128. if(!buf->user_data)
  129. free(buf->data);
  130. free(buf);
  131. return 0;
  132. }