#include #include #include #include #include "tree.h" #define DELIMITERS " \t\n,.;:§!#%&/()=?@_£${[]}\\+-'*^\"0123456789<>|" #define ARRSIZE 10 FILE *outindex = NULL; int lnumber = 1; int page = 1; typedef struct post{ char *word; int *pages; int pos; } Post; char* strtoupper(char *ch){ int i; for (i=0; ch[i]; ++i) ch[i] = toupper(ch[i]); return ch; } int postcomp(Post *element, Post *elementintree){ return strcmp(strtoupper(element->word), strtoupper(elementintree->word)); } void func(Post *ret){ fprintf(outindex, "\n%s\n", ret->word); int i; for(i = 0;i < ret->pos + 1; i++) fprintf(outindex, "%d ", *(ret->pages + i)); fprintf(outindex, "\n"); } int addpage(Post *post, int page){ if ((post->pos + 1) % ARRSIZE == 0 && *(post->pages + post->pos) != page){ if(post->pages = realloc(post->pages, sizeof(int) * (post->pos + ARRSIZE))){ ++post->pos; *(post->pages + post->pos) = page; return 1; } return 0; } else if (*(post->pages + post->pos) != page){ ++post->pos; *(post->pages + post->pos) = page; return 1; } return 1; } void maketree(Binsearchtree bst, char *inname, int lpp){ char *words; char line[BUFSIZ]; Post* new, *tmp; FILE *in; if (!(in = fopen(inname, "r"))){ fprintf(stderr, "File does not exist: %s!\n", inname); exit(EXIT_FAILURE); } while (fgets(line, BUFSIZ, in)){ words = strtok(line, DELIMITERS); while(words){ new = malloc(sizeof(Post)); new->word = malloc((strlen(words)+1) * sizeof(char)); strcpy(new->word, words); new->pages = malloc(ARRSIZE * sizeof(int)); *new->pages = page; new->pos = 0; if ((tmp = find(bst, new))){ free(new->word); free(new->pages); free(new); addpage(tmp, page); } else inserttree(bst, new); words = strtok(NULL, DELIMITERS); } if (lnumber == lpp){ lnumber = 0; page++; } lnumber++; } fclose(in); } void fileconcat(char *inname, char* outname, int docopy){ if (docopy){ FILE *in; if (!(in = fopen(inname, "r"))){ fprintf(stderr, "File does not exist: %s!\n", inname); exit(EXIT_FAILURE); } FILE *out = outname?fopen(outname, "a"):stdout; char line[BUFSIZ]; while (fgets(line, BUFSIZ, in)) fprintf(out,"%s", line); fclose(in); if (out != stdout) fclose(out); } } int main(int argc,char **argv){ setlocale(LC_CTYPE, "sv_SE"); Binsearchtree bst = inittree((int(*)(void*, void*))postcomp); char *filename = NULL; char fileargs[BUFSIZ]; char *tmp = NULL, *tmpargs = NULL, *farg = NULL; int ch = 0, buffsize = BUFSIZ; int docopy = 0, lpp = 60; int flag = 0; const char* delimit = " \t\n,;:§!#%&/()=?@_£${[]}\\+-'*^\"0123456789<>|"; while(*++argv){ if (**argv == '-'){ ++*argv; if (**argv == 'a') docopy = 1; else if (**argv == 'p') lpp = strtol(++*argv, NULL, 10); else if (**argv == 'o'){ filename = ++*argv; fclose(fopen(filename, "w+")); } else fprintf(stderr, "Unknown expression: %s\n", *argv); } else{ fileconcat(*argv, filename, docopy); maketree(bst, *argv, lpp); flag = 1; } } if (!flag){ while (ch != EOF){ tmpargs = fileargs; while ((ch = getchar()) != EOF) *tmpargs++ = ch; fgets(fileargs, BUFSIZ - 1, stdin); farg = strtok(fileargs, delimit); while(farg){ fileconcat(farg, filename, docopy); maketree(bst, farg, lpp); tmp = farg; while (*tmp++ != '\0'); farg = strtok(tmp, delimit); } } } outindex = filename?fopen(filename, "a"):stdout; doforall(bst,(void(*)(void*))func); if (outindex != stdout) fclose(outindex); return 0; }