1 /**
2  * Copyright (c) 2006-2016 LOVE Development Team
3  *
4  * This software is provided 'as-is', without any express or implied
5  * warranty.  In no event will the authors be held liable for any damages
6  * arising from the use of this software.
7  *
8  * Permission is granted to anyone to use this software for any purpose,
9  * including commercial applications, and to alter it and redistribute it
10  * freely, subject to the following restrictions:
11  *
12  * 1. The origin of this software must not be misrepresented; you must not
13  *    claim that you wrote the original software. If you use this software
14  *    in a product, an acknowledgment in the product documentation would be
15  *    appreciated but is not required.
16  * 2. Altered source versions must be plainly marked as such, and must not be
17  *    misrepresented as being the original software.
18  * 3. This notice may not be removed or altered from any source distribution.
19  **/
20 
21 #include "FileData.h"
22 
23 // C++
24 #include <iostream>
25 #include <limits>
26 
27 namespace love
28 {
29 namespace filesystem
30 {
31 
FileData(uint64 size,const std::string & filename)32 FileData::FileData(uint64 size, const std::string &filename)
33 	: data(nullptr)
34 	, size((size_t) size)
35 	, filename(filename)
36 {
37 	try
38 	{
39 		data = new char[(size_t) size];
40 	}
41 	catch (std::bad_alloc &)
42 	{
43 		throw love::Exception("Out of memory.");
44 	}
45 
46 	if (filename.rfind('.') != std::string::npos)
47 		extension = filename.substr(filename.rfind('.')+1);
48 }
49 
~FileData()50 FileData::~FileData()
51 {
52 	delete [] data;
53 }
54 
getData() const55 void *FileData::getData() const
56 {
57 	return (void *)data;
58 }
59 
getSize() const60 size_t FileData::getSize() const
61 {
62 	size_t sizemax = std::numeric_limits<size_t>::max();
63 	return size > sizemax ? sizemax : (size_t) size;
64 }
65 
getFilename() const66 const std::string &FileData::getFilename() const
67 {
68 	return filename;
69 }
70 
getExtension() const71 const std::string &FileData::getExtension() const
72 {
73 	return extension;
74 }
75 
getConstant(const char * in,Decoder & out)76 bool FileData::getConstant(const char *in, Decoder &out)
77 {
78 	return decoders.find(in, out);
79 }
80 
getConstant(Decoder in,const char * & out)81 bool FileData::getConstant(Decoder in, const char *&out)
82 {
83 	return decoders.find(in, out);
84 }
85 
86 StringMap<FileData::Decoder, FileData::DECODE_MAX_ENUM>::Entry FileData::decoderEntries[] =
87 {
88 	{"file", FileData::FILE},
89 	{"base64", FileData::BASE64},
90 };
91 
92 StringMap<FileData::Decoder, FileData::DECODE_MAX_ENUM> FileData::decoders(FileData::decoderEntries, sizeof(FileData::decoderEntries));
93 
94 } // filesystem
95 } // love
96