1/*2* LibXDiff by Davide Libenzi ( File Differential Library )3* Copyright (C) 2003 Davide Libenzi4*5* This library is free software; you can redistribute it and/or6* modify it under the terms of the GNU Lesser General Public7* License as published by the Free Software Foundation; either8* version 2.1 of the License, or (at your option) any later version.9*10* This library is distributed in the hope that it will be useful,11* but WITHOUT ANY WARRANTY; without even the implied warranty of12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU13* Lesser General Public License for more details.14*15* You should have received a copy of the GNU Lesser General Public16* License along with this library; if not, write to the Free Software17* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA18*19* Davide Libenzi <davidel@xmailserver.org>20*21*/2223#include "xinclude.h"2425262728static long xdl_get_rec(xdfile_t *xdf, long ri, char const **rec);29static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t *ecb);30static xdchange_t *xdl_get_hunk(xdchange_t *xscr, xdemitconf_t const *xecfg);3132333435static long xdl_get_rec(xdfile_t *xdf, long ri, char const **rec) {3637*rec = xdf->recs[ri]->ptr;3839return xdf->recs[ri]->size;40}414243static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t *ecb) {44long size, psize = strlen(pre);45char const *rec;4647size = xdl_get_rec(xdf, ri, &rec);48if (xdl_emit_diffrec(rec, size, pre, psize, ecb) < 0) {4950return -1;51}5253return 0;54}555657/*58* Starting at the passed change atom, find the latest change atom to be included59* inside the differential hunk according to the specified configuration.60*/61static xdchange_t *xdl_get_hunk(xdchange_t *xscr, xdemitconf_t const *xecfg) {62xdchange_t *xch, *xchp;6364for (xchp = xscr, xch = xscr->next; xch; xchp = xch, xch = xch->next)65if (xch->i1 - (xchp->i1 + xchp->chg1) > 2 * xecfg->ctxlen)66break;6768return xchp;69}707172static long def_ff(const char *rec, long len, char *buf, long sz, void *priv)73{74if (len > 0 &&75(isalpha((unsigned char)*rec) || /* identifier? */76*rec == '_' || /* also identifier? */77*rec == '$')) { /* identifiers from VMS and other esoterico */78if (len > sz)79len = sz;80while (0 < len && isspace((unsigned char)rec[len - 1]))81len--;82memcpy(buf, rec, len);83return len;84}85return -1;86}8788static void xdl_find_func(xdfile_t *xf, long i, char *buf, long sz, long *ll,89find_func_t ff, void *ff_priv) {9091/*92* Be quite stupid about this for now. Find a line in the old file93* before the start of the hunk (and context) which starts with a94* plausible character.95*/9697const char *rec;98long len;99100while (i-- > 0) {101len = xdl_get_rec(xf, i, &rec);102if ((*ll = ff(rec, len, buf, sz, ff_priv)) >= 0)103return;104}105*ll = 0;106}107108109static int xdl_emit_common(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,110xdemitconf_t const *xecfg) {111xdfile_t *xdf = &xe->xdf1;112const char *rchg = xdf->rchg;113long ix;114115for (ix = 0; ix < xdf->nrec; ix++) {116if (rchg[ix])117continue;118if (xdl_emit_record(xdf, ix, "", ecb))119return -1;120}121return 0;122}123124int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,125xdemitconf_t const *xecfg) {126long s1, s2, e1, e2, lctx;127xdchange_t *xch, *xche;128char funcbuf[80];129long funclen = 0;130find_func_t ff = xecfg->find_func ? xecfg->find_func : def_ff;131132if (xecfg->flags & XDL_EMIT_COMMON)133return xdl_emit_common(xe, xscr, ecb, xecfg);134135for (xch = xche = xscr; xch; xch = xche->next) {136xche = xdl_get_hunk(xch, xecfg);137138s1 = XDL_MAX(xch->i1 - xecfg->ctxlen, 0);139s2 = XDL_MAX(xch->i2 - xecfg->ctxlen, 0);140141lctx = xecfg->ctxlen;142lctx = XDL_MIN(lctx, xe->xdf1.nrec - (xche->i1 + xche->chg1));143lctx = XDL_MIN(lctx, xe->xdf2.nrec - (xche->i2 + xche->chg2));144145e1 = xche->i1 + xche->chg1 + lctx;146e2 = xche->i2 + xche->chg2 + lctx;147148/*149* Emit current hunk header.150*/151152if (xecfg->flags & XDL_EMIT_FUNCNAMES) {153xdl_find_func(&xe->xdf1, s1, funcbuf,154sizeof(funcbuf), &funclen,155ff, xecfg->find_func_priv);156}157if (xdl_emit_hunk_hdr(s1 + 1, e1 - s1, s2 + 1, e2 - s2,158funcbuf, funclen, ecb) < 0)159return -1;160161/*162* Emit pre-context.163*/164for (; s1 < xch->i1; s1++)165if (xdl_emit_record(&xe->xdf1, s1, " ", ecb) < 0)166return -1;167168for (s1 = xch->i1, s2 = xch->i2;; xch = xch->next) {169/*170* Merge previous with current change atom.171*/172for (; s1 < xch->i1 && s2 < xch->i2; s1++, s2++)173if (xdl_emit_record(&xe->xdf1, s1, " ", ecb) < 0)174return -1;175176/*177* Removes lines from the first file.178*/179for (s1 = xch->i1; s1 < xch->i1 + xch->chg1; s1++)180if (xdl_emit_record(&xe->xdf1, s1, "-", ecb) < 0)181return -1;182183/*184* Adds lines from the second file.185*/186for (s2 = xch->i2; s2 < xch->i2 + xch->chg2; s2++)187if (xdl_emit_record(&xe->xdf2, s2, "+", ecb) < 0)188return -1;189190if (xch == xche)191break;192s1 = xch->i1 + xch->chg1;193s2 = xch->i2 + xch->chg2;194}195196/*197* Emit post-context.198*/199for (s1 = xche->i1 + xche->chg1; s1 < e1; s1++)200if (xdl_emit_record(&xe->xdf1, s1, " ", ecb) < 0)201return -1;202}203204return 0;205}