Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define EOL '\n'
- typedef struct chat chat;
- typedef struct active active;
- struct chat {
- char *date;
- char *client;
- char *protocol;
- char *user;
- char *message;
- chat *next;
- };
- struct active {
- char *user;
- char *message;
- int messageLength;
- int occur;
- active *next;
- };
- void menuShow(int *choosePoint);
- char *readLine(FILE *file);
- void cutTime(char *str, size_t cutpos, size_t cutlen);
- char *splitLine(FILE *fp, char endC);
- void mostActiveUser(chat *ptrHead);
- int addNode(chat** current,FILE *fp1);
- int fillData(FILE *fp1 , chat **elem,int *ch);
- void deleteByUser(chat** head, char* user);
- void userMood(chat** head, char* user);
- void printList(chat* head);
- void string_acquire(char *s,int size);
- int main() {
- chat *head = NULL;
- chat *current = NULL;
- int length;
- FILE *fp,*fp1;
- char file_name[12];
- printf("Enter file name:\n");
- string_acquire(file_name,12);
- fp = fopen(file_name,"r"); //TODO: user input file name
- fp1 = fopen("history.db","w+");
- if ((!fp) || (!fp1)) {
- fprintf(stderr,"Error opening file!\n");
- exit(1);
- }
- char *line;
- while(!feof(fp)) {
- line = readLine(fp);
- cutTime(line, 11, 9);
- fprintf(fp1,"%s\n",line);
- printf("LOG: write a line to database: %s\n", line);
- free(line);
- }
- fclose(fp);
- rewind(fp1);
- fseek(fp1, 1, SEEK_SET);
- addNode(&head,fp1);
- current = head;
- while(1) { //filler
- if (!addNode(¤t,fp1)) break;
- }
- fclose(fp1);
- //MENU
- int choose = 0;
- char user_name[12];
- menuShow(&choose);
- switch(choose) {
- case 1:
- printf("Enter username: ");
- string_acquire(user_name,12);
- deleteByUser(&head, user_name);
- printf("Users left:\n");
- printList(head);
- break;
- case 2:
- printf("Enter username: ");
- string_acquire(user_name,12);
- userMood(&head, user_name);
- break;
- case 3:
- mostActiveUser(head);
- break;
- case 4:
- return 0;
- default:
- break;
- }
- free(head);
- free(current);
- return 0;
- }
- void string_acquire(char *s,int size){
- int i;
- fflush(stdin);
- fgets(s,size,stdin);
- i=strlen(s)-1;
- if(s[i]!='\n') while(getchar()!='\n');
- if(s[i]=='\n') s[i]='\0';
- }
- void menuShow(int *choosePoint) {
- int k = -1;
- do {
- printf("\n1.Deleting all messages from a given user.\n");
- printf("\n2.Find out what is the mood of a given user.\n");
- printf("\n3.Display the most active user(s)\n");
- printf("\n4.EXIT\n");
- printf("Please enter your option: ");
- k = scanf("%d", &(*choosePoint));
- } while ((k != 1) && ((*choosePoint) < 1) && ((*choosePoint) > 4));
- }
- void printList(chat* head) {
- while(head) {
- printf("\n");
- printf("%s\n", head->user);
- head = head->next;
- }
- }
- void deleteByUser(chat** head, char* user) {
- chat* temp = *head;
- chat* node = NULL;
- while(temp) {
- if(strcmp(temp->user, user) == 0) {
- if(temp == *head) {
- *head = (*head)->next;
- free(temp);
- temp = *head;
- } else {
- node->next = temp->next;
- free(temp);
- temp = node; //->next;
- }
- }
- if(temp == NULL) {
- break;
- }
- node = temp;
- temp = temp->next;
- }
- }
- void userMood(chat** head, char* user) {
- const char* message , *lastFind = NULL , *ptr;
- int sad = 0, happy = 0;
- double mood;
- chat* temp = *head;
- while(temp) {
- if(strcmp(temp->user, user) == 0) {
- message = temp->message;
- for(ptr=message; (lastFind=strstr(ptr, ":)")); ptr=lastFind+1) happy++;
- for(ptr=message; (lastFind=strstr(ptr, ":D")); ptr=lastFind+1) happy++;
- for(ptr=message; (lastFind=strstr(ptr, "=]")); ptr=lastFind+1) happy++;
- for(ptr=message; (lastFind=strstr(ptr, ":P")); ptr=lastFind+1) happy++;
- for(ptr=message; (lastFind=strstr(ptr, ":(")); ptr=lastFind+1) sad++;
- for(ptr=message; (lastFind=strstr(ptr, ":'(")); ptr=lastFind+1) sad++;
- for(ptr=message; (lastFind=strstr(ptr, "=(")); ptr=lastFind+1) sad++;
- }
- temp = temp->next;
- if(temp == NULL) break;
- }
- mood = happy/sad;
- if (mood > 1.5) printf("%s is happy!\n",user);
- else if (mood < 0.5) printf("%s is sad!\n",user);
- else printf("%s is neutral!\n",user);
- }
- void mostActiveUser(chat *ptrHead) {
- chat *row;
- row = ptrHead;
- active *person = NULL, *temp, *prev;
- while(row!=NULL) {
- temp = person;
- while(temp != NULL && strcmp(temp->user,row->user) != 0) {
- prev = temp;
- temp = temp->next;
- }
- if(temp == NULL) {
- temp = (active*)malloc(sizeof(active));
- temp->user = row->user;
- temp->message = row->message;
- temp->messageLength = strlen(temp->message);
- temp->occur = 1;
- temp->next = NULL;
- if(person!=NULL) {
- prev->next=temp;
- } else {
- person = temp;
- }
- } else {
- temp->message = row->message;
- temp->messageLength += strlen(temp->message);
- temp->occur += 1;
- }
- row = row->next;
- }
- active *most_ocurrences = NULL;
- active *most_messages = NULL;
- while(person!=NULL) {
- if(most_ocurrences == NULL) {
- most_ocurrences = person;
- } else if(most_ocurrences-> occur < person->occur) {
- most_ocurrences = person;
- }
- if(most_messages == NULL) {
- most_messages = person;
- } else if(most_messages->messageLength < person->messageLength) {
- most_messages = person;
- }
- person = person->next;
- }
- printf("%s is the user with most total messages posted : %d\n", most_ocurrences->user, most_ocurrences->occur);
- printf("%s is the user with most total symbols written : %d\n", most_ocurrences->user, most_ocurrences->messageLength);
- free(person);
- }
- int addNode(chat** current,FILE *fp1) {
- int check , ch;
- chat *temp = malloc(sizeof(chat));
- check = fillData(fp1, &temp,&ch);
- temp->next = NULL;
- if(*current == NULL) {
- *current = temp;
- } else {
- (*current)->next = temp;
- (*current) = (*current)->next;
- }
- if (!check) return 0;
- return 1;
- }
- int fillData(FILE *fp1 , chat **elem,int *ch) {
- (*elem)->date = splitLine(fp1, ']');
- fseek(fp1,2,SEEK_CUR);
- (*elem)->client = splitLine(fp1, '.');
- (*elem)->protocol = splitLine(fp1, ']');
- fseek(fp1, 1, SEEK_CUR);
- (*elem)->user = splitLine(fp1, ':');
- fseek(fp1, 1, SEEK_CUR);
- (*elem)->message = splitLine(fp1, EOL);
- if (((*ch) = fgetc(fp1)) == EOF) { //proverka za EOF i EOL escaping (+1byte)
- return 0;
- }
- return 1;
- }
- char *readLine(FILE *file) {
- if (file == NULL) {
- printf("Error: file pointer is null.");
- exit(1);
- }
- int maximumLineLength = 128;
- char *lineBuffer = (char *)malloc(sizeof(char) * maximumLineLength);
- if (lineBuffer == NULL) {
- printf("Error allocating memory for line buffer.");
- exit(1);
- }
- char ch = getc(file);
- int count = 0;
- while ((ch != EOL) && (ch != EOF)) {
- if (count == maximumLineLength) { //refill
- maximumLineLength += 128;
- lineBuffer = realloc(lineBuffer, maximumLineLength);
- if (lineBuffer == NULL) {
- printf("Error reallocating space for line buffer.");
- exit(1);
- }
- }
- lineBuffer[count++] = ch;
- ch = getc(file);
- }
- lineBuffer[count] = '\0'; //osvobojdavam sled obrabotkata v main funkciqta
- realloc(lineBuffer, count + 1);
- if (lineBuffer == NULL) {
- printf("Error allocating memory for line buffer.");
- exit(1);
- }
- return lineBuffer;
- }
- void cutTime(char *str, size_t cutpos, size_t cutlen) {
- size_t len;
- len = strlen(str);
- memmove( str+cutpos, str+cutpos+cutlen, 1+len - cutpos - cutlen);
- }
- char *splitLine(FILE *fp, char endC) {
- char *string;
- size_t len = 10;
- int cnt = 0;
- char c;
- if(!( string = (char*) malloc(len) )) {
- printf("Error allocating memory for line buffer.");
- exit(1);
- }
- while((c = fgetc(fp)) != endC)
- {
- if(c == EOF)
- {
- free(string);
- return NULL;
- }
- if(cnt>=len-1)
- string = (char *) realloc(string, (len += 10));
- string[cnt++] = c;
- }
- string[cnt] = '\0';
- return string;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement