PageRenderTime 71ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/types/regex.c

http://github.com/mongodb/mongo-php-driver
C | 112 lines | 60 code | 22 blank | 30 comment | 11 complexity | 53d3f4a8489b998fd7bef87aec6c3711 MD5 | raw file
Possible License(s): Apache-2.0
  1. /**
  2. * Copyright 2009-2014 MongoDB, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <php.h>
  17. #include <zend_exceptions.h>
  18. #include "../php_mongo.h"
  19. extern zend_class_entry *mongo_ce_Exception;
  20. zend_class_entry *mongo_ce_Regex = NULL;
  21. /* {{{ MongoRegex::__construct()
  22. */
  23. PHP_METHOD(MongoRegex, __construct)
  24. {
  25. zval *regex;
  26. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &regex) == FAILURE) {
  27. return;
  28. }
  29. if (Z_TYPE_P(regex) == IS_OBJECT && Z_OBJCE_P(regex) == mongo_ce_Regex) {
  30. zval *oregex, *oflags;
  31. oregex = zend_read_property(mongo_ce_Regex, regex, "regex", strlen("regex"), NOISY TSRMLS_CC);
  32. zend_update_property(mongo_ce_Regex, getThis(), "regex", strlen("regex"), oregex TSRMLS_CC);
  33. oflags = zend_read_property(mongo_ce_Regex, regex, "flags", strlen("flags"), NOISY TSRMLS_CC);
  34. zend_update_property(mongo_ce_Regex, getThis(), "flags", strlen("flags"), oflags TSRMLS_CC);
  35. } else if (Z_TYPE_P(regex) == IS_STRING) {
  36. int pattern_len, flags_len;
  37. char *re = Z_STRVAL_P(regex);
  38. char *eopattern = strrchr(re, '/');
  39. if (!eopattern) {
  40. zend_throw_exception(mongo_ce_Exception, "invalid regex", 9 TSRMLS_CC);
  41. return;
  42. }
  43. pattern_len = eopattern - re - 1;
  44. if (pattern_len < 0) {
  45. zend_throw_exception(mongo_ce_Exception, "invalid regex", 9 TSRMLS_CC);
  46. return;
  47. }
  48. /* move beyond the second '/' in /foo/bar */
  49. eopattern++;
  50. flags_len = Z_STRLEN_P(regex) - (eopattern - re);
  51. zend_update_property_stringl(mongo_ce_Regex, getThis(), "regex", strlen("regex"), re + 1, pattern_len TSRMLS_CC);
  52. zend_update_property_stringl(mongo_ce_Regex, getThis(), "flags", strlen("flags"), eopattern, flags_len TSRMLS_CC);
  53. }
  54. }
  55. /* }}} */
  56. /* {{{ MongoRegex::__toString()
  57. */
  58. PHP_METHOD(MongoRegex, __toString)
  59. {
  60. char *field_name;
  61. zval *zre = zend_read_property(mongo_ce_Regex, getThis(), "regex", strlen("regex"), NOISY TSRMLS_CC);
  62. zval *zopts = zend_read_property(mongo_ce_Regex, getThis(), "flags", strlen("flags"), NOISY TSRMLS_CC);
  63. char *re = Z_STRVAL_P(zre);
  64. char *opts = Z_STRVAL_P(zopts);
  65. spprintf(&field_name, 0, "/%s/%s", re, opts);
  66. RETVAL_STRING(field_name, 0);
  67. }
  68. /* }}} */
  69. static zend_function_entry MongoRegex_methods[] = {
  70. PHP_ME(MongoRegex, __construct, NULL, ZEND_ACC_PUBLIC)
  71. PHP_ME(MongoRegex, __toString, NULL, ZEND_ACC_PUBLIC)
  72. PHP_FE_END
  73. };
  74. void mongo_init_MongoRegex(TSRMLS_D)
  75. {
  76. zend_class_entry ce;
  77. INIT_CLASS_ENTRY(ce, "MongoRegex", MongoRegex_methods);
  78. ce.create_object = php_mongo_type_object_new;
  79. mongo_ce_Regex = zend_register_internal_class(&ce TSRMLS_CC);
  80. zend_declare_property_string(mongo_ce_Regex, "regex", strlen("regex"), "", ZEND_ACC_PUBLIC|MONGO_ACC_READ_ONLY TSRMLS_CC);
  81. zend_declare_property_string(mongo_ce_Regex, "flags", strlen("flags"), "", ZEND_ACC_PUBLIC|MONGO_ACC_READ_ONLY TSRMLS_CC);
  82. }
  83. /*
  84. * Local variables:
  85. * tab-width: 4
  86. * c-basic-offset: 4
  87. * End:
  88. * vim600: fdm=marker
  89. * vim: noet sw=4 ts=4
  90. */