FaceGen 3 SDKs Reference
Loading...
Searching...
No Matches
binaryFile.cpp
1//
2// Copyright (c) Singular Inversions Inc. 2000
3//
4// Authors: Andrew Beatty
5// Created: Sept. 4, 2000
6//
7
8#include "stdafx.h"
9
10#include "binaryFile.hpp"
11#include "FgFile.hpp"
12#include "FgSerial.hpp"
13
14using namespace std;
15
16namespace Fg {
17
18FutBinaryFileC::FutBinaryFileC(
19 string a_fileId)
20{
21 fileId = a_fileId;
22 FGASSERT(fileId.size() == 8);
23 fptr = NULL;
24 fileId[5] = '0'; // No encryption
25}
26
27// Shouldn't use this function if file is already openned.
28void
29FutBinaryFileC::changeFileId(string const &newFileId)
30{
31 FGASSERT(newFileId.size() == 8);
32 FGASSERT(fptr == NULL);
33
34 fileId = newFileId;
35 fileId[5] = '0';
36}
37
38// Note that this function closes the file and reports an error if the file ID
39// does not agree.
40bool
41FutBinaryFileC::openForRead(
42 String8 const & a_fname)
43{
44 close(); // Ensure any previous are closed.
45
46 fptr = openFile(a_fname,false);
47 if (fptr == NULL)
48 fgThrow("Unable to read-open binary file",a_fname);
49
50 vector<char> buff(fileId.size()+1);
51
52 if (!readHeader(&buff[0],static_cast<uint>(fileId.size()),1))
53 return false;
54
55 vector<char> buff2 = buff;
56 buff2[5] = fileId[5];
57
58 if (fileId != &buff2[0])
59 {
60 close();
61 fgThrow("Incorrect file type",a_fname);
62 }
63
64 if (buff[5] > '0' && fileId[5] != buff[5])
65 {
66 close();
67 fgThrow("Incorrect file type",a_fname);
68 }
69 fileId[5] = buff[5];
70
71 m_fname = a_fname;
72 return true;
73}
74
75// Return false if error.
76bool
77FutBinaryFileC::openForOverwrite(
78 String8 const & a_fname)
79{
80 close(); // Ensure any previous are closed.
81
82 fptr = openFile(a_fname,true);
83 if (fptr == NULL)
84 fgThrow("Unable to write-open file",a_fname);
85
86 // Save file id. (No encryption for writing fileID).
87 if (!writeHeader(fileId.c_str(),static_cast<uint>(fileId.size()),1))
88 {
89 close();
90 fgThrow("Unable to write to file",a_fname);
91 }
92
93 m_fname = a_fname; // Save name for error reporting.
94 return true;
95}
96
97bool
98FutBinaryFileC::writeHeader(const void *data, uint size, uint num)
99{
100 if (fptr == NULL) return false; // This way the client doesn't have
101 // to check for an error after every
102 // single write - can just check at the
103 // end.
104 if (fwrite(data,size,num,fptr) != num)
105 {
106 close();
107 fgThrow("Unable to write header data to file");
108 }
109
110 return true;
111}
112
113bool
114FutBinaryFileC::readHeader(void *data, uint size, uint num)
115{
116 if (fptr == NULL) return false;
117 if (fread(data,size,num,fptr) != num)
118 {
119 close();
120 fgThrow("Unable to read header data from file");
121 }
122 return true;
123}
124
125// This function just calls writeHeader(). It is defined as a separate
126// function so that it can be overided by derived classes.
127bool
128FutBinaryFileC::write(
129 const void *data, // Pointer to data
130 uint size, // size of each item
131 uint num) // number of data items in sequence
132{
133 return writeHeader(data,size,num);
134}
135
136// This function just calls readHeader(). It is defined as a separate
137// function so that it can be overided by derived classes.
138bool
139FutBinaryFileC::read(
140
141 void *data, // Pointer to data
142 uint size, // size of each item
143 uint num) // number of data items in sequence
144{
145 return readHeader(data,size,num);
146}
147
148
149// Moves current file pointer to a specified offset location from the
150// beginning of file.
151// Returns true if successful.
152bool
153FutBinaryFileC::seek(long offsetFromBeginning)
154{
155 if (fptr == NULL) return false;
156 if (fseek(fptr,offsetFromBeginning,SEEK_SET))
157 {
158 close();
159 fgThrow("Unable to seek-read from file");
160 }
161 return true;
162}
163
164void
165FutBinaryFileC::close()
166{
167 if (fptr != NULL)
168 fclose(fptr);
169
170 m_fname = "";
171 fptr = NULL;
172}
173
174bool
175FutBinaryFileC::eof()
176{
177 FGASSERT(fptr != NULL);
178 long pos = ftell(fptr);
179 bool ret = (getc(fptr) == EOF) ? true : false;
180 fseek(fptr,pos,0);
181 return ret;
182}
183
184}