clean up and optimize nth_packed_object_sha1() usage
[gitweb.git] / xdiff-interface.c
index f7d6f988298e3906a1a525fed484dc093404e4f9..10816e95a07c23dda7554c102c306d7e9beca606 100644 (file)
@@ -1,10 +1,10 @@
 #include "cache.h"
 #include "xdiff-interface.h"
 
-static int parse_num(char **cp_p, unsigned int *num_p)
+static int parse_num(char **cp_p, int *num_p)
 {
        char *cp = *cp_p;
-       unsigned int num = 0;
+       int num = 0;
        int read_some;
 
        while ('0' <= *cp && *cp <= '9')
@@ -17,8 +17,8 @@ static int parse_num(char **cp_p, unsigned int *num_p)
 }
 
 int parse_hunk_header(char *line, int len,
-                     unsigned int *ob, unsigned int *on,
-                     unsigned int *nb, unsigned int *nn)
+                     int *ob, int *on,
+                     int *nb, int *nn)
 {
        char *cp;
        cp = line + 4;
@@ -69,9 +69,9 @@ int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
        for (i = 0; i < nbuf; i++) {
                if (mb[i].ptr[mb[i].size-1] != '\n') {
                        /* Incomplete line */
-                       priv->remainder = realloc(priv->remainder,
-                                                 priv->remainder_size +
-                                                 mb[i].size);
+                       priv->remainder = xrealloc(priv->remainder,
+                                                  priv->remainder_size +
+                                                  mb[i].size);
                        memcpy(priv->remainder + priv->remainder_size,
                               mb[i].ptr, mb[i].size);
                        priv->remainder_size += mb[i].size;
@@ -83,9 +83,9 @@ int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
                        consume_one(priv, mb[i].ptr, mb[i].size);
                        continue;
                }
-               priv->remainder = realloc(priv->remainder,
-                                         priv->remainder_size +
-                                         mb[i].size);
+               priv->remainder = xrealloc(priv->remainder,
+                                          priv->remainder_size +
+                                          mb[i].size);
                memcpy(priv->remainder + priv->remainder_size,
                       mb[i].ptr, mb[i].size);
                consume_one(priv, priv->remainder,
@@ -102,3 +102,24 @@ int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
        }
        return 0;
 }
+
+int read_mmfile(mmfile_t *ptr, const char *filename)
+{
+       struct stat st;
+       FILE *f;
+       size_t sz;
+
+       if (stat(filename, &st))
+               return error("Could not stat %s", filename);
+       if ((f = fopen(filename, "rb")) == NULL)
+               return error("Could not open %s", filename);
+       sz = xsize_t(st.st_size);
+       ptr->ptr = xmalloc(sz);
+       if (fread(ptr->ptr, sz, 1, f) != 1)
+               return error("Could not read %s", filename);
+       fclose(f);
+       ptr->size = sz;
+       return 0;
+}
+
+