mirror of
https://github.com/facebook/zstd.git
synced 2025-07-29 11:21:22 +03:00
Merge pull request #1844 from AhmedAbdellah19/adding_read_files_from_file_feature
Adding --file=FILE feature
This commit is contained in:
222
programs/util.c
222
programs/util.c
@ -187,6 +187,228 @@ U64 UTIL_getTotalFileSize(const char* const * const fileNamesTable, unsigned nbF
|
||||
return error ? UTIL_FILESIZE_UNKNOWN : total;
|
||||
}
|
||||
|
||||
|
||||
int UTIL_readLineFromFile(char* buf, size_t len, FILE* file) {
|
||||
char* fgetsCheck = NULL;
|
||||
|
||||
if (feof(file)) {
|
||||
UTIL_DISPLAYLEVEL(1, "[ERROR] end of file reached and need to read\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
fgetsCheck = fgets(buf, (int) len, file);
|
||||
|
||||
if(fgetsCheck == NULL || fgetsCheck != buf) {
|
||||
UTIL_DISPLAYLEVEL(1, "[ERROR][UTIL_readLineFromFile] fgets has a problem check: %s buf: %s \n",
|
||||
fgetsCheck == NULL ? "NULL" : fgetsCheck, buf);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return (int) strlen(buf)-1; /* -1 to ignore '\n' character */
|
||||
}
|
||||
|
||||
/* Warning: inputFileSize should be less than or equal buf capacity and buf should be initialized*/
|
||||
static int readFromFile(char* buf, size_t inputFileSize, const char* inputFileName) {
|
||||
|
||||
FILE* inputFile = fopen(inputFileName, "r");
|
||||
int nbFiles = -1;
|
||||
unsigned pos = 0;
|
||||
|
||||
|
||||
if(!buf) {
|
||||
UTIL_DISPLAYLEVEL(1, "[ERROR][UTIL_readFileNamesTableFromFile] Can't create buffer.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(!inputFile) {
|
||||
UTIL_DISPLAYLEVEL(1, "[ERROR][UTIL_readFileNamesTableFromFile] Can't open file to read input file names.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
for(nbFiles=0; !feof(inputFile) ; ) {
|
||||
if(UTIL_readLineFromFile(buf+pos, inputFileSize, inputFile) > 0) {
|
||||
int len = (int) strlen(buf+pos);
|
||||
buf[pos+len-1] = '\0'; /* replace '\n' with '\0'*/
|
||||
pos += len;
|
||||
++nbFiles;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(inputFile);
|
||||
|
||||
if(pos > inputFileSize) return -1;
|
||||
|
||||
return nbFiles;
|
||||
}
|
||||
|
||||
/*Note: buf is not freed in case function successfully created table because filesTable->fileNames[0] = buf*/
|
||||
FileNamesTable*
|
||||
UTIL_createFileNamesTable_fromFileName(const char* inputFileName) {
|
||||
U64 inputFileSize = 0;
|
||||
unsigned nbFiles = 0;
|
||||
int ret_nbFiles = -1;
|
||||
char* buf = NULL;
|
||||
size_t i = 0, pos = 0;
|
||||
|
||||
FileNamesTable* filesTable = NULL;
|
||||
|
||||
if(!UTIL_fileExist(inputFileName) || !UTIL_isRegularFile(inputFileName))
|
||||
return NULL;
|
||||
|
||||
inputFileSize = UTIL_getFileSize(inputFileName) + 1; /* (+1) to add '\0' at the end of last filename */
|
||||
|
||||
if(inputFileSize > MAX_FILE_OF_FILE_NAMES_SIZE)
|
||||
return NULL;
|
||||
|
||||
buf = (char*) malloc((size_t) inputFileSize * sizeof(char));
|
||||
if(!buf) {
|
||||
UTIL_DISPLAYLEVEL(1, "[ERROR][UTIL_readFileNamesTableFromFile] Can't create buffer.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ret_nbFiles = readFromFile(buf, (size_t) inputFileSize, inputFileName);
|
||||
|
||||
if(ret_nbFiles <= 0) {
|
||||
free(buf);
|
||||
return NULL;
|
||||
}
|
||||
nbFiles = ret_nbFiles;
|
||||
|
||||
filesTable = UTIL_createFileNamesTable(NULL, NULL, 0);
|
||||
if(!filesTable) {
|
||||
free(buf);
|
||||
UTIL_DISPLAYLEVEL(1, "[ERROR][UTIL_readFileNamesTableFromFile] Can't create table for files.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
filesTable->tableSize = nbFiles;
|
||||
filesTable->fileNames = (const char**) malloc((nbFiles+1) * sizeof(char*));
|
||||
|
||||
|
||||
|
||||
for(i = 0, pos = 0; i < nbFiles; ++i) {
|
||||
filesTable->fileNames[i] = buf+pos;
|
||||
pos += strlen(buf+pos)+1;
|
||||
}
|
||||
|
||||
|
||||
if(pos > inputFileSize){
|
||||
UTIL_freeFileNamesTable(filesTable);
|
||||
if(buf) free(buf);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
filesTable->buf = buf;
|
||||
|
||||
return filesTable;
|
||||
}
|
||||
|
||||
FileNamesTable*
|
||||
UTIL_createFileNamesTable(const char** filenames, char* buf, size_t tableSize){
|
||||
FileNamesTable* table = (FileNamesTable*) malloc(sizeof(FileNamesTable));
|
||||
if(!table) {
|
||||
return NULL;
|
||||
}
|
||||
table->fileNames = filenames;
|
||||
table->buf = buf;
|
||||
table->tableSize = tableSize;
|
||||
return table;
|
||||
}
|
||||
|
||||
void UTIL_freeFileNamesTable(FileNamesTable* table) {
|
||||
if(table) {
|
||||
if(table->fileNames) {
|
||||
free((void*)table->fileNames);
|
||||
}
|
||||
|
||||
if(table && table->buf) {
|
||||
free(table->buf);
|
||||
}
|
||||
|
||||
free(table);
|
||||
}
|
||||
}
|
||||
|
||||
static size_t getTotalTableSize(FileNamesTable* table) {
|
||||
size_t i = 0, totalSize = 0;
|
||||
for(i = 0 ; i < table->tableSize && table->fileNames[i] ; ++i) {
|
||||
totalSize += strlen(table->fileNames[i]) + 1; /* +1 to add '\0' at the end of each fileName */
|
||||
}
|
||||
|
||||
return totalSize;
|
||||
}
|
||||
|
||||
FileNamesTable*
|
||||
UTIL_concatenateTwoTables(FileNamesTable* table1, FileNamesTable* table2) {
|
||||
unsigned newTableIdx = 0, idx1 = 0, idx2 = 0;
|
||||
size_t i = 0, pos = 0;
|
||||
size_t newTotalTableSize = 0;
|
||||
|
||||
FileNamesTable* newTable = NULL;
|
||||
|
||||
char* buf = NULL;
|
||||
|
||||
|
||||
newTable = UTIL_createFileNamesTable(NULL, NULL, 0);
|
||||
|
||||
if(!newTable) {
|
||||
UTIL_DISPLAYLEVEL(1, "[ERROR][UTIL_concatenateTwoTables] Can't create new table for concatenation output.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
newTotalTableSize = getTotalTableSize(table1) + getTotalTableSize(table2);
|
||||
|
||||
buf = (char*) malloc(newTotalTableSize * sizeof(char));
|
||||
if(!buf) {
|
||||
UTIL_freeFileNamesTable(newTable);
|
||||
UTIL_DISPLAYLEVEL(1, "[ERROR][UTIL_concatenateTwoTables] Can't create buf for concatenation output.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for(i = 0; i < newTotalTableSize ; ++i) buf[i] = '\0';
|
||||
|
||||
newTable->tableSize = table1->tableSize + table2->tableSize;
|
||||
newTable->fileNames = (const char **) malloc(newTable->tableSize * sizeof(char*));
|
||||
|
||||
if(!newTable->fileNames) {
|
||||
UTIL_freeFileNamesTable(newTable);
|
||||
if(buf) free(buf);
|
||||
UTIL_DISPLAYLEVEL(1, "[ERROR][UTIL_concatenateTwoTables] Can't create new table for concatenation output.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (i = 0; i < newTable->tableSize; ++i)
|
||||
newTable->fileNames[i] = NULL;
|
||||
|
||||
for( ; idx1 < table1->tableSize && table1->fileNames[idx1] && pos < newTotalTableSize; ++idx1, ++newTableIdx) {
|
||||
size_t curLen = strlen(table1->fileNames[idx1]);
|
||||
memcpy(buf+pos, table1->fileNames[idx1], curLen);
|
||||
newTable->fileNames[newTableIdx] = buf+pos;
|
||||
pos += curLen+1;
|
||||
}
|
||||
|
||||
|
||||
for( ; idx2 < table2->tableSize && table2->fileNames[idx2] && pos < newTotalTableSize ; ++idx2, ++newTableIdx) {
|
||||
size_t curLen = strlen(table2->fileNames[idx2]);
|
||||
memcpy(buf+pos, table2->fileNames[idx2], curLen);
|
||||
newTable->fileNames[newTableIdx] = buf+pos;
|
||||
pos += curLen+1;
|
||||
}
|
||||
|
||||
if(pos > newTotalTableSize) {
|
||||
UTIL_freeFileNamesTable(newTable);
|
||||
if(buf) free(buf);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
newTable->buf = buf;
|
||||
|
||||
UTIL_freeFileNamesTable(table1);
|
||||
UTIL_freeFileNamesTable(table2);
|
||||
|
||||
return newTable;
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks)
|
||||
{
|
||||
|
Reference in New Issue
Block a user