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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. cucul_buffer_t *cucul_load_file(char const *file)
  57. {
  58. cucul_buffer_t *buf;
  59. FILE *fp;
  60. long int size;
  61. fp = fopen(file, "rb");
  62. if(!fp)
  63. return NULL;
  64. buf = malloc(sizeof(cucul_buffer_t));
  65. if(!buf)
  66. {
  67. fclose(fp);
  68. return NULL;
  69. }
  70. fseek(fp, 0, SEEK_END);
  71. size = ftell(fp);
  72. buf->data = malloc(size);
  73. if(!buf->data)
  74. {
  75. free(buf);
  76. fclose(fp);
  77. return NULL;
  78. }
  79. buf->size = size;
  80. buf->user_data = 0;
  81. fseek(fp, 0, SEEK_SET);
  82. fread(buf->data, buf->size, 1, fp);
  83. fclose(fp);
  84. return buf;
  85. }
  86. /** \brief Get the buffer size.
  87. *
  88. * This function returns the length (in bytes) of the memory area stored
  89. * in the given \e libcucul buffer.
  90. *
  91. * This function never fails.
  92. *
  93. * \param buf A \e libcucul buffer
  94. * \return The buffer data length.
  95. */
  96. unsigned long int cucul_get_buffer_size(cucul_buffer_t *buf)
  97. {
  98. return buf->size;
  99. }
  100. /** \brief Get the buffer data.
  101. *
  102. * This function returns a pointer to the memory area stored in the given
  103. * \e libcucul buffer.
  104. *
  105. * This function never fails.
  106. *
  107. * \param buf A \e libcucul buffer
  108. * \return A pointer to the buffer memory area.
  109. */
  110. void * cucul_get_buffer_data(cucul_buffer_t *buf)
  111. {
  112. return buf->data;
  113. }
  114. /** \brief Free a buffer.
  115. *
  116. * This function frees the structures associated with the given
  117. * \e libcucul buffer.
  118. *
  119. * This function never fails.
  120. *
  121. * \param buf A \e libcucul buffer
  122. * \return This function always returns 0.
  123. */
  124. int cucul_free_buffer(cucul_buffer_t *buf)
  125. {
  126. if(!buf->user_data)
  127. free(buf->data);
  128. free(buf);
  129. return 0;
  130. }