sta-s2z

Untitled

Jul 23rd, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.39 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. 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. void findLargestSquareHere(long * h, result_t *t, long len, long minsize) {
  43.   long maxs = 0;
  44.   long maxp = 0, maxh = 0, maxl = 0;
  45.   long pos = 0;
  46.  
  47.   while (pos < len) {
  48.     long tosize = h[pos];
  49.     long size = tosize;
  50.     if (tosize >= minsize) {
  51.       while (size > 0) {
  52.         if (size > minsize) {
  53.           int isSquare = 1;
  54.           if (pos + size <= len) {
  55.             long i = pos;
  56.             while (i < pos + size) {
  57.               if (h[i] < size) {
  58.                 isSquare = 0;
  59.                 i = len;
  60.               } else i++;
  61.             }
  62.             if ((isSquare == 1) && (size * size > maxs)) {
  63.               maxs =  size * size;
  64.               maxp = pos;
  65.               maxl = size;
  66.               maxh = size;
  67.               size = 0;
  68.             }
  69.           }
  70.           size--;
  71.         } else size = 0;
  72.       }
  73.     }
  74.     pos++;
  75.   }
  76.   t->sqsize = maxs;
  77.   t->sqx = maxp;
  78.   t->sqy = maxh;
  79. }
  80.  
  81. void main(void) {
  82.   long i;
  83.   char cobs, cfree, cfill;
  84.   long firstcrpos = 0;
  85.   char fname[] = "testmap.bsq";
  86.   long fsize = filesizecr(fname, &firstcrpos);
  87.  
  88.   result_t res;
  89.   res.sqx = 0;
  90.   res.sqy = 0;
  91.   res.sqsize = 0;
  92.   res.sqline = 0;
  93.  
  94.   if (fsize != -1) printf("File size = %lu\n", fsize);
  95.   else {
  96.     printf("Error while opening file!\n");
  97.     exit(0);
  98.   }
  99.   printf("First \\n in file is %ld\n", firstcrpos);
  100.  
  101.   char *fbuf;
  102.   FILE *fp;
  103.   if ((fp = fopen(fname, "rb")) == NULL) {
  104.     printf("Error while opening file!\n");
  105.     exit(0);
  106.   }
  107.  
  108.   printf("Mallocing %ld bytes\n", firstcrpos * sizeof(char) + 1);
  109.   fbuf = (char*) malloc(firstcrpos * sizeof(char) + 1);
  110.   long bytesread = fread(fbuf, 1, firstcrpos * sizeof(char), fp);
  111.   printf("Bytes read: %ld\n", bytesread);
  112.  
  113.   if ((bytesread != firstcrpos * sizeof(char)) || (bytesread < 4)) {
  114.     printf("Error while reading file!\n");
  115.     exit(0);
  116.   }
  117.  
  118.   fbuf[bytesread] = '\0';
  119.   printf("Header read: %s\n", fbuf);
  120.   cfill = fbuf[bytesread - 1];
  121.   cobs = fbuf[bytesread - 2];
  122.   cfree = fbuf[bytesread - 3];
  123.   fbuf[bytesread - 3] = '\0';
  124.   i = bytesread - 4;
  125.   long cpow = 0;
  126.   long clines = 0;
  127.   char temp;
  128.   fread(&temp, 1, sizeof(char), fp);
  129.   if (temp != '\n') {
  130.     printf("Error while reading file!\n");
  131.     exit(0);
  132.   }
  133.  
  134.   while (i >= 0) { // atoi
  135.     if (fbuf[i] > '9' || fbuf[i] < '0') {
  136.       printf("Wrong symbols in lines count");
  137.       exit(0);
  138.     }
  139.     clines +=  (fbuf[i] - '0') * pw(10, cpow);
  140.     cpow++;
  141.     i--;
  142.   }
  143.   printf("Fill [%c], free [%c], obstacle[%c], Lines: %ld\n", cfill, cfree, cobs, clines);
  144.  
  145.   long linelen = ((fsize - bytesread) / clines) - 1;
  146.   free(fbuf);
  147.  
  148.   i = 0;
  149.   long * hysto;
  150.   long * lasthysto;
  151.   lasthysto = malloc(linelen * sizeof(long));
  152.   long j = 0;
  153.   while (j < linelen) {
  154.     lasthysto[j] = 0;
  155.     j++;
  156.   }
  157.   j = 0;
  158.   while (i < clines) {
  159.     fbuf = malloc(linelen + 1);
  160.     bytesread = fread(fbuf, 1, linelen + 1, fp);
  161.     if ((fbuf[linelen] != '\n') || (bytesread != linelen + 1)) {
  162.       printf("Error while reading file (%x, %x, %ld)!\n", fbuf[linelen], '\n', bytesread);
  163.       exit(0);
  164.     }
  165.     fbuf[linelen] = '\0';
  166.     hysto = malloc(linelen * sizeof(long));
  167.     j = 0;
  168.     while (j < linelen) {
  169.       if ((fbuf[j] != cobs) && (fbuf[j] != cfree)) {
  170.         printf("Error (%ld, %x)\n", j, fbuf[j]);
  171.         exit(0);
  172.       }
  173.       if (fbuf[j] == cobs) hysto[j] = 0;
  174.       if (fbuf[j] == cfree) hysto[j] = lasthysto[j] + 1;
  175.       lasthysto[j] = hysto[j];
  176.       j++;
  177.     }
  178.     result_t tres;
  179.     tres.sqsize = 0;
  180.     tres.sqx = 0;
  181.     tres.sqy = 0;
  182.     findLargestSquareHere(hysto, &tres, linelen, res.sqy);
  183.  
  184.     if (res.sqsize < tres.sqsize) {
  185.       res.sqsize = tres.sqsize;
  186.       res.sqx = tres.sqx;
  187.       res.sqy = tres.sqy;
  188.       res.sqline = i;
  189.     }
  190.     free(hysto);
  191.     free(fbuf);
  192.     i++;
  193.   }
  194.   free(lasthysto);
  195.   fclose(fp);
  196.  
  197.   if ((fp = fopen(fname, "rb")) == NULL) {
  198.     printf("Error while opening file!\n");
  199.     exit(0);
  200.   }
  201.  
  202.   fbuf = (char*) malloc(firstcrpos * sizeof(char) + 1);
  203.   bytesread = fread(fbuf, 1, firstcrpos * sizeof(char) + 1, fp);
  204.   i = 0;
  205.   while (i < clines) {
  206.     fbuf = malloc(linelen + 1);
  207.     bytesread = fread(fbuf, 1, linelen + 1, fp);
  208.     if ((fbuf[linelen] != '\n') || (bytesread != linelen + 1)) {
  209.       printf("Error while reading file (%x, %x, %ld)!\n", fbuf[linelen], '\n', bytesread);
  210.       exit(0);
  211.     }
  212.     fbuf[linelen] = '\0';
  213.     j = 0;
  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 printf("%c", fbuf[j]);
  218.       j++;
  219.     }
  220.     free(fbuf);
  221.     printf("\n");
  222.     i++;
  223.   }
  224.   fclose(fp);
  225. }
Advertisement
Add Comment
Please, Sign In to add comment