sta-s2z

Untitled

Jul 24th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct RESULT_T {
  5.     long sqsize, sqx, sqy, sqline;
  6.     } result_t;
  7.  
  8. inline long pw(long n, int p) {
  9.   long nn = 1;
  10.   if (p == 0) return 1;
  11.   int c = 0;
  12.   while (c < p) {
  13.     nn *= n;
  14.     c++;
  15.   }
  16.   return nn;
  17. }
  18.  
  19. long filesizecr(char filename[], long *crpos)
  20. {
  21.   char buf[300000];
  22.   unsigned long fs = 0, size = 1;
  23.   *crpos = -1;
  24.   FILE *fp;
  25.   if ((fp = fopen(filename, "rb")) == NULL) return -1;
  26.  
  27.   while (size > 0) {
  28.     size = fread(&buf, 1, sizeof(buf), fp);
  29.     if (*crpos == -1) {
  30.       long c = 0;
  31.       while (c < size) {
  32.         if ((buf[c] == '\n') && (*crpos == -1)) *crpos = (long) fs + c;
  33.         c++;
  34.       }
  35.     }
  36.     fs += size;
  37.   }
  38.   fclose(fp);
  39.   return fs;
  40. }
  41.  
  42. typedef struct SQUARE_FINDER {
  43.   long pos;
  44.   long isSquare;
  45.   long size;
  46. } finder_t;  
  47.  
  48. inline void findLargestSquareHere(long * h, result_t *t, long len) {
  49.   long i;
  50.   finder_t f;
  51.   f.pos = 0;
  52.   while (f.pos < len) {
  53.     f.size = h[f.pos];
  54.     if (f.size > t->sqy) {
  55.       while (f.size > t->sqy) {
  56.           f.isSquare = 1;
  57.           if (f.pos + f.size < len) {
  58.             i = f.pos;
  59.             while ((f.isSquare >= 0) && (i < f.pos + f.size)) {
  60.         f.isSquare = h[i] - f.size;
  61.         i++;
  62.         }
  63.             if ((f.isSquare >= 0) && (f.size > t->sqy)) {
  64.               t->sqx = f.pos;
  65.               t->sqy = f.size;
  66.             }
  67.           }
  68.           f.size--;
  69.       }
  70.     }
  71.     f.pos++;
  72.   }
  73.   t->sqsize = t->sqy * t->sqy;
  74. }
  75.  
  76. int main(int ac, char **av) {
  77.   long i;
  78.   char cobs, cfree, cfill;
  79.   long firstcrpos = 0;
  80.   if (ac <= 1) {
  81.     printf("Usage %s [filename]\n", av[0]);
  82.     exit(1);
  83.   }
  84.   char * fname = av[1];
  85.   long fsize = filesizecr(fname, &firstcrpos);
  86.  
  87.   result_t res;
  88.   res.sqx = 0;
  89.   res.sqy = 0;
  90.   res.sqsize = 0;
  91.   res.sqline = 0;
  92.  
  93.   if (fsize != -1) printf("File size = %lu\n", fsize);
  94.   else {
  95.     printf("Error while opening file!\n");
  96.     exit(0);
  97.   }
  98.   printf("First \\n in file is %ld\n", firstcrpos);
  99.  
  100.   char *fbuf;
  101.   FILE *fp;
  102.   if ((fp = fopen(fname, "rb")) == NULL) {
  103.     printf("Error while opening file!\n");
  104.     exit(0);
  105.   }
  106.  
  107.   printf("Mallocing %ld bytes\n", firstcrpos * sizeof(char) + 1);
  108.   fbuf = (char*) malloc(firstcrpos * sizeof(char) + 1);
  109.   long bytesread = fread(fbuf, 1, firstcrpos * sizeof(char), fp);
  110.   printf("Bytes read: %ld\n", bytesread);
  111.  
  112.   if ((bytesread != firstcrpos * sizeof(char)) || (bytesread < 4)) {
  113.     printf("Error while reading file!\n");
  114.     exit(0);
  115.   }
  116.  
  117.   fbuf[bytesread] = '\0';
  118.   printf("Header read: %s\n", fbuf);
  119.   cfill = fbuf[bytesread - 1];
  120.   cobs = fbuf[bytesread - 2];
  121.   cfree = fbuf[bytesread - 3];
  122.   fbuf[bytesread - 3] = '\0';
  123.   i = bytesread - 4;
  124.   long cpow = 0;
  125.   long clines = 0;
  126.   while (i >= 0) { // atoi
  127.     if (fbuf[i] > '9' || fbuf[i] < '0') {
  128.       printf("Wrong symbols in lines count");
  129.       exit(0);
  130.     }
  131.     clines +=  (fbuf[i] - '0') * pw(10, cpow);
  132.     cpow++;
  133.     i--;
  134.   }
  135.   printf("Fill [%c], free [%c], obstacle[%c], Lines: %ld\n", cfill, cfree, cobs, clines);
  136.  
  137.   long linelen = ((fsize - bytesread) / clines) - 1;
  138.   free(fbuf);
  139.  
  140.   char temp;
  141.   bytesread = fread(&temp, 1, sizeof(char), fp);
  142.   if (temp != '\n') {
  143.     printf("Error while reading file!\n");
  144.     exit(0);
  145.   }
  146.  
  147.   i = 0;
  148.   long * hysto;
  149.   long * lasthysto;
  150.   lasthysto = (long *) malloc(linelen * sizeof(long));
  151.   long j = 0;
  152.   while (j < linelen) {
  153.     lasthysto[j] = 0;
  154.     j++;
  155.   }
  156.   j = 0;
  157.   while (i < clines) {
  158.     fbuf = (char *) malloc(linelen + 1);
  159.     bytesread = fread(fbuf, 1, linelen + 1, fp);
  160.     if ((fbuf[linelen] != '\n') || (bytesread != linelen + 1)) {
  161.       printf("Error while reading file (%x, %x, %ld)!\n", fbuf[linelen], '\n', bytesread);
  162.       exit(0);
  163.     }
  164.     fbuf[linelen] = '\0';
  165.     hysto = (long *) malloc(linelen * sizeof(long));
  166.     j = 0;
  167.     while (j < linelen) {
  168.       if ((fbuf[j] != cobs) && (fbuf[j] != cfree)) {
  169.         printf("Error (%ld, %x)\n", j, fbuf[j]);
  170.         exit(0);
  171.       }
  172.       if (fbuf[j] == cobs) hysto[j] = 0;
  173.       if (fbuf[j] == cfree) hysto[j] = lasthysto[j] + 1;
  174.       lasthysto[j] = hysto[j];
  175.       j++;
  176.     }
  177.     free(fbuf);
  178.     result_t tres;
  179.     tres.sqsize = 0;
  180.     tres.sqx = 0;
  181.     tres.sqy = res.sqy;
  182.     findLargestSquareHere(hysto, &tres, linelen);
  183.     free(hysto);
  184.  
  185.     if (res.sqsize < tres.sqsize) {
  186.       res.sqsize = tres.sqsize;
  187.       res.sqx = tres.sqx;
  188.       res.sqy = tres.sqy;
  189.       res.sqline = i;
  190.     }
  191.     i++;
  192.   }
  193.   free(lasthysto);
  194.   fclose(fp);
  195.  
  196.   if ((fp = fopen(fname, "rb")) == NULL) {
  197.     printf("Error while opening file!\n");
  198.     exit(0);
  199.   }
  200.  
  201.   fbuf = (char*) malloc(firstcrpos * sizeof(char) + 1);
  202.   bytesread = fread(fbuf, 1, firstcrpos * sizeof(char) + 1, fp);
  203.   i = 0;
  204.   while (i < clines) {
  205.     fbuf = (char *) malloc(linelen + 1);
  206.     bytesread = fread(fbuf, 1, linelen + 1, fp);
  207.     if ((fbuf[linelen] != '\n') || (bytesread != linelen + 1)) {
  208.       printf("Error while reading file (%x, %x, %ld)!\n", fbuf[linelen], '\n', bytesread);
  209.       exit(0);
  210.     }
  211.     fbuf[linelen] = '\0';
  212.     j = 0;
  213.     char * linebuf = (char *) malloc (linelen * sizeof(char));
  214.     while (j < linelen) {
  215.       if ((i > res.sqline - res.sqy) && (i <= res.sqline) && (j >= res.sqx) && (j < res.sqx + res.sqy))
  216.         printf("%c", cfill);
  217.       else
  218.         printf("%c", fbuf[j]);
  219.       j++;
  220.     }
  221.     free(fbuf);
  222.     printf("\n");
  223.     i++;
  224.   }
  225.   fclose(fp);
  226. }
Advertisement
Add Comment
Please, Sign In to add comment