Simple Image Loading LibrarY  0.1.0
SILLYFileDataSource.cpp
1 /***********************************************************************
2  filename: SILLYFileDataSource.cpp
3  created: 20 Jul 2006
4  author: Olivier Delannoy
5 
6  purpose: Implementation of the FileDataSource methods
7 *************************************************************************/
8 /***************************************************************************
9  * Copyright (C) 2004 - 2006 Paul D Turner & The CEGUI Development Team
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining
12  * a copy of this software and associated documentation files (the
13  * "Software"), to deal in the Software without restriction, including
14  * without limitation the rights to use, copy, modify, merge, publish,
15  * distribute, sublicense, and/or sell copies of the Software, and to
16  * permit persons to whom the Software is furnished to do so, subject to
17  * the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be
20  * included in all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28  * OTHER DEALINGS IN THE SOFTWARE.
29  ***************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33 
34 #include "SILLYFileDataSource.h"
35 
36 #ifndef SILLY_OPT_INLINE
37 #define inline
38 #include "SILLYFileDataSource.icpp"
39 #undef inline
40 #endif
41 #include <stdio.h>
42 // Start section of namespace SILLY
43 namespace SILLY
44 {
45 
46 FileDataSource::FileDataSource(const char* filename)
47  : d_error(true), d_bytes(0), d_size(0)
48 {
49  FILE* data = fopen(filename, "rb");
50  if (data)
51  {
52  long status = fseek(data, 0, SEEK_END);
53  if (status == -1)
54  {
55  fclose(data);
56  return;
57  }
58  status = ftell(data);
59  if (status == -1)
60  {
61  fclose(data);
62  return;
63  }
64  d_size = status;
65  if (d_size)
66  d_bytes = new byte[d_size];
67  if (! d_bytes)
68  {
69  fclose(data);
70  return;
71  }
72  rewind(data);
73  status = fread(reinterpret_cast<void*>(d_bytes), 1, d_size, data);
74  if (status != d_size)
75  {
76  delete [] d_bytes;
77  d_bytes = 0;
78  fclose(data);
79  }
80  fclose(data);
81  d_error = false;
82  }
83 }
84 
85 
87 {
88  delete [] d_bytes;
89 }
90 
91 
92 } // End section of namespace SILLY
FileDataSource(const char *filename)
Create a data source from an existing file.
unsigned char byte
Typename for a byte.
Definition: SILLYBase.h:80
Simple Image Loading LibrarY namespace.