The BIG graph update
[rrdtool.git] / libraries / freetype-2.0.5 / ftsystem.c
1 /***************************************************************************/
2 /*                                                                         */
3 /*  ftsystem.c                                                             */
4 /*                                                                         */
5 /*    ANSI-specific FreeType low-level system interface (body).            */
6 /*                                                                         */
7 /*  Copyright 1996-2001 by                                                 */
8 /*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
9 /*                                                                         */
10 /*  This file is part of the FreeType project, and may only be used,       */
11 /*  modified, and distributed under the terms of the FreeType project      */
12 /*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
13 /*  this file you indicate that you have read the license and              */
14 /*  understand and accept it fully.                                        */
15 /*                                                                         */
16 /***************************************************************************/
17
18   /*************************************************************************/
19   /*                                                                       */
20   /* This file contains the default interface used by FreeType to access   */
21   /* low-level, i.e. memory management, i/o access as well as thread       */
22   /* synchronisation.  It can be replaced by user-specific routines if     */
23   /* necessary.                                                            */
24   /*                                                                       */
25   /*************************************************************************/
26
27
28 #include <ft2build.h>
29 #include FT_CONFIG_CONFIG_H
30 #include FT_INTERNAL_DEBUG_H
31 #include FT_SYSTEM_H
32 #include FT_ERRORS_H
33 #include FT_TYPES_H
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39
40   /*************************************************************************/
41   /*                                                                       */
42   /*                       MEMORY MANAGEMENT INTERFACE                     */
43   /*                                                                       */
44   /*************************************************************************/
45
46   /*************************************************************************/
47   /*                                                                       */
48   /* It is not necessary to do any error checking for the                  */
49   /* allocation-related functions.  This will be done by the higher level  */
50   /* routines like FT_Alloc() or FT_Realloc().                             */
51   /*                                                                       */
52   /*************************************************************************/
53
54
55   /*************************************************************************/
56   /*                                                                       */
57   /* <Function>                                                            */
58   /*    ft_alloc                                                           */
59   /*                                                                       */
60   /* <Description>                                                         */
61   /*    The memory allocation function.                                    */
62   /*                                                                       */
63   /* <Input>                                                               */
64   /*    memory :: A pointer to the memory object.                          */
65   /*                                                                       */
66   /*    size   :: The requested size in bytes.                             */
67   /*                                                                       */
68   /* <Return>                                                              */
69   /*    The address of newly allocated block.                              */
70   /*                                                                       */
71   FT_CALLBACK_DEF( void* )
72   ft_alloc( FT_Memory  memory,
73             long       size )
74   {
75     FT_UNUSED( memory );
76
77     return malloc( size );
78   }
79
80
81   /*************************************************************************/
82   /*                                                                       */
83   /* <Function>                                                            */
84   /*    ft_realloc                                                         */
85   /*                                                                       */
86   /* <Description>                                                         */
87   /*    The memory reallocation function.                                  */
88   /*                                                                       */
89   /* <Input>                                                               */
90   /*    memory   :: A pointer to the memory object.                        */
91   /*                                                                       */
92   /*    cur_size :: The current size of the allocated memory block.        */
93   /*                                                                       */
94   /*    new_size :: The newly requested size in bytes.                     */
95   /*                                                                       */
96   /*    block    :: The current address of the block in memory.            */
97   /*                                                                       */
98   /* <Return>                                                              */
99   /*    The address of the reallocated memory block.                       */
100   /*                                                                       */
101   FT_CALLBACK_DEF( void* )
102   ft_realloc( FT_Memory  memory,
103               long       cur_size,
104               long       new_size,
105               void*      block )
106   {
107     FT_UNUSED( memory );
108     FT_UNUSED( cur_size );
109
110     return realloc( block, new_size );
111   }
112
113
114   /*************************************************************************/
115   /*                                                                       */
116   /* <Function>                                                            */
117   /*    ft_free                                                            */
118   /*                                                                       */
119   /* <Description>                                                         */
120   /*    The memory release function.                                       */
121   /*                                                                       */
122   /* <Input>                                                               */
123   /*    memory  :: A pointer to the memory object.                         */
124   /*                                                                       */
125   /*    block   :: The address of block in memory to be freed.             */
126   /*                                                                       */
127   FT_CALLBACK_DEF( void )
128   ft_free( FT_Memory  memory,
129            void*      block )
130   {
131     FT_UNUSED( memory );
132
133     free( block );
134   }
135
136
137   /*************************************************************************/
138   /*                                                                       */
139   /*                     RESOURCE MANAGEMENT INTERFACE                     */
140   /*                                                                       */
141   /*************************************************************************/
142
143
144   /*************************************************************************/
145   /*                                                                       */
146   /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
147   /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
148   /* messages during execution.                                            */
149   /*                                                                       */
150 #undef  FT_COMPONENT
151 #define FT_COMPONENT  trace_io
152
153   /* We use the macro STREAM_FILE for convenience to extract the       */
154   /* system-specific stream handle from a given FreeType stream object */
155 #define STREAM_FILE( stream )  ( (FILE*)stream->descriptor.pointer )
156
157
158   /*************************************************************************/
159   /*                                                                       */
160   /* <Function>                                                            */
161   /*    ft_close_stream                                                    */
162   /*                                                                       */
163   /* <Description>                                                         */
164   /*    The function to close a stream.                                    */
165   /*                                                                       */
166   /* <Input>                                                               */
167   /*    stream :: A pointer to the stream object.                          */
168   /*                                                                       */
169   FT_CALLBACK_DEF( void )
170   ft_close_stream( FT_Stream  stream )
171   {
172     fclose( STREAM_FILE( stream ) );
173
174     stream->descriptor.pointer = NULL;
175     stream->size               = 0;
176     stream->base               = 0;
177   }
178
179
180   /*************************************************************************/
181   /*                                                                       */
182   /* <Function>                                                            */
183   /*    ft_io_stream                                                       */
184   /*                                                                       */
185   /* <Description>                                                         */
186   /*    The function to open a stream.                                     */
187   /*                                                                       */
188   /* <Input>                                                               */
189   /*    stream :: A pointer to the stream object.                          */
190   /*                                                                       */
191   /*    offset :: The position in the data stream to start reading.        */
192   /*                                                                       */
193   /*    buffer :: The address of buffer to store the read data.            */
194   /*                                                                       */
195   /*    count  :: The number of bytes to read from the stream.             */
196   /*                                                                       */
197   /* <Return>                                                              */
198   /*    The number of bytes actually read.                                 */
199   /*                                                                       */
200   FT_CALLBACK_DEF( unsigned long )
201   ft_io_stream( FT_Stream       stream,
202                 unsigned long   offset,
203                 unsigned char*  buffer,
204                 unsigned long   count )
205   {
206     FILE*  file;
207
208
209     file = STREAM_FILE( stream );
210
211     fseek( file, offset, SEEK_SET );
212
213     return (unsigned long)fread( buffer, 1, count, file );
214   }
215
216
217   /* documentation is in ftobjs.h */
218
219   FT_EXPORT_DEF( FT_Error )
220   FT_New_Stream( const char*  filepathname,
221                  FT_Stream    astream )
222   {
223     FILE*  file;
224
225
226     if ( !astream )
227       return FT_Err_Invalid_Stream_Handle;
228
229     file = fopen( filepathname, "rb" );
230     if ( !file )
231     {
232       FT_ERROR(( "FT_New_Stream:" ));
233       FT_ERROR(( " could not open `%s'\n", filepathname ));
234
235       return FT_Err_Cannot_Open_Resource;
236     }
237
238     fseek( file, 0, SEEK_END );
239     astream->size = ftell( file );
240     fseek( file, 0, SEEK_SET );
241
242     astream->descriptor.pointer = file;
243     astream->pathname.pointer   = (char*)filepathname;
244     astream->pos                = 0;
245
246     astream->read  = ft_io_stream;
247     astream->close = ft_close_stream;
248
249     FT_TRACE1(( "FT_New_Stream:" ));
250     FT_TRACE1(( " opened `%s' (%d bytes) successfully\n",
251                 filepathname, astream->size ));
252
253     return FT_Err_Ok;
254   }
255
256
257   /* documentation is in ftobjs.h */
258
259   FT_EXPORT_DEF( FT_Memory )
260   FT_New_Memory( void )
261   {
262     FT_Memory  memory;
263
264
265     memory = (FT_Memory)malloc( sizeof ( *memory ) );
266     if ( memory )
267     {
268       memory->user    = 0;
269       memory->alloc   = ft_alloc;
270       memory->realloc = ft_realloc;
271       memory->free    = ft_free;
272     }
273
274     return memory;
275   }
276
277
278   /* documentation is in ftobjs.h */
279
280   FT_EXPORT_DEF( void )
281   FT_Done_Memory( FT_Memory  memory )
282   {
283     memory->free( memory, memory );
284   }
285
286
287 /* END */