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