rich_string.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 "xlswriter.h"
  13. zend_class_entry *vtiful_rich_string_ce;
  14. /* {{{ rich_string_objects_new
  15. */
  16. static zend_object_handlers rich_string_handlers;
  17. static zend_always_inline void *vtiful_rich_string_object_alloc(size_t obj_size, zend_class_entry *ce) {
  18. void *obj = emalloc(obj_size);
  19. memset(obj, 0, obj_size);
  20. return obj;
  21. }
  22. PHP_VTIFUL_API zend_object *rich_string_objects_new(zend_class_entry *ce)
  23. {
  24. rich_string_object *rich_string = vtiful_rich_string_object_alloc(sizeof(rich_string_object), ce);
  25. zend_object_std_init(&rich_string->zo, ce);
  26. object_properties_init(&rich_string->zo, ce);
  27. rich_string->ptr.tuple = NULL;
  28. rich_string->zo.handlers = &rich_string_handlers;
  29. return &rich_string->zo;
  30. }
  31. /* }}} */
  32. /* {{{ rich_string_objects_free
  33. */
  34. static void rich_string_objects_free(zend_object *object)
  35. {
  36. rich_string_object *intern = php_vtiful_rich_string_fetch_object(object);
  37. if (intern->ptr.tuple != NULL) {
  38. efree(intern->ptr.tuple);
  39. intern->ptr.tuple = NULL;
  40. }
  41. zend_object_std_dtor(&intern->zo);
  42. }
  43. /* }}} */
  44. /* {{{ ARG_INFO
  45. */
  46. ZEND_BEGIN_ARG_INFO_EX(rich_string_construct_arginfo, 0, 0, 1)
  47. ZEND_ARG_INFO(0, text)
  48. ZEND_ARG_INFO(0, format_handle)
  49. ZEND_END_ARG_INFO()
  50. /* }}} */
  51. /** {{{ \Vtiful\Kernel\RichString::__construct(string $text, resource $format)
  52. */
  53. PHP_METHOD(vtiful_rich_string, __construct)
  54. {
  55. zend_string *text = NULL;
  56. zval *format_handle = NULL;
  57. rich_string_object *obj = NULL;
  58. ZEND_PARSE_PARAMETERS_START(1, 2)
  59. Z_PARAM_STR(text)
  60. Z_PARAM_OPTIONAL
  61. Z_PARAM_RESOURCE_OR_NULL(format_handle)
  62. ZEND_PARSE_PARAMETERS_END();
  63. ZVAL_COPY(return_value, getThis());
  64. obj = Z_RICH_STR_P(getThis());
  65. if (obj->ptr.tuple != NULL) {
  66. return;
  67. }
  68. lxw_rich_string_tuple *instance = (lxw_rich_string_tuple *)ecalloc(1, sizeof(lxw_rich_string_tuple));
  69. zend_string *zstr = zend_string_copy(text);
  70. if (format_handle == NULL) {
  71. instance->format = NULL;
  72. instance->string = ZSTR_VAL(zstr);
  73. } else {
  74. instance->format = zval_get_format(format_handle);
  75. instance->string = ZSTR_VAL(zstr);
  76. }
  77. obj->ptr.tuple = instance;
  78. }
  79. /* }}} */
  80. /** {{{ rich_string_methods
  81. */
  82. zend_function_entry rich_string_methods[] = {
  83. PHP_ME(vtiful_rich_string, __construct, rich_string_construct_arginfo, ZEND_ACC_PUBLIC)
  84. PHP_FE_END
  85. };
  86. /* }}} */
  87. /** {{{ VTIFUL_STARTUP_FUNCTION
  88. */
  89. VTIFUL_STARTUP_FUNCTION(rich_string) {
  90. zend_class_entry ce;
  91. INIT_NS_CLASS_ENTRY(ce, "Vtiful\\Kernel", "RichString", rich_string_methods);
  92. ce.create_object = rich_string_objects_new;
  93. vtiful_rich_string_ce = zend_register_internal_class(&ce);
  94. memcpy(&rich_string_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  95. rich_string_handlers.offset = XtOffsetOf(rich_string_object, zo);
  96. rich_string_handlers.free_obj = rich_string_objects_free;
  97. return SUCCESS;
  98. }
  99. /* }}} */