// xxx cmt this assumes new_key doesn't need freeing. // Before: // "x" => "3" // "y" => "4" <-- pold // "z" => "5" <-- pnew // // Rename y to z // // After: // "x" => "3" // "z" => "4" // void lrec_rename(lrec_t* prec, char* old_key, char* new_key) { lrece_t* pold = lrec_find_entry(prec, old_key); if (pold != NULL) { lrece_t* pnew = lrec_find_entry(prec, new_key); if (pnew == NULL) { // E.g. rename "x" to "y" when "y" is not present if (pold->free_flags & LREC_FREE_ENTRY_KEY) { free(pold->key); pold->key = new_key; pold->free_flags &= ~LREC_FREE_ENTRY_KEY; } else { pold->key = new_key; } } else { // E.g. rename "x" to "y" when "y" is already present if (pnew->free_flags & LREC_FREE_ENTRY_VALUE) { free(pnew->value); } if (pold->free_flags & LREC_FREE_ENTRY_KEY) { free(pold->key); pold->free_flags &= ~LREC_FREE_ENTRY_KEY; } pold->key = new_key; lrec_unlink(prec, pnew); free(pnew); } } }
void lrec_move_to_tail(lrec_t* prec, char* key) { lrece_t* pe = lrec_find_entry(prec, key); if (pe == NULL) return; lrec_unlink(prec, pe); lrec_link_at_tail(prec, pe); }
void lrec_unlink_and_free(lrec_t* prec, lrece_t* pe) { if (pe->free_flags & FREE_ENTRY_KEY) free(pe->key); if (pe->free_flags & FREE_ENTRY_VALUE) free(pe->value); lrec_unlink(prec, pe); free(pe); }
// ---------------------------------------------------------------- void lrec_remove(lrec_t* prec, char* key) { lrece_t* pe = lrec_find_entry(prec, key); if (pe == NULL) return; lrec_unlink(prec, pe); if (pe->free_flags & LREC_FREE_ENTRY_KEY) { free(pe->key); } if (pe->free_flags & LREC_FREE_ENTRY_VALUE) { free(pe->value); } free(pe); }