LIBSSDP
 All Classes Files Functions Variables Typedefs Macros
ssdp_cache_display.c
Go to the documentation of this file.
1 
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/ioctl.h>
10 
11 #include "common_definitions.h"
12 #include "log.h"
13 #include "ssdp_cache.h"
15 
22 static void get_window_size(int *width, int *height) {
23  struct winsize w;
24  ioctl(0, TIOCGWINSZ, &w);
25 
26  *width = w.ws_col;
27  *height = w.ws_row;
28 }
29 
36 static void move_cursor(int row, int col) {
37  printf("\033[%d;%dH", row, col);
38 }
39 
40 void display_ssdp_cache(ssdp_cache_s *ssdp_cache, BOOL draw_asci) {
41  int horizontal_lines_printed = 4;
42  const char **tbl_ele = NULL;
43 
44  const char *single_line_table_elements[] = {
45  "┤", // 185 "\e(0\x75\e(B"
46  "│", // 186 "\e(0\x78\e(B"
47  "┐", // 187 "\e(0\x6b\e(B"
48  "┘", // 188 "\e(0\x6a\e(B"
49  "└", // 200 "\e(0\x6d\e(B"
50  "┌", // 201 "\e(0\x6c\e(B"
51  "┴", // 202 "\e(0\x76\e(B"
52  "┬", // 203 "\e(0\x77\e(B"
53  "├", // 204 "\e(0\x74\e(B"
54  "─", // 205 "\e(0\x71\e(B"
55  "┼" // 206 "\e(0\x6e\e(B"
56  };
57 
58  const char *asci_table_elements[] = {
59  "+",
60  "|",
61  "+",
62  "+",
63  "+",
64  "+",
65  "+",
66  "+",
67  "+",
68  "-",
69  "+",
70  };
71 
72  const char *columns[] = {
73  " ID ",
74  " IPv4 ",
75  " MAC ",
76  " Model ",
77  " Version "
78  };
79 
80  if (draw_asci) {
81  tbl_ele = asci_table_elements;
82  } else {
83  tbl_ele = single_line_table_elements;
84  }
85 
86  move_cursor(0, 0);
87 
88  /* Draw the topmost line */
89  int i;
90  for (i = 0; i < sizeof(columns) / sizeof(char *); i++) {
91  printf("%s",
92  (i == 0 ? tbl_ele[5] : tbl_ele[7]));
93  int j;
94  for (j = 0; j < strlen(columns[i]); j++) {
95  printf("%s", tbl_ele[9]);
96  }
97  }
98  printf("%s\n", tbl_ele[2]);
99 
100  /* Draw first row with column titles */
101  for (i = 0; i < sizeof(columns) / sizeof(char *); i++) {
102  printf("%s\x1b[1m%s\x1b[0m", tbl_ele[1], columns[i]);
103  }
104  printf("%s\n", tbl_ele[1]);
105 
106  if (ssdp_cache) {
107 
108  /* Draw a row-dividing line */
109  for (i = 0; i < sizeof(columns) / sizeof(char *); i++) {
110  printf("%s", (i == 0 ? tbl_ele[8] : tbl_ele[10]));
111  int j;
112  for (j = 0; j < strlen(columns[i]); j++) {
113  printf("%s", tbl_ele[9]);
114  }
115  }
116  printf("%s\n", tbl_ele[0]);
117 
118  ssdp_custom_field_s *cf = NULL;
119  ssdp_cache = ssdp_cache->first;
120  const char no_info[] = "-";
121 
122  while (ssdp_cache) {
123  cf = get_custom_field(ssdp_cache->ssdp_message, "serialNumber");
124  printf("%s %-20s", tbl_ele[1],
125  (cf && cf->contents ? cf->contents : no_info));
126  printf("%s %-16s", tbl_ele[1], ssdp_cache->ssdp_message->ip);
127  printf("%s %-18s", tbl_ele[1], (ssdp_cache->ssdp_message->mac &&
128  0 != strcmp(ssdp_cache->ssdp_message->mac, "") ?
129  ssdp_cache->ssdp_message->mac : no_info));
130  cf = get_custom_field(ssdp_cache->ssdp_message, "modelName");
131  printf("%s %-*s", tbl_ele[1], 16,
132  (cf && cf->contents ? cf->contents : no_info));
133  cf = get_custom_field(ssdp_cache->ssdp_message, "modelNumber");
134  printf("%s %-16s", tbl_ele[1],
135  (cf && cf->contents ? cf->contents : no_info));
136  printf("%s\n", tbl_ele[1]);
137 
138  horizontal_lines_printed++;
139 
140  ssdp_cache = ssdp_cache->next;
141  }
142  }
143 
144  /* Draw the bottom line */
145  for (i = 0; i < sizeof(columns) / sizeof(char *); i++) {
146  printf("%s",
147  (i == 0 ? tbl_ele[4] : tbl_ele[6]));
148  int j;
149  for (j = 0; j < strlen(columns[i]); j++) {
150  printf("%s", tbl_ele[9]);
151  }
152  }
153  printf("%s\n", tbl_ele[3]);
154  int width, height;
155 
156  get_window_size(&width, &height);
157  for (i = 1; i < height - horizontal_lines_printed; i++) {
158  printf("%*s\n", width, " ");
159  }
160 }
161 
ssdp_custom_field_s * get_custom_field(const ssdp_message_s *ssdp_message, const char *custom_field)
Definition: ssdp_message.c:134
struct ssdp_cache_struct * next
Definition: ssdp_cache.h:27
void display_ssdp_cache(ssdp_cache_s *ssdp_cache, BOOL draw_asci)
static void get_window_size(int *width, int *height)
static void move_cursor(int row, int col)
struct ssdp_cache_struct * first
Definition: ssdp_cache.h:23
ssdp_message_s * ssdp_message
Definition: ssdp_cache.h:25
int BOOL