[PHP-DEV] CVS update: php3/functions From: ssb (php-dev <email protected>)
Date: 10/27/98

Date: Tuesday October 27, 1998 @ 17:42
Author: ssb

Update of /repository/php3/functions
In directory asf:/u2/tmp/cvs-serv12138

Modified Files:
        xml.c
Log Message:
Added UTF-8 output decoding.

Index: php3/functions/xml.c
diff -c php3/functions/xml.c:1.11 php3/functions/xml.c:1.12
*** php3/functions/xml.c:1.11 Tue Oct 27 14:56:41 1998
--- php3/functions/xml.c Tue Oct 27 17:42:53 1998
***************
*** 27,33 ****
     +----------------------------------------------------------------------+
   */
  
! /* $Id: xml.c,v 1.11 1998/10/27 19:56:41 ssb Exp $ */
  #define IS_EXT_MODULE
  #if COMPILE_DL
  # if PHP_31
--- 27,33 ----
     +----------------------------------------------------------------------+
   */
  
! /* $Id: xml.c,v 1.12 1998/10/27 22:42:53 ssb Exp $ */
  #define IS_EXT_MODULE
  #if COMPILE_DL
  # if PHP_31
***************
*** 56,69 ****
  # include "php3_string.h"
  
  /* Short-term TODO list:
- * - Fix the expat library so you can install your own memory manager
- * functions
   * - Implement XML_ExternalEntityParserCreate()
   */
  
  /* Long-term TODO list:
! * - Investigate real Unicode support
! * - Implement XML_SetUnknownEncodingHandler()
   */
  
  /* Known bugs:
--- 56,68 ----
  # include "php3_string.h"
  
  /* Short-term TODO list:
   * - Implement XML_ExternalEntityParserCreate()
   */
  
  /* Long-term TODO list:
! * - Support other character sets than ISO-8859-1
! * - Fix the expat library so you can install your own memory manager
! * functions
   */
  
  /* Known bugs:
***************
*** 112,117 ****
--- 111,117 ----
  static char *php3i_pvalstrcpy(pval *);
  static void xml_destroy_parser(xml_parser *);
  static void xml_set_handler(char **, pval *);
+ static char *xml_utf8_decode(const XML_Char *, int, int *);
  static pval *xml_call_handler(xml_parser *, char *, int, pval **);
  static pval *php3i_longpval(long);
  static pval *php3i_stringpval(const char *);
***************
*** 382,387 ****
--- 382,432 ----
  }
  
  /* }}} */
+ /* {{{ xml_utf8_decode */
+ static char *
+ xml_utf8_decode(const XML_Char *s, int len, int *newlen)
+ {
+ int c, pos = len;
+ char *newbuf = emalloc(len);
+
+ *newlen = 0;
+
+ while (pos > 0) {
+ #ifdef XML_UNICODE /* defined if we are using multi-byte characters */
+ c = (*s);
+ len--;
+ s++;
+ #else
+ c = (unsigned char)(*s);
+ if (c >= 0xf0) { /* four bytes encoded, 21 bits */
+ c = ((s[0]&7)<<18) | ((s[1]&63)<<12) | ((s[2]&63)<<6) | (s[3]&63);
+ s += 4;
+ pos -= 4;
+ } else if (c >= 0xe0) { /* three bytes encoded, 16 bits */
+ c = ((s[0]&63)<<12) | ((s[1]&63)<<6) | (s[2]&63);
+ s += 3;
+ pos -= 3;
+ } else if (c >= 0xc0) { /* two bytes encoded, 11 bits */
+ c = ((s[0]&63)<<6) | (s[1]&63);
+ s += 2;
+ pos -= 2;
+ } else {
+ s++;
+ pos--;
+ }
+ #endif
+ if (c > 0xff) {
+ c = '?';
+ }
+ newbuf[*newlen] = c;
+ ++*newlen;
+ }
+ if (*newlen < len) {
+ newbuf = erealloc(newbuf, *newlen);
+ }
+ return newbuf;
+ }
+ /* }}} */
      /* {{{ php3i_longpval() */
  
  static pval *php3i_longpval(long value)
***************
*** 421,446 ****
          if (len == 0) {
                  len = php3i_xmlcharlen(s);
          }
! if (sizeof(XML_Char) == 1) {
! ret->type = IS_STRING;
! ret->value.str.len = len;
! ret->value.str.val = emalloc(len);
! memcpy(ret->value.str.val, s, len);
! } else {
! /* XXX this is still not tested! */
! int pos = 0, added, i;
! char * buf = emalloc(len * 4); /* the maximum UTF-8 encoded size */
!
! for (i = 0; i < len; i++) {
! added = XmlUtf8Encode((int)s[i], &buf[pos]);
! pos += added;
! }
! buf = erealloc(buf, pos);
! ret = emalloc(sizeof(pval));
! ret->type = IS_STRING;
! ret->value.str.len = pos;
! ret->value.str.val = buf;
! }
          return ret;
  }
  
--- 466,473 ----
          if (len == 0) {
                  len = php3i_xmlcharlen(s);
          }
! ret->type = IS_STRING;
! ret->value.str.val = xml_utf8_decode(s, len, &ret->value.str.len);
          return ret;
  }
  

--
PHP Development Mailing List   http://www.php.net/
To unsubscribe send an empty message to php-dev-unsubscribe <email protected>
For help: php-dev-help <email protected>