int main(int argc, char **argv) { int i; Tree tree= TREE_INITIALIZER(Node_compare); mt_random_init(); for (i= 0; i < 100; ++i) { Node v= { mt_random() % 1000, i }; Node *vv= TREE_FIND(&tree, _Node, tree, &v); if (vv) { printf("already inserted "); Node_print(vv, stdout); printf("\n"); } else { printf("insert " ); Node_print(&v, stdout); printf("\n"); TREE_INSERT(&tree, _Node, tree, Node_new(v.key, v.value)); TREE_FORWARD_APPLY(&tree, _Node, tree, Node_printer, stdout); printf("\n"); } } TREE_FORWARD_APPLY(&tree, _Node, tree, Node_printer, stdout); printf("\n"); for (i= 0; i < 1000; ++i) { Node *v= Node_new(mt_random() % 1000, 0); Node *vv= TREE_FIND(&tree, _Node, tree, v); printf("looking for %d - ", v->key); if (vv) { printf("found "); Node_print(vv, stdout); printf("\n"); TREE_FORWARD_APPLY(&tree, _Node, tree, Node_printer, stdout); printf("\n"); } else { printf("not found\n"); } TREE_REMOVE(&tree, _Node, tree, v); } TREE_FORWARD_APPLY(&tree, _Node, tree, Node_printer, stdout); printf("\n"); return 0; }
static int load_history_to_tree(const char *fname) { int n; FILE *f; char s[256]; Node tmp; Node *ptr; char *exclude; exclude = nvram_safe_get("cstats_exclude"); _dprintf("%s: cstats_exclude='%s'\n", __FUNCTION__, exclude); _dprintf("%s: fname=%s\n", __FUNCTION__, fname); unlink(uncomp_fn); n = -1; // Initial value, will be returned if we failed to parse a data file sprintf(s, "gzip -dc %s > %s", fname, uncomp_fn); if (system(s) == 0) { if ((f = fopen(uncomp_fn, "rb")) != NULL) { n = 0; // Initial counter while (fread(&tmp, sizeof(Node), 1, f) > 0) { if ((find_word(exclude, tmp.ipaddr))) { _dprintf("%s: not loading excluded ip '%s'\n", __FUNCTION__, tmp.ipaddr); continue; } if (tmp.id == CURRENT_ID) { _dprintf("%s: found data for ip %s\n", __FUNCTION__, tmp.ipaddr); ptr = TREE_FIND(&tree, _Node, linkage, &tmp); if (ptr) { _dprintf("%s: removing/reloading new data for ip %s\n", __FUNCTION__, ptr->ipaddr); TREE_REMOVE(&tree, _Node, linkage, ptr); free(ptr); ptr = NULL; } TREE_INSERT(&tree, _Node, linkage, Node_new(tmp.ipaddr)); ptr = TREE_FIND(&tree, _Node, linkage, &tmp); memcpy(ptr->daily, &tmp.daily, sizeof(data_t) * MAX_NDAILY); ptr->dailyp = tmp.dailyp; memcpy(ptr->monthly, &tmp.monthly, sizeof(data_t) * MAX_NMONTHLY); ptr->monthlyp = tmp.monthlyp; ptr->utime = tmp.utime; #ifdef SPEED_SUPPORT memcpy(ptr->speed, &tmp.speed, sizeof(uint64_t) * MAX_NSPEED * MAX_COUNTER); ptr->tail = tmp.tail; #endif memcpy(ptr->last, &tmp.last, sizeof(uint64_t) * MAX_COUNTER); // ptr->sync = tmp.sync; ptr->sync = -1; if (ptr->utime > current_uptime) { ptr->utime = current_uptime; ptr->sync = 1; } ++n; } else { _dprintf("%s: data for ip '%s' version %d not loaded (current version is %d)\n", __FUNCTION__, tmp.ipaddr, tmp.id, CURRENT_ID); } } fclose(f); } } else { _dprintf("%s: %s != 0\n", __FUNCTION__, s); } unlink(uncomp_fn); if (n == -1) _dprintf("%s: Failed to parse the data file!\n", __FUNCTION__); else _dprintf("%s: Loaded %d records\n", __FUNCTION__, n); return n; }