Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.58 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. int TDAsize = 0;
  14.  
  15. typedef struct vertexData {
  16. char fileName[100];
  17. char directoryName[1000];
  18. int deadlock; // 0 - false 1 - true;
  19. int minotaur; // 0 - false 1 - true;
  20. int arrayOfEdges[200]; // -1 if null
  21. int sizeofArrEdges ;
  22.  
  23. } vertexData;
  24.  
  25. vertexData tempDataArray[200];
  26.  
  27. void recordEdgeData(char* fileName, char* directoryName,int deadlock,int minotaur, int* arrayOfEdges,int size){
  28. strcpy(tempDataArray[TDAcurrPos].fileName,fileName);
  29. strcpy(tempDataArray[TDAcurrPos].directoryName,directoryName);
  30. if(minotaur == 1){
  31. tempDataArray[TDAcurrPos].minotaur = 1;
  32. tempDataArray[TDAcurrPos].deadlock = 0;
  33. tempDataArray[TDAcurrPos].arrayOfEdges[0] = -1;
  34. } else if(deadlock == 1){
  35. tempDataArray[TDAcurrPos].minotaur = 0;
  36. tempDataArray[TDAcurrPos].deadlock = 1;
  37. tempDataArray[TDAcurrPos].arrayOfEdges[0] = -1;
  38. } else if(deadlock == 0 && minotaur == 0){
  39. tempDataArray[TDAcurrPos].minotaur = 0;
  40. tempDataArray[TDAcurrPos].deadlock = 0;
  41. tempDataArray[TDAcurrPos].sizeofArrEdges = size;
  42. //TODO вынести в отдельную функцию
  43. for(int i=0; i<size; i++ )
  44. tempDataArray[TDAcurrPos].arrayOfEdges[i] = arrayOfEdges[i];
  45. }
  46.  
  47. TDAcurrPos++;
  48. TDAsize++;
  49. }
  50.  
  51. char* parseInsideData(char* fileName, char* dirPath, char* readBuffer, int iterator){
  52.  
  53. printf("%s\n", "------Start parse-----------");
  54. int arrayOfEdges[200];
  55. int k = 0;
  56. if(readBuffer[0] == 'D'){
  57.  
  58. printf("%s\n", "deadlock" );
  59. recordEdgeData(fileName, dirPath, 1, 0, NULL , -1) ;
  60.  
  61. } else if(readBuffer[0] == 'M'){
  62. printf("%s\n", "minotaur" );
  63. recordEdgeData(fileName, dirPath, 0, 1, NULL, -1) ;
  64.  
  65. } else {
  66.  
  67. int j = 0;
  68. char* temp = NULL;
  69. for (int i = 0; i< iterator; i++){
  70. if(readBuffer[i] == '\n') continue;
  71. temp = (char*)realloc(temp, j+1);
  72. temp[j] = readBuffer[i];
  73. j++;
  74.  
  75.  
  76. if(readBuffer[i+1] == '\n'){
  77. arrayOfEdges[k] = (int) strtol(temp, (char **)NULL, 10);
  78. k++;
  79. free(temp);
  80. temp = NULL;
  81. j = 0;
  82. }
  83. }
  84. recordEdgeData(fileName, dirPath, 0, 0, arrayOfEdges, k);
  85. }
  86.  
  87. for(int z= 0; z<k; z++)
  88. printf("%s %d\n", "---->",arrayOfEdges[z] );
  89.  
  90. printf("%s\n", "--------End parse---------");
  91.  
  92. }
  93.  
  94. // int* parseInt(char* readBuffer, int size){
  95.  
  96. // for(int i = 0; i< iterator; i++){
  97.  
  98. // }
  99. // return intArray;
  100. // }
  101.  
  102. void listdir(const char *name, int indent)
  103. {
  104. DIR *dir;
  105. struct dirent *entry;
  106. char path[1024];
  107. if (!(dir = opendir(name)))
  108. return;
  109.  
  110. while ((entry = readdir(dir)) != NULL) {
  111.  
  112. if (entry->d_type == DT_DIR) {
  113.  
  114.  
  115. if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
  116. continue;
  117. snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
  118. // printf("%*s[%s]\n", indent, "", entry->d_name);
  119. listdir(path, indent + 2);
  120. //printf("%s\n",path);
  121. }
  122. else {
  123. char ch,readBuffer[200], file_name[25],dirPath[200], fileName[200],tempStr[200];
  124.  
  125. printf("%s/\n", name);
  126. printf("%s\n", entry->d_name);
  127.  
  128. strcpy(dirPath, name);
  129. strcpy(fileName, entry->d_name);
  130. strcat(dirPath, "/");
  131. strcpy(tempStr, dirPath);
  132. strcat(tempStr, fileName);
  133. /*------------------------------------------------------*/
  134. FILE* fp = fopen(tempStr,"r"); // read mode
  135. int iterator = 0;
  136. if( fp == NULL )
  137. {
  138. perror("Error while opening the file.\n");
  139. exit(EXIT_FAILURE);
  140. }
  141.  
  142. printf("The contents of %s file are :\n", fileName);
  143.  
  144. while( ( ch = fgetc(fp) ) != EOF ){
  145. printf("%c",ch);
  146. readBuffer[iterator] = ch;
  147. iterator++;
  148. }
  149. fclose(fp);
  150.  
  151. //for(int i = 0; i< iterator; i++) printf("%c\n", readBuffer[i]);
  152. /*------------------------------------------------------*/
  153. parseInsideData(fileName, dirPath, readBuffer, iterator);
  154.  
  155. }
  156. }
  157. // printf("%s\n",path);
  158. closedir(dir);
  159. }
  160.  
  161. /* part 2*/
  162.  
  163. // /* reverse: переворачиваем строку s на месте */
  164. // void reverse(char s[])
  165. // {
  166. // int i, j;
  167. // char c;
  168.  
  169. // for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
  170. // c = s[i];
  171. // s[i] = s[j];
  172. // s[j] = c;
  173. // }
  174. // }
  175.  
  176.  
  177. // char* itoa(int n, char s[])
  178. // {
  179. // int i, sign;
  180.  
  181. // if ((sign = n) < 0) /* записываем знак */
  182. // n = -n; /* делаем n положительным числом */
  183. // i = 0;
  184. // do { /* генерируем цифры в обратном порядке */
  185. // s[i++] = n % 10 + '0'; /* берем следующую цифру */
  186. // } while ((n /= 10) > 0); /* удаляем */
  187. // if (sign < 0)
  188. // s[i++] = '-';
  189. // s[i] = '\0';
  190. // reverse(s);
  191. // return s;
  192. // }
  193.  
  194. char *itoa(int n, char *s, int b) {
  195. static char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
  196. int i=0, sign;
  197.  
  198. if ((sign = n) < 0)
  199. n = -n;
  200. do {
  201. s[i++] = digits[n % b];
  202. } while ((n /= b) > 0);
  203. if (sign < 0)
  204. s[i++] = '-';
  205. s[i] = '\0';
  206. return strrev(s);
  207. }
  208.  
  209.  
  210. char* toFileName(int number){
  211.  
  212. char* file = "file";
  213. char* num = NULL;
  214. if(number < 10) num = (char*)realloc(num, 2);
  215. else if (number < 100 && number > 10) num = (char*)realloc(num, 3);
  216. else if (number > 100) char num[3];
  217. num = itoa(number, num);
  218. strcat(file, num);
  219. strcat(file, ".txt");
  220. return file;
  221. }
  222.  
  223.  
  224. // void findPath(){
  225. // //list1 = []
  226. // vertexData* list2 = tempDataArray;
  227. // int arraySize = sizeof(tempDataArray)/sizeof(tempDataArray[0]);
  228. // printf("%d\n", tempDataArray[1].sizeofArrEdges);
  229. // char* currentFile = "33.txt"; //FILE S MINOTAVROM
  230. // printf("%d\n", TDAsize);
  231. // for (int i=0;i<TDAsize;i++) {
  232. // for (int j=0;j< tempDataArray[j].sizeofArrEdges ;j++) {
  233. // if (strcmp(list2[i].arrayOfEdges[j].,currentFile) == 0){
  234. // strcpy(currentFile, list2[j].fileName); // ILI PUT
  235. // //list2.delete(j);
  236. // j = 0;
  237. // break;
  238. // }
  239. // break;
  240. // }
  241. // }
  242. // }
  243. // /* --------------- */
  244.  
  245.  
  246.  
  247. int main(void) {
  248. int u = 6;
  249. listdir("LIST DIR", 0);
  250. printf("%s\n", "###########start############");
  251. // findPath();
  252. printf("%s\n", toFileName(10));
  253. // printf("%s\n", tempDataArray[u].fileName);
  254. // printf("%s\n", tempDataArray[u].directoryName);
  255. // printf("%d\n",tempDataArray[u].deadlock);
  256. // printf("%d\n", tempDataArray[u].minotaur);
  257. // printf("%s\n", "###########edges############");
  258. // printf("%d\n", tempDataArray[u].arrayOfEdges[0]);
  259. // printf("%d\n", tempDataArray[u].arrayOfEdges[1]);
  260. // printf("%d\n",tempDataArray[u].arrayOfEdges[2]);
  261. // printf("%d\n",tempDataArray[u].arrayOfEdges[3]);
  262. // printf("%s\n", "#########end##############");
  263. return 0;
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement