k1a  1.1
Accelerated functionalities for k1lib
StrIterCat.cpp
Go to the documentation of this file.
1 #define PY_SSIZE_T_CLEAN
2 #include "StrIterCat.h"
3 
4 #include <Python.h>
5 #include <structmember.h>
6 
7 #include <fstream>
8 #include <iostream>
9 #include <string>
10 #include <vector>
11 
12 #include "utils.h"
13 
14 namespace k1a {
15 
16 StrIterCat::StrIterCat(PyObject *pyObj, std::string fileName) : StrIter(pyObj) {
17  this->fileName = new std::string(fileName);
18  this->fp = new std::ifstream(fileName);
19 };
20 
21 std::string StrIterCat::next() {
22  std::string line;
23  if (std::getline(*fp, line))
24  return line;
25  else {
26  done = true;
27  return "";
28  }
29 };
30 
37 PyObject *PyStrIterCat_new(std::string fileName) {
38  if (debug) log_println("PyStrIterCat_new");
39  PyStrIterCat *res = PyObject_New(PyStrIterCat, &PyStrIterCat_Type);
40  res->val = new StrIterCat((PyObject *)res, fileName);
41  return (PyObject *)res;
42 }
43 
44 PyObject *PyStrIterCat_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) {
45  PyStrIterCat *return_value = PyObject_New(PyStrIterCat, type);
46  char *fileName;
47  PyArg_ParseTuple(args, "s", &fileName);
48  return PyStrIterCat_new(std::string(fileName));
49 }
50 
52  fp->close();
53  delete fileName;
54 }
55 
56 PyObject *PyStrIterCat_conjugate(PyStrIterCat *self, PyObject *Py_UNUSED(ignored)) {
57  return PyUnicode_FromString("str_iter_conjugate");
58 };
59 
60 PyMethodDef PyStrIterCat_methods[] = {
61  {"conjugate", (PyCFunction)PyStrIterCat_conjugate, METH_NOARGS, "conjugate docs"},
62  {NULL, NULL}};
63 
64 PyTypeObject PyStrIterCat_Type = {
65  PyVarObject_HEAD_INIT(&PyType_Type, 0) "StrIterCat",
66  sizeof(PyStrIterCat),
67  0,
68  (destructor)StrIter::Py_dealloc<PyStrIterCat>, /* tp_dealloc (destructor)str_iter_dealloc */
69  0, /* tp_vectorcall_offset */
70  0, /* tp_getattr */
71  0, /* tp_setattr */
72  0, /* tp_as_async */
73  (reprfunc)StrIter::Py_repr<PyStrIterCat>, /* tp_repr */
74  0, /* tp_as_number */
75  StrIter::Py_as_sequence<PyStrIterCat>(), /* tp_as_sequence */
76  0, /* tp_as_mapping */
77  0, /* tp_hash */
78  0, /* tp_call */
79  0, /* tp_str */
80  PyObject_GenericGetAttr, /* tp_getattro */
81  0, /* tp_setattro */
82  0, /* tp_as_buffer */
83  Py_TPFLAGS_DEFAULT, /* tp_flags */
84  "StrIterCat(filename)", /* tp_doc */
85  0, /* tp_traverse */
86  0, /* tp_clear */
87  0, /* tp_richcompare */
88  0, /* tp_weaklistoffset */
89  PyObject_SelfIter, /* tp_iter */
90  (iternextfunc)StrIter::Py_next<PyStrIterCat>, /* tp_iternext */
91  PyStrIterCat_methods, /* tp_methods */
92  0, /* tp_members */
93  0, /* tp_getset */
94  0, /* tp_base */
95  0, /* tp_dict */
96  0, /* tp_descr_get */
97  0, /* tp_descr_set */
98  0, /* tp_dictoffset */
99  0, /* tp_init */
100  PyType_GenericAlloc, /* tp_alloc */
101  PyStrIterCat_new, /* tp_new */
102  PyObject_Del, /* tp_free PyObject_Del */
103 };
104 
105 } // namespace k1a
Line iterator from file.
Definition: StrIterCat.h:25
std::string next()
Definition: StrIterCat.cpp:21
StrIterCat(PyObject *pyObj, std::string fileName)
Definition: StrIterCat.cpp:16
std::string * fileName
Definition: StrIterCat.h:27
std::ifstream * fp
Definition: StrIterCat.h:33
String iterator.
Definition: StrIter.h:79
bool done
Whether the iterator has any elements left.
Definition: StrIter.h:82
Definition: funcs.cpp:15
PyMethodDef PyStrIterCat_methods[]
Definition: StrIterCat.cpp:60
bool debug
Definition: utils.cpp:14
PyTypeObject PyStrIterCat_Type
Definition: StrIterCat.cpp:64
PyObject * PyStrIterCat_new(std::string fileName)
Creates a new PyStrIterCat object.
Definition: StrIterCat.cpp:37
PyObject * PyStrIterCat_conjugate(PyStrIterCat *self, PyObject *Py_UNUSED(ignored))
Definition: StrIterCat.cpp:56
void log_println(T s)
Definition: utils.h:21
StrIterCat * val
Definition: StrIterCat.h:38