static PyObject * curve_append_from_file(SKCurveObject * self, PyObject * args) { PyObject * pyfile = NULL; PyObject * line = NULL; const char *buf; if (!PyArg_ParseTuple(args, "O", &pyfile)) return NULL; while ((line = PyFile_GetLine(pyfile, 0)) && (PyString_Size(line) != 0)) { buf = PyString_AsString(line); if (buf[0] != 'b' || (buf[1] != 'c' && buf[1] != 's')) break; if (!curve_parse_string_append(self, buf)) { Py_DECREF(line); return NULL; } Py_DECREF(line); } return line; }
/* * read lines from fp into the hunk. The hunk is parsed into two arrays * a and b. a gets the old state of the text, b gets the new state * The control char from the hunk is saved when inserting into a, but not b * (for performance while deleting files) */ static PyObject * addlines(PyObject *self, PyObject *args) { PyObject *fp, *hunk, *a, *b, *x; int i; int lena, lenb; int num; int todoa, todob; char *s, c; PyObject *l; if (!PyArg_ParseTuple(args, "OOiiOO", &fp, &hunk, &lena, &lenb, &a, &b)) return NULL; while (1) { todoa = lena - PyList_Size(a); todob = lenb - PyList_Size(b); num = todoa > todob ? todoa : todob; if (num == 0) break; for (i = 0; i < num; i++) { x = PyFile_GetLine(fp, 0); s = PyString_AS_STRING(x); c = *s; if (strcmp(s, "\\ No newline at end of file\n") == 0) { _fix_newline(hunk, a, b); continue; } if (c == '\n') { /* Some patches may be missing the control char * on empty lines. Supply a leading space. */ Py_DECREF(x); x = PyString_FromString(" \n"); } PyList_Append(hunk, x); if (c == '+') { l = PyString_FromString(s + 1); PyList_Append(b, l); Py_DECREF(l); } else if (c == '-') { PyList_Append(a, x); } else { l = PyString_FromString(s + 1); PyList_Append(b, l); Py_DECREF(l); PyList_Append(a, x); } Py_DECREF(x); } } return Py_BuildValue("l", 0); }
int _Py_DisplaySourceLine(PyObject *f, PyObject *filename, int lineno, int indent) { int err = 0; int fd; int i; char *found_encoding; char *encoding; PyObject *io; PyObject *binary; PyObject *fob = NULL; PyObject *lineobj = NULL; PyObject *res; char buf[MAXPATHLEN+1]; int kind; void *data; /* open the file */ if (filename == NULL) return 0; io = PyImport_ImportModuleNoBlock("io"); if (io == NULL) return -1; binary = _PyObject_CallMethodId(io, &PyId_open, "Os", filename, "rb"); if (binary == NULL) { PyErr_Clear(); binary = _Py_FindSourceFile(filename, buf, sizeof(buf), io); if (binary == NULL) { Py_DECREF(io); return -1; } } /* use the right encoding to decode the file as unicode */ fd = PyObject_AsFileDescriptor(binary); if (fd < 0) { Py_DECREF(io); Py_DECREF(binary); return 0; } found_encoding = PyTokenizer_FindEncodingFilename(fd, filename); if (found_encoding == NULL) PyErr_Clear(); encoding = (found_encoding != NULL) ? found_encoding : "utf-8"; /* Reset position */ if (lseek(fd, 0, SEEK_SET) == (off_t)-1) { Py_DECREF(io); Py_DECREF(binary); PyMem_FREE(found_encoding); return 0; } fob = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "Os", binary, encoding); Py_DECREF(io); Py_DECREF(binary); PyMem_FREE(found_encoding); if (fob == NULL) { PyErr_Clear(); return 0; } /* get the line number lineno */ for (i = 0; i < lineno; i++) { Py_XDECREF(lineobj); lineobj = PyFile_GetLine(fob, -1); if (!lineobj) { err = -1; break; } } res = _PyObject_CallMethodId(fob, &PyId_close, ""); if (res) Py_DECREF(res); else PyErr_Clear(); Py_DECREF(fob); if (!lineobj || !PyUnicode_Check(lineobj)) { Py_XDECREF(lineobj); return err; } /* remove the indentation of the line */ kind = PyUnicode_KIND(lineobj); data = PyUnicode_DATA(lineobj); for (i=0; i < PyUnicode_GET_LENGTH(lineobj); i++) { Py_UCS4 ch = PyUnicode_READ(kind, data, i); if (ch != ' ' && ch != '\t' && ch != '\014') break; } if (i) { PyObject *truncated; truncated = PyUnicode_Substring(lineobj, i, PyUnicode_GET_LENGTH(lineobj)); if (truncated) { Py_DECREF(lineobj); lineobj = truncated; } else { PyErr_Clear(); } } /* Write some spaces before the line */ strcpy(buf, " "); assert (strlen(buf) == 10); while (indent > 0) { if (indent < 10) buf[indent] = '\0'; err = PyFile_WriteString(buf, f); if (err != 0) break; indent -= 10; } /* finally display the line */ if (err == 0) err = PyFile_WriteObject(lineobj, f, Py_PRINT_RAW); Py_DECREF(lineobj); if (err == 0) err = PyFile_WriteString("\n", f); return err; }
static PyObject * obj_read(PyObject *self, PyObject *args) { /* The :compress tells PyArg_ParseTuple what function to use * in its error message */ char *pstr; if (!PyArg_ParseTuple(args, "s:read", &pstr)) { return NULL; } PyFileObject* f; f = PyFile_FromString((char*) pstr, "r"); int bufsize = -1; PyObject* faces = PyList_New(0); PyObject* points = PyList_New(0); PyObject* normals = PyList_New(0); PyObject* faces_normals = PyList_New(0); if (f != NULL) { PyFile_SetBufSize(f, bufsize); for (;;) { /* From PyFile_GetLine doc * * If n is 0, exactly one line is read, * regardless of the length of the line. If n is * greater than 0, no more than n bytes will be read * from the file; a partial line can be returned. In * both cases, an empty string is returned if the end * of the file is reached immediately. */ PyObject* line = PyFile_GetLine(f, 0); /* Invalid line ? */ if (! line || ! PyString_Check(line)) break; /* Empty line ? */ int num = PyString_Size(line); if (num == 0) break; /* * sscanf params * http://www.cs.utah.edu/~zachary/isp/tutorials/io/io.html */ char* cline = PyString_AsString(line); if (cline[0] == 'f') { char p1[255]; int has_normals = 0; int f1, f2, f3, f4; int n1, n2, n3, n4; int tmp; int cnt = sscanf(cline+2, "%s %s %s %s", p1, p1, p1, p1); // printf("%d\n", cnt); if (strchr(p1, '/') == NULL) { if (cnt == 3) sscanf(cline+2, "%d %d %d", &f1, &f2, &f3); else sscanf(cline+2, "%d %d %d", &f1, &f2, &f3); } else { has_normals = 1; if (strstr(p1, "//")) { if (cnt == 3) sscanf(cline+2, "%d//%d %d//%d %d//%d", &f1, &n1, &f2, &n2, &f3, &n3); else sscanf(cline+2, "%d//%d %d//%d %d//%d %d//%d", &f1, &n1, &f2, &n2, &f3, &n3, &f4, &n4); } else { if (cnt == 3) sscanf(cline+2, "%d/%d/%d %d/%d/%d %d/%d/%d", &f1, &tmp, &n1, &f2, &tmp, &n2, &f3, &tmp, &n3); else { sscanf(cline+2, "%d/%d/%d %d/%d/%d %d/%d/%d %d/%d/%d", &f1, &tmp, &n1, &f2, &tmp, &n2, &f3, &tmp, &n3, &f4, &tmp, &n4); } } } PyObject* face = PyList_New(3); PyList_SetItem(face, 0, PyInt_FromLong(f1-1)); PyList_SetItem(face, 1, PyInt_FromLong(f2-1)); PyList_SetItem(face, 2, PyInt_FromLong(f3-1)); PyList_Append(faces, face); if (cnt == 4) { PyObject* face2 = PyList_New(3); PyList_SetItem(face2, 0, PyInt_FromLong(f3-1)); PyList_SetItem(face2, 1, PyInt_FromLong(f4-1)); PyList_SetItem(face2, 2, PyInt_FromLong(f1-1)); PyList_Append(faces, face2); } if (has_normals) { PyObject* n = PyList_New(3); PyList_SetItem(n, 0, PyInt_FromLong(n1-1)); PyList_SetItem(n, 1, PyInt_FromLong(n2-1)); PyList_SetItem(n, 2, PyInt_FromLong(n3-1)); PyList_Append(faces_normals, n); if (cnt == 4) { PyObject* p = PyList_New(3); PyList_SetItem(p, 0, PyInt_FromLong(n3-1)); PyList_SetItem(p, 1, PyInt_FromLong(n4-1)); PyList_SetItem(p, 2, PyInt_FromLong(n1-1)); PyList_Append(faces_normals, p); } } } else if (cline[0] == 'v' && cline[1] == ' ') { double a, b, c; PyObject* vertex = PyList_New(3); sscanf(cline+2, "%lf %lf %lf", &a, &b, &c); // printf("%lf %lf %lf\n", a, b, c); PyList_SetItem(vertex, 0, PyFloat_FromDouble(a)); PyList_SetItem(vertex, 1, PyFloat_FromDouble(b)); PyList_SetItem(vertex, 2, PyFloat_FromDouble(c)); PyList_Append(points, vertex); } else if (cline[0] == 'v' && cline[1] == 'n') { double a, b, c; PyObject* normal = PyList_New(3); sscanf(cline+3, "%lf %lf %lf", &a, &b, &c); // printf("%lf %lf %lf\n", a, b, c); PyList_SetItem(normal, 0, PyFloat_FromDouble(a)); PyList_SetItem(normal, 1, PyFloat_FromDouble(b)); PyList_SetItem(normal, 2, PyFloat_FromDouble(c)); PyList_Append(normals, normal); } } } fclose(PyFile_AsFile(f)); PyObject* tuple = PyList_New(4); PyList_SetItem(tuple, 0, points); PyList_SetItem(tuple, 1, faces); PyList_SetItem(tuple, 2, normals); PyList_SetItem(tuple, 3, faces_normals); return tuple; }
int _Py_DisplaySourceLine(PyObject *f, PyObject *filename, int lineno, int indent) { int err = 0; int fd; int i; char *found_encoding; char *encoding; PyObject *io; PyObject *binary; PyObject *fob = NULL; PyObject *lineobj = NULL; PyObject *res; char buf[MAXPATHLEN+1]; Py_UNICODE *u, *p; Py_ssize_t len; /* open the file */ if (filename == NULL) return 0; io = PyImport_ImportModuleNoBlock("io"); if (io == NULL) return -1; binary = PyObject_CallMethod(io, "open", "Os", filename, "rb"); if (binary == NULL) { binary = _Py_FindSourceFile(filename, buf, sizeof(buf), io); if (binary == NULL) { Py_DECREF(io); return 0; } } /* use the right encoding to decode the file as unicode */ fd = PyObject_AsFileDescriptor(binary); found_encoding = PyTokenizer_FindEncoding(fd); encoding = (found_encoding != NULL) ? found_encoding : "utf-8"; lseek(fd, 0, 0); /* Reset position */ fob = PyObject_CallMethod(io, "TextIOWrapper", "Os", binary, encoding); Py_DECREF(io); Py_DECREF(binary); PyMem_FREE(found_encoding); if (fob == NULL) { PyErr_Clear(); return 0; } /* get the line number lineno */ for (i = 0; i < lineno; i++) { Py_XDECREF(lineobj); lineobj = PyFile_GetLine(fob, -1); if (!lineobj) { err = -1; break; } } res = PyObject_CallMethod(fob, "close", ""); if (res) Py_DECREF(res); else PyErr_Clear(); Py_DECREF(fob); if (!lineobj || !PyUnicode_Check(lineobj)) { Py_XDECREF(lineobj); return err; } /* remove the indentation of the line */ u = PyUnicode_AS_UNICODE(lineobj); len = PyUnicode_GET_SIZE(lineobj); for (p=u; *p == ' ' || *p == '\t' || *p == '\014'; p++) len--; if (u != p) { PyObject *truncated; truncated = PyUnicode_FromUnicode(p, len); if (truncated) { Py_DECREF(lineobj); lineobj = truncated; } else { PyErr_Clear(); } } /* Write some spaces before the line */ strcpy(buf, " "); assert (strlen(buf) == 10); while (indent > 0) { if(indent < 10) buf[indent] = '\0'; err = PyFile_WriteString(buf, f); if (err != 0) break; indent -= 10; } /* finally display the line */ if (err == 0) err = PyFile_WriteObject(lineobj, f, Py_PRINT_RAW); Py_DECREF(lineobj); if (err == 0) err = PyFile_WriteString("\n", f); return err; }