Show More
@@ -1,159 +1,161 | |||
|
1 | 1 | /* |
|
2 | 2 | * diffhelpers.c - helper routines for mpatch |
|
3 | 3 | * |
|
4 | 4 | * Copyright 2007 Chris Mason <chris.mason@oracle.com> |
|
5 | 5 | * |
|
6 | 6 | * This software may be used and distributed according to the terms |
|
7 | 7 | * of the GNU General Public License v2, incorporated herein by reference. |
|
8 | 8 | */ |
|
9 | 9 | |
|
10 | 10 | #include <Python.h> |
|
11 | 11 | #include <stdlib.h> |
|
12 | 12 | #include <string.h> |
|
13 | 13 | |
|
14 | 14 | static char diffhelpers_doc[] = "Efficient diff parsing"; |
|
15 | 15 | static PyObject *diffhelpers_Error; |
|
16 | 16 | |
|
17 | 17 | |
|
18 | 18 | /* fixup the last lines of a and b when the patch has no newline at eof */ |
|
19 | 19 | static void _fix_newline(PyObject *hunk, PyObject *a, PyObject *b) |
|
20 | 20 | { |
|
21 | 21 | int hunksz = PyList_Size(hunk); |
|
22 | 22 | PyObject *s = PyList_GET_ITEM(hunk, hunksz-1); |
|
23 | 23 | char *l = PyString_AS_STRING(s); |
|
24 | int alen = PyList_Size(a); | |
|
25 | int blen = PyList_Size(b); | |
|
26 | char c = l[0]; | |
|
27 | PyObject *hline; | |
|
24 | 28 | int sz = PyString_GET_SIZE(s); |
|
29 | ||
|
25 | 30 | if (sz > 1 && l[sz-2] == '\r') |
|
26 | 31 |
|
|
27 | 32 |
|
|
28 | int alen = PyList_Size(a); | |
|
29 | int blen = PyList_Size(b); | |
|
30 | char c = l[0]; | |
|
33 | hline = PyString_FromStringAndSize(l, sz-1); | |
|
31 | 34 | |
|
32 | PyObject *hline = PyString_FromStringAndSize(l, sz-1); | |
|
33 | 35 | if (c == ' ' || c == '+') { |
|
34 | 36 | PyObject *rline = PyString_FromStringAndSize(l+1, sz-2); |
|
35 | 37 | PyList_SetItem(b, blen-1, rline); |
|
36 | 38 | } |
|
37 | 39 | if (c == ' ' || c == '-') { |
|
38 | 40 | Py_INCREF(hline); |
|
39 | 41 | PyList_SetItem(a, alen-1, hline); |
|
40 | 42 | } |
|
41 | 43 | PyList_SetItem(hunk, hunksz-1, hline); |
|
42 | 44 | } |
|
43 | 45 | |
|
44 | 46 | /* python callable form of _fix_newline */ |
|
45 | 47 | static PyObject * |
|
46 | 48 | fix_newline(PyObject *self, PyObject *args) |
|
47 | 49 | { |
|
48 | 50 | PyObject *hunk, *a, *b; |
|
49 | 51 | if (!PyArg_ParseTuple(args, "OOO", &hunk, &a, &b)) |
|
50 | 52 | return NULL; |
|
51 | 53 | _fix_newline(hunk, a, b); |
|
52 | 54 | return Py_BuildValue("l", 0); |
|
53 | 55 | } |
|
54 | 56 | |
|
55 | 57 | /* |
|
56 | 58 | * read lines from fp into the hunk. The hunk is parsed into two arrays |
|
57 | 59 | * a and b. a gets the old state of the text, b gets the new state |
|
58 | 60 | * The control char from the hunk is saved when inserting into a, but not b |
|
59 | 61 | * (for performance while deleting files) |
|
60 | 62 | */ |
|
61 | 63 | static PyObject * |
|
62 | 64 | addlines(PyObject *self, PyObject *args) |
|
63 | 65 | { |
|
64 | 66 | |
|
65 | 67 | PyObject *fp, *hunk, *a, *b, *x; |
|
66 | 68 | int i; |
|
67 | 69 | int lena, lenb; |
|
68 | 70 | int num; |
|
69 | 71 | int todoa, todob; |
|
70 | 72 | char *s, c; |
|
71 | 73 | PyObject *l; |
|
72 | 74 | if (!PyArg_ParseTuple(args, "OOiiOO", &fp, &hunk, &lena, &lenb, &a, &b)) |
|
73 | 75 | return NULL; |
|
74 | 76 | |
|
75 | 77 | while(1) { |
|
76 | 78 | todoa = lena - PyList_Size(a); |
|
77 | 79 | todob = lenb - PyList_Size(b); |
|
78 | 80 | num = todoa > todob ? todoa : todob; |
|
79 | 81 | if (num == 0) |
|
80 | 82 | break; |
|
81 | 83 | for (i = 0 ; i < num ; i++) { |
|
82 | 84 | x = PyFile_GetLine(fp, 0); |
|
83 | 85 | s = PyString_AS_STRING(x); |
|
84 | 86 | c = *s; |
|
85 | 87 | if (strcmp(s, "\\ No newline at end of file\n") == 0) { |
|
86 | 88 | _fix_newline(hunk, a, b); |
|
87 | 89 | continue; |
|
88 | 90 | } |
|
89 | 91 | if (c == '\n') { |
|
90 | 92 | /* Some patches may be missing the control char |
|
91 | 93 | * on empty lines. Supply a leading space. */ |
|
92 | 94 | Py_DECREF(x); |
|
93 | 95 | x = PyString_FromString(" \n"); |
|
94 | 96 | } |
|
95 | 97 | PyList_Append(hunk, x); |
|
96 | 98 | if (c == '+') { |
|
97 | 99 | l = PyString_FromString(s + 1); |
|
98 | 100 | PyList_Append(b, l); |
|
99 | 101 | Py_DECREF(l); |
|
100 | 102 | } else if (c == '-') { |
|
101 | 103 | PyList_Append(a, x); |
|
102 | 104 | } else { |
|
103 | 105 | l = PyString_FromString(s + 1); |
|
104 | 106 | PyList_Append(b, l); |
|
105 | 107 | Py_DECREF(l); |
|
106 | 108 | PyList_Append(a, x); |
|
107 | 109 | } |
|
108 | 110 | Py_DECREF(x); |
|
109 | 111 | } |
|
110 | 112 | } |
|
111 | 113 | return Py_BuildValue("l", 0); |
|
112 | 114 | } |
|
113 | 115 | |
|
114 | 116 | /* |
|
115 | 117 | * compare the lines in a with the lines in b. a is assumed to have |
|
116 | 118 | * a control char at the start of each line, this char is ignored in the |
|
117 | 119 | * compare |
|
118 | 120 | */ |
|
119 | 121 | static PyObject * |
|
120 | 122 | testhunk(PyObject *self, PyObject *args) |
|
121 | 123 | { |
|
122 | 124 | |
|
123 | 125 | PyObject *a, *b; |
|
124 | 126 | long bstart; |
|
125 | 127 | int alen, blen; |
|
126 | 128 | int i; |
|
127 | 129 | char *sa, *sb; |
|
128 | 130 | |
|
129 | 131 | if (!PyArg_ParseTuple(args, "OOl", &a, &b, &bstart)) |
|
130 | 132 | return NULL; |
|
131 | 133 | alen = PyList_Size(a); |
|
132 | 134 | blen = PyList_Size(b); |
|
133 | 135 | if (alen > blen - bstart) { |
|
134 | 136 | return Py_BuildValue("l", -1); |
|
135 | 137 | } |
|
136 | 138 | for (i = 0 ; i < alen ; i++) { |
|
137 | 139 | sa = PyString_AS_STRING(PyList_GET_ITEM(a, i)); |
|
138 | 140 | sb = PyString_AS_STRING(PyList_GET_ITEM(b, i + bstart)); |
|
139 | 141 | if (strcmp(sa+1, sb) != 0) |
|
140 | 142 | return Py_BuildValue("l", -1); |
|
141 | 143 | } |
|
142 | 144 | return Py_BuildValue("l", 0); |
|
143 | 145 | } |
|
144 | 146 | |
|
145 | 147 | static PyMethodDef methods[] = { |
|
146 | 148 | {"addlines", addlines, METH_VARARGS, "add lines to a hunk\n"}, |
|
147 | 149 | {"fix_newline", fix_newline, METH_VARARGS, "fixup newline counters\n"}, |
|
148 | 150 | {"testhunk", testhunk, METH_VARARGS, "test lines in a hunk\n"}, |
|
149 | 151 | {NULL, NULL} |
|
150 | 152 | }; |
|
151 | 153 | |
|
152 | 154 | PyMODINIT_FUNC |
|
153 | 155 | initdiffhelpers(void) |
|
154 | 156 | { |
|
155 | 157 | Py_InitModule3("diffhelpers", methods, diffhelpers_doc); |
|
156 | 158 | diffhelpers_Error = PyErr_NewException("diffhelpers.diffhelpersError", |
|
157 | 159 | NULL, NULL); |
|
158 | 160 | } |
|
159 | 161 |
General Comments 0
You need to be logged in to leave comments.
Login now