Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 28.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define n 65
  6. #define block 5
  7.  
  8. int indexToBlock(int cell);
  9. int blockToIndex(int blk);
  10. long powerExp(int exp);
  11. int power(int value, int powerValue);
  12. int findReqBlocksWithPtr(int rows);
  13. int *findFreeBlock(int reqBlocks, long decBitmap, char* method);
  14. long decimalToBinary(long dec);
  15. long binaryToDecimal(long bin);
  16. int setBitmap(long decBitmap, int blkNo, int value);
  17. int *getDirectoryData(int data, char* method);
  18. int *newMemory();
  19. void printDiskMap(char* method, int *memArray);
  20. int getFileName(int fileData);
  21. int *readcsv(int *memArray, char* method);
  22. void AddFile(char* method, int *memory, int *csvArray);
  23. void deleteFile(char* method, int *memArray, int *csvArray);
  24. int concat123(int xxx, int yyy);
  25.  
  26.  
  27. /* main function to call above defined function */
  28. // @main
  29. int main () {
  30. int *memory;
  31. char * allocMethod;
  32. /*
  33. allocMethod = "indexed";
  34. memory = newMemory();
  35. //readcsv(memory, allocMethod);
  36. //printDiskMap(allocMethod,memory);
  37.  
  38.  
  39. free(memory);
  40. allocMethod = "contiguous";
  41. memory = newMemory();
  42. //readcsv(memory, allocMethod);
  43. //printDiskMap(allocMethod,memory);
  44.  
  45. free(memory);*/
  46.  
  47. allocMethod = "linked";
  48. memory = newMemory();
  49.  
  50. readcsv(memory, allocMethod);
  51. printDiskMap(allocMethod,memory);
  52.  
  53. /*
  54. //testing findFreeBlock function
  55. long todec = 1111100000;
  56. printf("%ld\n",todec);
  57.  
  58.  
  59. int *testArray;
  60. long bitmap = binaryToDecimal(todec);
  61. testArray = findFreeBlock(3, bitmap, "contiguous");
  62. //printf("Contiguous free block: %d\n\n",testArray[0]);
  63.  
  64. testArray = findFreeBlock(6, bitmap, "linked");
  65. printf("linked free block: ");
  66. for (int i = 0; i < 6; i++){
  67. //printf("%d ",testArray[i]);
  68. }
  69.  
  70. testArray = findFreeBlock(5, bitmap, "indexed");
  71. printf("\nindexed free block: ");
  72. for (int i = 0; i < 6; i++){
  73. printf("%d ",testArray[i]);
  74. }
  75. */
  76.  
  77.  
  78. return 0;
  79. }
  80.  
  81. int indexToBlock(int cell){
  82. return cell/5;
  83. }
  84.  
  85. int blockToIndex(int blk){
  86. return blk*5;
  87. }
  88.  
  89. long powerExp(int exp){
  90. long result = 1;
  91. int base = 10;
  92. for (exp;exp>0;exp--){
  93. result *= base;
  94. }
  95. return result;
  96.  
  97. }
  98.  
  99. int power(int value, int powerValue){
  100. int result = 1;
  101. for (int i = 0; i < powerValue; i++){
  102. result *= value;
  103. }
  104. return result;
  105. }
  106.  
  107. // @findReqBlocksWithPtr
  108. int findReqBlocksWithPtr(int rows){
  109. int reqBlocks = 0;
  110. // if theres 5 or less rows, only 1 block is needed
  111. if (rows <= 5){
  112. reqBlocks = 1;
  113. }
  114.  
  115. // if theres more than 5 rows, each block can only store
  116. // the location of 4 blocks, and 1 pointer
  117. else {
  118. while (rows > 0) {
  119. // if 1 block is remaining, it does not need 1 more index block, it can fit in the last index block
  120. // there is only 4 blocks used in the last index block and we dont need a pointer for it
  121. if (rows != 1){
  122. rows -= 4;
  123. reqBlocks++;
  124. }
  125. else {
  126. break;
  127. }
  128. }
  129. }
  130. return reqBlocks;
  131. }
  132.  
  133. // @findFreeBlock
  134. int *findFreeBlock(int reqBlocks, long decBitmap, char* method){
  135. // convert dec bitmap to bin bitmap
  136. long binBitmap = decimalToBinary(decBitmap);
  137. // how many bits the bitmap have
  138. int nDigits = 1;
  139. long countData = binBitmap;
  140. // divide by 10 until countData is left with a single digit
  141. while (countData > 9) {
  142. nDigits++;
  143. countData /= 10;
  144. }
  145. //printf("digits: %d\n", nDigits);
  146.  
  147. int freeBlocksCount = 0, counter = 0;
  148.  
  149. long remainder;
  150. // finding free blocks for contiguous
  151. if (strcmp(method, "contiguous") == 0){
  152. // create array of size 1 and set it to -1
  153. static int freeBlockFound[1] = {-1};
  154. while(binBitmap != 0) {
  155. remainder = binBitmap%10;
  156. if (remainder == 1){
  157. // free block found
  158. if (freeBlocksCount == 0){
  159. // new free block
  160. freeBlockFound[0] = counter+2;
  161. }
  162. freeBlocksCount +=1;
  163. if (freeBlocksCount == reqBlocks){
  164. return freeBlockFound;
  165. }
  166. }
  167. else {
  168. freeBlocksCount = 0;
  169. freeBlockFound[0] = -1;
  170. }
  171.  
  172. binBitmap /= 10;
  173. counter++;
  174. }
  175. // no space for conti allocation
  176. freeBlockFound[0] = -1;
  177. return freeBlockFound;
  178. }
  179.  
  180. // finding free blocks for linked
  181. else if (strcmp(method, "linked") == 0){
  182. // static int freeBlockFound[reqBlocks];
  183. // create array of size required blocks size and set it to -1
  184. int *freeBlockFound = malloc(reqBlocks * sizeof *freeBlockFound);
  185.  
  186. while(binBitmap != 0) {
  187. remainder = binBitmap%10;
  188. if (remainder == 1){
  189. // free block found
  190. freeBlocksCount++;
  191. freeBlockFound[freeBlocksCount-1] = counter + 2;
  192. if (freeBlocksCount == reqBlocks){
  193. return freeBlockFound;
  194. }
  195. }
  196.  
  197. binBitmap /= 10;
  198. counter++;
  199. }
  200. // no space for linked allocation
  201. freeBlockFound[0] = -1;
  202. return freeBlockFound;
  203. }
  204.  
  205. // finding free blocks for indexed
  206. else {
  207. // calculate number of index blocks required for index allocation
  208. int reqIndexBlocks = findReqBlocksWithPtr(reqBlocks);
  209. // create array of and set it to -1. Every free data and index block found will be stored here
  210. int *freeBlockFound = malloc((reqBlocks + reqIndexBlocks) * sizeof *freeBlockFound);
  211. int enoughFreeIndex = 0;
  212. while(binBitmap != 0) {
  213. remainder = binBitmap%10;
  214. // 5 blocks for index, find free index blocks
  215. if (counter < 5){
  216. // dont need to check already once we know we have enough index blocks
  217. if (enoughFreeIndex == 0){
  218. printf("\nChecking indexed block");
  219. if (remainder == 1) {
  220. printf ("\nfree index block found at: %d",counter+2);
  221. // free index block found
  222. freeBlocksCount++;
  223. freeBlockFound[freeBlocksCount-1] = counter + 2;
  224. if (freeBlocksCount == reqIndexBlocks) {
  225. enoughFreeIndex = 1;
  226. printf("enough index");
  227. }
  228. }
  229. }
  230.  
  231. }
  232. // will run below code once done checking for free index blocks
  233. else if (enoughFreeIndex == 0) {
  234. // no space in index blocks
  235. freeBlockFound[0] = -1;
  236. printf("not enough index");
  237. return freeBlockFound;
  238. }
  239. else {
  240. if (remainder == 1) {
  241. printf ("\nfree data block found at: %d",counter+2);
  242. freeBlocksCount++;
  243. freeBlockFound[freeBlocksCount-1] = counter + 2;
  244. if (freeBlocksCount == reqBlocks + reqIndexBlocks){
  245. return freeBlockFound;
  246. }
  247. }
  248. }
  249. binBitmap /= 10;
  250. counter++;
  251. }
  252. // no space for indexed allocation
  253. freeBlockFound[0] = -1;
  254. return freeBlockFound;
  255.  
  256.  
  257. }
  258. }
  259.  
  260. /* Function to convert a decinal number to binary number */
  261. long decimalToBinary(long dec) {
  262. long remainder;
  263. long binary = 0, i = 1;
  264.  
  265. while(dec != 0) {
  266. remainder = dec % 2;
  267. dec /= 2;
  268. binary += (remainder*i);
  269. i = i*10;
  270. }
  271. return binary;
  272. }
  273.  
  274. /* Function to convert a binary number to decimal number */
  275. long binaryToDecimal(long bin) {
  276. long remainder;
  277. long decimal = 0, i = 0;
  278. while(bin != 0) {
  279. remainder = bin % 10;
  280. bin /= 10;
  281. decimal += (remainder*power(2,i));
  282. ++i;
  283. }
  284. return decimal;
  285. }
  286.  
  287. // @setBitmap
  288. int setBitmap(long decBitmap, int blkNo,int value){
  289. long binBitmap = decimalToBinary(decBitmap);
  290.  
  291. // how many digits the data have
  292. int nDigits = 1, countData = binBitmap;
  293. while (countData > 9) {
  294. nDigits++;
  295. countData /= 10;
  296. }
  297.  
  298. // extracting individual digits into array
  299. int digitsArray[11] = {0};
  300. //memset( digitsArray, 0,nDigits*sizeof(int) );
  301. int digitsArrayInd = 10;
  302.  
  303.  
  304. int remainder;
  305. while(binBitmap != 0)
  306. {
  307. remainder = binBitmap%10;
  308. digitsArray[digitsArrayInd--] = remainder;
  309. binBitmap /= 10;
  310. }
  311.  
  312. digitsArray[12 - blkNo] = value;
  313.  
  314. // putting bitmap back into 1 number
  315. long newBitmap = 0;
  316. for (int i = 0; i < 11; i++){
  317. if (digitsArray[i] == 1){
  318. newBitmap += powerExp(10-i);
  319. }
  320. }
  321.  
  322. printf("%ld\n",newBitmap);
  323. return binaryToDecimal(newBitmap);
  324. }
  325.  
  326. // @getDirectoryData
  327. int *getDirectoryData(int data, char* method){
  328.  
  329. // how many digits the data have
  330. int nDigits = 1, countData = data;
  331. while (countData > 9) {
  332. nDigits++;
  333. countData /= 10;
  334. }
  335. // printf("digits: %d\n", nDigits);
  336.  
  337. // extracting individual digits
  338. int digitsArray[nDigits];
  339. memset( digitsArray, 0,nDigits*sizeof(int) );
  340. int digitsArrayInd = nDigits - 1;
  341.  
  342. int remainder;
  343. while(data != 0)
  344. {
  345. remainder = data%10;
  346. digitsArray[digitsArrayInd--] = remainder;
  347. data /= 10;
  348. }
  349.  
  350. // converting individual digits into correct data
  351. static int dataArray[3] = {0};
  352. int dataArrayInd = nDigits -1;
  353.  
  354. if (strcmp(method, "indexed") == 0){
  355. // INDEXED
  356. // e.g. [1,2,3,4,5] = filename: 123, indexedLocation: 45
  357. dataArray[0] = 0;
  358. dataArray[1] = digitsArray[dataArrayInd--] + (digitsArray[dataArrayInd--]*10);
  359. for (int i = 0; i < nDigits-2; i++){
  360. if (i == 0){
  361. dataArray[0] += digitsArray[dataArrayInd--];
  362. }
  363. else {
  364. dataArray[0] += (digitsArray[dataArrayInd--] * powerExp(i));
  365. }
  366. }
  367. }
  368. else {
  369. // CONTIGUOUS/LINKED
  370. // e.g. [1,2,3,4,5,6,7] = filename: 123, start: 45, end: 67
  371. dataArray[0] = 0;
  372. dataArray[2] = digitsArray[dataArrayInd--] + (digitsArray[dataArrayInd--]*10);
  373. dataArray[1] = digitsArray[dataArrayInd--] + (digitsArray[dataArrayInd--]*10);
  374. for (int i = 0; i < nDigits-4; i++){
  375. if (i == 0){
  376. dataArray[0] += digitsArray[dataArrayInd--];
  377. }
  378. else {
  379. dataArray[0] += (digitsArray[dataArrayInd--] * powerExp(i));
  380. }
  381. }
  382. }
  383.  
  384.  
  385. return dataArray;
  386.  
  387. }
  388.  
  389.  
  390. int *newMemory(){
  391.  
  392. int *mem = malloc(n * sizeof *mem);
  393.  
  394. // all allocation methods will have 1 block reserved for directory
  395. for (int i = 0; i < block; i++){
  396. // -1 meaning empty
  397. mem[i] = -1;
  398. }
  399.  
  400. // all allocation methods will have 1 block reserved for bitmap
  401. for (int i = block; i < 2*block; i++){
  402. // every entry is pointing to next entry (empty space)
  403. if (i == block){
  404. // only first cell of bitmap block will be used
  405. mem[i] = binaryToDecimal(11111111111);
  406. }
  407. else {
  408. // the rest set as -1
  409. mem[i] = -1;
  410. }
  411. }
  412.  
  413. // everything else will be empty data blocks
  414. for (int i = 2*block; i < n; i++){
  415. mem[i] = -1;
  416. }
  417.  
  418. return mem;
  419. }
  420.  
  421. // @printDiskMap
  422. void printDiskMap(char* method, int *memArray){
  423. printf("-Disk map for %s allocation method-\n", method);
  424.  
  425. int *dataArray;
  426.  
  427. //printing directory
  428. if (strcmp("indexed",method) == 0){
  429. for (int i = 0; i < block; i++){
  430. if (memArray[i] < 0){
  431. printf("%2d - B%-2d - %s \n",i,indexToBlock(i),"empty");
  432. }
  433. else {
  434. dataArray = getDirectoryData(memArray[i],method);
  435. printf("%2d - B%-2d - %d, %d \n",i,indexToBlock(i),dataArray[0],dataArray[1]);
  436. }
  437. }
  438. }
  439. else {
  440. for (int i = 0; i < block; i++){
  441. if (memArray[i] < 0){
  442. printf("%2d - B%-2d - %s \n",i,indexToBlock(i),"empty");
  443. }
  444. else {
  445. dataArray = getDirectoryData(memArray[i],method);
  446. printf("%2d - B%-2d - %d, %d, %d \n",i,indexToBlock(i),dataArray[0],dataArray[1],dataArray[2]);
  447. }
  448. }
  449. }
  450.  
  451.  
  452. // printing bitmap block
  453. printf("%2d - B%-2d - ",block,indexToBlock(block));
  454.  
  455. // convert dec bitmap to bin bitmap
  456. long binBitmap = decimalToBinary(memArray[block]);
  457. // how many bits the bitmap have
  458. int nDigits = 1;
  459. long countData = binBitmap;
  460. // divide by 10 until countData is left with a single digit
  461. while (countData > 9) {
  462. nDigits++;
  463. countData /= 10;
  464. }
  465. for (int i = 11-nDigits; i > 0; i--){
  466. printf("0");
  467. }
  468. printf("%ld\n", binBitmap);
  469.  
  470. for (int i = block+1; i < 2*block; i++){
  471. printf("%2d - B%-2d - Unused\n",i,indexToBlock(i));
  472. }
  473.  
  474.  
  475. // printing index and data
  476. for (int i = 2*block; i < n; i++){
  477. printf("%2d - B%-2d - %d\n",i,indexToBlock(i),memArray[i]);
  478. }
  479.  
  480. }
  481.  
  482.  
  483. int getFileName(int fileData){
  484. return (fileData/100)*100;
  485. }
  486.  
  487. // @readcsv
  488. int *readcsv(int *memArray, char* method){
  489. int command[3];
  490. FILE *fp;
  491. char buffer[255];
  492. char * split;
  493. char * retrieveData;
  494. fp = fopen("../data.csv", "r");
  495. // if there is no such file available
  496. // display an error message, break from the method
  497. if (fp == NULL) {
  498. printf("Unable to open file.\n");
  499. return 0;
  500. }
  501. else {
  502. //while (!feof(fp)) {
  503. while ( fgets( buffer, sizeof( buffer ), fp ) != NULL ) {
  504. retrieveData = "";
  505. int count = 0;
  506. // removes \n at the end of the line
  507. buffer[strcspn(buffer, "\n")] = 0;
  508. // splits the line with comma and space as the delimiter
  509. split = strtok(buffer, ", ");
  510.  
  511. while (split != NULL) {
  512. retrieveData = split;
  513. split = strtok(NULL, ", ");
  514. if (count == 0){
  515. // start of new line, obtain command
  516. //printf("%s, ",retrieveData);
  517. if (strcmp(retrieveData,"add") == 0){
  518. command[0] = 0;
  519. }
  520. else if (strcmp(retrieveData,"read") == 0){
  521. command[0] = 1;
  522. }
  523. else {
  524. command[0] = 2;
  525. }
  526. }
  527. else if (count == 1){
  528. // obtain file name
  529. //printf("%s, ",retrieveData);
  530. command[1] = atoi(retrieveData);
  531. }
  532.  
  533. if (split == NULL){
  534. // last entry of the line
  535. //printf("%s\n",retrieveData);
  536. if (count != 1){
  537. command[2] = atoi(retrieveData);
  538. }
  539. else {
  540. command[2] = -1;
  541. }
  542. }
  543. else {
  544. count++;
  545. }
  546.  
  547.  
  548. }
  549.  
  550. if (command[0] == 0){
  551. // call add function
  552. printf("ADD FILE %d\n",command[1]);
  553. AddFile(method, memArray, command);
  554. }
  555. else if (command[0] == 1){
  556. // call read function
  557. printf("READ FILE %d\n",command[1]);
  558. }
  559. else {
  560. // call delete function
  561. printf("DELETE FILE %d\n",command[1]);
  562. // deleteFile(method, memArray, command);
  563. }
  564.  
  565. }
  566.  
  567. }
  568. fclose(fp);
  569.  
  570. }
  571.  
  572. // @concat123
  573. int concat123(int xxx, int yyy){
  574. int temp=0;
  575. int z=xxx;
  576. while(yyy>0)
  577. {
  578. // take reciprocal of y into temp
  579. temp=(temp*10)+(yyy%10);
  580. yyy=yyy/10;
  581. }
  582. while(temp>0)
  583. {
  584. // take each number from last of temp and add to last of z
  585. z=(z*100)+(temp%10);
  586. temp=temp/10;
  587.  
  588. }
  589. return z;
  590. //printf("%d\n",z);
  591.  
  592. }
  593.  
  594. // @addFile
  595. void AddFile(char* method, int *memory, int *csvArray) {
  596.  
  597. // Universal code: Works for all 3 methods, dont need to repeat
  598.  
  599. // result stores how many data files we need to add
  600. int result = csvArray[2] - csvArray[1];
  601.  
  602. int blockcounter;
  603. if(strcmp(method, "linked") == 0){
  604. blockcounter = findReqBlocksWithPtr(result);
  605. }
  606. else {
  607. // blockcounter stores how many data blocks are needed
  608. blockcounter = 0;
  609. if (result % 5 == 0){
  610. blockcounter = result / 5;
  611. }
  612. else {
  613. blockcounter = (result / 5 )+ 1;
  614. }
  615. }
  616.  
  617. printf("csv[1], csv[2]: %d, %d\n",csvArray[1], csvArray[2]);
  618. // obtaining array of free blocks
  619. int* freeBlocks;
  620. freeBlocks = findFreeBlock(blockcounter, memory[5], method);
  621. if (freeBlocks[0] == -1){
  622. // no free space, exit function
  623. printf("Disk full, unable to add file.\n");
  624. return;
  625. }
  626.  
  627. // @addFile conti
  628. if(strcmp(method, "contiguous") == 0){
  629.  
  630. int firstFreeBlock = freeBlocks[0];
  631.  
  632. // join numbers together and store in directory
  633. int firstconcat = concat123(csvArray[1],firstFreeBlock);
  634. int secondconcat = concat123(firstconcat,blockcounter);
  635. //printf("%d\n", firstconcat);
  636. //printf("%d\n", secondconcat);
  637.  
  638. // look for free directory space and update directory entries
  639. int freeDirectoryFound = 0;
  640. for (int i = 0; i < block; i++) {
  641.  
  642. if (memory[i] == -1) {
  643. memory[i] = secondconcat;
  644. //printf("%d\n", memory[i]);
  645. freeDirectoryFound = 1;
  646. break;
  647. }
  648. }
  649.  
  650. if (freeDirectoryFound == 0){
  651. printf("Not enough directory space.\n");
  652. return;
  653. }
  654.  
  655. csvArray[1]++;
  656. for (int i = firstFreeBlock * block; i < firstFreeBlock * block + result; i++) {
  657.  
  658. memory[i] = csvArray[1]++;
  659.  
  660. // if multiple of 5, update bitmap
  661. if (i % 5 == 0){
  662. memory[5] = setBitmap(memory[5], i/5, 0);
  663.  
  664. }
  665. }
  666. }
  667.  
  668. // @addFile linked
  669. else if(strcmp(method, "linked") == 0){
  670.  
  671. int firstFreeBlock = freeBlocks[0];
  672.  
  673. int endingblock = firstFreeBlock-1 + blockcounter;
  674. printf("patrickk%d\n",endingblock);
  675.  
  676.  
  677. // join numbers together and store in directory
  678. int firstconcat = concat123(csvArray[1], firstFreeBlock);
  679. int secondconcat = concat123(firstconcat, endingblock);
  680. printf("spongebob%d\n",secondconcat);
  681.  
  682. // look for free directory space
  683. int freeDirectoryFound = 0;
  684. for (int i = 0; i < block; i++) {
  685.  
  686. if (memory[i] == -1) {
  687. memory[i] = secondconcat;
  688.  
  689. freeDirectoryFound = 1;
  690. break;
  691. }
  692. }
  693.  
  694. if (freeDirectoryFound == 0) {
  695. printf("Not enough directory space.\n");
  696. return;
  697. }
  698. //////////////////////////
  699.  
  700. int toadd = ++csvArray[1];
  701. int curDataBlock;
  702.  
  703. printf("\n\n\n");
  704. // loop freeBlocks
  705.  
  706. //printf("kfc%d", csvArray[0]);
  707.  
  708. printf("Adding file100 and found free B1,B4 with %d entries\n", blockcounter);
  709. printf("Added file100 at B1(101,102,103,104), B04(105,106) %d\n", memory[curDataBlock]);
  710.  
  711.  
  712. for (int c = 0; c < blockcounter; c++) {
  713.  
  714.  
  715. curDataBlock = blockToIndex(freeBlocks[c]);
  716.  
  717. for (int a = 0; a < 4; a++) {
  718. if (toadd <= csvArray[2]) {
  719. //printf("Adding %d to curDataBlock %d\n",toadd, curDataBlock);
  720. memory[curDataBlock++] = toadd++;
  721.  
  722. }
  723. else {
  724.  
  725.  
  726. memory[5] = setBitmap(memory[5], freeBlocks[0], 0);
  727.  
  728. return;
  729. }
  730.  
  731. }
  732. if (c == blockcounter -1){
  733.  
  734.  
  735. if (toadd <= csvArray[2]){
  736. memory[curDataBlock++] = toadd++;
  737.  
  738. memory[5] = setBitmap(memory[5], freeBlocks[0], 0);
  739.  
  740. }
  741. return;
  742.  
  743. }
  744.  
  745. else {
  746. memory[curDataBlock++] = freeBlocks[c+1] * -1;
  747.  
  748. //printf("AFTER ADDINGGGG: %d\n\n\n", memory[curDataBlock]);
  749. curDataBlock++;
  750. }
  751.  
  752. memory[5] = setBitmap(memory[5], freeBlocks[c], 0);
  753. memory[5] = setBitmap(memory[5], freeBlocks[c+1], 0);
  754.  
  755.  
  756.  
  757. }
  758.  
  759.  
  760.  
  761. }
  762.  
  763. // @addFile indexed
  764. else{
  765.  
  766. int firstFreeBlock = freeBlocks[0];
  767.  
  768. // look for free directory space and update directory entries
  769. int firstconcat = concat123(csvArray[1],firstFreeBlock);
  770.  
  771. int reqIndexBlocks = findReqBlocksWithPtr(blockcounter);
  772. printf("\n\nRequired Blocks: %d", blockcounter);
  773. printf("\nRequired Index Blocks: %d\n\n", reqIndexBlocks);
  774.  
  775. // look for free directory space
  776. int freeDirectoryFound = 0;
  777. for (int i = 0; i < block; i++) {
  778. if (memory[i] == -1) {
  779. memory[i] = firstconcat;
  780. freeDirectoryFound = 1;
  781. break;
  782. }
  783. }
  784.  
  785. if (freeDirectoryFound == 0){
  786. printf("Not enough directory space.\n");
  787. return;
  788. }
  789.  
  790. printf("blockcounter: %d\n",blockcounter);
  791.  
  792. printf("freeblocks: ");
  793. for (int i = 0; i < (blockcounter + reqIndexBlocks); i++){
  794. printf("%d ", freeBlocks[i]);
  795. }
  796.  
  797. // insert pointers into index blocks
  798. int remainingDataBlocks = blockcounter;
  799. int curIndexEntry;
  800. int addDataBlock = reqIndexBlocks;
  801.  
  802. // loop through index block location in freeBlocks array
  803. for (int i = 0; i < reqIndexBlocks; i++){
  804. // index location where I need to add the pointer to my data blocks
  805. curIndexEntry = blockToIndex(freeBlocks[i]);
  806. // loop 4 times, adding pointers of data blocks
  807. for (int j = 0; j < 4; j++){
  808. if (remainingDataBlocks < 1){
  809. break;
  810. }
  811. // adding in index entry
  812. memory[curIndexEntry] = freeBlocks[addDataBlock];
  813. memory[5] = setBitmap(memory[5],(curIndexEntry/5),0);
  814. curIndexEntry++;
  815. addDataBlock++;
  816. remainingDataBlocks--;
  817. }
  818. // if left 1 data block remaining, dont need it to be pointer already
  819. if (remainingDataBlocks == 1){
  820. memory[curIndexEntry] = freeBlocks[addDataBlock];
  821. memory[5] = setBitmap(memory[5],(curIndexEntry/5),0);
  822. curIndexEntry++;
  823. addDataBlock++;
  824. remainingDataBlocks--;
  825. }
  826. else if (i != reqIndexBlocks - 1){
  827. memory[curIndexEntry] = (freeBlocks[i+1]) * -1;
  828. curIndexEntry = freeBlocks[i+1];
  829. }
  830.  
  831. }
  832.  
  833. // insert into data blocks
  834. int doneAdding = 0;
  835. int curIndexBlock = freeBlocks[0];
  836. int actualData = csvArray[1]+1;
  837. while (doneAdding == 0){
  838. int indexEntry = blockToIndex(curIndexBlock);
  839. for (int i = indexEntry; i < indexEntry + 5; i++){
  840. if (memory[i] == -1) {
  841. doneAdding = 1;
  842. break;
  843. }
  844. else if (memory[i] > 0) {
  845. for (int j = blockToIndex(memory[i]); j < blockToIndex(memory[i])+5; j++){
  846. memory[j] = actualData++;
  847. memory[5] = setBitmap(memory[5],(j/5),0);
  848. if (actualData > csvArray[2]){
  849. return;
  850. }
  851. }
  852. }
  853. else {
  854. curIndexBlock = memory[i] * -1;
  855. break;
  856. }
  857. }
  858. }
  859.  
  860. //put counter for time here for adding
  861. }
  862.  
  863. }
  864.  
  865. // @deleteFile
  866. void deleteFile(char* method, int *memArray, int *csvArray) {
  867.  
  868. int foundStart, foundEnd;
  869. int startIndex;
  870.  
  871. int fileToDelete = csvArray[1];
  872. int fileFound = 0;
  873.  
  874. // searching for file in directory, then clear that directory entry
  875. for (int i = 0; i < 5; i++) {
  876. if (fileToDelete == getDirectoryData(memArray[i], method)[0]) {
  877. foundStart = getDirectoryData(memArray[i], method)[1]; // start block;
  878. foundEnd = getDirectoryData(memArray[i], method)[2];
  879. memArray[i] = -1;
  880. startIndex = blockToIndex(foundStart);
  881.  
  882. fileFound = 1;
  883. //printf("%d", startIndex);
  884. }
  885. }
  886.  
  887. if (fileFound == 0){
  888. printf("File do not exists.\n");
  889. return;
  890. }
  891.  
  892. // @deleteFile linked
  893. if (strcmp(method, "linked") == 0) {
  894. while (foundStart != foundEnd) {
  895.  
  896. foundStart = memArray[startIndex + 4];
  897.  
  898. for (int i = startIndex; i < startIndex + 5; i++) {
  899. memArray[i] = -1;
  900. if (i % 5 == 0){
  901. memArray[5] = setBitmap(memArray[5], i/5, 1);
  902. }
  903.  
  904. }
  905.  
  906. startIndex = blockToIndex(foundStart);
  907.  
  908. printf("%d", foundStart);
  909. }
  910.  
  911. for (int i = startIndex; i < (foundEnd-foundStart) ; i++) {
  912. memArray[i] = -1;
  913. if (i % 5 == 0){
  914. memArray[5] = setBitmap(memArray[5], i/5, 1);
  915. }
  916. }
  917. }
  918.  
  919.  
  920. // @deleteFile conti
  921. else if (strcmp(method, "contiguous") == 0) {
  922. for (int i = startIndex; i < (foundStart*5) + startIndex; i++){
  923. memArray[i] = -1;
  924. // if multiple of 5, update bitmap
  925. if (i % 5 == 0){
  926. memArray[5] = setBitmap(memArray[5], i/5, 1);
  927. }
  928. }
  929. }
  930.  
  931. // @deleteFile indexed
  932. else if (strcmp(method, "indexed") == 0) {
  933.  
  934. int doneDeleting = 0;
  935. int curIndexBlock = foundStart;
  936.  
  937. printf("%d", curIndexBlock);
  938.  
  939. while (doneDeleting ==0) {
  940.  
  941. int indexEntry = blockToIndex(curIndexBlock);
  942. printf("%d", indexEntry);
  943.  
  944. for ( int i= indexEntry; i< indexEntry +5; i++) {
  945.  
  946. if(memArray[i] == -1) {
  947.  
  948. doneDeleting = 1;
  949. break;
  950. }
  951. else if(memArray[i] > 0) {
  952.  
  953. int blockToDelete = memArray[i];
  954. int blockDeleteIndex = blockToIndex(blockToDelete);
  955.  
  956. for (int j = blockDeleteIndex; j < blockDeleteIndex+5; j++) {
  957. memArray[j]= -1;
  958. // if multiple of 5, update bitmap
  959. if (i % 5 == 0){
  960. memArray[5] = setBitmap(memArray[5], j/5, 1);
  961. }
  962.  
  963. }
  964.  
  965. memArray[i]=-1;
  966. if (i % 5 == 0){
  967. memArray[5] = setBitmap(memArray[5], i/5, 1);
  968. }
  969.  
  970. }
  971. else {
  972. curIndexBlock = memArray[i] * -1;
  973. }
  974.  
  975. memArray[i]=-1;
  976. if (i % 5 == 0){
  977. memArray[5] = setBitmap(memArray[5], i/5, 1);
  978. }
  979.  
  980. }
  981.  
  982.  
  983.  
  984.  
  985.  
  986.  
  987. }
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994.  
  995.  
  996.  
  997. }
  998.  
  999.  
  1000. }
  1001.  
  1002. /*****************************************************************
  1003. /* NOTES / TO-DO / QUESTIONS
  1004. /*****************************************************************
  1005.  
  1006. [Directory data - Block 0]
  1007. When empty: -1
  1008. When not empty:
  1009. Indexed: 543289
  1010. (5432 - file name[1-4 digits], immediately followed by 89 - index location[MUST be 2 digits])
  1011.  
  1012. Linked & Contiguous: 54326789
  1013. (5432 - file name[1-4 digits], immediately followed by 67 - start[Must be 2 digits], then 89 - end[Must be 2 digits])
  1014.  
  1015. [Bitmap - Block 1]
  1016. 1 representing empty space, 0 representing used space
  1017. Read backwards - LSB is block 2
  1018.  
  1019.  
  1020. [Index]
  1021. When empty: -1
  1022. When not empty: pointer to data location
  1023.  
  1024. [File data]
  1025. When empty: -1
  1026. when not empty: a positive number
  1027.  
  1028.  
  1029.  
  1030.  
  1031. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement