| 1 | /** |
| 2 | * Copyright (c) 2006-2023 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 | |
| 32 | love::Type FileData::type("FileData" , &Data::type); |
| 33 | |
| 34 | FileData::FileData(uint64 size, const std::string &filename) |
| 35 | : data(nullptr) |
| 36 | , size((size_t) size) |
| 37 | , filename(filename) |
| 38 | { |
| 39 | try |
| 40 | { |
| 41 | data = new char[(size_t) size]; |
| 42 | } |
| 43 | catch (std::bad_alloc &) |
| 44 | { |
| 45 | throw love::Exception("Out of memory." ); |
| 46 | } |
| 47 | |
| 48 | size_t dotpos = filename.rfind('.'); |
| 49 | |
| 50 | if (dotpos != std::string::npos) |
| 51 | { |
| 52 | extension = filename.substr(dotpos + 1); |
| 53 | name = filename.substr(0, dotpos); |
| 54 | } |
| 55 | else |
| 56 | name = filename; |
| 57 | } |
| 58 | |
| 59 | FileData::FileData(const FileData &c) |
| 60 | : data(nullptr) |
| 61 | , size(c.size) |
| 62 | , filename(c.filename) |
| 63 | , extension(c.extension) |
| 64 | , name(c.name) |
| 65 | { |
| 66 | try |
| 67 | { |
| 68 | data = new char[(size_t) size]; |
| 69 | } |
| 70 | catch (std::bad_alloc &) |
| 71 | { |
| 72 | throw love::Exception("Out of memory." ); |
| 73 | } |
| 74 | memcpy(data, c.data, size); |
| 75 | } |
| 76 | |
| 77 | FileData::~FileData() |
| 78 | { |
| 79 | delete [] data; |
| 80 | } |
| 81 | |
| 82 | FileData *FileData::clone() const |
| 83 | { |
| 84 | return new FileData(*this); |
| 85 | } |
| 86 | |
| 87 | void *FileData::getData() const |
| 88 | { |
| 89 | return data; |
| 90 | } |
| 91 | |
| 92 | size_t FileData::getSize() const |
| 93 | { |
| 94 | size_t sizemax = std::numeric_limits<size_t>::max(); |
| 95 | return size > sizemax ? sizemax : (size_t) size; |
| 96 | } |
| 97 | |
| 98 | const std::string &FileData::getFilename() const |
| 99 | { |
| 100 | return filename; |
| 101 | } |
| 102 | |
| 103 | const std::string &FileData::getExtension() const |
| 104 | { |
| 105 | return extension; |
| 106 | } |
| 107 | |
| 108 | const std::string &FileData::getName() const |
| 109 | { |
| 110 | return name; |
| 111 | } |
| 112 | |
| 113 | } // filesystem |
| 114 | } // love |
| 115 | |