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

Last change on this file since 896 was 896, checked in by Sam Hocevar, 15 years ago
  • Implemented cucul_load_file() and cucul_load_memory() to load respectively a file and a memory area into a libcucul buffer.
  • Changed the cucul_import_canvas() prototype so that it uses libcucul buffers instead of simple memory areas.
  • 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 896 2006-04-26 11:54:26Z 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 *  This function creates a \e libcucul buffer that points to the given
33 *  memory area. The data is not duplicated and any changes made to the
34 *  original memory area appear in the buffer.
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 */
41cucul_buffer_t *cucul_load_memory(void *data, unsigned long int size)
42{
43    cucul_buffer_t *buf;
44
45    buf = malloc(sizeof(cucul_buffer_t));
46    if(!buf)
47        return NULL;
48
49    buf->data = data;
50    buf->size = size;
51    buf->user_data = 1;
52
53    return buf;
54}
55
56/** \brief Load a file into a buffer.
57 *
58 *  This function loads a file into memory and returns a \e libcucul buffer
59 *  for use with other functions.
60 *
61 *  \param file The filename
62 *  \return A \e libcucul buffer containing the file's contents, or NULL
63 *          if an error occurred.
64 */
65cucul_buffer_t *cucul_load_file(char const *file)
66{
67    cucul_buffer_t *buf;
68    FILE *fp;
69    long int size;
70
71    fp = fopen(file, "rb");
72    if(!fp)
73        return NULL;
74
75    buf = malloc(sizeof(cucul_buffer_t));
76    if(!buf)
77    {
78        fclose(fp);
79        return NULL;
80    }
81
82    fseek(fp, 0, SEEK_END);
83    size = ftell(fp);
84
85    buf->data = malloc(size);
86    if(!buf->data)
87    {
88        free(buf);
89        fclose(fp);
90        return NULL;
91    }
92    buf->size = size;
93    buf->user_data = 0;
94
95    fseek(fp, 0, SEEK_SET);
96    fread(buf->data, buf->size, 1, fp);
97    fclose(fp);
98
99    return buf;
100}
101
102/** \brief Get the buffer size.
103 *
104 *  This function returns the length (in bytes) of the memory area stored
105 *  in the given \e libcucul buffer.
106 *
107 *  This function never fails.
108 *
109 *  \param buf A \e libcucul buffer
110 *  \return The buffer data length.
111 */
112unsigned long int cucul_get_buffer_size(cucul_buffer_t *buf)
113{
114    return buf->size;
115}
116
117/** \brief Get the buffer data.
118 *
119 *  This function returns a pointer to the memory area stored in the given
120 *  \e libcucul buffer.
121 *
122 *  This function never fails.
123 *
124 *  \param buf A \e libcucul buffer
125 *  \return A pointer to the buffer memory area.
126 */
127void * cucul_get_buffer_data(cucul_buffer_t *buf)
128{
129    return buf->data;
130}
131
132/** \brief Free a buffer.
133 *
134 *  This function frees the structures associated with the given
135 *  \e libcucul buffer.
136 *
137 *  This function never fails.
138 *
139 *  \param buf A \e libcucul buffer
140 *  \return This function always returns 0.
141 */
142int cucul_free_buffer(cucul_buffer_t *buf)
143{
144    if(!buf->user_data)
145        free(buf->data);
146
147    free(buf);
148
149    return 0;
150}
151
Note: See TracBrowser for help on using the repository browser.