/** * Split a single card without regard to anything it may be attached to. * @param c the card to split. * @param at point before which to split * @param o the orphanage where all the parents and children hang out * @param log record failures in the log * @return 1 if it worked else 0 */ static int card_split_one( card *c, int at, orphanage *o, plugin_log *log ) { int res = 1; // q is the new trailing pair int is_parent = pair_is_parent( c->p ); int id = (is_parent)?pair_id(c->p):0; pair *q = pair_split( &c->p, at ); if ( q != NULL ) { card *c2 = card_create( q, log ); if ( c2 != NULL ) { card_set_text_off( c2, card_end(c) ); card_add_after(c,c2); } else res = 0; if ( is_parent ) { pair_set_id( c->p,id ); int id2 = orphanage_next_id( o ); pair_set_id( q, id2 ); } } else { plugin_log_add(log,"failed to split pair\n"); res = 0; } return res; }
/** * Turn a card into a parent pair possibly with no children * @param c VAR param: the original card, maybe already a parent * @param log the log to record errors in * @return the parent or NULL if it failed */ card *card_make_parent( card **c, plugin_log *log ) { if ( pair_is_parent((*c)->p) ) return *c; else if ( pair_is_child((*c)->p) ) return NULL; else // ordinary card { card *oldc = *c; pair *pp = pair_create_parent( pair_versions(oldc->p), pair_data(oldc->p), pair_len(oldc->p) ); if ( pp != NULL ) { card *parent = card_create(pp,log); // creates dangling parent - needs at least one child card_replace( oldc, parent ); card_dispose( oldc ); *c = parent; return parent; } else plugin_log_add(log,"card: failed to create parent pair\n"); return NULL; } }
static int make_test_data( plugin_log *log ) { c1 = c2 = c3 = c4 = cr = cl = cc = NULL; bs1 = bs2 = bs3 = bs4 = bs5 = bsl = bsr = bsm = NULL; p1 = p2 = p3 = p4 = pl = pr = pc = NULL; bs1 = bitset_create(); bitset_set(bs1,3); bitset_set(bs1,27); bs2 = bitset_create(); bitset_set(bs2,5); bitset_set(bs2,17); bs3 = bitset_create(); bitset_set(bs3,7); bitset_set(bs3,14); bitset_set(bs3,19); bs4 = bitset_create(); bitset_set(bs4,7); bitset_set(bs4,14); bitset_set(bs4,22); bs5 = bitset_create(); bitset_set(bs5,7); bitset_set(bs5,14); bsl = bitset_create(); bsr = bitset_create(); bsl = bitset_set(bsl,3); if ( bsl != NULL ) bsl = bitset_set(bsl,23); bsr = bitset_set(bsr,3); if ( bsr != NULL ) bsr = bitset_set(bsr,21); bsm = bitset_create(); bitset_set(bsm,23); bitset_set(bsm,7); if ( bs1 != NULL && bs2 != NULL && bs3 != NULL && bs4 != NULL && bs5 != NULL && bsl != NULL && bsr != NULL && bsm != NULL ) { p1 = pair_create_basic(bs1, data1, 6); p2 = pair_create_basic(bs2, data2, 5); p3 = pair_create_basic(bs3, data3, 4); p4 = pair_create_basic(bs4, data4, 6); pl = pair_create_basic(bsl,data4,6); pr = pair_create_basic(bsr,data5,5); pc = pair_create_basic(bsm,data6,7); if ( p1 != NULL && p2 != NULL && p3 != NULL && p4 != NULL && pl != NULL && pr != NULL && pc != NULL ) { c1 = card_create( p1, log ); c2 = card_create( p2, log ); c3 = card_create( p3, log ); c4 = card_create( p4, log ); cl = card_create( pl, log ); cr = card_create( pr, log ); cc = card_create( pc, log ); if ( c1 != NULL && c2 != NULL && c3 != NULL && c4 != NULL && cl != NULL && cr != NULL && cc != NULL ) return 1; } } return 0; }
// create a new card with the given inital_amount as the balance and add it to the card_list // success: SUCCESS // failure: ERRNO_MP_ALLOC, ERRNO_MP_LIST_PUSH int card_create_and_add_to_list(card_t ** card, list_t * card_list, uint32_t initial_amount) { // create card RET_ON_FAIL(card_create(card, card_list->count, initial_amount)); // add card to list RET_ON_FAIL(card_add_to_list(*card, card_list)); return SUCCESS; }
card *card_create_blank_bs( bitset *bs, plugin_log *log ) { card *c = NULL; pair *p = pair_create_basic( bs, ustring_empty, 0 ); if ( p != NULL ) c = card_create( p, log ); else if ( log != NULL ) plugin_log_add(log,"card: failed to create pair"); return c; }
/** * Create an empty card * @param version the version of the empty card * @param log the log to record errors in * @return an empty card */ card *card_create_blank( int version, plugin_log *log ) { card *c = NULL; bitset *bs = bitset_create(); if ( bs != NULL ) { bitset_set( bs, version ); pair *p = pair_create_basic( bs, ustring_empty, 0 ); if ( p != NULL ) c = card_create( p, log ); else plugin_log_add(log,"card: failed to create pair"); bitset_dispose( bs ); } else plugin_log_add(log,"card: failed to create bitset"); return c; }
/** * Create a child from the new version * @param parent the parent we are a child of * @param version the version of the child * @param log record errors here * @return the newborn child */ card *card_make_child( card *parent, int version, plugin_log *log ) { card *cc = NULL; bitset *bs = bitset_create(); if ( bs != NULL ) { bs = bitset_set( bs, version ); if ( bs != NULL ) { pair *child = pair_create_child( bs ); pair_add_child(card_pair(parent), child ); bitset_dispose( bs ); cc = card_create( child, log ); } else plugin_log_add(log,"card: failed to create bitset\n"); } return cc; }