1
0
mirror of https://gitlab.com/klmp200/fish.git synced 2024-11-24 10:04:20 +00:00

un globbing récursif, mais obscur et cassé

This commit is contained in:
Aethor 2017-05-30 11:31:00 +02:00
parent 9c0c89365a
commit 04d20b726c
2 changed files with 105 additions and 33 deletions

View File

@ -54,52 +54,120 @@ WordList * fishExpand(WordList *wordList) {
} }
WordList* expandWord(char* word){ WordList* expandWord(char* path){
if(!stringContains(word, '/')){ if(!stringContains(path, '/')){
return getFiles((char*) "./", word); return getFiles((char*) "./", path);
} }
else{ else{
WordList* pathList = splitWordIntoList(word, '/'); WordList* expandedList = createWordList();
WordList* expandedArgsList = createWordList(); recursiveExpandWord(path, expandedList);
if (expandedArgsList == NULL) crash(); return expandedList;
WordListElement* tempElement;
int i;
if(pathList->size >= 1){
tempElement = pathList->first;
for(i=0; i < pathList->size; i++){
printf("\nBASE PATH %s\n", tempElement->word);
char* tempPath = getPath(tempElement->word);
printf("\nPATH : %s\n", tempPath);
char* tempFileName = getFileName(tempElement->word);
printf("\nFILENAME : %s\n", tempFileName);
concatWordList(expandedArgsList, getFiles(tempPath, tempFileName));
printWordList(expandedArgsList);
tempElement = tempElement->next;
free(tempPath);
free(tempFileName);
}
}
freeWordList(pathList);
return expandedArgsList;
//return getFiles(word, (char*) "*");//temporary
} }
} }
void recursiveExpandWord(char* path, WordList* listToExpand){
int lastToExpand = 1;
int i = 0;
int indexToExpand = -1;
//TODO : free
WordList* pathToList = splitWordIntoList(path, '/');
WordListElement* tempElement; //beware of the size, should be checked before anyway
tempElement = pathToList->first;
while(i < pathToList->size - 1 && indexToExpand == -1){
if(stringContains(tempElement->word, '*') || stringContains(tempElement->word, '?')){
indexToExpand = i;
lastToExpand = 0;
}
i++;
tempElement = tempElement->next;
}
if(lastToExpand){
concatWordList(listToExpand, getFiles(getPath(path), getFileName(path)));
free(pathToList);
}
else{
char* correctedPath = concatWordListToWord(pathToList,0, indexToExpand);
WordList* foundFiles = getFiles(getPath(correctedPath), getFileName(correctedPath));
if(foundFiles->size > 0){
tempElement = foundFiles->first;
char* concatenedEndOfPath = concatWordListToWord(foundFiles, indexToExpand + 1, foundFiles->size - 1);
for(i=0; i < foundFiles->size; i++){
tempElement->word = trueStrcat(tempElement->word, concatenedEndOfPath);
recursiveExpandWord(tempElement->word, listToExpand);
tempElement = tempElement->next;
}
free(concatenedEndOfPath);
}
freeWordList(pathToList);
free(correctedPath);
freeWordList(foundFiles);
}
}
char* concatWordListToWord(WordList* list,int firstElemIndex, int lastElemIndex){
if(list->size == 0 || list == NULL) crash();
int i;
char* concatenedString = (char*) malloc(sizeof(char));
if(concatenedString == NULL) crash();
if(lastElemIndex > list->size -1){
lastElemIndex = list->size - 1;
fprintf(stderr, "fish : Warning : you are a miserable failure, your element is beyond the list. pfff. I corrected it for you.");
}
if(firstElemIndex > lastElemIndex){
firstElemIndex = lastElemIndex;
fprintf(stderr, "fish : Warning : how are you so bad ? your inferior index is superior to your superior index. pfff. I corrected it for you.");
}
WordListElement* tempElement = list->first;
for(i=0; i < firstElemIndex; i++){
tempElement = tempElement->next;
}
for(i=firstElemIndex; i < lastElemIndex; i++){
trueStrcat(concatenedString, tempElement->word);
tempElement = tempElement->next;
}
return concatenedString;
}
char* getFileName(char* string){ char* getFileName(char* string){
if(!stringContains(string, '/')){ if(!stringContains(string, '/')){

View File

@ -10,6 +10,8 @@ WordList* getFiles(char* path, char* wildcardedString);
WordList* expandWord(char* word); WordList* expandWord(char* word);
void recursiveExpandWord(char* path, WordList* listToExpand);
int wildcardedStringMatches(char* string1, char* string2); int wildcardedStringMatches(char* string1, char* string2);
WordList* splitWordIntoList(char* string, char splitchar); WordList* splitWordIntoList(char* string, char splitchar);
@ -18,4 +20,6 @@ char* getFileName(char* string);
char* getPath(char* string); char* getPath(char* string);
char* concatWordListToWord(WordList* list, int firstElemIndex, int lastElemIndex);
#endif //FISH_FISH_GLOBBING_H #endif //FISH_FISH_GLOBBING_H