Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.03 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <sys/types.h>
  3. #include <dirent.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <ctype.h>
  11.  
  12. int TDAcurrPos = 0;
  13.  
  14. typedef struct vertexData {
  15.     char fileName[100];
  16.     char directoryName[1000];
  17.     int deadlock; // 0 - false 1 - true;
  18.     int minotaur; // 0 - false 1 - true;
  19.     int arrayOfEdges[200]; // -1 if null
  20.    
  21. } vertexData;
  22.  
  23. vertexData tempDataArray[200];
  24.  
  25. void readDataOutside(char* fileName, char* directoryName,int deadlock,int minotaur, int arrayOfEdges,int size){
  26.     strcpy(tempDataArray[TDAcurrPos].fileName,fileName);
  27.     strcpy(tempDataArray[TDAcurrPos].directoryName,directoryName);
  28.     if(minotaur == 1){
  29.         tempDataArray[TDAcurrPos].minotaur = 1;
  30.         tempDataArray[TDAcurrPos].deadlock = 0;
  31.         tempDataArray[TDAcurrPos].arrayOfEdges[0] = -1;
  32.     }
  33.  
  34.     if(deadlock == 1){
  35.         tempDataArray[TDAcurrPos].minotaur = 0;
  36.         tempDataArray[TDAcurrPos].deadlock = 1;
  37.         tempDataArray[TDAcurrPos].arrayOfEdges[0] = -1;
  38.     }
  39.  
  40.     if(deadlock == 0 && minotaur == 0){
  41.         tempDataArray[TDAcurrPos].minotaur = 0;
  42.         tempDataArray[TDAcurrPos].deadlock = 0;
  43.  
  44.     //TODO вынести в отдельную функцию
  45.         for(int i=0; i<size; i++ )
  46.         tempDataArray[TDAcurrPos].arrayOfEdges[i] = arrayOfEdges[i];
  47.     }
  48.    
  49. }
  50.  
  51. char* parseInsideDataAndADD(char* fileName, char* dirPath, char* readBuffer, int iterator){
  52.     // if(readBuffer[0] == 'D') return "Deadlock";
  53.     // if(readBuffer[0] == 'M']) return "Minotaur";
  54.     // int len = strlen(readBuffer);
  55.     // char* ret[200];
  56.     // for(int i = 0; i < len; i++){
  57.     //     if (!isdigit(readBuffer[0])) exit(666);
  58.     //     return  readBuffer;
  59.     // }
  60.  
  61.     if(readBuffer[0] == 'M') readDataOutside(fileName, dirPath, 0, 1, -1, 0);
  62.     if(readBuffer[0] == 'D') readDataOutside(fileName, dirPath, 1, 0, -1, 0);
  63.     if(isdigit(readBuffer[0])) {
  64.         //int edges[200] = parseInt(readBuffer,iterator);
  65.         int edges[200] = { 0, 1 , 2 ,4 ,5};
  66.         readDataOutside(fileName, dirPath, 0, 0, edges, iterator);
  67.     }
  68.  
  69.  
  70. }
  71.  
  72. // int* parseInt(char* readBuffer, int size){
  73.  
  74. //     for(int i = 0; i< iterator; i++){
  75.  
  76. //     }
  77. //     return intArray;
  78. // }
  79.  
  80. void listdir(const char *name, int indent)
  81. {
  82.     DIR *dir;
  83.     struct dirent *entry;
  84.  char path[1024];
  85.     if (!(dir = opendir(name)))
  86.         return;
  87.  
  88.     while ((entry = readdir(dir)) != NULL) {
  89.  
  90.         if (entry->d_type == DT_DIR) {
  91.  
  92.  
  93.             if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
  94.                 continue;
  95.             snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
  96.            // printf("%*s[%s]\n", indent, "", entry->d_name);
  97.             listdir(path, indent + 2);
  98.             //printf("%s\n",path);
  99.         }
  100.         else {
  101.             char ch,readBuffer[200], file_name[25],dirPath[200], fileName[200],tempStr[200];
  102.  
  103.              printf("%s/\n", name);
  104.              printf("%s\n", entry->d_name);
  105.  
  106.              strcpy(dirPath, name);
  107.              strcpy(fileName, entry->d_name);
  108.              strcat(dirPath, "/");
  109.              strcpy(tempStr, dirPath);
  110.              strcat(tempStr, fileName);
  111. /*------------------------------------------------------*/
  112.          FILE*  fp = fopen(tempStr,"r"); // read mode
  113.         int iterator = 0;
  114.            if( fp == NULL )
  115.            {
  116.               perror("Error while opening the file.\n");
  117.               exit(EXIT_FAILURE);
  118.            }
  119.  
  120.            printf("The contents of %s file are :\n", file_name);
  121.  
  122.            while( ( ch = fgetc(fp) ) != EOF ){
  123.             printf("%c",ch);
  124.             readBuffer[iterator++] = ch;
  125.             }
  126.            fclose(fp);
  127. /*------------------------------------------------------*/
  128.           parseInsideDataAndADD(fileName, dirPath, readBuffer, iterator);
  129.  
  130.                 }
  131.     }
  132.  //   printf("%s\n",path);
  133.     closedir(dir);
  134. }
  135.  
  136. int main(void) {
  137.     listdir("LIST DIR", 0);
  138.     return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement