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