Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <stddef.h>
- #include <string.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <sys/mman.h>
- #include <sys/types.h>
- char* trim(char* s, int flag)
- {
- int i, len;
- len = strlen(s)+1;
- for (i = 0; i < len; i++)
- {
- if (flag == 1)
- {
- break;
- }
- else if (*s == '*' && *(s+1) == '/' )
- {
- s=s+2;
- printf("S loop: %s\n",s);
- if (*(s+1) == '\0' || *(s+1) == EOF)
- {
- flag = 0;
- }
- else
- {
- flag = 1;
- }
- }
- else
- {
- flag = 0;
- s++;
- if (*s == '\0' || *s == EOF)
- {
- s ="";
- break;
- }
- }
- }
- printf("S: %s\n",s);
- return s;
- }
- int cleaner(char* file)
- {
- int fin, fout;
- char* s;
- char* t;
- char* b;
- char* f;
- int flag = 0;
- char *src, *dst;
- struct stat statbuf;
- s = malloc(80);
- t = malloc(80);
- f = malloc(80);
- // Open the input file
- if ((fin = open (file, O_RDONLY)) < 0)
- fprintf(stderr, "Can't open %s for reading", file);
- strcpy(f,file);
- // Open/create the output file
- if ((fout = open (strcat(f,".clean"), O_RDWR | O_CREAT |
- O_TRUNC, 0751)) < 0)
- fprintf(stderr,"Can't create %s for writing\n", f);
- //Find size of input file
- if (fstat (fin,&statbuf) < 0)
- fprintf(stderr, "Error with fstat.\n");
- //Find the location corresponding to the last byte
- if (lseek (fout, statbuf.st_size - 1, SEEK_SET) == -1)
- fprintf(stderr, "Error with lseek.\n");
- // Writing a dummy byte at the last location
- if (write (fout, "", 1) != 1)
- fprintf(stderr, "Write error\n");
- // mmap:ning the input file
- if ((src = mmap (0, statbuf.st_size, PROT_READ, MAP_SHARED, fin, 0))
- == (caddr_t) -1)
- fprintf(stderr, "Error with mmap for input file.\n");
- // mmap:ing the output file
- if ((dst = mmap (0, statbuf.st_size, PROT_READ | PROT_WRITE,
- MAP_SHARED, fout, 0)) == (caddr_t) -1)
- fprintf(stderr, "Error with mmap for output\n");
- while(sscanf(src, "\n%[^\n]", s) !=EOF )
- {
- //printf("Rivi: %s\n",s);
- strncpy(t, s, 2);
- if (strcmp(t, "/*")!=0 && flag == -1) {
- b = trim(s,flag);
- flag = 0;
- if (strlen(b)==0)
- {
- flag = -1;
- }
- else
- {
- strcat(b,"\n");
- strcat(dst,b);
- }
- }
- else
- {
- if (strcmp(t,"//")==0)
- {
- flag = 0;
- }
- else if (strcmp(t,"/*")==0)
- {
- b = malloc(80);
- flag = 0;
- b = strcpy(b,trim(s+2,flag));
- if (strlen(b)==0)
- {
- flag = -1;
- }
- else
- {
- strcat(b,"\n");
- strcat(dst,b);
- }
- free(b);
- }
- else
- {
- flag = 0;
- strcat(s,"\n");
- strcat(dst,s);
- }
- }
- }
- printf("\nTiedosto2:%s",dst);
- munmap(src,statbuf.st_size);
- munmap(dst,statbuf.st_size);
- free(s);
- free(t);
- free(f);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement