Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <errno.h>
  8. #include <string.h>
  9. #include <dirent.h>
  10. #include <unistd.h>
  11.  
  12. /* Functii pentru validarea argumentelor */
  13. int checkIntegerArgument(char* s);
  14. int checkFilePath(char* path);
  15.  
  16. /* Functii pentru task1(Histogram) */
  17. int createMask(int noBits);
  18. char* charToString(char c);
  19. int stringToDecimal(char* c);
  20. void shiftRight(char c[], int bits);
  21. int* getFreqForChar(char b, int bits);
  22. void histogram(int noBits, char* path);
  23.  
  24. /* Functii pentru task2(Cea mai lunga secventa de 1) */
  25. void findSequence(char c, int* longestSequence, int* bytesRead, int* maxSequence, int* zeroBefore, int* newSeq, int* begOfLongestSeq);
  26. void longestSetSequence(char* path);
  27.  
  28. /* Functii pentru task3(Cautarea template-urilor) */
  29. char* shiftLeft(char *c, char b, int dim);
  30. void findTemplate(char* sablon, int dim, int fd);
  31. void umplere(char c, char* sir, char* sablon, int dim, int* i, int* nr);
  32. int checkTemplateFileVersion(char* path);
  33. int getNumberOfTemplates(char* path);
  34. void getFileMetaData(char* path, int bits, int* dimensions, int* offsets, int* noTemplates);
  35. void templateFunctionV1(int bits, int templateNumber, char* templatePath, char* filePath);
  36. void templateFunctionV2(int bits, char* templatePath, char* filePath);
  37.  
  38. int main(int argc, char* argv[])
  39. {
  40. char* option = argv[1];
  41. /* HISTOGRAM */
  42. if(strcmp(option,"histogram") == 0)
  43. {
  44. if(argc != 4)
  45. {
  46. perror("Argumente insuficiente!");
  47. exit(1);
  48. }
  49.  
  50. char* bits = argv[2];
  51. char* pathToFile = argv[3];
  52.  
  53. char* ptr = strtok(argv[2],"=");
  54. char* array[2];
  55. int count = 0;
  56. while(ptr != NULL)
  57. {
  58. array[count++] = ptr;
  59. ptr = strtok(NULL,"=");
  60. }
  61.  
  62. if(strcmp(array[0],"bits") != 0)
  63. {
  64. printf("Argumentul 2 este invalid\n");
  65. exit(2);
  66. }
  67.  
  68. if(checkIntegerArgument(array[1]) == 0)
  69. {
  70. printf("Argumentul 2 este invalid. Dupa egal nu se afla intreg\n");
  71. exit(2);
  72. }
  73.  
  74. int intValueOfArg = atoi(array[1]);
  75. if(intValueOfArg != 1 && intValueOfArg != 2 && intValueOfArg != 4 && intValueOfArg != 8)
  76. {
  77. printf("ERROR\nInvalid number of bits\nSupported values are: 1, 2, 4, 8\n");
  78. exit(3);
  79. }
  80.  
  81. char* ptr2 = strtok(argv[3],"=");
  82. while(ptr2 != NULL)
  83. {
  84. //printf("%s\n",ptr2);
  85. if(strchr(ptr2,'/') != NULL)
  86. {
  87. if(checkFilePath(ptr2) == 0)
  88. {
  89. printf("ERROR\nInvalid file path\n");
  90. exit(3);
  91. }
  92. else
  93. {
  94. int fd = open(ptr2,O_RDONLY);
  95. if(fd == -1)
  96. {
  97. printf("Nu s-a putut deschide fisierul!");
  98. exit(1);
  99. }
  100. else
  101. {
  102. histogram(intValueOfArg,ptr2);
  103. }
  104. }
  105. }
  106. ptr2 = strtok(NULL,"=");
  107. }
  108. }
  109. /////////////////////////////////////////////////////////////////////////////////////////////////////
  110. /* LONGEST SET SEQUENCE */
  111. if(strcmp(argv[1],"runs") == 0)
  112. {
  113. if(argc != 3)
  114. {
  115. perror("Argumente insuficiente!");
  116. exit(1);
  117. }
  118.  
  119. char* pathToFile = argv[2];
  120. char* ptr2 = strtok(argv[2],"=");
  121. int count2 = 0;
  122. while(ptr2 != NULL)
  123. {
  124. if(strchr(ptr2,'/') != NULL)
  125. {
  126. if(checkFilePath(ptr2) == 0)
  127. {
  128. printf("ERROR\nInvalid file path\n");
  129. exit(3);
  130. }
  131. else
  132. {
  133. int fd = open(ptr2,O_RDONLY);
  134. if(fd == -1)
  135. {
  136. printf("Nu s-a putut deschide fisierul!");
  137. exit(1);
  138. }
  139. else
  140. {
  141. longestSetSequence(ptr2);
  142. }
  143. }
  144. }
  145. ptr2 = strtok(NULL,"=");
  146. }
  147. }
  148. /////////////////////////////////////////////////////////////////////////////////////////////////////
  149.  
  150. if(strcmp(argv[1],"template") == 0)
  151. {
  152. if(argc == 5)
  153. {
  154. char* pathToTemplate = argv[3];
  155. char* pathToFile = argv[4];
  156. char* bits = argv[2];
  157.  
  158.  
  159. char* ptr = strtok(argv[2],"=");
  160. char* array[2];
  161. int count = 0;
  162. while(ptr != NULL)
  163. {
  164. array[count++] = ptr;
  165. ptr = strtok(NULL,"=");
  166. }
  167.  
  168. if(strcmp(array[0],"bits") != 0)
  169. {
  170. printf("Argumentul 2 este invalid\n");
  171. exit(2);
  172. }
  173.  
  174. if(checkIntegerArgument(array[1]) == 0)
  175. {
  176. printf("Argumentul 2 este invalid. Dupa egal nu se afla intreg\n");
  177. exit(2);
  178. }
  179.  
  180. int intValueOfArg = atoi(array[1]);
  181. if(intValueOfArg != 8 && intValueOfArg != 16 && intValueOfArg != 24 && intValueOfArg != 32)
  182. {
  183. printf("ERROR\nInvalid number of bits\nSupported values are: 8, 16, 24, 32\n");
  184. exit(3);
  185. }
  186.  
  187.  
  188. char* ptr2 = strtok(argv[3],"=");
  189. while(ptr2 != NULL)
  190. {
  191. if(strchr(ptr2,'/') != NULL)
  192. {
  193. if(checkFilePath(ptr2) == 0)
  194. {
  195. printf("ERROR\nInvalid file path\n");
  196. exit(3);
  197. }
  198. else
  199. {
  200. int fd = open(ptr2,O_RDONLY);
  201. if(fd == -1)
  202. {
  203. printf("Nu s-a putut deschide fisierul!");
  204. exit(1);
  205. }
  206. else
  207. {
  208. break;
  209. //printf("%s\n",pathToTemplate);
  210. }
  211. }
  212. }
  213. ptr2 = strtok(NULL,"=");
  214. }
  215.  
  216. char* ptr3 = strtok(argv[4],"=");
  217.  
  218. while(ptr3 != NULL)
  219. {
  220. if(strchr(ptr3,'/') != NULL)
  221. {
  222. if(checkFilePath(ptr3) == 0)
  223. {
  224. printf("ERROR\nInvalid file path\n");
  225. exit(3);
  226. }
  227. else
  228. {
  229. int fd = open(ptr3,O_RDONLY);
  230. if(fd == -1)
  231. {
  232. printf("Nu s-a putut deschide fisierul!");
  233. exit(1);
  234. }
  235. else
  236. {
  237. templateFunctionV2(intValueOfArg,ptr2,ptr3);
  238. }
  239. }
  240. }
  241. ptr3 = strtok(NULL,"=");
  242. }
  243. }
  244. }
  245. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  246. if(strcmp(argv[1],"template") == 0)
  247. {
  248. if(argc == 6)
  249. {
  250. char* pathToTemplate = argv[4];
  251. char* pathToFile = argv[5];
  252. char* bits = argv[2];
  253. char* templateNumber = argv[3];
  254.  
  255. char* ptr = strtok(argv[2],"=");
  256. char* array[2];
  257. int count = 0;
  258. while(ptr != NULL)
  259. {
  260. array[count++] = ptr;
  261. ptr = strtok(NULL,"=");
  262. }
  263.  
  264. if(strcmp(array[0],"bits") != 0)
  265. {
  266. printf("Argumentul 2 este invalid\n");
  267. exit(2);
  268. }
  269.  
  270. if(checkIntegerArgument(array[1]) == 0)
  271. {
  272. printf("Argumentul 2 este invalid. Dupa egal nu se afla intreg\n");
  273. exit(2);
  274. }
  275.  
  276. int intValueOfArg = atoi(array[1]);
  277. if(intValueOfArg != 8 && intValueOfArg != 16 && intValueOfArg != 24 && intValueOfArg != 32)
  278. {
  279. printf("ERROR\nInvalid number of bits\nSupported values are: 8, 16, 24, 32\n");
  280. exit(3);
  281. }
  282.  
  283. //int intValueOfTemplate = 5;
  284. char* ptrT = strtok(argv[3],"=");
  285. char* arrayT[2];
  286. int countT = 0;
  287.  
  288. while(ptrT != NULL)
  289. {
  290. arrayT[countT++] = ptrT;
  291. ptrT = strtok(NULL,"=");
  292. }
  293.  
  294. if(strcmp(arrayT[0],"template") != 0)
  295. {
  296. printf("Argumentul 3 este invalid\nCorect este template=dimension");
  297. exit(2);
  298. }
  299.  
  300. if(checkIntegerArgument(arrayT[1]) == 0)
  301. {
  302. printf("Argumentul 3 este invalid. Dupa egal nu se afla un intreg\n");
  303. exit(2);
  304. }
  305.  
  306. int intValueOfTemplate = atoi(arrayT[1]);
  307.  
  308. char* ptr2 = strtok(argv[4],"=");
  309. while(ptr2 != NULL)
  310. {
  311. if(strchr(ptr2,'/') != NULL)
  312. {
  313. if(checkFilePath(ptr2) == 0)
  314. {
  315. printf("ERROR\nInvalid file path\n");
  316. exit(3);
  317. }
  318. else
  319. {
  320. int fd = open(ptr2,O_RDONLY);
  321. if(fd == -1)
  322. {
  323. printf("Nu s-a putut deschide fisierul!");
  324. exit(1);
  325. }
  326. else
  327. {
  328. break;
  329. //printf("%s\n",pathToTemplate);
  330. }
  331. }
  332. }
  333. ptr2 = strtok(NULL,"=");
  334. }
  335.  
  336. char* ptr3 = strtok(argv[5],"=");
  337.  
  338. while(ptr3 != NULL)
  339. {
  340. if(strchr(ptr3,'/') != NULL)
  341. {
  342. if(checkFilePath(ptr3) == 0)
  343. {
  344. printf("ERROR\nInvalid file path\n");
  345. exit(3);
  346. }
  347. else
  348. {
  349. int fd = open(ptr3,O_RDONLY);
  350. if(fd == -1)
  351. {
  352. printf("Nu s-a putut deschide fisierul!");
  353. exit(1);
  354. }
  355. else
  356. {
  357. templateFunctionV1(intValueOfArg,intValueOfTemplate,ptr2,ptr3);
  358. }
  359. }
  360. }
  361. ptr3 = strtok(NULL,"=");
  362. }
  363. }
  364. }
  365. }
  366.  
  367. /* FUNCTII PENTRU VALIDAREA ARGUMENTELOR */
  368.  
  369. int checkFilePath(char* path)
  370. {
  371. int fd = open(path,O_RDONLY);
  372. if(fd == -1)
  373. {
  374. printf("ERROR\nInvalid file path\n");
  375. exit(1);
  376. }
  377. struct stat statbuf;
  378. lstat(path,&statbuf);
  379. if(S_ISREG(statbuf.st_mode))
  380. {
  381. return 1;
  382. }
  383. else
  384. {
  385. printf("ERROR\nInvalid file path\nThe specified path does not lead to a valid file\n");
  386. exit(1);
  387. }
  388. close(fd);
  389. }
  390.  
  391. int checkIntegerArgument(char* s)
  392. {
  393. for(int i = 0; i < strlen(s); i++)
  394. {
  395. if(s[i] < '0' || s[i] > '9')
  396. {
  397. return 0;
  398. }
  399. }
  400. return 1;
  401. }
  402.  
  403. int checkTemplateFileVersion(char* path)
  404. {
  405. int fd = open(path,O_RDONLY);
  406. if(fd == -1)
  407. {
  408. perror("Fisierul nu s-a putut deschide!");
  409. exit(1);
  410. }
  411.  
  412. char c;
  413. char c2;
  414. long cursor = lseek(fd,0,SEEK_SET);
  415. read(fd,&c,sizeof(char));
  416. read(fd,&c2,sizeof(char));
  417. char* p = charToString(c);
  418. char* p1 = charToString(c2);
  419. strcat(p1,p);
  420. int v = stringToDecimal(p1);
  421. if(v >= 12345 && v <= 54321)
  422. {
  423. return v;
  424. }
  425. else
  426. {
  427. printf("ERROR\nWrong file version\nIt must be 12345 and 54321\n");
  428. exit(1);
  429. }
  430. }
  431.  
  432. int getNumberOfTemplates(char* path)
  433. {
  434. int fd = open(path,O_RDONLY);
  435. if(fd == -1)
  436. {
  437. perror("Fisierul nu s-a putut deschide!");
  438. exit(1);
  439. }
  440.  
  441. char c;
  442. char c2;
  443. long cursor = lseek(fd,2,SEEK_SET);
  444. read(fd,&c,sizeof(char));
  445. read(fd,&c2,sizeof(char));
  446. char* p = charToString(c);
  447. char* p1 = charToString(c2);
  448. strcat(p1,p);
  449. int nrSabloane = stringToDecimal(p1);
  450. return nrSabloane;
  451. }
  452.  
  453. void getFileMetaData(char* path, int bits, int* dimensions, int* offsets, int* noTemplates)
  454. {
  455. int fd = open(path,O_RDONLY);
  456. if(fd == -1)
  457. {
  458. perror("Fisierul nu s-a putut deschide!");
  459. exit(1);
  460. }
  461.  
  462. int nrSabloane = getNumberOfTemplates(path);
  463.  
  464. long cursor = lseek(fd,4,SEEK_SET);
  465. char c,c2;
  466. char* p;
  467. char* p1;
  468. char* pp;
  469. char* pp1;
  470.  
  471. int count = 0;
  472. for(int i = 1; i <= nrSabloane; i++)
  473. {
  474. read(fd,&c,sizeof(char));
  475. read(fd,&c2,sizeof(char));
  476. p = charToString(c);
  477. p1 = charToString(c2);
  478. strcat(p1,p);
  479. int dim = stringToDecimal(p1);
  480.  
  481. if(dim == bits)
  482. {
  483. (*dimensions) = dim;
  484. read(fd,&c,sizeof(char));
  485. read(fd,&c2,sizeof(char));
  486. p = charToString(c);
  487. p1 = charToString(c2);
  488. strcat(p1,p);
  489. int nrSab = stringToDecimal(p1);
  490. (*noTemplates) = nrSab;
  491.  
  492.  
  493. read(fd,&c,sizeof(char));
  494. read(fd,&c2,sizeof(char));
  495. p = charToString(c);
  496. p1 = charToString(c2);
  497. strcat(p1,p);
  498. pp = p1;
  499.  
  500.  
  501. read(fd,&c,sizeof(char));
  502. read(fd,&c2,sizeof(char));
  503. p = charToString(c);
  504. p1 = charToString(c2);
  505. strcat(p1,p);
  506. pp1 = p1;
  507. strcat(pp1,pp);
  508. int offsetInceput = stringToDecimal(pp1);
  509. (*offsets) = offsetInceput;
  510.  
  511. printf("Dimensiunea sablon %d este %d | Numarul de sabloane este %d | Offsetul: %d \n",i,dim,nrSab,offsetInceput);
  512. }
  513. else
  514. {
  515. read(fd,&c,sizeof(char));
  516. read(fd,&c2,sizeof(char));
  517. p = charToString(c);
  518. p1 = charToString(c2);
  519. strcat(p1,p);
  520. int nrSab = stringToDecimal(p1);
  521. //(*noTemplates) = nrSab;
  522.  
  523.  
  524. read(fd,&c,sizeof(char));
  525. read(fd,&c2,sizeof(char));
  526. p = charToString(c);
  527. p1 = charToString(c2);
  528. strcat(p1,p);
  529. pp = p1;
  530.  
  531.  
  532. read(fd,&c,sizeof(char));
  533. read(fd,&c2,sizeof(char));
  534. p = charToString(c);
  535. p1 = charToString(c2);
  536. strcat(p1,p);
  537. pp1 = p1;
  538. strcat(pp1,pp);
  539. int offsetInceput = stringToDecimal(pp1);
  540. //(*offsets) = offsetInceput;
  541. }
  542.  
  543. }
  544. }
  545.  
  546. /* FUNCTIILE PRINCIPALE */
  547.  
  548. void histogram(int noBits, char* path)
  549. {
  550. int fd = open(path,O_RDONLY);
  551. if(fd == -1)
  552. {
  553. perror("Fisierul nu s-a putut deschide\n");
  554. exit(1);
  555. }
  556.  
  557. char buffer[16384];
  558.  
  559. if(read(fd,&buffer,sizeof(buffer)) < 0)
  560. {
  561. printf("Nu am putut citi din fisier!\n");
  562. exit(4);
  563. }
  564.  
  565. long cursor = lseek(fd,0,SEEK_SET);
  566. int* ap = calloc((1 << noBits), sizeof(int));
  567. int* aparitii = calloc((1 << noBits), sizeof(int));
  568. if(ap == NULL || aparitii == NULL)
  569. {
  570. printf("Eroare la alocarea memoriei\n");
  571. exit(4);
  572. }
  573. while(read(fd,&buffer,sizeof(buffer)) != 0)
  574. {
  575. for(int i = 0; i < sizeof(buffer); i++)
  576. {
  577. ap = getFreqForChar(buffer[i],noBits);
  578. for(int i = 0; i < (1 << noBits); i++)
  579. {
  580. aparitii[i] += ap[i];
  581. }
  582. }
  583. }
  584. for(int j = 0; j < (1 << noBits); j++)
  585. {
  586. char c = j;
  587. printf("%s: %d\n",charToString(c),aparitii[j]);
  588. }
  589. free(ap);
  590. free(aparitii);
  591. close(fd);
  592. }
  593.  
  594. void longestSetSequence(char* path)
  595. {
  596. int fd = open(path,O_RDONLY);
  597. if(fd == -1)
  598. {
  599. printf("Fisierul nu s-a putut deschide!");
  600. exit(1);
  601. }
  602. char buffer[16348];
  603. if(read(fd,&buffer,sizeof(buffer)) < 0)
  604. {
  605. printf("Nu am putut citi din fisier!\n");
  606. exit(4);
  607. }
  608. long cursor = lseek(fd,0,SEEK_SET);
  609. int longestSequence = 0;
  610. int bytesRead = 0;
  611. int maxSequence = 0;
  612. int newSequence = 0;
  613. int begOfLongestSequence = 0;
  614. int zeroBefore = 0;
  615. while(read(fd,&buffer,sizeof(buffer)) != 0)
  616. {
  617. for(int i = 0; i < sizeof(buffer); i++)
  618. {
  619. findSequence(buffer[i],&longestSequence,&bytesRead,&maxSequence,&zeroBefore,&newSequence,&begOfLongestSequence);
  620. }
  621. }
  622. printf("SUCCES\nLength of the longest run: %d\nOffset: %d bytes + %d bits\n",longestSequence,begOfLongestSequence / 8, begOfLongestSequence % 8);
  623. }
  624.  
  625. void templateFunctionV2(int bits, char* templatePath, char* filePath)
  626. {
  627. int fd = open(templatePath,O_RDONLY);
  628. if(fd == -1)
  629. {
  630. printf("Nu s-a putut citi din fisier!");
  631. exit(1);
  632. }
  633.  
  634. int v = checkTemplateFileVersion(templatePath);
  635. printf("Versiune fisier: %d\n",v);
  636.  
  637. int nrSabloane = getNumberOfTemplates(templatePath);
  638. printf("Tipuri de sabloane: %d\n",nrSabloane);
  639.  
  640. int offsets;
  641. int dimensions;
  642. int noTemplates;
  643. getFileMetaData(templatePath,bits,&dimensions,&offsets,&noTemplates);
  644.  
  645. int start = offsets;
  646. int dimension = dimensions;
  647. int noTemp = noTemplates;
  648.  
  649. long cursor = lseek(fd,start,SEEK_SET);
  650. printf("START: %d\n",start);
  651. while(noTemp--)
  652. {
  653. char toRead[dimension];
  654. read(fd,toRead,sizeof(toRead) / 8);
  655. char toReadCopy[dimension/8][8];
  656. int nr1 = 0;
  657. int l = 0;
  658. int q = 0;
  659.  
  660. while(q < dimension)
  661. {
  662. char* ss = charToString(toRead[q]);
  663. nr1++;
  664. if(nr1 <= dimension / 8)
  665. {
  666. strcpy(toReadCopy[nr1-1],ss);
  667. }
  668. else
  669. {
  670. break;
  671. }
  672. q++;
  673. }
  674.  
  675. int fd2 = open(filePath,O_RDONLY);
  676. char template[dimension];
  677. for(int i = 0; i < dimension / 8; i++)
  678. {
  679. for(int j = 0; j < dimension; j++)
  680. {
  681. template[8 * i + j] = toReadCopy[i][j];
  682. template[dimension] = '\0';
  683. }
  684. }
  685. printf("\nSablonul: %s ",template);
  686. findTemplate(template,dimension,fd2);
  687. }
  688. printf("\n");
  689. }
  690.  
  691. void templateFunctionV1(int bits, int templateNumber, char* templatePath, char* filePath)
  692. {
  693. int fd = open(templatePath,O_RDONLY);
  694. if(fd == -1)
  695. {
  696. printf("Nu s-a putut citi din fisier!");
  697. exit(1);
  698. }
  699.  
  700. int v = checkTemplateFileVersion(templatePath);
  701. printf("Versiune fisier: %d\n",v);
  702.  
  703. int nrSabloane = getNumberOfTemplates(templatePath);
  704. printf("Tipuri de sabloane: %d\n",nrSabloane);
  705.  
  706. int* offsets = (int*)malloc(4 * sizeof(int));
  707. int* dimensions = (int*)malloc(4 * sizeof(int));
  708. int* noTemplates = (int*)malloc(4 * sizeof(int));
  709. //getFileMetaData(templatePath,&dimensions,&offsets,&noTemplates);
  710.  
  711. int start;
  712. int dimension;
  713. int noTemp;
  714. switch(bits)
  715. {
  716. case 8: start = offsets[0]; dimension = dimensions[0]; noTemp = noTemplates[0]; break;
  717. case 16: start = offsets[1]; dimension = dimensions[1]; noTemp = noTemplates[1]; break;
  718. case 24: start = offsets[2]; dimension = dimensions[2]; noTemp = noTemplates[2]; break;
  719. case 32: start = offsets[3]; dimension = dimensions[3]; noTemp = noTemplates[3]; break;
  720. }
  721.  
  722. long cursor = lseek(fd,start,SEEK_SET);
  723. char toRead[dimension];
  724. char toReadCopy[dimension/8][8];
  725. int i = 0;
  726. while(i < noTemp)
  727. {
  728. read(fd,toRead,sizeof(toRead) / 8);
  729.  
  730. if(i == templateNumber)
  731. {
  732. int nr1 = 0;
  733. int l = 0;
  734. int q = 0;
  735.  
  736. while(q < dimension)
  737. {
  738. char* ss = charToString(toRead[q]);
  739. nr1++;
  740. if(nr1 <= dimension / 8)
  741. {
  742. strcpy(toReadCopy[nr1-1],ss);
  743. }
  744. else
  745. {
  746. break;
  747. }
  748. q++;
  749. }
  750.  
  751. int fd2 = open(filePath,O_RDONLY);
  752. char template[dimension];
  753. for(int i = 0; i < dimension / 8; i++)
  754. {
  755. for(int j = 0; j < dimension; j++)
  756. {
  757. template[8 * i + j] = toReadCopy[i][j];
  758. template[dimension] = '\0';
  759. }
  760. }
  761. printf("\nSablonul: %s ",template);
  762. findTemplate(template,dimension,fd2);
  763. }
  764.  
  765. i++;
  766. }
  767. printf("\n");
  768. }
  769.  
  770. /* FUNCTII AUXILIARE */
  771.  
  772. int createMask(int noBits)
  773. {
  774. int rez = 0;
  775. for(int i = 0; i < noBits; i++)
  776. {
  777. rez |= 1 << i;
  778. }
  779. return rez;
  780. }
  781.  
  782. char* charToString(char c)
  783. {
  784. int valC = (int)c;
  785. char* octet = (char*)malloc(9 * sizeof(char));
  786. if(octet == NULL)
  787. {
  788. printf("Eroare la alocarea memoriei!");
  789. exit(4);
  790. }
  791. octet[0] = '\0';
  792. for(int z = (1 << 7); z > 0; z >>= 1)
  793. {
  794. strcat(octet,(((valC & z) == z) ? "1" : "0"));
  795. }
  796. return octet;
  797. }
  798.  
  799. int stringToDecimal(char* c)
  800. {
  801. int decValue = 0;
  802. int putere = 0;
  803. for(int i = strlen(c) - 1; i >= 0; i--)
  804. {
  805. int k = c[i] - '0';
  806. k = k <<= putere;
  807. putere++;
  808. decValue += k;
  809. }
  810. return decValue;
  811. }
  812.  
  813. void shiftRight(char c[], int bits)
  814. {
  815. int i = 0;
  816. while(i < bits)
  817. {
  818. memmove(c+1,c,strlen(c) - 1);
  819. c[0] = '0';
  820. i++;
  821. }
  822. }
  823.  
  824. char* shiftLeft(char *c, char b, int dim)
  825. {
  826. memmove(c,c+1,dim);
  827. c[dim - 1] = b;
  828. return c;
  829. }
  830.  
  831. int* getFreqForChar(char b, int bits)
  832. {
  833. int* aparitii = (int*)calloc((1<<bits) , sizeof(int));
  834. if(aparitii == NULL)
  835. {
  836. printf("Eroare la alocarea memoriei!");
  837. exit(4);
  838. }
  839. char* c = charToString(b);
  840. while (strcmp(c,"00000000") != 0)
  841. {
  842. int i = 0;
  843. while(i < 8 / bits)
  844. {
  845. int extract = stringToDecimal(c);
  846. int val = createMask(bits);
  847. int ap = extract & val;
  848. aparitii[ap]++;
  849. shiftRight(c,bits);
  850. i++;
  851. }
  852. }
  853. return aparitii;
  854. }
  855.  
  856. void findSequence(char c, int* longestSequence, int* bytesRead, int* maxSequence, int* zeroBefore, int* newSeq, int* begOfLongestSeq)
  857. {
  858. int valC = (int)c;
  859. int var = *maxSequence;
  860. for(int j = 7; j >= 0; j--)
  861. {
  862. int copy = (valC >> j);
  863. if((copy & 1) != 0)
  864. {
  865. if((*zeroBefore) == 0)
  866. {
  867. (*zeroBefore) = 1;
  868. (*newSeq) = (*bytesRead);
  869. }
  870. (*maxSequence)++;
  871. if((*maxSequence) > (*longestSequence))
  872. {
  873. (*begOfLongestSeq) = (*newSeq);
  874. (*longestSequence) = (*maxSequence);
  875. }
  876. }
  877. else
  878. {
  879. (*zeroBefore) = 0;
  880. (*maxSequence) = 0;
  881. }
  882. (*bytesRead)++;
  883. }
  884. }
  885.  
  886. void umplere(char c, char* sir, char* sablon, int dim, int* i, int* nr)
  887. {
  888. int valC = (int)c;
  889. for(unsigned char z = (1 << 7); z > 0; z >>= 1)
  890. {
  891. if(*i < dim)
  892. {
  893. strcat(sir,(((valC & z) == z) ? "1" : "0"));
  894. (*i)++;
  895. if(((*i) == dim) && (strcmp(sir,sablon) == 0))
  896. {
  897. (*nr)++;
  898. }
  899. }
  900. else
  901. {
  902. if((valC & z) == z)
  903. {
  904. sir = shiftLeft(sir,'1',dim);
  905. }
  906. else
  907. {
  908. sir = shiftLeft(sir,'0',dim);
  909. }
  910.  
  911. if(strcmp(sablon,sir) == 0)
  912. {
  913. (*nr)++;
  914. }
  915. }
  916. }
  917. }
  918.  
  919. void findTemplate(char* sablon, int dim, int fd)
  920. {
  921. char* sir = (char*)malloc(dim * sizeof(char));
  922. char c;
  923. int i = 0;
  924. int nr = 0;
  925. while(read(fd,&c,sizeof(char)) != 0)
  926. {
  927. umplere(c,sir,sablon,dim,&i,&nr);
  928. }
  929. printf(" | Numar de aparitii: %d",nr);
  930. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement