Spinning Topp Logo BlackTopp Studios
inc
filestream.cpp
1 // © Copyright 2010 - 2016 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  {
55  IOStream(&this->FileBuffer)
56  { /*this->init(&this->FileBuffer);*/ }
57 
58  FileStream::FileStream(const String& File, const Whole Mode) :
59  IOStream(&this->FileBuffer)
60  { /*this->init(&this->FileBuffer);*/ this->OpenFile(File,Mode); }
61 
62  FileStream::FileStream(const String& FileName, const String& FilePath, const Whole Mode) :
63  IOStream(&this->FileBuffer)
64  { /*this->init(&this->FileBuffer);*/ this->OpenFile(FileName,FilePath,Mode); }
65 
67  { if( this->IsOpenToFile() ) this->CloseFile(); }
68 
69  ///////////////////////////////////////////////////////////////////////////////
70  // Utility Methods
71 
72  void FileStream::OpenFile(const String& File, const Whole Mode)
73  {
74  //this->open(File.c_str(),static_cast<const std::ios_base::openmode>(Mode));
75  if( !this->FileBuffer.open(File.c_str(),static_cast<const std::ios_base::openmode>(Mode)) ) {
76  this->setstate(ios_base::failbit);
77  }else{
78  this->clear();
79  }
80 
81  if( !this->IsOpenToFile() ) {
82  MEZZ_EXCEPTION(ExceptionBase::IO_FILE_NOT_FOUND_EXCEPTION,"Unable to create or locate file \"" + File + "\".");
83  }
84 
86  this->Size = (StreamSize)this->GetReadPosition();
87  this->flush();
88  this->SetStreamPosition(0);
89  }
90 
91  void FileStream::OpenFile(const String& FileName, const String& FilePath, const Whole Mode)
92  {
93  String FullPath;
94  char Check = FilePath.at(FilePath.size() - 1);
95  #ifdef MEZZ_WINDOWS
96  char SysSlash = '\\';
97  #else
98  char SysSlash = '/';
99  #endif
100  if( SysSlash != Check ) {
101  FullPath = FilePath + SysSlash + FileName;
102  }else{
103  FullPath = FilePath + FileName;
104  }
105 
106  this->OpenFile(FullPath,Mode);
107  }
108 
110  {
111  return this->FileBuffer.is_open();
112  }
113 
115  {
116  this->OpenFileName.clear();
117  this->Flags = Resource::SF_None;
118 
119  //this->close();
120  if( !this->FileBuffer.close() ) {
121  this->setstate(std::ios_base::failbit);
122  }
123  }
124 
126  { return this->OpenFileName; }
127 
129  { return this->Flags; }
130 
131  ///////////////////////////////////////////////////////////////////////////////
132  // Stream Base Operations
133 
135  { return this->Size; }
136  }//Resource
137 }//Mezzanine
138 
139 #endif
void OpenFile(const String &File, const Whole Mode=Resource::SF_Read|Resource::SF_Write)
Opens this stream to a file. the stream fails to open the file specified, a IO_FILE_NOT_FOUND_EXCEPTI...
Definition: filestream.cpp:72
StreamSize Size
The size of the stream.
Definition: filestream.h:71
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
Declaration of FileStream.
Whole Flags
The type of access this stream has to the file.
Definition: filestream.h:68
Thrown when a file was expected to be there, but was not.
Definition: exception.h:77
Base class for streams that support both read and write operations.
Definition: datastream.h:263
This implements the exception hiearchy for Mezzanine.
virtual StreamPos GetReadPosition()
Gets the current read position in this stream.
Definition: datastream.cpp:192
virtual void SetReadPosition(StreamPos Position)
Sets the position of the read cursor explicitly.
Definition: datastream.cpp:186
String OpenFileName
The path and name of the file this stream is currently open to.
Definition: filestream.h:65
void CloseFile()
Closes the file that is currently opened.
Definition: filestream.cpp:114
const String & GetFilePathAndName() const
Gets the path and name of the file that this stream is currently open to.
Definition: filestream.cpp:125
The end of the stream.
Definition: datastream.h:81
Boole IsOpenToFile() const
Gets whether or not this stream is currently open to a file.
Definition: filestream.cpp:109
virtual ~FileStream()
Class destructor.
Definition: filestream.cpp:66
FileStream()
Blank constructor.
Definition: filestream.cpp:54
virtual StreamSize GetSize() const
Definition: filestream.cpp:134
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
unsigned long Whole
Whole is an unsigned integer, it will be at least 32bits in size.
Definition: datatypes.h:151
std::streamsize StreamSize
Convenience define for the stream size datatype.
Definition: datastream.h:61
std::filebuf FileBuffer
The buffer object containing most or all of the functionality for this stream.
Definition: filestream.h:61
virtual void SetStreamPosition(StreamPos Position)
Sets the position of the read and write cursors explicitly.
Definition: datastream.cpp:219
Error/no special initialization.
Definition: datastream.h:67
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
Whole GetSteamFlags() const
Gets the flags that were used to open the file.
Definition: filestream.cpp:128