Guest User

main.cpp

a guest
Mar 26th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. #include <list>
  2. #include <string>
  3. #include <iostream>
  4. #include "3rdparty/pybind11/embed.h"
  5.  
  6. namespace py = pybind11;
  7. typedef std::list<std::list<std::string> > SheetData;
  8.  
  9.  
  10. SheetData CastToSheetData(PyObject *obj)
  11. {
  12.     SheetData data;
  13.     PyObject *iter = PyObject_GetIter(obj);
  14.  
  15.     if (!iter)
  16.         return data;
  17.     while (true) {
  18.         std::list<std::string> aux_list;
  19.         PyObject *next = PyIter_Next(iter);
  20.         if (!next) {
  21.             // nothing left in the iterator
  22.             break;
  23.         }
  24.         PyObject *iter2 = PyObject_GetIter(next);
  25.         if (!iter2)
  26.             continue;
  27.         while(true) {
  28.             PyObject *next2 = PyIter_Next(iter2);
  29.             if (!next2) {
  30.                 // nothing left in the iterator
  31.                 break;
  32.             }
  33.             PyObject* pyStrObj = PyUnicode_AsUTF8String(next2);
  34.             char* zStr = PyBytes_AsString(pyStrObj);
  35.             std::string foo(strdup(zStr));
  36.             aux_list.push_back(foo);
  37.             Py_DECREF(pyStrObj);
  38.         }
  39.         data.push_back(aux_list);
  40.     }
  41.  
  42.     return data;
  43. }
  44.  
  45.  
  46. int main(int argc,char * argv[])
  47. {
  48.     py::scoped_interpreter guard{};
  49.  
  50.     py::module _module = py::module::import("xlanalyser");
  51.     py::object _tool = _module.attr("XlAnalyser")("sample.xls","E:/Computer Science/Python/Programs/Resources");
  52.  
  53.     SheetData data;
  54.     py::object dataTable = _tool.attr("return_sheet")();
  55.     data = CastToSheetData(dataTable.ptr());
  56.  
  57.     for(auto table:data)
  58.     {
  59.         for(auto list:table)
  60.         {
  61.             std::cout << list << "\t";
  62.         }
  63.         std::cout << std::endl;
  64.     }
  65.  
  66.     return 0;
  67. }
Add Comment
Please, Sign In to add comment