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

Last change on this file since 1247 was 1247, checked in by Jean-Yves Lamoureux, 14 years ago
  • Documentation a bit clearer while importing a string into a buffer
  • Property svn:keywords set to Id
File size: 3.3 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 1247 2006-10-27 20:18:23Z jylam $
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. If you are importing a string, the
35 *  terminating zero must NOT be counted in size.
36 *
37 *  \param data The memory area to load.
38 *  \param size The size of the memory area.
39 *  \return A \e libcucul buffer pointing to the memory area, or NULL
40 *          if an error occurred.
41 */
42cucul_buffer_t *cucul_load_memory(void *data, unsigned long int size)
43{
44    cucul_buffer_t *buf;
45
46    buf = malloc(sizeof(cucul_buffer_t));
47    if(!buf)
48        return NULL;
49
50    buf->data = data;
51    buf->size = size;
52    buf->user_data = 1;
53
54    return buf;
55}
56
57/** \brief Load a file into a buffer.
58 *
59 *  Load a file into memory and returns a \e libcucul buffer for use with
60 *  other functions.
61 *
62 *  \param file The filename
63 *  \return A \e libcucul buffer containing the file's contents, or NULL
64 *          if an error occurred.
65 */
66#if !defined(__KERNEL__)
67cucul_buffer_t *cucul_load_file(char const *file)
68{
69    cucul_buffer_t *buf;
70    FILE *fp;
71    long int size;
72
73    fp = fopen(file, "rb");
74    if(!fp)
75        return NULL;
76
77    buf = malloc(sizeof(cucul_buffer_t));
78    if(!buf)
79    {
80        fclose(fp);
81        return NULL;
82    }
83
84    fseek(fp, 0, SEEK_END);
85    size = ftell(fp);
86
87    buf->data = malloc(size);
88    if(!buf->data)
89    {
90        free(buf);
91        fclose(fp);
92        return NULL;
93    }
94    buf->size = size;
95    buf->user_data = 0;
96
97    fseek(fp, 0, SEEK_SET);
98    fread(buf->data, buf->size, 1, fp);
99    fclose(fp);
100
101    return buf;
102}
103#endif
104/** \brief Get the buffer size.
105 *
106 *  Return the length (in bytes) of the memory area stored in the given
107 *  \e libcucul buffer.
108 *
109 *  This function never fails.
110 *
111 *  \param buf A \e libcucul buffer
112 *  \return The buffer data length.
113 */
114unsigned long int cucul_get_buffer_size(cucul_buffer_t *buf)
115{
116    return buf->size;
117}
118
119/** \brief Get the buffer data.
120 *
121 *  Get a pointer to the memory area stored in the given
122 *  \e libcucul buffer.
123 *
124 *  This function never fails.
125 *
126 *  \param buf A \e libcucul buffer
127 *  \return A pointer to the buffer memory area.
128 */
129void * cucul_get_buffer_data(cucul_buffer_t *buf)
130{
131    return buf->data;
132}
133
134/** \brief Free a buffer.
135 *
136 *  Free the structures associated with the given \e libcucul buffer.
137 *
138 *  This function never fails.
139 *
140 *  \param buf A \e libcucul buffer
141 *  \return This function always returns 0.
142 */
143int cucul_free_buffer(cucul_buffer_t *buf)
144{
145    if(!buf->user_data)
146        free(buf->data);
147
148    free(buf);
149
150    return 0;
151}
152
Note: See TracBrowser for help on using the repository browser.