MCPcopy Create free account
hub / github.com/defold/defold / ReadFile

Function ReadFile

engine/dlib/src/dlib/testutil.cpp:84–125  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

82}
83
84uint8_t* ReadFile(const char* path, uint32_t* file_size)
85{
86 SetupFS();
87
88 FILE* f = fopen(path, "rb");
89 if (!f)
90 {
91 dmLogError("Failed to load file %s", path);
92 return 0;
93 }
94
95 if (fseek(f, 0, SEEK_END) != 0)
96 {
97 fclose(f);
98 dmLogError("Failed to seek to end of file %s", path);
99 return 0;
100 }
101
102 size_t size = (size_t)ftell(f);
103 if (fseek(f, 0, SEEK_SET) != 0)
104 {
105 fclose(f);
106 dmLogError("Failed to seek to start of file %s", path);
107 return 0;
108 }
109
110 uint8_t* buffer;
111 dmMemory::AlignedMalloc((void**)&buffer, 16, size);
112
113 size_t nread = fread(buffer, 1, size, f);
114 fclose(f);
115
116 if (nread != size)
117 {
118 dmMemory::AlignedFree((void*)buffer);
119 dmLogError("Failed to read %u bytes from file %s", (uint32_t)size, path);
120 return 0;
121 }
122
123 *file_size = size;
124 return buffer;
125}
126
127uint8_t* ReadHostFile(const char* path, uint32_t* file_size)
128{

Callers 1

ReadHostFileFunction · 0.70

Calls 3

SetupFSFunction · 0.85
AlignedMallocFunction · 0.85
AlignedFreeFunction · 0.85

Tested by

no test coverage detected