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 "File.h" |
22 | |
23 | namespace love |
24 | { |
25 | namespace filesystem |
26 | { |
27 | |
28 | love::Type File::type("File" , &Object::type); |
29 | |
30 | File::~File() |
31 | { |
32 | } |
33 | |
34 | FileData *File::read(int64 size) |
35 | { |
36 | bool isopen = isOpen(); |
37 | |
38 | if (!isopen && !open(MODE_READ)) |
39 | throw love::Exception("Could not read file %s." , getFilename().c_str()); |
40 | |
41 | int64 max = getSize(); |
42 | int64 cur = tell(); |
43 | size = (size == ALL) ? max : size; |
44 | |
45 | if (size < 0) |
46 | throw love::Exception("Invalid read size." ); |
47 | |
48 | // Clamping because the file offset may be in a weird position. |
49 | if (cur < 0) |
50 | cur = 0; |
51 | else if (cur > max) |
52 | cur = max; |
53 | |
54 | if (cur + size > max) |
55 | size = max - cur; |
56 | |
57 | FileData *fileData = new FileData(size, getFilename()); |
58 | int64 bytesRead = read(fileData->getData(), size); |
59 | |
60 | if (bytesRead < 0 || (bytesRead == 0 && bytesRead != size)) |
61 | { |
62 | delete fileData; |
63 | throw love::Exception("Could not read from file." ); |
64 | } |
65 | |
66 | if (bytesRead < size) |
67 | { |
68 | FileData *tmpFileData = new FileData(bytesRead, getFilename()); |
69 | memcpy(tmpFileData->getData(), fileData->getData(), (size_t) bytesRead); |
70 | fileData->release(); |
71 | fileData = tmpFileData; |
72 | } |
73 | |
74 | if (!isopen) |
75 | close(); |
76 | |
77 | return fileData; |
78 | } |
79 | |
80 | bool File::write(const Data *data, int64 size) |
81 | { |
82 | return write(data->getData(), (size == ALL) ? data->getSize() : size); |
83 | } |
84 | |
85 | std::string File::getExtension() const |
86 | { |
87 | const std::string &filename = getFilename(); |
88 | std::string::size_type idx = filename.rfind('.'); |
89 | |
90 | if (idx != std::string::npos) |
91 | return filename.substr(idx+1); |
92 | else |
93 | return std::string(); |
94 | } |
95 | |
96 | bool File::getConstant(const char *in, Mode &out) |
97 | { |
98 | return modes.find(in, out); |
99 | } |
100 | |
101 | bool File::getConstant(Mode in, const char *&out) |
102 | { |
103 | return modes.find(in, out); |
104 | } |
105 | |
106 | std::vector<std::string> File::getConstants(Mode) |
107 | { |
108 | return modes.getNames(); |
109 | } |
110 | |
111 | bool File::getConstant(const char *in, BufferMode &out) |
112 | { |
113 | return bufferModes.find(in, out); |
114 | } |
115 | |
116 | bool File::getConstant(BufferMode in, const char *&out) |
117 | { |
118 | return bufferModes.find(in, out); |
119 | } |
120 | |
121 | std::vector<std::string> File::getConstants(BufferMode) |
122 | { |
123 | return bufferModes.getNames(); |
124 | } |
125 | |
126 | StringMap<File::Mode, File::MODE_MAX_ENUM>::Entry File::modeEntries[] = |
127 | { |
128 | { "c" , MODE_CLOSED }, |
129 | { "r" , MODE_READ }, |
130 | { "w" , MODE_WRITE }, |
131 | { "a" , MODE_APPEND }, |
132 | }; |
133 | |
134 | StringMap<File::Mode, File::MODE_MAX_ENUM> File::modes(File::modeEntries, sizeof(File::modeEntries)); |
135 | |
136 | StringMap<File::BufferMode, File::BUFFER_MAX_ENUM>::Entry File::bufferModeEntries[] = |
137 | { |
138 | { "none" , BUFFER_NONE }, |
139 | { "line" , BUFFER_LINE }, |
140 | { "full" , BUFFER_FULL }, |
141 | }; |
142 | |
143 | StringMap<File::BufferMode, File::BUFFER_MAX_ENUM> File::bufferModes(File::bufferModeEntries, sizeof(File::bufferModeEntries)); |
144 | |
145 | } // filesystem |
146 | } // love |
147 | |