MCPcopy Create free account
hub / github.com/TactilityProject/Tactility / readBinaryInternal

Function readBinaryInternal

TactilityCore/Source/file/File.cpp:153–193  ·  view source on GitHub ↗

Read a file. * @param[in] filepath * @param[out] outSize the amount of bytes that were read, excluding the sizePadding * @param[in] sizePadding optional padding to add at the end of the output data (the values are not set) */

Source from the content-addressed store, hash-verified

151 * @param[in] sizePadding optional padding to add at the end of the output data (the values are not set)
152 */
153static std::unique_ptr<uint8_t[]> readBinaryInternal(const std::string& filepath, size_t& outSize, size_t sizePadding = 0) {
154 FILE* file = fopen(filepath.c_str(), "rb");
155
156 if (file == nullptr) {
157 LOGGER.error("Failed to open {}", filepath);
158 return nullptr;
159 }
160
161 long content_length = getSize(file);
162 if (content_length == -1) {
163 LOGGER.error("Failed to determine content length for {}", filepath);
164 return nullptr;
165 }
166
167 auto data = std::make_unique<uint8_t[]>(content_length + sizePadding);
168 if (data == nullptr) {
169 LOGGER.error("Insufficient memory. Failed to allocate {} bytes.", content_length);
170 return nullptr;
171 }
172
173 size_t buffer_offset = 0;
174 while (buffer_offset < content_length) {
175 size_t bytes_read = fread(&data.get()[buffer_offset], 1, content_length - buffer_offset, file);
176 LOGGER.debug("Read {} bytes", bytes_read);
177 if (bytes_read > 0) {
178 buffer_offset += bytes_read;
179 } else { // Something went wrong?
180 break;
181 }
182 }
183
184 fclose(file);
185
186 if (buffer_offset == content_length) {
187 outSize = buffer_offset;
188 return data;
189 } else {
190 outSize = 0;
191 return nullptr;
192 }
193}
194
195std::unique_ptr<uint8_t[]> readBinary(const std::string& filepath, size_t& outSize) {
196 return readBinaryInternal(filepath, outSize);

Callers 2

readBinaryFunction · 0.85
readStringFunction · 0.85

Calls 4

getSizeFunction · 0.85
errorMethod · 0.80
debugMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected