int main() { struct list l = { NULL }; struct record *rec, *ref; l.head = NULL; printf("\n testing rec_alloc..."); rec = rec_alloc(101,"Abi",70); if(rec == NULL) goto fail; else printf("[ok]"); printf("\n testing rec_add [while adding first record]..."); rec_add(&l, rec); if(l.head != rec) goto fail; else printf("[ok]"); printf("\n adding several records..."); rec = rec_alloc(102,"Anu",80); rec_add(&l, rec); ref = rec_alloc(103,"Balu",85); rec_add(&l, ref); rec = rec_alloc(104,"Chandran",90); rec_add(&l, rec); printf("[ok]"); printf("\n testing rec_insert_before with next pointer..."); rec = rec_alloc(105,"kiti",95); rec_insert_before(&l, ref, rec); if( rec->next != ref ) goto fail; printf("[ok]"); printf("\n testing rec_insert_before with rec_find..."); rec = rec_find(&l, 105); if( rec->next != ref ) goto fail; printf("[ok]\n"); return 0; fail: printf("[failed]\n"); return 0; }
bool BST::rec_add(Node * &in_root, int &in_data){ if(in_root == NULL){ in_root = new Node(in_data, NULL, NULL); return true; } else if(in_root->getData() == in_data){ return false; } else if(in_data < in_root->getData()){ return rec_add(in_root->left, in_data); } else if(in_data > in_root->getData()){ return rec_add(in_root->right, in_data); } return false; }
void expire_grp() { int pos, fd; char fgrp[80], fold[80], fnew[80]; SLOT grp; pos = 0; sprintf(fgrp, "2nd/%s", FN_GRP); sprintf(fnew, "2nd/%s.new", FN_GRP); fd = open(fnew, O_CREAT | O_TRUNC, 0600); // touch a new file if(fd > 0) close(fd); while(rec_get(fgrp, &grp, sizeof(SLOT), pos) != -1) { if(grp.prop & PROP_G_CANCEL) { sprintf(fold, "2nd/%s", grp.fn); f_rm(fold); } else { rec_add(fnew, &grp, sizeof(SLOT)); } pos++; } sprintf(fold, "2nd/%s.old", FN_GRP); f_cp(fgrp, fold, O_TRUNC); f_mv(fnew, fgrp); return; }
static void new_class() { HDR hdr; memset(&hdr, 0, sizeof(HDR)); time(&hdr.chrono); strcpy(hdr.owner, STR_SYSOP); strcpy(hdr.nick, SYSOPNICK); str_stamp(hdr.date, &hdr.chrono); strcpy(hdr.xname, "@Class"); strcpy(hdr.title, "Class/ 看板精華區"); hdr.xmode = GEM_FOLDER; rec_add("gem/.DIR", &hdr, sizeof(HDR)); }
void expire_item() { int pos, pos1, fd; char fgrp[80], fitem[80], fname[80], fold[80], fnew[80]; time_t expire; SLOT grp, item; expire = time(0) - EXP * 86400; pos = pos1 = 0; sprintf(fgrp, "2nd/%s", FN_GRP); while(rec_get(fgrp, &grp, sizeof(SLOT), pos) != -1) { sprintf(fitem, "2nd/%s/%s", grp.fn, FN_ITEM); sprintf(fnew, "2nd/%s/%s.new", grp.fn, FN_ITEM); fd = open(fnew, O_CREAT | O_TRUNC, 0600); // touch a new file if(fd > 0) close(fd); fd = 0; while(rec_get(fitem, &item, sizeof(SLOT), pos1) != -1) { if((item.prop & PROP_I_CANCEL) || (item.chrono < expire)) { sprintf(fname, "2nd/%s/%c/%s", grp.fn, item.fn[7], item.fn); f_rm(fname); } else { fd++; rec_add(fnew, &item, sizeof(SLOT)); } pos1++; } sprintf(fold, "2nd/%s/%s.old", grp.fn, FN_ITEM); f_cp(fitem, fold, O_TRUNC); f_mv(fnew, fitem); grp.reply = fd; rec_put(fgrp, &grp, sizeof(SLOT), pos); pos++; } return; }
int main() { struct list l = { NULL }; struct record *rec; printf("testing rec_alloc..."); rec = rec_alloc(101,"Abi",70); if(rec == NULL) goto fail; printf("[ok]\n"); printf("testing rec_add_and_cache [while adding first record]..."); rec_add(&l, rec); if(l.head != rec) goto fail; printf("[ok]\n"); printf("adding several records..."); rec = rec_alloc(102,"Anu",80); rec_add_and_cache(&l, rec); if(l.head != rec) goto fail; rec = rec_alloc(103,"Balu",85); rec_add_and_cache(&l, rec); if(l.head != rec) goto fail; rec = rec_alloc(104,"Chandran",90); rec_add_and_cache(&l, rec); if(l.head != rec) goto fail; printf("[ok]\n"); printf("testing rec_find_and_cache\n"); printf("\t(1) record is in the middle of the list..."); rec = rec_find_and_cache(&l, 102); if(rec == NULL) { printf("error: record is not identified\n"); goto fail; } else if(rec->roll_num != 102) { printf("error: identified a wrong record\n"); goto fail; } else if (l.head != rec) { printf("error: record found, but not cached\n"); goto fail; } else printf("[ok]\n"); printf("\t(2) record is at the end of the list..."); rec = rec_find_and_cache(&l, 104); if(rec == NULL) { printf("error: record is not identified\n"); goto fail; } else if(rec->roll_num != 104) { printf("error: identified a wrong record\n"); goto fail; } else if (l.head != rec) { printf("error: record found, but not cached\n"); goto fail; } else printf("[ok]\n"); printf("\t(3) record is not in the list..."); rec = NULL; rec = rec_find_and_cache(&l, 1); if(rec != NULL) goto fail; printf("[ok]\n"); return 0; fail: printf("[failed]\n"); return 0; }
bool BST::add(int data){ #if DEBUG std::cout << "add(" << data << ")\n"; #endif return rec_add(root, data); }