MCPcopy Create free account
hub / github.com/F-Stack/f-stack / DiB_loadFiles

Function DiB_loadFiles

freebsd/contrib/zstd/programs/dibio.c:104–145  ·  view source on GitHub ↗

* File related operations **********************************************************/ DiB_loadFiles() : * load samples from files listed in fileNamesTable into buffer. * works even if buffer is too small to load all samples. * Also provides the size of each sample into sampleSizes table * which must be sized correctly, using DiB_fileStats(). * @return : nb of samples effectively loaded i

Source from the content-addressed store, hash-verified

102 * sampleSizes is filled with the size of each sample.
103 */
104static unsigned DiB_loadFiles(void* buffer, size_t* bufferSizePtr,
105 size_t* sampleSizes, unsigned sstSize,
106 const char** fileNamesTable, unsigned nbFiles, size_t targetChunkSize,
107 unsigned displayLevel)
108{
109 char* const buff = (char*)buffer;
110 size_t pos = 0;
111 unsigned nbLoadedChunks = 0, fileIndex;
112
113 for (fileIndex=0; fileIndex<nbFiles; fileIndex++) {
114 const char* const fileName = fileNamesTable[fileIndex];
115 unsigned long long const fs64 = UTIL_getFileSize(fileName);
116 unsigned long long remainingToLoad = (fs64 == UTIL_FILESIZE_UNKNOWN) ? 0 : fs64;
117 U32 const nbChunks = targetChunkSize ? (U32)((fs64 + (targetChunkSize-1)) / targetChunkSize) : 1;
118 U64 const chunkSize = targetChunkSize ? MIN(targetChunkSize, fs64) : fs64;
119 size_t const maxChunkSize = (size_t)MIN(chunkSize, SAMPLESIZE_MAX);
120 U32 cnb;
121 FILE* const f = fopen(fileName, "rb");
122 if (f==NULL) EXM_THROW(10, "zstd: dictBuilder: %s %s ", fileName, strerror(errno));
123 DISPLAYUPDATE(2, "Loading %s... \r", fileName);
124 for (cnb=0; cnb<nbChunks; cnb++) {
125 size_t const toLoad = (size_t)MIN(maxChunkSize, remainingToLoad);
126 if (toLoad > *bufferSizePtr-pos) break;
127 { size_t const readSize = fread(buff+pos, 1, toLoad, f);
128 if (readSize != toLoad) EXM_THROW(11, "Pb reading %s", fileName);
129 pos += readSize;
130 sampleSizes[nbLoadedChunks++] = toLoad;
131 remainingToLoad -= targetChunkSize;
132 if (nbLoadedChunks == sstSize) { /* no more space left in sampleSizes table */
133 fileIndex = nbFiles; /* stop there */
134 break;
135 }
136 if (toLoad < targetChunkSize) {
137 fseek(f, (long)(targetChunkSize - toLoad), SEEK_CUR);
138 } } }
139 fclose(f);
140 }
141 DISPLAYLEVEL(2, "\r%79s\r", "");
142 *bufferSizePtr = pos;
143 DISPLAYLEVEL(4, "loaded : %u KB \n", (unsigned)(pos >> 10))
144 return nbLoadedChunks;
145}
146
147#define DiB_rotl32(x,r) ((x << r) | (x >> (32 - r)))
148static U32 DiB_rand(U32* src)

Callers 1

DiB_trainFromFilesFunction · 0.85

Calls 1

UTIL_getFileSizeFunction · 0.85

Tested by

no test coverage detected