MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
filestream.cpp
1 // © Copyright 2010 - 2014 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 #ifndef _resourcefilestream_cpp
41 #define _resourcefilestream_cpp
42 
43 #include "Resource/filestream.h"
44 #include "stringtool.h"
45 #include "exception.h"
46 
47 #include <fstream>
48 #include <algorithm>
49 
50 namespace Mezzanine
51 {
52  namespace Resource
53  {
54 #ifdef USENEWDATASTREAM
55  /// @todo Implement this
56 #else //USENEWDATASTREAM
57  FileStream::FileStream(std::fstream* Stream, const UInt16 Mode)
58  : DataStream( Mode ),
59  StandardStream(Stream)
60  {
61  StandardStream->seekg(0,std::ios_base::end);
62  Size = (size_t)StandardStream->tellg();
63  StandardStream->flush();
64  this->SetStreamPosition(0);
65  }
66 
67  FileStream::FileStream(const String& FileName, const String& Path, const UInt16 Mode)
68  : DataStream( Mode )
69  {
70  StandardStream = new std::fstream();
71 
72  std::ios_base::openmode Options = (std::ios_base::in | std::ios_base::out);
74  {
75  Options = (Options | std::ios_base::binary);
76  }
78  {
79  Options = (Options | std::ios_base::trunc);
80  }
81 
82  String FullPath;
83  char Check = Path.at(Path.size() - 1);
84  #ifdef WINDOWS
85  char SysSlash = '\\';
86  #else
87  char SysSlash = '/';
88  #endif
89  if( SysSlash != Check )
90  FullPath = Path+SysSlash+FileName;
91  else
92  FullPath = Path+FileName;
93  StandardStream->open(FullPath.c_str(),Options);
94 
95  if(!StandardStream->is_open())
96  {
97  MEZZ_EXCEPTION(Exception::IO_FILE_NOT_FOUND_EXCEPTION,"Unable to create or locate file \""+FileName+"\".");
98  }
99 
100  StandardStream->seekg(0,std::ios_base::end);
101  Size = (size_t)StandardStream->tellg();
102  StandardStream->flush();
103  this->SetStreamPosition(0);
104  }
105 
107  {
108  Close();
109  }
110 
111  ///////////////////////////////////////////////////////////////////////////////
112  // Stream Access and Manipulation
113 
114  size_t FileStream::Read(void* Buffer, const size_t& Count)
115  {
116  if(IsReadable())
117  {
118  StandardStream->read(static_cast<char*>(Buffer),static_cast<std::streamsize>(Count));
119  StandardStream->seekp(static_cast<std::ifstream::pos_type>(StandardStream->gcount()),std::ios::cur);
120  return StandardStream->gcount();
121  }else{
122  return 0;
123  }
124  }
125 
126  size_t FileStream::Write(const void* Buffer, const size_t& Count)
127  {
128  size_t Written = 0;
129  if(IsWriteable())
130  {
131  StandardStream->write(static_cast<const char*>(Buffer), static_cast<std::streamsize>(Count));
132  StandardStream->seekg(static_cast<std::ifstream::pos_type>(Count),std::ios::cur);
133  Written = Count;
134  }
135  return Written;
136  }
137 
138  void FileStream::Advance(const StreamOff Count)
139  {
140  StandardStream->clear();
141  this->SetStreamPosition(Count,SO_Current);
142  }
143 
145  {
146  StandardStream->clear();
147  StandardStream->seekg(Position);
148  StandardStream->seekp(Position);
149  }
150 
152  {
153  StandardStream->clear();
154  StandardStream->seekg(Offset,std::ios::cur);
155  StandardStream->seekp(Offset,std::ios::cur);
156  }
157 
159  {
160  if(Read) return StandardStream->tellg();
161  else return StandardStream->tellp();
162  }
163 
164  bool FileStream::EoF() const
165  {
166  return StandardStream->eof();
167  }
168 
170  {
171  if(StandardStream->is_open())
172  {
173  StandardStream->flush();
174  StandardStream->close();
175  }
176  }
177 #endif
178  }//Resource
179 }//Mezzanine
180 
181 #endif