2 #include "xdiff-interface.h"
4 static int parse_num(char **cp_p, int *num_p)
10 while ('0' <= *cp && *cp <= '9')
11 num = num * 10 + *cp++ - '0';
12 if (!(read_some = cp - *cp_p))
19 int parse_hunk_header(char *line, int len,
25 if (parse_num(&cp, ob)) {
27 return error("malformed diff output: %s", line);
31 if (parse_num(&cp, on))
36 if (*cp++ != ' ' || *cp++ != '+')
38 if (parse_num(&cp, nb))
42 if (parse_num(&cp, nn))
47 return -!!memcmp(cp, " @@", 3);
50 static void consume_one(void *priv_, char *s, unsigned long size)
52 struct xdiff_emit_state *priv = priv_;
55 unsigned long this_size;
56 ep = memchr(s, '\n', size);
57 this_size = (ep == NULL) ? size : (ep - s + 1);
58 priv->consume(priv, s, this_size);
64 int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
66 struct xdiff_emit_state *priv = priv_;
69 for (i = 0; i < nbuf; i++) {
70 if (mb[i].ptr[mb[i].size-1] != '\n') {
72 priv->remainder = xrealloc(priv->remainder,
73 priv->remainder_size +
75 memcpy(priv->remainder + priv->remainder_size,
76 mb[i].ptr, mb[i].size);
77 priv->remainder_size += mb[i].size;
81 /* we have a complete line */
82 if (!priv->remainder) {
83 consume_one(priv, mb[i].ptr, mb[i].size);
86 priv->remainder = xrealloc(priv->remainder,
87 priv->remainder_size +
89 memcpy(priv->remainder + priv->remainder_size,
90 mb[i].ptr, mb[i].size);
91 consume_one(priv, priv->remainder,
92 priv->remainder_size + mb[i].size);
93 free(priv->remainder);
94 priv->remainder = NULL;
95 priv->remainder_size = 0;
97 if (priv->remainder) {
98 consume_one(priv, priv->remainder, priv->remainder_size);
99 free(priv->remainder);
100 priv->remainder = NULL;
101 priv->remainder_size = 0;
106 int read_mmfile(mmfile_t *ptr, const char *filename)
112 if (stat(filename, &st))
113 return error("Could not stat %s", filename);
114 if ((f = fopen(filename, "rb")) == NULL)
115 return error("Could not open %s", filename);
116 sz = xsize_t(st.st_size);
117 ptr->ptr = xmalloc(sz);
118 if (fread(ptr->ptr, sz, 1, f) != 1)
119 return error("Could not read %s", filename);