Adding --file=FILE feature

This commit is contained in:
Ahmed Abdellah
2019-10-15 07:49:13 +01:00
parent 0e76000dee
commit 779ea72953
3 changed files with 335 additions and 2 deletions
+229
View File
@@ -163,6 +163,235 @@ 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) {
if (feof(file)) {
UTIL_DISPLAYLEVEL(1, "[ERROR] end of file reached and need to read\n");
return -1;
}
UTIL_DISPLAY("[TRACE] read line\n");
char* fgetsCheck = fgets(buf, 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;
}
UTIL_DISPLAY("[TRACE] length of Line: %d\n" , (int)strlen(buf));
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) {
if(!buf) {
UTIL_DISPLAYLEVEL(1, "[ERROR][UTIL_readFileNamesTableFromFile] Can't create buffer.\n");
return -1;
}
UTIL_DISPLAY("[TRACE] open file\n");
FILE* inputFile = fopen(inputFileName, "r");
int nbFiles = -1;
if(!inputFile) {
UTIL_DISPLAYLEVEL(1, "[ERROR][UTIL_readFileNamesTableFromFile] Can't open file to read input file names.\n");
return -1;
}
unsigned pos = 0;
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'*/
UTIL_DISPLAY("[TRACE] line: %s\n", buf+pos);
pos += len;
++nbFiles;
}
else
UTIL_DISPLAY("[TRACE] ignored line: %s", buf+pos);
}
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) {
UTIL_DISPLAY("file check\n");
if(!UTIL_fileExist(inputFileName) || !UTIL_isRegularFile(inputFileName))
return NULL;
UTIL_DISPLAY("[TRACE] start function readFileNamesTableFromFile\n");
U64 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;
char* buf = (char*) malloc(inputFileSize * sizeof(char));
if(!buf) {
UTIL_DISPLAYLEVEL(1, "[ERROR][UTIL_readFileNamesTableFromFile] Can't create buffer.\n");
return NULL;
}
int nbFiles = readFromFile(buf, inputFileSize, inputFileName);
if(nbFiles <= 0) {
free(buf);
return NULL;
}
UTIL_DISPLAY("[TRACE] file closed with %d read lines\n", nbFiles);
FileNamesTable* filesTable = (FileNamesTable*) malloc(sizeof(FileNamesTable));
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*));
UTIL_DISPLAY("[TRACE] Start migration\n");
size_t i = 0, pos = 0;
for(i = 0, pos = 0; i < nbFiles; ++i) {
filesTable->fileNames[i] = buf+pos;
UTIL_DISPLAY("[TRACE] file %zu: %s\n", i, filesTable->fileNames[i]);
pos += strlen(buf+pos)+1;
}
UTIL_DISPLAY("[TRACE] migration done\n");
UTIL_DISPLAY("[TRACE] pos %zu inputFileSize %lu\n", pos, inputFileSize);
if(pos > inputFileSize){
UTIL_freeFileNamesTable(filesTable);
if(buf) free(buf);
return NULL;
}
filesTable->buf = buf;
UTIL_DISPLAY("[TRACE] finished reading\n");
return filesTable;
}
void UTIL_freeFileNamesTable(FileNamesTable* table) {
if(table) {
if(table->fileNames) {
if(table->buf)
free(table->buf);
free(table->fileNames);
}
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) {
UTIL_DISPLAY("[TRACE] Start concatenation\n");
unsigned newTableIdx = 0, idx1 = 0, idx2 = 0;
size_t i = 0;
FileNamesTable* newTable = (FileNamesTable*) malloc(sizeof(FileNamesTable));
if(!newTable) {
UTIL_DISPLAYLEVEL(1, "[ERROR][UTIL_concatenateTwoTables] Can't create new table for concatenation output.\n");
return NULL;
}
int newTotalTableSize = getTotalTableSize(table1) + getTotalTableSize(table2);
UTIL_DISPLAY("[TRACE] buf total size is: %d\n", newTotalTableSize);
char* 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;
UTIL_DISPLAY("[TRACE] add table1 concatenation of size %zu\n", table1->tableSize);
size_t pos = 0;
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;
}
UTIL_DISPLAY("[TRACE] table1 actual size %u\n", idx1);
UTIL_DISPLAY("[TRACE] add table2 concatenation of size %zu\n", table2->tableSize);
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;
}
UTIL_DISPLAY("[TRACE] table2 actual size %u\n", idx2);
UTIL_DISPLAY("[TRACE] new table actual size %u\n", newTableIdx);
assert(newTableIdx == newTable->tableSize || newTable->fileNames[newTableIdx] == NULL);
UTIL_DISPLAY("[TRACE] table1:\n");
for(idx1 = 0 ; idx1 < table1->tableSize && table1->fileNames[idx1] ; ++idx1)
UTIL_DISPLAY("[TRACE] %u %s\n", idx1, table1->fileNames[idx1]);
UTIL_DISPLAY("[TRACE] table2:\n");
for(idx2 = 0 ; idx2 < table2->tableSize && table2->fileNames[idx2] ; ++idx2)
UTIL_DISPLAY("[TRACE] %u %s\n", idx2, table2->fileNames[idx2]);
UTIL_DISPLAY("[TRACE] new table:\n");
for(newTableIdx = 0; newTableIdx < newTable->tableSize && newTable->fileNames[newTableIdx] ; ++newTableIdx)
UTIL_DISPLAY("[TRACE] %u %s\n", newTableIdx, newTable->fileNames[newTableIdx]);
UTIL_freeFileNamesTable(table1);
UTIL_freeFileNamesTable(table2);
UTIL_DISPLAY("[TRACE] concatenation finished\n");
newTable->buf = buf;
return newTable;
}
#ifdef _WIN32
int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks)
{