Permalink
Browse files

xstring functions

  • Loading branch information...
1 parent bb6d3ea commit 33471aac8dcc7a97013cd7a50dbd452c7dc96d58 @c9s committed Mar 20, 2013
Showing with 145 additions and 108 deletions.
  1. +1 −1 ext/config.m4
  2. +1 −107 ext/php_sqlbuilder.c
  3. +116 −0 ext/xstring.c
  4. +27 −0 ext/xstring.h
View
@@ -7,5 +7,5 @@ if test $PHP_SQLBUILDER != "no"; then
PHP_REQUIRE_CXX()
PHP_SUBST(SQLBUILDER_SHARED_LIBADD)
PHP_ADD_LIBRARY(stdc++, 1, SQLBUILDER_SHARED_LIBADD)
- PHP_NEW_EXTENSION(sqlbuilder, php_sqlbuilder.c, $ext_shared)
+ PHP_NEW_EXTENSION(sqlbuilder, php_sqlbuilder.c xstring.c, $ext_shared)
fi
View
@@ -1,6 +1,7 @@
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
+#include "xstring.h"
#include "php.h"
#include "php_sqlbuilder.h"
@@ -56,113 +57,6 @@ PHP_MINIT_FUNCTION(sqlbuilder_driver)
return SUCCESS;
}
-typedef struct {
- char *str;
- int len;
- int cap;
-} xstring;
-
-
-
-PHPAPI xstring * xstring_alloc();
-PHPAPI xstring * xstring_new(int cap);
-PHPAPI xstring * xstring_new_from_string(char * str, int len);
-PHPAPI void xstring_free(xstring *xstr);
-PHPAPI void xstring_scale(xstring *xstr, int size);
-PHPAPI void xstring_scale_large(xstring *xstr, int size);
-
-
-/* allocate an empty xstring structure */
-PHPAPI xstring * xstring_alloc()
-{
- return emalloc( sizeof(xstring) );
-}
-
-
-PHPAPI xstring * xstring_new(int cap)
-{
- xstring * x = xstring_alloc();
- x->cap = cap;
- x->str = emalloc(sizeof(char) * cap);
- x->len = 0;
- return x;
-}
-
-
-/* create an xstring from exsiting string */
-PHPAPI xstring * xstring_new_from_string(char * str, int len)
-{
- xstring * xstr;
- xstr = xstring_alloc();
- xstr->str = str;
- xstr->len = len;
- xstr->cap = len;
- return xstr;
-}
-
-
-PHPAPI void xstring_concat_string(xstring * xstr, char * str, int len)
-{
- if ( xstr->len + len > xstr->cap ) {
- // do realloc
- xstring_scale(xstr, len);
- }
- memcpy(xstr->str + xstr->len, str, len);
- xstr->len += len;
- memcpy(xstr->str + xstr->len, "\0", 1);
-}
-
-
-PHPAPI void xstring_realloc(xstring *xstr, int size)
-{
- xstr->cap += size;
- xstr->str = realloc( xstr->str , sizeof(char) * xstr->cap );
-}
-
-
-PHPAPI void xstring_scale(xstring *xstr, int size)
-{
- xstr->cap += size + 256;
- xstr->str = realloc( xstr->str , sizeof(char) * xstr->cap );
-}
-
-PHPAPI void xstring_scale_large(xstring *xstr, int size)
-{
- xstr->cap += size + 512;
- xstr->str = realloc( xstr->str , sizeof(char) * xstr->cap );
-}
-
-
-
-PHPAPI xstring * xstring_quote_string(xstring * xstr, char * quote , int quote_len)
-{
- if ( xstr->len + quote_len > xstr->cap ) {
- xstring_realloc(xstr, quote_len);
- }
-
- xstring *xnewstr;
-
- if ( xstr->len + quote_len > xstr->cap ) {
- // create a new empty string with larger capacity
- xnewstr = xstring_new( xstr->cap + quote_len );
- } else {
- xnewstr = xstring_new( xstr->cap );
- }
-
- xnewstr->len = xstr->len + quote_len;
- memcpy(xnewstr, quote, quote_len);
- memcpy(xnewstr + quote_len, xstr->str, xstr->len);
- memcpy(xnewstr + quote_len + xstr->len, quote, quote_len);
- return xnewstr;
-}
-
-PHPAPI void xstring_free(xstring *xstr)
-{
- // free up the string
- efree(xstr->str);
- // free up the structure itself
- efree(xstr);
-}
View
@@ -0,0 +1,116 @@
+#include "xstring.h"
+#include <string.h>
+
+/* allocate an empty xstring structure */
+PHPAPI xstring * xstring_alloc()
+{
+ return emalloc( sizeof(xstring) );
+}
+
+
+PHPAPI xstring * xstring_new(int cap)
+{
+ xstring * x = xstring_alloc();
+ x->cap = cap;
+ x->str = emalloc(sizeof(char) * cap);
+ x->len = 0;
+ return x;
+}
+
+
+/* create an xstring from exsiting string */
+PHPAPI xstring * xstring_new_from_stringl(char * str, int len)
+{
+ xstring * xstr;
+ xstr = xstring_alloc();
+ xstr->str = str;
+ xstr->len = len;
+ xstr->cap = len;
+ return xstr;
+}
+
+PHPAPI xstring * xstring_new_from_string(char * str)
+{
+ return xstring_new_from_stringl(str, strlen(str));
+}
+
+
+PHPAPI void xstring_concat_string(xstring * xstr, char * str, int len)
+{
+ if ( xstr->len + len > xstr->cap ) {
+ // do realloc
+ xstring_scale(xstr, len);
+ }
+ memcpy(xstr->str + xstr->len, str, len);
+ xstr->len += len;
+ memcpy(xstr->str + xstr->len, "\0", 1);
+}
+
+
+PHPAPI void xstring_realloc(xstring *xstr, int size)
+{
+ xstr->cap += size;
+ xstr->str = realloc( xstr->str , sizeof(char) * xstr->cap );
+}
+
+
+PHPAPI void xstring_scale(xstring *xstr, int size)
+{
+ xstr->cap += size + 256;
+ xstr->str = realloc( xstr->str , sizeof(char) * xstr->cap );
+}
+
+PHPAPI void xstring_scale_large(xstring *xstr, int size)
+{
+ xstr->cap += size + 512;
+ xstr->str = realloc( xstr->str , sizeof(char) * xstr->cap );
+}
+
+
+
+PHPAPI xstring * xstring_quote_string(xstring * xstr, char * quote , int quote_len)
+{
+ if ( xstr->len + quote_len > xstr->cap ) {
+ xstring_realloc(xstr, quote_len);
+ }
+
+ xstring *xnewstr;
+
+ if ( xstr->len + quote_len > xstr->cap ) {
+ // create a new empty string with larger capacity
+ xnewstr = xstring_new( xstr->cap + quote_len );
+ } else {
+ xnewstr = xstring_new( xstr->cap );
+ }
+
+ xnewstr->len = xstr->len + quote_len;
+ memcpy(xnewstr, quote, quote_len);
+ memcpy(xnewstr + quote_len, xstr->str, xstr->len);
+ memcpy(xnewstr + quote_len + xstr->len, quote, quote_len);
+ return xnewstr;
+}
+
+PHPAPI void xstring_free(xstring *xstr)
+{
+ // free up the string
+ efree(xstr->str);
+ // free up the structure itself
+ efree(xstr);
+}
+
+
+
+// Zend related methods
+PHPAPI zval * xstring_to_zval(xstring *xstr)
+{
+ zval *zstr;
+ MAKE_STD_ZVAL(zstr);
+
+ zstr->type = IS_STRING;
+ Z_STRVAL_P(zstr) = xstr->str;
+ Z_STRLEN_P(zstr) = xstr->len;
+ return zstr;
+}
+
+
+
View
@@ -0,0 +1,27 @@
+#ifndef XSTRING_H
+#define XSTRING_H
+#include "php.h"
+
+typedef struct {
+ char *str;
+ int len;
+ int cap;
+} xstring;
+
+PHPAPI xstring * xstring_alloc();
+PHPAPI xstring * xstring_new(int cap);
+PHPAPI xstring * xstring_new_from_stringl(char * str, int len);
+PHPAPI xstring * xstring_new_from_string(char * str);
+
+
+PHPAPI void xstring_free(xstring *xstr);
+
+PHPAPI void xstring_realloc(xstring *xstr, int size);
+PHPAPI void xstring_scale(xstring *xstr, int size);
+PHPAPI void xstring_scale_large(xstring *xstr, int size);
+
+PHPAPI xstring * xstring_quote_string(xstring * xstr, char * quote , int quote_len);
+
+PHPAPI zval * xstring_to_zval(xstring *xstr);
+
+#endif

0 comments on commit 33471aa

Please sign in to comment.