Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.47 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <stddef.h>
  5. #include <string.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <sys/mman.h>
  9. #include <sys/types.h>
  10.  
  11. char* trim(char* s, int flag)
  12. {
  13.   int i, len;
  14.   len = strlen(s)+1;
  15.   for (i = 0; i < len; i++)
  16.     {
  17.       if (flag == 1)
  18.         {
  19.           break;
  20.         }
  21.       else if (*s == '*' && *(s+1) == '/' )
  22.         {
  23.           s=s+2;
  24.           printf("S loop: %s\n",s);
  25.           if (*(s+1) == '\0' || *(s+1) == EOF)
  26.             {
  27.               flag = 0;
  28.             }
  29.           else
  30.             {
  31.               flag = 1;
  32.             }
  33.         }
  34.       else
  35.         {
  36.           flag = 0;
  37.           s++;
  38.           if (*s == '\0' || *s == EOF)
  39.             {
  40.               s ="";
  41.               break;
  42.             }
  43.         }
  44.     }
  45.   printf("S: %s\n",s);
  46.   return s;
  47. }
  48.  
  49. int cleaner(char* file)
  50. {
  51.   int fin, fout;
  52.   char* s;
  53.   char* t;
  54.   char* b;
  55.   char* f;
  56.   int flag = 0;
  57.   char *src, *dst;
  58.   struct stat statbuf;
  59.  
  60.   s = malloc(80);
  61.   t = malloc(80);
  62.   f = malloc(80);
  63.  
  64.   // Open the input file
  65.   if ((fin = open (file, O_RDONLY)) < 0)
  66.     fprintf(stderr, "Can't open %s for reading", file);
  67.  
  68.    strcpy(f,file);
  69.  
  70.    // Open/create the output file
  71.    if ((fout = open (strcat(f,".clean"), O_RDWR | O_CREAT |
  72.                      O_TRUNC, 0751)) < 0)
  73.      fprintf(stderr,"Can't create %s for writing\n", f);
  74.  
  75.    //Find size of input file
  76.    if (fstat (fin,&statbuf) < 0)
  77.      fprintf(stderr, "Error with fstat.\n");
  78.  
  79.    //Find the location corresponding to the last byte
  80.    if (lseek (fout, statbuf.st_size - 1, SEEK_SET) == -1)
  81.      fprintf(stderr, "Error with lseek.\n");
  82.  
  83.    // Writing a dummy byte at the last location
  84.    if (write (fout, "", 1) != 1)
  85.      fprintf(stderr, "Write error\n");
  86.    
  87.    // mmap:ning the input file
  88.    if ((src = mmap (0, statbuf.st_size, PROT_READ, MAP_SHARED, fin, 0))
  89.        == (caddr_t) -1)
  90.      fprintf(stderr, "Error with mmap for input file.\n");
  91.  
  92.    // mmap:ing the output file
  93.    if ((dst = mmap (0, statbuf.st_size, PROT_READ | PROT_WRITE,
  94.                     MAP_SHARED, fout, 0)) == (caddr_t) -1)
  95.      fprintf(stderr, "Error with mmap for output\n");
  96.  
  97.    while(sscanf(src, "\n%[^\n]", s) !=EOF )
  98.      {
  99.        //printf("Rivi: %s\n",s);
  100.        strncpy(t, s, 2);
  101.        if (strcmp(t, "/*")!=0 && flag == -1) {
  102.          b = trim(s,flag);
  103.          flag = 0;
  104.          if (strlen(b)==0)
  105.            {
  106.              flag = -1;
  107.            }
  108.          else
  109.            {
  110.              strcat(b,"\n");
  111.              strcat(dst,b);
  112.            }
  113.        }
  114.        else
  115.          {
  116.            if (strcmp(t,"//")==0)
  117.              {
  118.                flag = 0;
  119.              }
  120.            else if (strcmp(t,"/*")==0)
  121.              {
  122.                b = malloc(80);
  123.                flag = 0;
  124.                b = strcpy(b,trim(s+2,flag));
  125.                if (strlen(b)==0)
  126.                  {
  127.                    flag = -1;
  128.                  }
  129.                else
  130.                  {
  131.                    strcat(b,"\n");
  132.                    strcat(dst,b);
  133.                  }
  134.                free(b);
  135.              }
  136.            else
  137.              {
  138.                flag = 0;
  139.                strcat(s,"\n");
  140.                strcat(dst,s);
  141.              }
  142.          }
  143.      }
  144.  
  145.    printf("\nTiedosto2:%s",dst);
  146.    munmap(src,statbuf.st_size);
  147.    munmap(dst,statbuf.st_size);
  148.    
  149.    free(s);
  150.    free(t);
  151.    free(f);
  152.    return 0;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement