Advertisement
STANAANDREY

cadasters

Dec 22nd, 2022 (edited)
655
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #define SZ_JUD_MAX 30
  6. #define SZ_LINE_MAX 300
  7. #define SIZE_UAT 50
  8. #define CHUNK 128
  9. #define SEP ","
  10.  
  11. typedef enum {
  12.   INTRA, EXTRA
  13. } ProdType;
  14.  
  15. typedef struct {
  16.   uint32_t id;
  17.   uint32_t code;
  18.   char county[SZ_JUD_MAX + 3], uat[SIZE_UAT + 1];
  19.   ProdType prodType;
  20.   uint32_t nrImmobile;
  21.   float area, areaUAT;
  22.   uint32_t nrAps;
  23. } Cadaster;
  24.  
  25. Cadaster procLine(char s[]) {
  26.   Cadaster cadaster = {};
  27.   char *p = NULL;
  28.  
  29.   p = strtok(s, SEP);//id
  30.   cadaster.id = strtoll(p, NULL, 10);
  31.  
  32.   p = strtok(NULL, SEP);//code
  33.   cadaster.code = strtoll(p, NULL, 10);
  34.  
  35.   p = strtok(NULL, SEP);//county
  36.   strcpy(cadaster.county, p);
  37.  
  38.   p = strtok(NULL, SEP);//uat
  39.   strcpy(cadaster.uat, p);
  40.  
  41.   p = strtok(NULL, SEP);//type
  42.   if (!strcmp(p, "extravilan")) {
  43.     cadaster.prodType = EXTRA;
  44.   } else {
  45.     cadaster.prodType = INTRA;
  46.   }
  47.  
  48.   p = strtok(NULL, SEP);//nri
  49.   cadaster.nrImmobile = strtoll(p, NULL, 10);
  50.  
  51.   p = strtok(NULL, SEP);//area
  52.   cadaster.area = strtof(p, NULL);
  53.  
  54.   p = strtok(NULL, SEP);//areauat
  55.   cadaster.areaUAT = strtof(p, NULL);
  56.  
  57.   p = strtok(NULL, SEP);//nraps
  58.   cadaster.nrAps = strtof(p, NULL);
  59.  
  60.   return cadaster;
  61. }
  62.  
  63. void printCadaster(const Cadaster *const cadaster) {
  64.   printf("{ %u %u %s %s ", cadaster->id, cadaster->code, cadaster->county, cadaster->uat);
  65.   if (cadaster->prodType == INTRA) {
  66.     printf("intravilan ");
  67.   } else {
  68.     printf("extravilan ");
  69.   }
  70.   printf("%d %g %g %u}\n", cadaster->nrImmobile, cadaster->area, cadaster->areaUAT, cadaster->nrAps);
  71. }
  72.  
  73. Cadaster *populateArr(int *n, FILE *f) {
  74.  
  75.   for (int ch; (ch = fgetc(f)) != EOF && ch != '\n';); //ignore first row
  76.  
  77.   static char s[SZ_LINE_MAX + 1];
  78.   Cadaster *arrCad = NULL;
  79.   int index = 0, currSize = 0;
  80.   while (fgets(s, SZ_LINE_MAX, f) != NULL) {
  81.     Cadaster cadaster = procLine(s);
  82.     if (index == currSize) {
  83.       currSize += CHUNK;
  84.       arrCad = (Cadaster*)realloc(arrCad, sizeof(Cadaster) * currSize);
  85.       if (arrCad == NULL) {
  86.     fprintf(stderr, "error at realloc!\n");
  87.     exit(-1);
  88.       }
  89.     }
  90.     arrCad[index++] = cadaster;//*/
  91.   }
  92.   *n = index;
  93.   arrCad = (Cadaster*)realloc(arrCad, sizeof(Cadaster) * *n);
  94.   if (arrCad == NULL) {
  95.     fprintf(stderr, "error at realloc!\n");
  96.     exit(-1);
  97.   }
  98.   return arrCad;
  99. }
  100.  
  101. int main(int argc, char **argv) {
  102.  
  103.   if (argc < 2) {
  104.     fprintf(stderr, "one more arg needed!\n");
  105.     exit(-1);
  106.   }
  107.  
  108.   FILE *f = fopen(argv[1], "r");
  109.   if (f == NULL) {
  110.     perror("");
  111.     exit(-1);
  112.   }
  113.  
  114.   Cadaster *arrCad = NULL;
  115.   int n;
  116.  
  117.   arrCad = populateArr(&n, f);
  118.   for (int i = 0; i < n; i++) {
  119.     printCadaster(arrCad + i);
  120.   }
  121.  
  122.   const int closeRes = fclose(f);
  123.   if (closeRes == EOF) {
  124.     fprintf(stderr, "error while closing!\n");
  125.   }
  126.   free(arrCad);
  127.   return 0;
  128. }
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement