common.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. /* {{{ */
  14. void xls_file_path(zend_string *file_name, zval *dir_path, zval *file_path)
  15. {
  16. zend_string *full_path, *zstr_path;
  17. zstr_path = zval_get_string(dir_path);
  18. if (Z_STRVAL_P(dir_path)[Z_STRLEN_P(dir_path)-1] == '/') {
  19. full_path = zend_string_extend(zstr_path, ZSTR_LEN(zstr_path) + ZSTR_LEN(file_name), 0);
  20. memcpy(ZSTR_VAL(full_path)+ZSTR_LEN(zstr_path), ZSTR_VAL(file_name), ZSTR_LEN(file_name)+1);
  21. } else {
  22. full_path = zend_string_extend(zstr_path, ZSTR_LEN(zstr_path) + ZSTR_LEN(file_name) + 1, 0);
  23. ZSTR_VAL(full_path)[ZSTR_LEN(zstr_path)] ='/';
  24. memcpy(ZSTR_VAL(full_path)+ZSTR_LEN(zstr_path)+1, ZSTR_VAL(file_name), ZSTR_LEN(file_name)+1);
  25. }
  26. ZVAL_STR(file_path, full_path);
  27. }
  28. /* }}} */
  29. /* {{{ */
  30. zend_string* str_pick_up(zend_string *left, const char *right, size_t len)
  31. {
  32. zend_string *full = NULL;
  33. size_t _left_length = ZSTR_LEN(left);
  34. size_t _extend_length = _left_length + len;
  35. full = zend_string_extend(left, _extend_length, 0);
  36. memcpy(ZSTR_VAL(full) + _left_length, right, len);
  37. ZSTR_VAL(full)[_extend_length] = '\0';
  38. return full;
  39. }
  40. /* }}} */
  41. /* {{{ */
  42. zend_string* char_join_to_zend_str(const char *left, const char *right)
  43. {
  44. size_t _new_len = strlen(left) + strlen(right);
  45. zend_string *str = zend_string_alloc(_new_len, 0);
  46. memcpy(ZSTR_VAL(str), left, strlen(left));
  47. memcpy(ZSTR_VAL(str) + strlen(left), right, strlen(right) + 1);
  48. ZSTR_VAL(str)[_new_len] = '\0';
  49. return str;
  50. }
  51. /* }}} */
  52. /* {{{ */
  53. void call_object_method(zval *object, const char *function_name, uint32_t param_count, zval *params, zval *ret_val)
  54. {
  55. uint32_t index;
  56. zval z_f_name;
  57. ZVAL_STRINGL(&z_f_name, function_name, strlen(function_name));
  58. call_user_function(NULL, object, &z_f_name, ret_val, param_count, params);
  59. if (Z_ISUNDEF_P(ret_val)) {
  60. ZVAL_NULL(ret_val);
  61. }
  62. for (index = 0; index < param_count; index++) {
  63. zval_ptr_dtor(&params[index]);
  64. }
  65. zval_ptr_dtor(&z_f_name);
  66. }
  67. /* }}} */
  68. lxw_datetime timestamp_to_datetime(zend_long timestamp)
  69. {
  70. int yearLocal = php_idate('Y', timestamp, 0);
  71. int monthLocal = php_idate('m', timestamp, 0);
  72. int dayLocal = php_idate('d', timestamp, 0);
  73. int hourLocal = php_idate('H', timestamp, 0);
  74. int minuteLocal = php_idate('i', timestamp, 0);
  75. int secondLocal = php_idate('s', timestamp, 0);
  76. lxw_datetime datetime = {
  77. yearLocal, monthLocal, dayLocal, hourLocal, minuteLocal, secondLocal
  78. };
  79. return datetime;
  80. }