source: libcaca/trunk/cucul/buffer.c @ 1301

Last change on this file since 1301 was 1301, checked in by Sam Hocevar, 14 years ago
  • Minor syntax fix in the doxygen documentation.
  • Property svn:keywords set to Id
File size: 3.5 KB
Line 
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: buffer.c 1301 2006-11-08 07:59:23Z sam $
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/*
15 *  This file contains buffer handling functions.
16 */
17
18#include "config.h"
19#include "common.h"
20
21#if !defined(__KERNEL__)
22#   include <stdio.h>
23#   include <stdlib.h>
24#   include <string.h>
25#endif
26
27#include "cucul.h"
28#include "cucul_internals.h"
29
30/** \brief Load a memory area into a buffer.
31 *
32 *  Create a \e libcucul buffer that points to the given memory area. The
33 *  data is not duplicated and any changes made to the original memory area
34 *  will appear in the buffer.
35 *
36 *  Keep in mind that buffers are not strings. When loading a C string, the
37 *  terminating '\\0' must not be part of the buffer, hence \e size should
38 *  be computed with strlen(). Conversely, the '\\0' is not appended to
39 *  exported buffers even when they could be parsed as strings.
40 *
41 *  \param data The memory area to load.
42 *  \param size The size of the memory area.
43 *  \return A \e libcucul buffer pointing to the memory area, or NULL
44 *          if an error occurred.
45 */
46cucul_buffer_t *cucul_load_memory(void *data, unsigned long int size)
47{
48    cucul_buffer_t *buf;
49
50    buf = malloc(sizeof(cucul_buffer_t));
51    if(!buf)
52        return NULL;
53
54    buf->data = data;
55    buf->size = size;
56    buf->user_data = 1;
57
58    return buf;
59}
60
61/** \brief Load a file into a buffer.
62 *
63 *  Load a file into memory and returns a \e libcucul buffer for use with
64 *  other functions.
65 *
66 *  \param file The filename
67 *  \return A \e libcucul buffer containing the file's contents, or NULL
68 *          if an error occurred.
69 */
70#if !defined(__KERNEL__)
71cucul_buffer_t *cucul_load_file(char const *file)
72{
73    cucul_buffer_t *buf;
74    FILE *fp;
75    long int size;
76
77    fp = fopen(file, "rb");
78    if(!fp)
79        return NULL;
80
81    buf = malloc(sizeof(cucul_buffer_t));
82    if(!buf)
83    {
84        fclose(fp);
85        return NULL;
86    }
87
88    fseek(fp, 0, SEEK_END);
89    size = ftell(fp);
90
91    buf->data = malloc(size);
92    if(!buf->data)
93    {
94        free(buf);
95        fclose(fp);
96        return NULL;
97    }
98    buf->size = size;
99    buf->user_data = 0;
100
101    fseek(fp, 0, SEEK_SET);
102    fread(buf->data, buf->size, 1, fp);
103    fclose(fp);
104
105    return buf;
106}
107#endif
108/** \brief Get the buffer size.
109 *
110 *  Return the length (in bytes) of the memory area stored in the given
111 *  \e libcucul buffer.
112 *
113 *  This function never fails.
114 *
115 *  \param buf A \e libcucul buffer
116 *  \return The buffer data length.
117 */
118unsigned long int cucul_get_buffer_size(cucul_buffer_t *buf)
119{
120    return buf->size;
121}
122
123/** \brief Get the buffer data.
124 *
125 *  Get a pointer to the memory area stored in the given
126 *  \e libcucul buffer.
127 *
128 *  This function never fails.
129 *
130 *  \param buf A \e libcucul buffer
131 *  \return A pointer to the buffer memory area.
132 */
133void * cucul_get_buffer_data(cucul_buffer_t *buf)
134{
135    return buf->data;
136}
137
138/** \brief Free a buffer.
139 *
140 *  Free the structures associated with the given \e libcucul buffer.
141 *
142 *  This function never fails.
143 *
144 *  \param buf A \e libcucul buffer
145 *  \return This function always returns 0.
146 */
147int cucul_free_buffer(cucul_buffer_t *buf)
148{
149    if(!buf->user_data)
150        free(buf->data);
151
152    free(buf);
153
154    return 0;
155}
156
Note: See TracBrowser for help on using the repository browser.