gw_stat_linked_list.c 1.95 KB
Newer Older
Vladislav Rykov's avatar
Vladislav Rykov committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "gw_stat_linked_list.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#define APP_KEY_SIZE 	8

typedef struct _gw_stat_linked_list gw_stat_linked_list_t;

typedef struct _gw_stat_linked_list {
	char app_key[APP_KEY_SIZE+1];
	uint8_t dev_id;
	uint64_t num_msgs;
	struct _gw_stat_linked_list *next;
} _gw_stat_linked_list;

static gw_stat_linked_list_t *root;
static uint64_t size;

void gw_stat_linked_list_init(void) {
	root = NULL;
	size = 0;
}

uint8_t gw_stat_linked_list_add(const char *app_key, const uint8_t dev_id) {
	gw_stat_linked_list_t *ptr = root;
	uint8_t found = 0, ret = 1;

	while (ptr && !found) {
		if (!memcmp(ptr->app_key, app_key, APP_KEY_SIZE) && ptr->dev_id == dev_id) {
			found = 1;
		} else {
			ptr = ptr->next;
		}
	}
	
	if (ptr) {
		ptr->num_msgs++;
	} else {
		ptr = (gw_stat_linked_list_t *)malloc(sizeof(gw_stat_linked_list_t));
		if (ptr) {
			memcpy(ptr->app_key, app_key, APP_KEY_SIZE);
			ptr->app_key[APP_KEY_SIZE] = '\0';
			ptr->dev_id = dev_id;
			ptr->num_msgs = 1;
			ptr->next = root;
			root = ptr;

			size++;
		} else {
			ret = 0;
		}
	}

	return ret;
}

void gw_stat_linked_list_flush(char *store, uint8_t file) {
	gw_stat_linked_list_t *ptr = root, *tmp;
	char buf[64];
	FILE *fp;
	
	if (ptr && size) {
		if (file) { // file output
			fp = fopen(store, "w");
			
			while (ptr) {
				snprintf(buf, 64, "%s#%d#%lld|", ptr->app_key, ptr->dev_id, ptr->num_msgs);
				fwrite(buf, strlen(buf), 1, fp);
				
				tmp = ptr;
				ptr = ptr->next;
				free(tmp);
			}

			fclose(fp);
		} else { // str output
			store[0] = '\0';

			while (ptr) {
				snprintf(buf, 64, "%s#%d#%lld|", ptr->app_key, ptr->dev_id, ptr->num_msgs);
				strcat(store, buf);
				
				tmp = ptr;
				ptr = ptr->next;
				free(tmp);
			}
		}
		size = 0;
		root = NULL;
	}
}

void gw_stat_linked_list_destroy(void) {
	gw_stat_linked_list_t *ptr = root, *tmp;
	
	while (ptr && size) {
		tmp = ptr;
		ptr = ptr->next;
		free(tmp);
		
		size--;
	}
	root = NULL;
}