Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- void searchfile(char *txt);
- inline void filter(char **line, char *spam, char *legit, int nSpam, int nLegit, int *totspam, int *totlegit);
- inline bool scanword(char **source, char *filter);
- inline void cpsubstr(char *dest, char **src, char c);
- inline void jmpline(char **p);
- int main()
- {
- /*legge tutto il file in una stringa*/
- size_t sizef = 0;
- char *text = 0;
- FILE *file;
- file = fopen("input.txt", "rb");
- fseek(file, 0, SEEK_END);
- sizef = ftell(file);
- rewind(file);
- text = (char*)malloc(sizeof(char) * sizef);
- fread(text, 1, sizef, file);
- fclose(file);
- searchfile(text);
- free(text);
- //getchar();
- return 0;
- }
- /*
- * funzione searchfile
- * *txt pointer a text
- * inizializza tutti i parametri e inizia la ricerca
- */
- void searchfile(char *txt)
- {
- char *line = (char*)malloc(sizeof(char) * 20480); /*crea un buffer di 20kB cioè 1000 parole da 20 caratteri l'una (il limite del correttore)*/
- char *spam = (char*)malloc(sizeof(char) * 20480);
- char *legit = (char*)malloc(sizeof(char) * 20480);
- int nMail = 0;
- int nSpam = 0;
- int nLegit = 0;
- int totalSpam = 0; /* counted mails.. */
- int totalLegit = 0;
- cpsubstr(line, &txt, '\n');
- nSpam = atoi(line);
- cpsubstr(spam, &txt, '\n');
- cpsubstr(line, &txt, '\n');
- nLegit = atoi(line);
- cpsubstr(legit, &txt, '\n');
- cpsubstr(line, &txt, '\n');
- nMail = atoi(line);
- while (nMail-- != 0)
- {
- jmpline(&txt);
- cpsubstr(line, &txt, '\n');
- filter (&line, spam, legit, nSpam, nLegit, &totalSpam, &totalLegit);
- }
- FILE * output;
- output = fopen("output.txt", "w");
- fprintf(output, "%d %d", totalSpam, totalLegit);
- fclose(output);
- //printf("TotalSpam = %d\nTotalLegit = %d\n", totalSpam, totalLegit);
- free(line);
- free(spam);
- free(legit);
- }
- inline void filter(char **line, char *spam, char *legit, int nSpam, int nLegit, int *totspam, int *totlegit)
- {
- char *line_tmp = *line;
- bool isSpam = 0;
- bool isLegit = 0;
- while (nSpam != 0)
- {
- if (*line_tmp == '\0')
- {
- line_tmp = *line; // se il primo carattere della linea è \0 allora resetta line_tmp in line cioè l'inzio della linea
- while (*spam++ != ' ' && *spam != '\0'); // scorri spam fin quando non trovi lo spazio
- nSpam--;
- }
- else if (*line_tmp != *spam)
- {
- while (*line_tmp++ != ' ' && *line_tmp != '\0');
- }
- else
- {
- isSpam = scanword(&line_tmp, spam); //&spam
- if (isSpam)
- break;
- }
- }
- line_tmp = *line;
- while (nLegit != 0)
- {
- if (*line_tmp == '\0')
- {
- line_tmp = *line;
- while (*legit++ != ' ' && *legit != '\0');
- nLegit--;
- }
- else if (*line_tmp != *legit)
- {
- while (*line_tmp++ != ' ' && *line_tmp != '\0');
- }
- else
- {
- isLegit = scanword(&line_tmp, legit);
- if (isLegit)
- break;
- }
- }
- //printf("IS SPAM: %d || IS LEGIT: %d\n", isSpam, isLegit);
- if (isSpam != isLegit)
- {
- if (isSpam)
- *totspam += 1;
- if (isLegit)
- *totlegit += 1;
- }
- }
- inline bool scanword(char **source, char *filter)
- {
- bool code = 1;
- char *src = *source;
- while (1)
- {
- if (*src != ' ' && *filter != ' ' && *src != '\0' && *filter != '\0')
- {
- if (*src++ != *filter++)
- {
- code = 0;
- break;
- }
- }
- else
- break;
- }
- *source = src;
- return code;
- }
- /*
- * funzione cpsubstr
- * *dest è il char di destinazione *
- * **src è il pointer da cui prendere la stringa *
- * char c è il delimiter *
- * returna la prossima riga di una stringa con più newline *
- */
- inline void cpsubstr(char *dest, char **src, char c)
- {
- char *tmp = *src;
- while(*tmp != c && *tmp != '\0')
- *dest++ = *tmp++;
- *dest = '\0'; //perchè se sovrascrive una parola più corta di quella già allocata rimangono dei pezzetti
- *tmp++; //perchè il loop si ferma al newline..
- *src = tmp; //returna il nuovo pointer
- }
- /*
- VERSIONE ALTERNATIVA DELLA FUNZIONE cpsubstr, parametri in più: size_t s_begin size_t s_end
- copia una substringa nella stringa a date coordinate inizio/fine
- avrei potuto usarla insieme a un altra funzione findline, che mi trovava la fine della riga.
- l'ho tolta perchè mi sono accorto che potevo fare in un modo più veloce
- char tmp[(s_end - s_begin)+1] = dest;
- int counter = 0;
- while(s_begin < s_end)
- tmp[counter++] = src[s_begin++];
- tmp[++counter] = '\0';
- return tmp;
- */
- inline void jmpline(char **p)
- {
- char *tmp = *p;
- while (*tmp++ != '\n');
- *p = tmp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement