]>
Commit | Line | Data |
---|---|---|
1 | /***************************************************************************/ | |
2 | /* */ | |
3 | /* ftsystem.c */ | |
4 | /* */ | |
5 | /* ANSI-specific FreeType low-level system interface (body). */ | |
6 | /* */ | |
7 | /* Copyright 1996-2000 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 <freetype/config/ftconfig.h> | |
29 | #include <freetype/internal/ftdebug.h> | |
30 | #include <freetype/ftsystem.h> | |
31 | #include <freetype/fterrors.h> | |
32 | #include <freetype/fttypes.h> | |
33 | ||
34 | #include <stdio.h> | |
35 | #include <stdlib.h> | |
36 | #include <string.h> | |
37 | ||
38 | ||
39 | /*************************************************************************/ | |
40 | /* */ | |
41 | /* MEMORY MANAGEMENT INTERFACE */ | |
42 | /* */ | |
43 | /*************************************************************************/ | |
44 | ||
45 | /*************************************************************************/ | |
46 | /* */ | |
47 | /* It is not necessary to do any error checking for the */ | |
48 | /* allocation-related functions. This will be done by the higher level */ | |
49 | /* routines like FT_Alloc() or FT_Realloc(). */ | |
50 | /* */ | |
51 | /*************************************************************************/ | |
52 | ||
53 | ||
54 | /*************************************************************************/ | |
55 | /* */ | |
56 | /* <Function> */ | |
57 | /* ft_alloc */ | |
58 | /* */ | |
59 | /* <Description> */ | |
60 | /* The memory allocation function. */ | |
61 | /* */ | |
62 | /* <Input> */ | |
63 | /* memory :: A pointer to the memory object. */ | |
64 | /* */ | |
65 | /* size :: The requested size in bytes. */ | |
66 | /* */ | |
67 | /* <Return> */ | |
68 | /* block :: The address of newly allocated block. */ | |
69 | /* */ | |
70 | static | |
71 | void* ft_alloc( FT_Memory memory, | |
72 | long size ) | |
73 | { | |
74 | FT_UNUSED( memory ); | |
75 | ||
76 | return malloc( size ); | |
77 | } | |
78 | ||
79 | ||
80 | /*************************************************************************/ | |
81 | /* */ | |
82 | /* <Function> */ | |
83 | /* ft_realloc */ | |
84 | /* */ | |
85 | /* <Description> */ | |
86 | /* The memory reallocation function. */ | |
87 | /* */ | |
88 | /* <Input> */ | |
89 | /* memory :: A pointer to the memory object. */ | |
90 | /* */ | |
91 | /* cur_size :: The current size of the allocated memory block. */ | |
92 | /* */ | |
93 | /* new_size :: The newly requested size in bytes. */ | |
94 | /* */ | |
95 | /* block :: The current address of the block in memory. */ | |
96 | /* */ | |
97 | /* <Return> */ | |
98 | /* The address of the reallocated memory block. */ | |
99 | /* */ | |
100 | static | |
101 | void* ft_realloc( FT_Memory memory, | |
102 | long cur_size, | |
103 | long new_size, | |
104 | void* block ) | |
105 | { | |
106 | FT_UNUSED( memory ); | |
107 | FT_UNUSED( cur_size ); | |
108 | ||
109 | return realloc( block, new_size ); | |
110 | } | |
111 | ||
112 | ||
113 | /*************************************************************************/ | |
114 | /* */ | |
115 | /* <Function> */ | |
116 | /* ft_free */ | |
117 | /* */ | |
118 | /* <Description> */ | |
119 | /* The memory release function. */ | |
120 | /* */ | |
121 | /* <Input> */ | |
122 | /* memory :: A pointer to the memory object. */ | |
123 | /* */ | |
124 | /* block :: The address of block in memory to be freed. */ | |
125 | /* */ | |
126 | static | |
127 | void ft_free( FT_Memory memory, | |
128 | void* block ) | |
129 | { | |
130 | FT_UNUSED( memory ); | |
131 | ||
132 | free( block ); | |
133 | } | |
134 | ||
135 | ||
136 | /*************************************************************************/ | |
137 | /* */ | |
138 | /* RESOURCE MANAGEMENT INTERFACE */ | |
139 | /* */ | |
140 | /*************************************************************************/ | |
141 | ||
142 | ||
143 | /*************************************************************************/ | |
144 | /* */ | |
145 | /* The macro FT_COMPONENT is used in trace mode. It is an implicit */ | |
146 | /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ | |
147 | /* messages during execution. */ | |
148 | /* */ | |
149 | #undef FT_COMPONENT | |
150 | #define FT_COMPONENT trace_io | |
151 | ||
152 | /* We use the macro STREAM_FILE for convenience to extract the */ | |
153 | /* system-specific stream handle from a given FreeType stream object */ | |
154 | #define STREAM_FILE( stream ) ( (FILE*)stream->descriptor.pointer ) | |
155 | ||
156 | ||
157 | /*************************************************************************/ | |
158 | /* */ | |
159 | /* <Function> */ | |
160 | /* ft_close_stream */ | |
161 | /* */ | |
162 | /* <Description> */ | |
163 | /* The function to close a stream. */ | |
164 | /* */ | |
165 | /* <Input> */ | |
166 | /* stream :: A pointer to the stream object. */ | |
167 | /* */ | |
168 | static | |
169 | void ft_close_stream( FT_Stream stream ) | |
170 | { | |
171 | fclose( STREAM_FILE( stream ) ); | |
172 | ||
173 | stream->descriptor.pointer = NULL; | |
174 | stream->size = 0; | |
175 | stream->base = 0; | |
176 | } | |
177 | ||
178 | ||
179 | /*************************************************************************/ | |
180 | /* */ | |
181 | /* <Function> */ | |
182 | /* ft_io_stream */ | |
183 | /* */ | |
184 | /* <Description> */ | |
185 | /* The function to open a stream. */ | |
186 | /* */ | |
187 | /* <Input> */ | |
188 | /* stream :: A pointer to the stream object. */ | |
189 | /* */ | |
190 | /* offset :: The position in the data stream to start reading. */ | |
191 | /* */ | |
192 | /* buffer :: The address of buffer to store the read data. */ | |
193 | /* */ | |
194 | /* count :: The number of bytes to read from the stream. */ | |
195 | /* */ | |
196 | /* <Return> */ | |
197 | /* The number of bytes actually read. */ | |
198 | /* */ | |
199 | static | |
200 | unsigned long ft_io_stream( FT_Stream stream, | |
201 | unsigned long offset, | |
202 | unsigned char* buffer, | |
203 | unsigned long count ) | |
204 | { | |
205 | FILE* file; | |
206 | ||
207 | ||
208 | file = STREAM_FILE( stream ); | |
209 | ||
210 | fseek( file, offset, SEEK_SET ); | |
211 | ||
212 | return (unsigned long)fread( buffer, 1, count, file ); | |
213 | } | |
214 | ||
215 | ||
216 | /*************************************************************************/ | |
217 | /* */ | |
218 | /* <Function> */ | |
219 | /* FT_New_Stream */ | |
220 | /* */ | |
221 | /* <Description> */ | |
222 | /* Creates a new stream object. */ | |
223 | /* */ | |
224 | /* <Input> */ | |
225 | /* filepathname :: The name of the stream (usually a file) to be */ | |
226 | /* opened. */ | |
227 | /* */ | |
228 | /* stream :: A pointer to the stream object. */ | |
229 | /* */ | |
230 | /* <Return> */ | |
231 | /* FreeType error code. 0 means success. */ | |
232 | /* */ | |
233 | FT_EXPORT_FUNC( FT_Error ) FT_New_Stream( const char* filepathname, | |
234 | FT_Stream stream ) | |
235 | { | |
236 | FILE* file; | |
237 | ||
238 | ||
239 | if ( !stream ) | |
240 | return FT_Err_Invalid_Stream_Handle; | |
241 | ||
242 | file = fopen( filepathname, "rb" ); | |
243 | if ( !file ) | |
244 | { | |
245 | FT_ERROR(( "FT_New_Stream:" )); | |
246 | FT_ERROR(( " could not open `%s'\n", filepathname )); | |
247 | ||
248 | return FT_Err_Cannot_Open_Resource; | |
249 | } | |
250 | ||
251 | fseek( file, 0, SEEK_END ); | |
252 | stream->size = ftell( file ); | |
253 | fseek( file, 0, SEEK_SET ); | |
254 | ||
255 | stream->descriptor.pointer = file; | |
256 | stream->pathname.pointer = (char*)filepathname; | |
257 | stream->pos = 0; | |
258 | ||
259 | stream->read = ft_io_stream; | |
260 | stream->close = ft_close_stream; | |
261 | ||
262 | FT_TRACE1(( "FT_New_Stream:" )); | |
263 | FT_TRACE1(( " opened `%s' (%d bytes) successfully\n", | |
264 | filepathname, stream->size )); | |
265 | ||
266 | return FT_Err_Ok; | |
267 | } | |
268 | ||
269 | ||
270 | /*************************************************************************/ | |
271 | /* */ | |
272 | /* <Function> */ | |
273 | /* FT_New_Memory */ | |
274 | /* */ | |
275 | /* <Description> */ | |
276 | /* Creates a new memory object. */ | |
277 | /* */ | |
278 | /* <Return> */ | |
279 | /* A pointer to the new memory object. 0 in case of error. */ | |
280 | /* */ | |
281 | FT_EXPORT_FUNC( FT_Memory ) FT_New_Memory( void ) | |
282 | { | |
283 | FT_Memory memory; | |
284 | ||
285 | ||
286 | memory = (FT_Memory)malloc( sizeof ( *memory ) ); | |
287 | if ( memory ) | |
288 | { | |
289 | memory->user = 0; | |
290 | memory->alloc = ft_alloc; | |
291 | memory->realloc = ft_realloc; | |
292 | memory->free = ft_free; | |
293 | } | |
294 | ||
295 | return memory; | |
296 | } | |
297 | ||
298 | ||
299 | /* END */ |