-
Notifications
You must be signed in to change notification settings - Fork 219
/
ktxfilestream.c
154 lines (134 loc) · 4.78 KB
/
ktxfilestream.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* -*- tab-width: 4; -*- */
/* vi: set sw=2 ts=4: */
/**
* @file
* @~English
*
* @brief Implementation of ktxStream for FILE.
*
* @author Maksim Kolesin, Under Development
* @author Georg Kolling, Imagination Technology
* @author Mark Callow, HI Corporation
*/
/*
Copyright (c) 2010 The Khronos Group Inc.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and/or associated documentation files (the
"Materials"), to deal in the Materials without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Materials, and to
permit persons to whom the Materials are furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
unaltered in all copies or substantial portions of the Materials.
Any additions, deletions, or changes to the original source files
must be clearly indicated in accompanying documentation.
If only executable code is distributed, then the accompanying
documentation must state that "this software is based in part on the
work of the Khronos Group".
THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
*/
#include <string.h>
#include <stdlib.h>
#include "KHR/khrplatform.h"
#include "ktx.h"
#include "ktxint.h"
#include "ktxfilestream.h"
/**
* @internal
* @~English
* @brief Read bytes from a ktxFileStream.
*
* @param [out] dst pointer to a block of memory with a size
of at least @p size bytes, converted to a void*.
* @param [in] size total size of bytes to be read.
* @param [in] src pointer to a FILE object, converted to a void*, that specifies an input stream.
*
* @return KTX_SUCCESS on success, other KTX_* enum values on error.
*
* @exception KTX_INVALID_VALUE @p dst is @c NULL or @p src is @c NULL.
* @exception KTX_UNEXPECTED_END_OF_FILE the file does not contain the expected amount of data.
*/
static
KTX_error_code ktxFileStream_read(void* dst, const GLsizei size, void* src)
{
if (!dst || !src)
return KTX_INVALID_VALUE;
if (fread(dst, size, 1, (FILE*)src) != 1)
return KTX_UNEXPECTED_END_OF_FILE;
return KTX_SUCCESS;
}
/**
* @internal
* @~English
* @brief Skip bytes in a ktxFileStream.
*
* @param [in] count number of bytes to be skipped.
* @param [in] src pointer to a FILE object, converted to a void*, that specifies an input stream.
*
* @return KTX_SUCCESS on success, other KTX_* enum values on error.
*
* @exception KTX_INVALID_VALUE @p dst is @c NULL or @p count is less than zero.
* @exception KTX_UNEXPECTED_END_OF_FILE the file does not contain the expected amount of data.
*/
static
KTX_error_code ktxFileStream_skip(const GLsizei count, void* src)
{
if (!src || (count < 0))
return KTX_INVALID_VALUE;
if (fseek((FILE*)src, count, SEEK_CUR) != 0)
return KTX_UNEXPECTED_END_OF_FILE;
return KTX_SUCCESS;
}
/**
* @internal
* @~English
* @brief Write bytes to a ktxFileStream.
*
* @param [in] src pointer to the array of elements to be written, converted to a const void*.
* @param [in] size size in bytes of each element to be written.
* @param [in] count number of elements, each one with a @p size of size bytes.
* @param [out] dst pointer to a FILE object, converted to a void*, that specifies an output stream.
*
* @return KTX_SUCCESS on success, other KTX_* enum values on error.
*
* @exception KTX_INVALID_VALUE @p dst is @c NULL or @p src is @c NULL.
* @exception KTX_FILE_WRITE_ERROR a system error occurred while writing the file.
*/
static
KTX_error_code ktxFileStream_write(const void *src, const GLsizei size, const GLsizei count, void* dst)
{
if (!dst || !src)
return KTX_INVALID_VALUE;
if (fwrite(src, size, count, (FILE*)dst) != count)
return KTX_FILE_WRITE_ERROR;
return KTX_SUCCESS;
}
/**
* @internal
* @~English
* @brief Initializes a ktxFileStream.
*
* @param [in] stream
* @param [in] file
*
* @return KTX_SUCCESS on success, KTX_INVALID_VALUE on error.
*
* @exception KTX_INVALID_VALUE @p stream is @c NULL or @p file is @c NULL.
*/
KTX_error_code ktxFileInit(struct ktxStream* stream, FILE* file)
{
if (!stream || !file)
return KTX_INVALID_VALUE;
stream->src = (void*)file;
stream->read = ktxFileStream_read;
stream->skip = ktxFileStream_skip;
stream->write = ktxFileStream_write;
return KTX_SUCCESS;
}