chart.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. +----------------------------------------------------------------------+
  3. | XlsWriter Extension |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2017-2018 The Viest |
  6. +----------------------------------------------------------------------+
  7. | http://www.viest.me |
  8. +----------------------------------------------------------------------+
  9. | Author: viest <[email protected]> |
  10. +----------------------------------------------------------------------+
  11. */
  12. #include "include.h"
  13. #include "chart.h"
  14. zend_class_entry *vtiful_chart_ce;
  15. /* {{{ format_objects_new
  16. */
  17. static zend_object_handlers chart_handlers;
  18. static zend_always_inline void *vtiful_char_object_alloc(size_t obj_size, zend_class_entry *ce) {
  19. void *obj = emalloc(obj_size);
  20. memset(obj, 0, obj_size);
  21. return obj;
  22. }
  23. PHP_VTIFUL_API zend_object *chart_objects_new(zend_class_entry *ce)
  24. {
  25. chart_object *format = (chart_object *)vtiful_char_object_alloc(sizeof(chart_object), ce);
  26. zend_object_std_init(&format->zo, ce);
  27. object_properties_init(&format->zo, ce);
  28. format->ptr.chart = NULL;
  29. format->ptr.series = NULL;
  30. format->zo.handlers = &chart_handlers;
  31. return &format->zo;
  32. }
  33. /* }}} */
  34. /* {{{ chart_objects_free
  35. */
  36. static void chart_objects_free(zend_object *object)
  37. {
  38. chart_object *intern = php_vtiful_chart_fetch_object(object);
  39. if (intern->ptr.series != NULL) {
  40. // free by workbook
  41. intern->ptr.series = NULL;
  42. }
  43. if (intern->ptr.chart != NULL) {
  44. // free by workbook
  45. intern->ptr.chart = NULL;
  46. }
  47. zend_object_std_dtor(&intern->zo);
  48. }
  49. /* }}} */
  50. /* {{{ ARG_INFO
  51. */
  52. ZEND_BEGIN_ARG_INFO_EX(chart_construct_arginfo, 0, 0, 2)
  53. ZEND_ARG_INFO(0, handle)
  54. ZEND_ARG_INFO(0, type)
  55. ZEND_END_ARG_INFO()
  56. ZEND_BEGIN_ARG_INFO_EX(chart_series_arginfo, 0, 0, 1)
  57. ZEND_ARG_INFO(0, value)
  58. ZEND_END_ARG_INFO()
  59. /* }}} */
  60. /** {{{ \Vtiful\Kernel\Chart::__construct(resource $handle, int $type)
  61. */
  62. PHP_METHOD(vtiful_chart, __construct)
  63. {
  64. zval *handle;
  65. zend_long type;
  66. chart_object *obj;
  67. xls_resource_t *xls_res;
  68. ZEND_PARSE_PARAMETERS_START(2, 2)
  69. Z_PARAM_RESOURCE(handle)
  70. Z_PARAM_LONG(type)
  71. ZEND_PARSE_PARAMETERS_END();
  72. ZVAL_COPY(return_value, getThis());
  73. xls_res = zval_get_resource(handle);
  74. obj = Z_CHART_P(getThis());
  75. if (obj->ptr.chart == NULL) {
  76. obj->ptr.chart = workbook_add_chart(xls_res->workbook, LXW_CHART_LINE);
  77. }
  78. }
  79. /* }}} */
  80. /** {{{ \Vtiful\Kernel\Chart::series()
  81. */
  82. PHP_METHOD(vtiful_chart, series)
  83. {
  84. chart_object *obj;
  85. zend_string *value;
  86. ZEND_PARSE_PARAMETERS_START(1, 1)
  87. Z_PARAM_STR(value)
  88. ZEND_PARSE_PARAMETERS_END();
  89. ZVAL_COPY(return_value, getThis());
  90. obj = Z_CHART_P(getThis());
  91. chart_add_series(obj->ptr.chart, NULL, ZSTR_VAL(value));
  92. }
  93. /* }}} */
  94. /** {{{ \Vtiful\Kernel\Chart::toResource()
  95. */
  96. PHP_METHOD(vtiful_chart, toResource)
  97. {
  98. chart_object *obj = Z_CHART_P(getThis());
  99. RETURN_RES(zend_register_resource(&obj->ptr, le_xls_writer));
  100. }
  101. /* }}} */
  102. /** {{{ chart_methods
  103. */
  104. zend_function_entry chart_methods[] = {
  105. PHP_ME(vtiful_chart, __construct, chart_construct_arginfo, ZEND_ACC_PUBLIC)
  106. PHP_ME(vtiful_chart, series, chart_series_arginfo, ZEND_ACC_PUBLIC)
  107. PHP_ME(vtiful_chart, toResource, NULL, ZEND_ACC_PUBLIC)
  108. PHP_FE_END
  109. };
  110. /* }}} */
  111. VTIFUL_STARTUP_FUNCTION(chart)
  112. {
  113. zend_class_entry ce;
  114. INIT_NS_CLASS_ENTRY(ce, "Vtiful\\Kernel", "Chart", chart_methods);
  115. ce.create_object = chart_objects_new;
  116. vtiful_chart_ce = zend_register_internal_class(&ce);
  117. memcpy(&chart_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  118. chart_handlers.offset = XtOffsetOf(chart_object, zo);
  119. chart_handlers.free_obj = chart_objects_free;
  120. REGISTER_CLASS_CONST_LONG(vtiful_chart_ce, "CHART_LINE", LXW_CHART_LINE)
  121. return SUCCESS;
  122. }