Advertisement
Guest User

KR

a guest
May 31st, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.11 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define EOL '\n'
  5.  
  6. typedef struct chat chat;
  7. typedef struct active active;
  8.  
  9. struct chat {
  10.     char *date;
  11.     char *client;
  12.     char *protocol;
  13.     char *user;
  14.     char *message;
  15.     chat *next;
  16. };
  17.  
  18. struct active {
  19.     char *user;
  20.     char *message;
  21.     int messageLength;
  22.     int occur;
  23.     active *next;
  24. };
  25.  
  26. void menuShow(int *choosePoint);
  27. char *readLine(FILE *file);
  28. void cutTime(char *str, size_t cutpos, size_t cutlen);
  29. char *splitLine(FILE *fp, char endC);
  30. void mostActiveUser(chat *ptrHead);
  31. int addNode(chat** current,FILE *fp1);
  32. int fillData(FILE *fp1 , chat **elem,int *ch);
  33. void deleteByUser(chat** head, char* user);
  34. void userMood(chat** head, char* user);
  35. void printList(chat* head);
  36. void string_acquire(char *s,int size);
  37. int main() {
  38.     chat *head = NULL;
  39.     chat *current = NULL;
  40.     int length;
  41.     FILE *fp,*fp1;
  42.     char file_name[12];
  43.     printf("Enter file name:\n");
  44.     string_acquire(file_name,12);
  45.     fp = fopen(file_name,"r"); //TODO: user input file name
  46.     fp1 = fopen("history.db","w+");
  47.     if ((!fp) || (!fp1)) {
  48.         fprintf(stderr,"Error opening file!\n");
  49.         exit(1);
  50.     }
  51.     char *line;
  52.     while(!feof(fp)) {
  53.         line = readLine(fp);
  54.         cutTime(line, 11, 9);
  55.         fprintf(fp1,"%s\n",line);
  56.         printf("LOG: write a line to database: %s\n", line);
  57.         free(line);
  58.     }
  59.     fclose(fp);
  60.    
  61.     rewind(fp1);
  62.     fseek(fp1, 1, SEEK_SET);
  63.     addNode(&head,fp1);
  64.     current = head;
  65.     while(1) {  //filler
  66.         if (!addNode(&current,fp1)) break;
  67.     }
  68.     fclose(fp1);
  69.     //MENU
  70.     int choose = 0;
  71.     char user_name[12];
  72.     menuShow(&choose);
  73.     switch(choose) {
  74.         case 1:
  75.             printf("Enter username: ");
  76.             string_acquire(user_name,12);
  77.             deleteByUser(&head, user_name);
  78.             printf("Users left:\n");
  79.             printList(head);
  80.             break; 
  81.         case 2:
  82.             printf("Enter username: ");
  83.             string_acquire(user_name,12);
  84.             userMood(&head, user_name);
  85.             break;
  86.         case 3:
  87.             mostActiveUser(head);
  88.             break;
  89.         case 4:
  90.             return 0;
  91.         default:
  92.             break;
  93.     }
  94.  
  95.     free(head);
  96.     free(current);
  97.  
  98.     return 0;
  99. }
  100.  
  101. void string_acquire(char *s,int size){
  102.     int i;
  103.     fflush(stdin);
  104.     fgets(s,size,stdin);
  105.     i=strlen(s)-1;
  106.     if(s[i]!='\n') while(getchar()!='\n');
  107.     if(s[i]=='\n') s[i]='\0';
  108. }
  109.  
  110. void menuShow(int *choosePoint) {
  111.     int k = -1;
  112.     do {
  113.         printf("\n1.Deleting all messages from a given user.\n");
  114.         printf("\n2.Find out what is the mood of a given user.\n");
  115.         printf("\n3.Display the most active user(s)\n");
  116.         printf("\n4.EXIT\n");
  117.         printf("Please enter your option: ");
  118.         k = scanf("%d", &(*choosePoint));
  119.     } while ((k != 1) && ((*choosePoint) < 1) && ((*choosePoint) > 4));
  120.  
  121. }
  122.  
  123. void printList(chat* head) {
  124.     while(head) {
  125.         printf("\n");
  126.         printf("%s\n", head->user);
  127.         head = head->next;
  128.     }
  129. }
  130.  
  131. void deleteByUser(chat** head, char* user) {
  132.     chat* temp = *head;
  133.     chat* node = NULL;
  134.     while(temp) {
  135.         if(strcmp(temp->user, user) == 0) {
  136.             if(temp == *head) {
  137.                 *head = (*head)->next;
  138.                 free(temp);
  139.                 temp = *head;
  140.             } else {
  141.                 node->next = temp->next;
  142.                 free(temp);
  143.                 temp = node; //->next;
  144.             }
  145.         }
  146.        
  147.         if(temp == NULL) {
  148.             break;
  149.         }
  150.        
  151.         node = temp;
  152.         temp = temp->next;
  153.     }
  154. }
  155.  
  156. void userMood(chat** head, char* user) {
  157.     const char* message , *lastFind = NULL , *ptr;
  158.     int sad = 0, happy = 0;
  159.     double mood;
  160.     chat* temp = *head;
  161.    
  162.     while(temp) {
  163.         if(strcmp(temp->user, user) == 0) {
  164.             message = temp->message;
  165.  
  166.             for(ptr=message; (lastFind=strstr(ptr, ":)")); ptr=lastFind+1) happy++;        
  167.             for(ptr=message; (lastFind=strstr(ptr, ":D")); ptr=lastFind+1) happy++;        
  168.             for(ptr=message; (lastFind=strstr(ptr, "=]")); ptr=lastFind+1) happy++;        
  169.             for(ptr=message; (lastFind=strstr(ptr, ":P")); ptr=lastFind+1) happy++;            
  170.             for(ptr=message; (lastFind=strstr(ptr, ":(")); ptr=lastFind+1) sad++;      
  171.             for(ptr=message; (lastFind=strstr(ptr, ":'(")); ptr=lastFind+1) sad++;
  172.             for(ptr=message; (lastFind=strstr(ptr, "=(")); ptr=lastFind+1) sad++;
  173.                
  174.         }
  175.         temp = temp->next;
  176.         if(temp == NULL) break;
  177.     }
  178.     mood = happy/sad;
  179.     if (mood > 1.5) printf("%s is happy!\n",user);
  180.     else if (mood < 0.5) printf("%s is sad!\n",user);
  181.     else printf("%s is neutral!\n",user);
  182. }
  183.  
  184. void mostActiveUser(chat *ptrHead) {
  185.    
  186.     chat *row;
  187.     row = ptrHead;
  188.     active *person = NULL, *temp, *prev;
  189.     while(row!=NULL) {
  190.         temp = person;
  191.         while(temp != NULL && strcmp(temp->user,row->user) != 0) {
  192.             prev = temp;
  193.             temp = temp->next;
  194.         }
  195.        
  196.         if(temp == NULL) {
  197.             temp = (active*)malloc(sizeof(active));
  198.             temp->user = row->user;
  199.             temp->message = row->message;
  200.             temp->messageLength = strlen(temp->message);
  201.             temp->occur = 1;
  202.             temp->next = NULL;
  203.             if(person!=NULL) {
  204.                 prev->next=temp;
  205.             } else {
  206.                 person = temp;
  207.             }
  208.         } else {
  209.             temp->message = row->message;
  210.             temp->messageLength += strlen(temp->message);
  211.             temp->occur += 1;
  212.         }
  213.         row = row->next;
  214.     }
  215.    
  216.     active *most_ocurrences = NULL;
  217.     active *most_messages = NULL;
  218.    
  219.     while(person!=NULL) {
  220.         if(most_ocurrences == NULL) {
  221.             most_ocurrences = person;
  222.         } else if(most_ocurrences-> occur < person->occur) {
  223.             most_ocurrences = person;
  224.         }
  225.        
  226.         if(most_messages == NULL) {
  227.             most_messages = person;
  228.         } else if(most_messages->messageLength < person->messageLength) {
  229.             most_messages = person;
  230.         }
  231.         person = person->next;
  232.     }
  233.    
  234.     printf("%s is the user with most total messages posted : %d\n", most_ocurrences->user, most_ocurrences->occur);
  235.     printf("%s is the user with most total symbols written : %d\n", most_ocurrences->user, most_ocurrences->messageLength);
  236.     free(person);
  237. }
  238.  
  239. int addNode(chat** current,FILE *fp1) {
  240.     int check , ch;
  241.     chat *temp = malloc(sizeof(chat));
  242.     check = fillData(fp1, &temp,&ch);
  243.     temp->next = NULL;
  244.     if(*current == NULL) {
  245.         *current = temp;
  246.     } else {
  247.         (*current)->next = temp;
  248.         (*current) = (*current)->next;
  249.     }
  250.    
  251.     if (!check) return 0;
  252.     return 1;
  253. }
  254.  
  255. int fillData(FILE *fp1 , chat **elem,int *ch) {
  256.         (*elem)->date = splitLine(fp1, ']');
  257.         fseek(fp1,2,SEEK_CUR);
  258.         (*elem)->client = splitLine(fp1, '.');     
  259.        
  260.         (*elem)->protocol = splitLine(fp1, ']');
  261.        
  262.         fseek(fp1, 1, SEEK_CUR);
  263.         (*elem)->user = splitLine(fp1, ':');
  264.        
  265.         fseek(fp1, 1, SEEK_CUR);
  266.         (*elem)->message = splitLine(fp1, EOL);
  267.         if (((*ch) = fgetc(fp1)) == EOF) { //proverka za EOF i EOL escaping (+1byte)
  268.             return 0;
  269.         }
  270.         return 1;
  271. }
  272.  
  273. char *readLine(FILE *file) {
  274.  
  275.     if (file == NULL) {
  276.         printf("Error: file pointer is null.");
  277.         exit(1);
  278.     }
  279.  
  280.     int maximumLineLength = 128;
  281.     char *lineBuffer = (char *)malloc(sizeof(char) * maximumLineLength);
  282.  
  283.     if (lineBuffer == NULL) {
  284.         printf("Error allocating memory for line buffer.");
  285.         exit(1);
  286.     }
  287.  
  288.     char ch = getc(file);
  289.     int count = 0;
  290.  
  291.     while ((ch != EOL) && (ch != EOF)) {
  292.         if (count == maximumLineLength) { //refill
  293.             maximumLineLength += 128;
  294.             lineBuffer = realloc(lineBuffer, maximumLineLength);
  295.             if (lineBuffer == NULL) {
  296.                 printf("Error reallocating space for line buffer.");
  297.                 exit(1);
  298.             }
  299.         }
  300.         lineBuffer[count++] = ch;
  301.         ch = getc(file);
  302.     }
  303.    
  304.     lineBuffer[count] = '\0'; //osvobojdavam sled obrabotkata v main funkciqta
  305.     realloc(lineBuffer, count + 1);
  306.     if (lineBuffer == NULL) {
  307.         printf("Error allocating memory for line buffer.");
  308.         exit(1);
  309.     }
  310.     return lineBuffer;
  311. }
  312.  
  313. void cutTime(char *str, size_t cutpos, size_t cutlen) {
  314.   size_t len;
  315.   len = strlen(str);
  316.   memmove( str+cutpos, str+cutpos+cutlen, 1+len - cutpos - cutlen);
  317. }
  318.  
  319. char *splitLine(FILE *fp, char endC) {
  320.     char *string;
  321.     size_t len = 10;
  322.     int cnt = 0;
  323.     char c;
  324.     if(!( string = (char*) malloc(len) )) {
  325.         printf("Error allocating memory for line buffer.");
  326.         exit(1);
  327.     }
  328.  
  329.     while((c = fgetc(fp)) != endC)
  330.     {
  331.         if(c == EOF)
  332.         {
  333.             free(string);
  334.             return NULL;
  335.         }
  336.  
  337.         if(cnt>=len-1)
  338.             string = (char *) realloc(string, (len += 10));
  339.         string[cnt++] = c;
  340.     }
  341.  
  342.     string[cnt] = '\0';
  343.     return string;
  344. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement