sta-s2z

Untitled

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