Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. /**
  2. * Mad Mad Access Patterns
  3. * CS 241 - Fall 2019
  4. */
  5. #include "tree.h"
  6. #include "utils.h"
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <sys/mman.h>
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <unistd.h>
  13. #include <fcntl.h>
  14. #include <string.h>
  15.  
  16. /*
  17. Look up a few nodes in the tree and print the info they contain.
  18. This version uses mmap to access the data.
  19.  
  20. ./lookup2 <data_file> <word> [<word> ...]
  21. */
  22. static uint32_t COUNT = 0;
  23. float findPrice(long int offset, char* word);
  24. static char* addr = NULL;
  25.  
  26. int main(int argc, char **argv) {
  27.  
  28. if (argc < 3) { printArgumentUsage(); exit(1); }
  29.  
  30. char* filename = argv[1];
  31. struct stat sb;
  32. int fd = open(argv[1], O_RDONLY);
  33. if (fd == -1) { openFail(filename); exit(2); }
  34.  
  35. fstat(fd, &sb);
  36.  
  37. off_t offset = 0;
  38. size_t length = sb.st_size; //is this correct?
  39.  
  40. off_t page_offset = offset & ~(sysconf(_SC_PAGE_SIZE) - 1);
  41.  
  42. addr = mmap(NULL, length + offset - page_offset, PROT_READ, MAP_PRIVATE, fd, page_offset);
  43.  
  44. if (addr == NULL) { mmapFail(filename); exit(1); }
  45.  
  46.  
  47. char buf[5];
  48. memcpy(buf, addr, 4);
  49. buf[4] = 0;
  50. if (strcmp(buf, "BTRE") != 0) { formatFail(filename); exit(2); }
  51.  
  52. for (int i = 2; i < argc; i++) {
  53. float price = findPrice(4, argv[i]);
  54. if (price == -1.0) {
  55. printNotFound(argv[i]);
  56. } else {
  57. printFound(argv[i], COUNT, price);
  58. }
  59. }
  60.  
  61.  
  62. munmap(addr, length + offset - page_offset);
  63. close(fd);
  64.  
  65.  
  66. return 0;
  67. }
  68.  
  69. float findPrice(long int offset, char* word) {
  70. char nodebuf[sizeof(BinaryTreeNode)];
  71. BinaryTreeNode* node = (BinaryTreeNode*)memcpy(nodebuf, addr + offset, sizeof(BinaryTreeNode));
  72.  
  73. char wordbuf[1024];
  74. strcpy(wordbuf, addr + offset + sizeof(BinaryTreeNode));
  75.  
  76. // printf("current word is: %s\n", wordbuf);
  77.  
  78. if (strcmp(wordbuf, word) == 0) { COUNT = node->count; return node->price; }
  79.  
  80. if (node->left_child) {
  81. char leftbuf[sizeof(BinaryTreeNode)];
  82. memcpy(leftbuf, addr + node->left_child, sizeof(BinaryTreeNode));
  83.  
  84. char leftwordbuf[1024];
  85. strcpy(leftwordbuf, addr + node->left_child + sizeof(BinaryTreeNode));
  86.  
  87. // printf("the left word is: %s\n", leftwordbuf);
  88. // printf("the size of the left word is: %lu\n", sizeof(leftwordbuf));
  89. // printf("other word is: %s\n", word);
  90. // printf("the size fo the other word is: %lu\n", sizeof(word));
  91.  
  92. if (strcmp(leftwordbuf, word) <= 0) { return findPrice(node->left_child, word); }
  93. }
  94.  
  95. if (node->right_child) {
  96. char rightbuf[sizeof(BinaryTreeNode)];
  97. memcpy(rightbuf, addr + node->right_child, sizeof(BinaryTreeNode));
  98.  
  99. char rightwordbuf[1024];
  100. strcpy(rightwordbuf, addr + node->right_child + sizeof(BinaryTreeNode));
  101.  
  102. // printf("the right word is: %s\n", rightwordbuf);
  103.  
  104. if (strcmp(rightwordbuf, word) > 0) { return findPrice(node->right_child, word); }
  105. }
  106.  
  107. return -1;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement