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

clean and safe structure for globbing

This commit is contained in:
Aethor 2017-05-27 17:36:55 +02:00
parent bde4e4e717
commit 2a7dad2662
4 changed files with 56 additions and 134 deletions

View File

@ -9,51 +9,56 @@
WordList * fishExpand(WordList *wordList) {
if(wordList->size > 1){
int i;
WordList* newWordList = createWordList();// creating the list to return
WordList* newWordList = createWordList();
if(newWordList == NULL){
if(newWordList == NULL){//crash when the allocation is unsuccessful
crash();
}
}
newWordList->first = wordList->first;
addEndWordList(newWordList, wordList->first->word);//copy the command into the returning word list
newWordList->size = 0;
WordListElement* tempElement = wordList->first->next;
WordListElement* tempElement = wordList->first->next; //temporary nav element
for(i=0; i<wordList->size; i++){
for(i=1; i<wordList->size; i++){
//newWordList = concatWordList(newWordList, recursiveExpand(tempElement->word));
//newWordList = concatWordList(newWordList, NULL);
//TODO : optimize the stringContains() function to test for a list of characters
//test if we have to expand a string or not, for optimization purposes
if(stringContains(tempElement->word, '*') || stringContains(tempElement->word, '?')){
if(tempElement != NULL){
concatWordList(newWordList, expandWord(tempElement->word));
tempElement = tempElement->next;
}
//If we dont have to expand, add the current word unchanged to the new list
else{
addEndWordList(newWordList, tempElement->word);
}
}
}
tempElement = tempElement->next;
}
freeWordList(wordList);
return newWordList;
}
else return wordList;
}
WordList* expandWord(char* word){
WordList* wordList = createWordList();
addEndWordList(wordList, word);
return wordList;
}
WordList* recursiveExpand(char * completePath){
if (stringContains(completePath, '/')){
return expandInDir("./",completePath);
}
return NULL;
}
WordArray * getFiles(char* path){
@ -100,87 +105,3 @@ WordArray * getFiles(char* path){
}
int comparator(char* string1, char* string2){//TODO
int i = 0;
char tempIChar;
int j = 0;
if(string1 != NULL && string2 != NULL){
while(string1[i] != '\0' && string2[j] != '\0'){
if(string1[i] == '*'){
tempIChar = string1[i+1];
while(string2[j] != tempIChar){
j++;
if(string2[j] == '\0' && tempIChar == '\0'){
return 1;
}
}
i++;
}
if(string1[i] != string2[j] && string1[i] != '?'){
return 0;
}
i++;
j++;
}
if(string1[i] == '\0' && string2[j] == '\0'){
return 1;
}
else{
return 0;
}
}
else{
printf("warning : fuck you, strings are considered null");
crash();
return 0;
}
}
WordList* expandInDir(char* dir, char* toExpand){
int i = 0;
WordList* list = createWordList();
WordArray* files = getFiles(dir);
for(i=0; i<files->size;i++){
if(comparator(toExpand, files->words[i])){
addWordList(list, files->words[i]);
}
}
return list;
}

View File

@ -8,11 +8,6 @@ WordList * fishExpand(WordList *wordArray);
WordArray* getFiles(char* path);
/*char1 is a string with characters such as '*', '.' or '?' having special meanings*/
int comparator(char* string1, char* string2);
WordList* expandInDir(char*, char*);
WordList* recursiveExpand(char* completePath);
WordList* expandWord(char* word);
#endif //FISH_FISH_GLOBBING_H

View File

@ -117,20 +117,28 @@ void freeWordList(WordList *list) {
free(list);
}
WordList* concatWordList(WordList* list1, WordList* list2){
void concatWordList(WordList* list1, WordList* list2){//return a sing list containing all elements of both lists
if(list1 == NULL || list2 == NULL){
crash();
}
else if(list2->size != 0){
if(list1 != NULL && list2 != NULL && list1->size >= 1 && list2->size >=1){
list1->last->next = list2->first;
list2->first->previous = list1->last;
list1->last = list2->last;
list1->size = list1->size + list2->size;
free(list2);
return list1;
}
else{
return NULL;
}
WordListElement* tempElement = list2->first;
for(int i = 0; i < list2->size; i++){
addEndWordList(list1, tempElement->word);
tempElement = tempElement->next;
}
freeWordList(list2);
}
else{
freeWordList(list2);
}
}
@ -286,12 +294,12 @@ int stringContains(char * string, char charToTest){
while(string[i] != '\0'){
if(string[i] != charToTest){
return 0;
if(string[i] == charToTest){
return 1;
}
i++;
}
return 1;
return 0;
}

View File

@ -35,12 +35,10 @@ WordList * splitWordList(WordList *list, char *regex);
char * splitWord(char * origin, int beginning_index, int size_to_delete); // Tested
WordList* concatWordList(WordList* list1, WordList* list2);
void concatWordList(WordList* list1, WordList* list2);
int stringContains(char* string, char charToTest);
WordList* concatWordList(WordList* list1, WordList* list2);
int stringContains(char* string, char charToTest);
#endif //FISH_FISH_UTILS_H