Advertisement
Guest User

Untitled

a guest
Dec 16th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <memory.h>
  4. #include "recordAttribute.h"
  5. #include "record.h"
  6.  
  7. int countQuery(FILE *,FILE *,Record *);
  8. void sortQuery(FILE *,Record *);
  9. Record *insertQuery(FILE *,Record *,int);
  10. void standardQuery(FILE *,Record *);
  11.  
  12. int main() {
  13.  
  14.  
  15. /*
  16. + This is the code to read in the data, conduct the versioning of multiple records,
  17. + and create a three deminsional linked list cube structure that acts as a list of
  18. + unique records, which have a list of versions with the same docID, which has a list
  19. + of attribute nodes attached to it.
  20. +
  21. + ================================ R E A D I N D A T A =============================
  22. +*/
  23.  
  24. FILE *fp = fopen("data.txt", "r");
  25. FILE *fp3 = fopen("jjcollins2.txt", "w");
  26.  
  27. if(fp3 == NULL) {
  28. fprintf(stderr, "File could not be open for writing.");
  29. exit(1);
  30. }
  31.  
  32. /* Variables for setting ID's and VN's */
  33. int vn = 1, sysID = 1;
  34.  
  35. if(fp == NULL) {
  36. fprintf(stderr, "File could not be open for reading.");
  37. exit(1);
  38. }
  39.  
  40. fprintf(fp3, "GRADER NOTE: THIS IS MY FINAL PROJECT, AND IT DOESNT DO TASK 1 or 2, \n");
  41. fprintf(fp3, "PLEASE DONT GRADE FOR THIS ON YOUR DATA FILE OR IT WILL BREAK IT!!\n\n");
  42. char ch;
  43. Record *head;
  44.  
  45. while(!(feof(fp))) {
  46.  
  47. Record *nuRecord = newRecordWithoutDoc(sysID++, vn);
  48.  
  49. while (!(feof(fp))) {
  50. ch = getc(fp);
  51. if (ch == '\n') {
  52. break;
  53. }
  54. ungetc(ch, fp);
  55.  
  56. char *attributeName = malloc(sizeof(char) * 250);
  57. char attributeValue[250] = {0};
  58. int i1 = 0, i2 = 0;
  59.  
  60. while (!(feof(fp))) {
  61. ch = getc(fp);
  62. if (ch == ':') {
  63. break;
  64. }
  65. attributeName[i1++] = ch;
  66. }
  67.  
  68. while (!(feof(fp))) {
  69. ch = getc(fp);
  70. if (ch == ' ') {
  71. break;
  72. }
  73. if (ch == '\n') {
  74. ungetc(ch, fp);
  75. break;
  76. }
  77. attributeValue[i2++] = ch;
  78. }
  79.  
  80. int attributeNum;
  81. //Converts a char array to its int value
  82. sscanf(attributeValue, "%d", &attributeNum);
  83.  
  84. if (strcmp(attributeName, "DocID") == 0) {
  85. //This is a docID
  86. setDocID(nuRecord, attributeNum);
  87. } else {
  88. //This is an attribute
  89. RecordAttribute *ra = newRecordAtt(attributeName, attributeNum);
  90. insertAttribute(nuRecord, ra);
  91. }
  92.  
  93.  
  94. }
  95.  
  96. if (head == NULL) {
  97. //Need to make head
  98. head = nuRecord;
  99. } else {
  100. //Head already exists
  101. head = insertNextRecord(head, nuRecord, NULL, NULL);
  102. }
  103.  
  104. }
  105.  
  106. fprintf(fp3, "Here is your database based off of your input file! \n\n");
  107. printRecordList(fp3, head);
  108.  
  109.  
  110. fclose(fp);
  111. /*
  112. ================================ E N D R E A D =============================
  113. */
  114.  
  115. /*
  116. ================================ B E G I N Q U E R I E S R E A D ==========
  117. */
  118.  
  119. FILE *fp2 = fopen("queries.txt", "r");
  120.  
  121. if(fp2 == NULL) {
  122. fprintf(stderr, "File could not be open for reading.");
  123. exit(1);
  124. }
  125.  
  126. char x;
  127. while(!feof(fp2)) {
  128. while (!(feof(fp2))) {
  129. x = getc(fp2);
  130. if (x == '.') {
  131. break;
  132. }
  133. }
  134.  
  135. int count;
  136.  
  137. x = getc(fp2);
  138.  
  139. if (x == 'i') {
  140. //Insert Query
  141. head = insertQuery(fp2, head, sysID);
  142. sysID++;
  143. fprintf(fp3, "Here is your new database! \n\n");
  144. printRecordList(fp3, head);
  145. } else if (x == 'c') {
  146. //Count Query
  147. count = countQuery(fp2, fp3, head);
  148. //Get rid of )
  149. x = getc(fp2);
  150. } else if (x == 's') {
  151. //Sort Query
  152. sortQuery(fp2, head);
  153. } else {
  154. //Run Query
  155. standardQuery(fp2, head);
  156. }
  157.  
  158. //Get rid of the /n
  159. x = getc(fp2);
  160. }
  161. /*
  162. ================================ E N D R E A D =============================
  163. */
  164.  
  165. return 0;
  166. }
  167.  
  168. int countQuery(FILE *fp, FILE *fpWrite,Record *head) {
  169.  
  170. int ch;
  171. char *attributeName = malloc(sizeof(char) * 250);
  172. int i = 0;
  173. int count = -1;
  174.  
  175. //Get to the first attribute name
  176. while(!feof(fp)) {
  177. ch = getc(fp);
  178. if(ch == '[') {
  179. break;
  180. }
  181. }
  182.  
  183. //Build attribute name
  184. while(true) {
  185. ch = getc(fp);
  186. if(ch == ']') {
  187. break;
  188. }
  189. attributeName[i++] = ch;
  190. }
  191.  
  192. //Check if end
  193. ch = getc(fp);
  194.  
  195. if(ch == ')') {
  196. count = countWithField(head, attributeName);
  197. fprintf(fpWrite, "\ncount_%s : %i \n", attributeName, count);
  198. return count;
  199. }
  200.  
  201. char versionNumber[50];
  202. int intVN;
  203. int index = 0;
  204.  
  205. while(true) {
  206. ch = getc(fp);
  207. if(ch == '[') {
  208. break;
  209. }
  210. }
  211.  
  212. //See if empty second param
  213. ch = getc(fp);
  214.  
  215. if(ch == ']') {
  216. count = countWithFieldAll(head, attributeName);
  217. fprintf(fpWrite, "\ncount_%s : %i\n", attributeName, count);
  218. return count;
  219. }
  220. else {
  221. versionNumber[index++] = ch;
  222. while (true) {
  223. ch = getc(fp);
  224. if(ch == ']') {
  225. break;
  226. }
  227. versionNumber[index++] = ch;
  228. }
  229. sscanf(versionNumber, "%d", &intVN);
  230. count = countWithFieldVers(head, intVN, attributeName);
  231. fprintf(fpWrite, "\ncount_%s : %i\n", attributeName, count);
  232. return count;
  233. }
  234. }
  235.  
  236. void sortQuery(FILE *fp,Record *head) {
  237.  
  238. }
  239.  
  240. Record *insertQuery(FILE *fp3,Record *head,int sysID) {
  241. //Return head
  242. char ch;
  243. Record *nuRecord = newRecordWithoutDoc(sysID, 1);
  244.  
  245.  
  246. while (true) {
  247. ch = getc(fp3);
  248. if (ch == '(') {
  249. break;
  250. }
  251. }
  252.  
  253. while(true) {
  254. if(ch == ')') {
  255. break;
  256. }
  257. ch = getc(fp3);
  258. if(ch == ')') {
  259. break;
  260. }
  261. ungetc(ch, fp3);
  262. char *attributeName = malloc(sizeof(char) * 250);
  263. char attributeValue[250] = {0};
  264. int i1 = 0, i2 = 0;
  265.  
  266. while (true) {
  267. ch = getc(fp3);
  268. if (ch == ':') {
  269. break;
  270. }
  271. attributeName[i1++] = ch;
  272. }
  273.  
  274. while(true) {
  275. ch = getc(fp3);
  276. if(ch == ' ' || ch == ')') {
  277. break;
  278. }
  279. attributeValue[i2++] = ch;
  280. }
  281.  
  282. int attributeNum;
  283. //Converts a char array to its int value
  284. sscanf(attributeValue, "%d", &attributeNum);
  285.  
  286. if (strcmp(attributeName, "DocID") == 0) {
  287. setDocID(nuRecord, attributeNum);
  288. } else {
  289. RecordAttribute *ra = newRecordAtt(attributeName, attributeNum);
  290. insertAttribute(nuRecord, ra);
  291. }
  292. }
  293.  
  294. if (head == NULL) {
  295. //Need to make head
  296. head = nuRecord;
  297. } else {
  298. //Head already exists
  299. head = .insertNextRecord(head, nuRecord, NULL, NULL);
  300. }
  301.  
  302. return head;
  303. }
  304.  
  305. void standardQuery(FILE *fp,Record *head) {
  306.  
  307. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement