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

Last change on this file since 1231 was 1231, checked in by Sam Hocevar, 14 years ago
  • Removed "This function..." constructs from documentation. Fixed a few documentation errors or imprecisions.
  • Property svn:keywords set to Id
File size: 3.2 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 1231 2006-10-25 22:06:21Z 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 *  \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 *  Load a file into memory and returns a \e libcucul buffer for use with
59 *  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 */
65#if !defined(__KERNEL__)
66cucul_buffer_t *cucul_load_file(char const *file)
67{
68    cucul_buffer_t *buf;
69    FILE *fp;
70    long int size;
71
72    fp = fopen(file, "rb");
73    if(!fp)
74        return NULL;
75
76    buf = malloc(sizeof(cucul_buffer_t));
77    if(!buf)
78    {
79        fclose(fp);
80        return NULL;
81    }
82
83    fseek(fp, 0, SEEK_END);
84    size = ftell(fp);
85
86    buf->data = malloc(size);
87    if(!buf->data)
88    {
89        free(buf);
90        fclose(fp);
91        return NULL;
92    }
93    buf->size = size;
94    buf->user_data = 0;
95
96    fseek(fp, 0, SEEK_SET);
97    fread(buf->data, buf->size, 1, fp);
98    fclose(fp);
99
100    return buf;
101}
102#endif
103/** \brief Get the buffer size.
104 *
105 *  Return the length (in bytes) of the memory area stored in the given
106 *  \e libcucul buffer.
107 *
108 *  This function never fails.
109 *
110 *  \param buf A \e libcucul buffer
111 *  \return The buffer data length.
112 */
113unsigned long int cucul_get_buffer_size(cucul_buffer_t *buf)
114{
115    return buf->size;
116}
117
118/** \brief Get the buffer data.
119 *
120 *  Get a pointer to the memory area stored in the given
121 *  \e libcucul buffer.
122 *
123 *  This function never fails.
124 *
125 *  \param buf A \e libcucul buffer
126 *  \return A pointer to the buffer memory area.
127 */
128void * cucul_get_buffer_data(cucul_buffer_t *buf)
129{
130    return buf->data;
131}
132
133/** \brief Free a buffer.
134 *
135 *  Free the structures associated with the given \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.