Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.16 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 recordEdgeData(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.     } else if(deadlock == 1){
  33.         tempDataArray[TDAcurrPos].minotaur = 0;
  34.         tempDataArray[TDAcurrPos].deadlock = 1;
  35.         tempDataArray[TDAcurrPos].arrayOfEdges[0] = -1;
  36.     } else if(deadlock == 0 && minotaur == 0){
  37.         tempDataArray[TDAcurrPos].minotaur = 0;
  38.         tempDataArray[TDAcurrPos].deadlock = 0;
  39.  
  40.     //TODO вынести в отдельную функцию
  41.         for(int i=0; i<size; i++ )
  42.         tempDataArray[TDAcurrPos].arrayOfEdges[i] = arrayOfEdges[i];
  43.     }
  44.  
  45.     TDAcurrPos++;
  46. }
  47.  
  48. char* parseInsideData(char* fileName, char* dirPath, char* readBuffer, int iterator){
  49.  
  50. printf("%s\n", "------Start parse-----------");
  51.     int arrayOfEdges[200];
  52.     int k = 0;
  53.     if(readBuffer[0] == 'D'){
  54.  
  55.         printf("%s\n", "deadlock" );
  56.         recordEdgeData(fileName, dirPath, 1, 0,  NULL , -1) ;
  57.  
  58.     } else if(readBuffer[0] == 'M'){
  59.         printf("%s\n", "minotaur" );
  60.         recordEdgeData(fileName, dirPath, 0, 1,  NULL, -1) ;
  61.  
  62.     } else {
  63.  
  64.         int j = 0;
  65.         char* temp = NULL;
  66.         for (int i = 0; i< iterator; i++){
  67.             if(readBuffer[i] == '\n') continue;
  68.                 temp = (char*)realloc(temp, j+1);
  69.                 temp[j] = readBuffer[i];
  70.                 j++;
  71.                
  72.  
  73.                 if(readBuffer[i+1] == '\n'){
  74.                     arrayOfEdges[k] = (int) strtol(temp, (char **)NULL, 10);
  75.                     k++;
  76.                     free(temp);
  77.                     temp = NULL;
  78.                     j = 0;
  79.                 }
  80.         }
  81.         recordEdgeData(fileName, dirPath, 0, 0, arrayOfEdges, k);
  82.     }
  83.  
  84.     for(int z= 0; z<k; z++)
  85.     printf("%s %d\n", "---->",arrayOfEdges[z] );
  86.  
  87. printf("%s\n", "--------End parse---------");
  88.  
  89. }
  90.  
  91. // int* parseInt(char* readBuffer, int size){
  92.  
  93. //     for(int i = 0; i< iterator; i++){
  94.  
  95. //     }
  96. //     return intArray;
  97. // }
  98.  
  99. void listdir(const char *name, int indent)
  100. {
  101.     DIR *dir;
  102.     struct dirent *entry;
  103.  char path[1024];
  104.     if (!(dir = opendir(name)))
  105.         return;
  106.  
  107.     while ((entry = readdir(dir)) != NULL) {
  108.  
  109.         if (entry->d_type == DT_DIR) {
  110.  
  111.  
  112.             if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
  113.                 continue;
  114.             snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
  115.            // printf("%*s[%s]\n", indent, "", entry->d_name);
  116.             listdir(path, indent + 2);
  117.             //printf("%s\n",path);
  118.         }
  119.         else {
  120.             char ch,readBuffer[200], file_name[25],dirPath[200], fileName[200],tempStr[200];
  121.  
  122.              printf("%s/\n", name);
  123.              printf("%s\n", entry->d_name);
  124.  
  125.              strcpy(dirPath, name);
  126.              strcpy(fileName, entry->d_name);
  127.              strcat(dirPath, "/");
  128.              strcpy(tempStr, dirPath);
  129.              strcat(tempStr, fileName);
  130. /*------------------------------------------------------*/
  131.          FILE*  fp = fopen(tempStr,"r"); // read mode
  132.         int iterator = 0;
  133.            if( fp == NULL )
  134.            {
  135.               perror("Error while opening the file.\n");
  136.               exit(EXIT_FAILURE);
  137.            }
  138.  
  139.            printf("The contents of %s file are :\n", fileName);
  140.  
  141.            while( ( ch = fgetc(fp) ) != EOF ){
  142.             printf("%c",ch);
  143.             readBuffer[iterator] = ch;
  144.             iterator++;
  145.             }
  146.            fclose(fp);
  147.  
  148.            //for(int i = 0; i<  iterator; i++) printf("%c\n", readBuffer[i]);
  149. /*------------------------------------------------------*/
  150.           parseInsideData(fileName, dirPath, readBuffer, iterator);
  151.  
  152.                 }
  153.     }
  154.  //   printf("%s\n",path);
  155.     closedir(dir);
  156. }
  157.  
  158. int main(void) {
  159.     int u = 6;
  160.     listdir("LIST DIR", 0);
  161.     printf("%s\n", "###########start############");
  162.     printf("%s\n", tempDataArray[u].fileName);
  163.     printf("%s\n", tempDataArray[u].directoryName);
  164.     printf("%d\n",tempDataArray[u].deadlock);
  165.     printf("%d\n", tempDataArray[u].minotaur);
  166.     printf("%s\n", "###########edges############");
  167.     printf("%d\n", tempDataArray[u].arrayOfEdges[0]);
  168.     printf("%d\n", tempDataArray[u].arrayOfEdges[1]);
  169.     printf("%d\n",tempDataArray[u].arrayOfEdges[2]);
  170.     printf("%d\n",tempDataArray[u].arrayOfEdges[3]);
  171.     printf("%s\n", "#########end##############");
  172.     return 0;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement