aboutsummaryrefslogtreecommitdiffstats
path: root/libpatch_reverse_hunks__.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2024-01-09 22:04:24 +0100
committerMattias Andrée <maandree@kth.se>2024-01-09 22:04:24 +0100
commitae850b1ac755f471beac4dbfef4654fe3fbaaae9 (patch)
tree319e7f7f1b99cd1ee75e100f0a29b0f7c827ccea /libpatch_reverse_hunks__.c
downloadlibpatch-ae850b1ac755f471beac4dbfef4654fe3fbaaae9.tar.gz
libpatch-ae850b1ac755f471beac4dbfef4654fe3fbaaae9.tar.bz2
libpatch-ae850b1ac755f471beac4dbfef4654fe3fbaaae9.tar.xz
First commit
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r--libpatch_reverse_hunks__.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/libpatch_reverse_hunks__.c b/libpatch_reverse_hunks__.c
new file mode 100644
index 0000000..a22e849
--- /dev/null
+++ b/libpatch_reverse_hunks__.c
@@ -0,0 +1,38 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+
+
+int
+libpatch_reverse_hunks__(struct libpatch_patch *patch, size_t patchlen,
+ const size_t *hunks, size_t nhunks,
+ struct libpatch_patch **patchcopy, size_t *patchcopysize)
+{
+ size_t i, j, first, count, lines;
+ void *new;
+
+ if (nhunks < 2)
+ return 0;
+
+ lines = patchlen - hunks[0];
+ if (lines > *patchcopysize) {
+ new = realloc(*patchcopy, lines * sizeof(**patchcopy));
+ if (!new)
+ return -1;
+ *patchcopy = new;
+ *patchcopysize = lines;
+ }
+
+ j = lines;
+ for (i = 1; i < nhunks; i++) {
+ first = hunks[i - 1];
+ count = hunks[i] - first;
+ memcpy(&patchcopy[j -= count], &patch[first], count * sizeof(*patch));
+ }
+ first = hunks[i - 1];
+ count = patchlen - first;
+ memcpy(&patchcopy[j -= count], &patch[first], count * sizeof(*patch));
+
+ memcpy(&patch[hunks[0]], &patchcopy[0], lines * sizeof(*patch));
+
+ return 0;
+}