Advertisement
Guest User

Untitled

a guest
Apr 10th, 2020
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <dirent.h>
  4. #include <sys/types.h>
  5. #include <regex.h>
  6. #include <stdlib.h>
  7.  
  8. typedef struct catalog_of_letters{
  9.     char name;
  10.     char* path;
  11. }catalog_of_letters;
  12.  
  13. void getStruct(catalog_of_letters** catalog, char* path, int count){
  14.     catalog[count]->name = ptr[strlen(path)-5];
  15.     catalog[count]->path = strdup(path);
  16. }
  17.  
  18. int cmp(const void* a, const void* b){
  19.     return (*(catalog_of_letters**)a)->name - (*(catalog_of_letters**)b)->name;
  20. }
  21.  
  22. int bsearch_cmp(const void* a, const void* b){
  23.     return *(char*)a - (*(catalog_of_letters**)b)->name;
  24. }
  25.  
  26. int isValid(char *filName){
  27.     char *regexp = "^[a-zA-z].txt$";
  28.     regex_t regexComp;
  29.    
  30.     if(regcomp(&regexComp, regexp, REG_EXTENDED)){
  31.         return 0;
  32.     }
  33.    
  34.     return regexec(&regexComp, fileName, 0, NULL, 0) == 0;
  35. }
  36.  
  37. void depthFirstSearch(char *startDir, catalog_of_letters** catalog, int* count){
  38.     char nextDir[200] = {0};
  39.     strcpy(nextDir, startDir);
  40.    
  41.     DIR *dir = opendir(startDir);
  42.    
  43.     if(!dir) {
  44.         return;
  45.     }
  46.    
  47.     struct dirent *dirEntity = readdir(dir);
  48.     while(dirEntity){
  49.        
  50.         if((dirEntity->d_type == DT_DIR) &&
  51.             (strcmp(dirEntity->d_name, ".") != 0) &&
  52.             (strcmp(dirEntity->d_name, "..") != 0)) {
  53.                 strcat(nextDir, "/");
  54.                 strcat(nextDir, dirEntity->d_name);
  55.                 depthFirstSearch(nextDir, catalog, count);
  56.                 nextDir[strlen(nextDir)] = '\0';
  57.         }  
  58.        
  59.         if((dirEntity->d_type == DT_REG) && isValid(dirEntity->d_name)) {
  60.             strcat(nextDir, "/");
  61.             strcat(nextDir, dirEntity->d_name);
  62.             getStruct(catalog, nextDir, *count)
  63.             nextDir[strlen(nextDir)] = '\0';
  64.             *count++;
  65.         }
  66.        
  67.         dirEntity = readdir(dir);
  68.     }
  69.    
  70.     closedir(dir);
  71. }
  72.  
  73. void printDirPathsEqWordFrag(
  74.     catalog_of_letters** catalog,
  75.     char* word,
  76.     int index,
  77.     int count,
  78.     FILE *f
  79. ){
  80.     if(index > strlen(word)-1) {
  81.         return;
  82.     }
  83.    
  84.     catalog_of_letters** catalogBinarySearch;
  85.    
  86.     catalogBinarySearch = bsearch(
  87.         &word[index],
  88.         catalog,
  89.         count,
  90.         sizeof(struct catalog_of_letters*),bsearch_cmp
  91.     );
  92.    
  93.     if(catalogBinarySearch){
  94.         fputs((*catalogBinarySearch)->path, f);
  95.         fputs("\n", f);
  96.         (*catalogBinarySearch)->path[0] = '\0';
  97.     }
  98.    
  99.     printDirPathsEqWordFrag(catalog, word, index + 1, count, f);
  100. }
  101.  
  102.  
  103. int main(){
  104.     FILE *f = fopen("result.txt", "w");
  105.     char word[100];
  106.     fgets(word,100,stdin);
  107.     word[strlen(word)-1]='\0';
  108.     catalog_of_letters** catalog = (catalog_of_letters**)malloc(66*sizeof(catalog_of_letters*));
  109.     int i;
  110.     for(i =0;i<66;i++)
  111.         catalog[i] = calloc(1,sizeof(catalog));
  112.  
  113.     int countOfLevels = 0;
  114.     depthFirstSearch("./tmp", catalog, &countOfLevels)
  115.     qsort(catalog, countOfLevels, sizeof(catalog_of_letters*), cmp);
  116.     printDirPathsEqWordFrag(catalog, word, 0, countOfLevels, f);
  117.    
  118.     return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement