Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.71 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stddef.h>
  4.  
  5. #include "point_list.h"
  6.  
  7. #define container_of(ptr, type, member) (type*)((char*)(ptr) - offsetof(type, member))
  8.  
  9. point_node *get_point(intrusive_node *node) {
  10.     return container_of(node, point_node, node);
  11. }
  12.  
  13. int match_point(intrusive_node *node, int x, int y) {
  14.     point_node *pnode = get_point(node);
  15.     return pnode->x == x && pnode->y == y;
  16. }
  17.  
  18. void delete_point(intrusive_list *list, intrusive_node *node) {
  19.     remove_node(list, node);
  20.     free(get_point(node));
  21. }
  22.  
  23. void print_point(intrusive_node *node) {
  24.     point_node *pnode = get_point(node);
  25.     printf("(%d, %d)", pnode->x, pnode->y);
  26. }
  27.  
  28.  
  29. void remove_point(intrusive_list *list, int x, int y) {
  30.     intrusive_node *head = &list->head;
  31.     intrusive_node *node = head->next;
  32.  
  33.     while (node != head) {
  34.     intrusive_node *next = node->next;
  35.  
  36.     if (match_point(node, x, y))
  37.         delete_point(list, node);
  38.  
  39.     node = next;
  40.     }
  41. }
  42.  
  43. void add_point(intrusive_list *list, int x, int y) {
  44.     point_node *pnode = malloc(sizeof(point_node));
  45.     pnode->x = x;
  46.     pnode->y = y;
  47.  
  48.     add_node(list, &pnode->node);
  49. }
  50.  
  51. void show_all_points(intrusive_list *list) {
  52.     intrusive_node *head = &list->head;
  53.     intrusive_node *node = head->next;
  54.  
  55.     printf("%d: ", get_length(list));
  56.  
  57.     for (int i = 0; node != head; i++, node = node->next) {
  58.     if (i) printf(" ");
  59.     print_point(node);
  60.     }
  61.  
  62.     printf("\n");
  63. }
  64.  
  65. void remove_all_points(intrusive_list *list) {
  66.     while (list->head.next != &list->head)
  67.     delete_point(list, list->head.next);
  68. }
  69.  
  70. void print(intrusive_node *node, void *fmt) {
  71.     point_node *pnode = get_point(node);
  72.     printf(fmt, pnode->x, pnode->y);
  73. }
  74.  
  75. void count(intrusive_node *node, void *counter) {
  76.     (void)node;
  77.     (*((int *)counter))++;
  78. }
  79.  
  80. void printtext(intrusive_node *node, void *fout) {
  81.     point_node *pnode = get_point(node);
  82.     fprintf(fout, "%d %d\n", pnode->x, pnode->y);
  83. }
  84.  
  85. void readtext(intrusive_list *list, void *fin) {
  86.     while (1) {
  87.         int x, y;
  88.         if (fscanf(fin, "%d %d", &x, &y) == -1) {
  89.             break;
  90.         }
  91.         fscanf(fin, "\n");
  92.         add_point(list, x, y);
  93.     }
  94. }
  95.  
  96. void printbin(intrusive_node *node, void *fout) {
  97.     point_node *pnode = get_point(node);
  98.     fwrite(&pnode->x, 3, 1, fout);
  99.     fwrite(&pnode->y, 3, 1, fout);
  100. }
  101.  
  102. void readbin(intrusive_list *list, void *fin) {
  103.     char array[3];
  104.     char flag = 1;
  105.     int counter = -1;
  106.     int x = 0;
  107.     int y = 0;
  108.     while (counter++) {
  109.         for (int i = 0; i < 3; i++) {
  110.             if (fread(array + i, sizeof(char), 1, fin) != 1) {
  111.                 flag = 0;
  112.                 break;
  113.             }
  114.         }
  115.         if (!flag) {
  116.             break;
  117.         }
  118.         for (int i = 2; i >= 0; i--) {
  119.             y *= 256;
  120.             y += (int)array[i];
  121.         }
  122.         if (counter % 2) {
  123.             add_point(list, x, y);
  124.             x = 0;
  125.             y = 0;
  126.         }
  127.         else {
  128.             x = y;
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement