Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <sys/types.h>
- #include <dirent.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- int TDAcurrPos = 0;
- typedef struct vertexData {
- char fileName[100];
- char directoryName[1000];
- int deadlock; // 0 - false 1 - true;
- int minotaur; // 0 - false 1 - true;
- int arrayOfEdges[200]; // -1 if null
- } vertexData;
- vertexData tempDataArray[200];
- void recordEdgeData(char* fileName, char* directoryName,int deadlock,int minotaur, int* arrayOfEdges,int size){
- strcpy(tempDataArray[TDAcurrPos].fileName,fileName);
- strcpy(tempDataArray[TDAcurrPos].directoryName,directoryName);
- if(minotaur == 1){
- tempDataArray[TDAcurrPos].minotaur = 1;
- tempDataArray[TDAcurrPos].deadlock = 0;
- tempDataArray[TDAcurrPos].arrayOfEdges[0] = -1;
- } else if(deadlock == 1){
- tempDataArray[TDAcurrPos].minotaur = 0;
- tempDataArray[TDAcurrPos].deadlock = 1;
- tempDataArray[TDAcurrPos].arrayOfEdges[0] = -1;
- } else if(deadlock == 0 && minotaur == 0){
- tempDataArray[TDAcurrPos].minotaur = 0;
- tempDataArray[TDAcurrPos].deadlock = 0;
- //TODO вынести в отдельную функцию
- for(int i=0; i<size; i++ )
- tempDataArray[TDAcurrPos].arrayOfEdges[i] = arrayOfEdges[i];
- }
- TDAcurrPos++;
- }
- char* parseInsideData(char* fileName, char* dirPath, char* readBuffer, int iterator){
- printf("%s\n", "------Start parse-----------");
- int arrayOfEdges[200];
- int k = 0;
- if(readBuffer[0] == 'D'){
- printf("%s\n", "deadlock" );
- recordEdgeData(fileName, dirPath, 1, 0, NULL , -1) ;
- } else if(readBuffer[0] == 'M'){
- printf("%s\n", "minotaur" );
- recordEdgeData(fileName, dirPath, 0, 1, NULL, -1) ;
- } else {
- int j = 0;
- char* temp = NULL;
- for (int i = 0; i< iterator; i++){
- if(readBuffer[i] == '\n') continue;
- temp = (char*)realloc(temp, j+1);
- temp[j] = readBuffer[i];
- j++;
- if(readBuffer[i+1] == '\n'){
- arrayOfEdges[k] = (int) strtol(temp, (char **)NULL, 10);
- k++;
- free(temp);
- temp = NULL;
- j = 0;
- }
- }
- recordEdgeData(fileName, dirPath, 0, 0, arrayOfEdges, k);
- }
- for(int z= 0; z<k; z++)
- printf("%s %d\n", "---->",arrayOfEdges[z] );
- printf("%s\n", "--------End parse---------");
- }
- // int* parseInt(char* readBuffer, int size){
- // for(int i = 0; i< iterator; i++){
- // }
- // return intArray;
- // }
- void listdir(const char *name, int indent)
- {
- DIR *dir;
- struct dirent *entry;
- char path[1024];
- if (!(dir = opendir(name)))
- return;
- while ((entry = readdir(dir)) != NULL) {
- if (entry->d_type == DT_DIR) {
- if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
- continue;
- snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
- // printf("%*s[%s]\n", indent, "", entry->d_name);
- listdir(path, indent + 2);
- //printf("%s\n",path);
- }
- else {
- char ch,readBuffer[200], file_name[25],dirPath[200], fileName[200],tempStr[200];
- printf("%s/\n", name);
- printf("%s\n", entry->d_name);
- strcpy(dirPath, name);
- strcpy(fileName, entry->d_name);
- strcat(dirPath, "/");
- strcpy(tempStr, dirPath);
- strcat(tempStr, fileName);
- /*------------------------------------------------------*/
- FILE* fp = fopen(tempStr,"r"); // read mode
- int iterator = 0;
- if( fp == NULL )
- {
- perror("Error while opening the file.\n");
- exit(EXIT_FAILURE);
- }
- printf("The contents of %s file are :\n", fileName);
- while( ( ch = fgetc(fp) ) != EOF ){
- printf("%c",ch);
- readBuffer[iterator] = ch;
- iterator++;
- }
- fclose(fp);
- //for(int i = 0; i< iterator; i++) printf("%c\n", readBuffer[i]);
- /*------------------------------------------------------*/
- parseInsideData(fileName, dirPath, readBuffer, iterator);
- }
- }
- // printf("%s\n",path);
- closedir(dir);
- }
- int main(void) {
- int u = 6;
- listdir("LIST DIR", 0);
- printf("%s\n", "###########start############");
- printf("%s\n", tempDataArray[u].fileName);
- printf("%s\n", tempDataArray[u].directoryName);
- printf("%d\n",tempDataArray[u].deadlock);
- printf("%d\n", tempDataArray[u].minotaur);
- printf("%s\n", "###########edges############");
- printf("%d\n", tempDataArray[u].arrayOfEdges[0]);
- printf("%d\n", tempDataArray[u].arrayOfEdges[1]);
- printf("%d\n",tempDataArray[u].arrayOfEdges[2]);
- printf("%d\n",tempDataArray[u].arrayOfEdges[3]);
- printf("%s\n", "#########end##############");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement