Advertisement
STANAANDREY

cadaster

Oct 19th, 2024
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.54 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.   //puts(s);
  27.   Cadaster cadaster = {};
  28.   char *p = NULL;
  29.  
  30.   p = strtok(s, SEP);//id
  31.   cadaster.id = strtoll(p, NULL, 10);
  32.  
  33.   p = strtok(NULL, SEP);//code
  34.   cadaster.code = strtoll(p, NULL, 10);
  35.  
  36.   p = strtok(NULL, SEP);//county
  37.   strcpy(cadaster.county, p);
  38.  
  39.   p = strtok(NULL, SEP);//uat
  40.   strcpy(cadaster.uat, p);
  41.  
  42.   p = strtok(NULL, SEP);//tp
  43.   if (!strcmp(p, "extravilan")) {
  44.     cadaster.prodType = EXTRA;
  45.   } else {
  46.     cadaster.prodType = INTRA;
  47.   }
  48.  
  49.   p = strtok(NULL, SEP);//nri
  50.   cadaster.nrImmobile = strtoll(p, NULL, 10);
  51.  
  52.   p = strtok(NULL, SEP);//area
  53.   cadaster.area = strtof(p, NULL);
  54.  
  55.   p = strtok(NULL, SEP);//areauat
  56.   cadaster.areaUAT = strtof(p, NULL);
  57.  
  58.   p = strtok(NULL, SEP);//nraps
  59.   cadaster.nrAps = strtof(p, NULL);
  60.  
  61.   return cadaster;
  62. }
  63.  
  64. void printCadaster(const Cadaster *const cadaster) {
  65.   printf("{ %u %u %s %s ", cadaster->id, cadaster->code, cadaster->county, cadaster->uat);
  66.   if (cadaster->prodType == INTRA) {
  67.     printf("intravilan ");
  68.   } else {
  69.     printf("extravilan ");
  70.   }
  71.   printf("%d %g %g %u}\n", cadaster->nrImmobile, cadaster->area, cadaster->areaUAT, cadaster->nrAps);
  72. }
  73.  
  74. Cadaster *populateArr(int *n, FILE *f) {
  75.   static char s[SZ_LINE_MAX + 1];
  76.   Cadaster *arrCad = NULL;
  77.   int index = 0, currSize = 0;
  78.   while (fgets(s, SZ_LINE_MAX, f) != NULL) {
  79.     Cadaster cadaster = procLine(s);
  80.     if (index == currSize) {
  81.       currSize += CHUNK;
  82.       arrCad = (Cadaster*)realloc(arrCad, sizeof(Cadaster) * currSize);
  83.     }
  84.     arrCad[index++] = cadaster;//*/
  85.   }
  86.   *n = index;
  87.   return arrCad;
  88. }
  89.  
  90. int main(int argc, char **argv) {
  91.  
  92.   if (argc < 2) {
  93.     fprintf(stderr, "one more arg needed!\n");
  94.     exit(-1);
  95.   }
  96.  
  97.   FILE *f = fopen(argv[1], "r");
  98.   if (f == NULL) {
  99.     perror("");
  100.     exit(-1);
  101.   }
  102.  
  103.   Cadaster *arrCad = NULL;
  104.   int n;
  105.  
  106.   arrCad = populateArr(&n, f);
  107.  
  108.   for (int i = 0; i < n; i++) {
  109.     printCadaster(arrCad + i);
  110.   }
  111.  
  112.   const int closeRes = fclose(f);
  113.   if (closeRes == EOF) {
  114.     fprintf(stderr, "error while closing!\n");
  115.   }
  116.   free(arrCad);
  117.   return 0;
  118. }
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement