Back to index

php5  5.3.10
com_olechar.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Author: Wez Furlong <wez@thebrainroom.com>                           |
00016    |         Harald Radi <h.radi@nme.at>                                  |
00017    +----------------------------------------------------------------------+
00018  */
00019 
00020 /* $Id: com_olechar.c 321634 2012-01-01 13:15:04Z felipe $ */
00021 
00022 #ifdef HAVE_CONFIG_H
00023 #include "config.h"
00024 #endif
00025 
00026 #include "php.h"
00027 #include "php_ini.h"
00028 #include "ext/standard/info.h"
00029 #include "php_com_dotnet.h"
00030 #include "php_com_dotnet_internal.h"
00031 
00032 
00033 PHPAPI OLECHAR *php_com_string_to_olestring(char *string, uint string_len, int codepage TSRMLS_DC)
00034 {
00035        OLECHAR *olestring = NULL;
00036        DWORD flags = codepage == CP_UTF8 ? 0 : MB_PRECOMPOSED | MB_ERR_INVALID_CHARS;
00037        BOOL ok;
00038 
00039        if (string_len == -1) {
00040               /* determine required length for the buffer (includes NUL terminator) */
00041               string_len = MultiByteToWideChar(codepage, flags, string, -1, NULL, 0);
00042        } else {
00043               /* allow room for NUL terminator */
00044               string_len++;
00045        }
00046 
00047        if (string_len > 0) {
00048               olestring = (OLECHAR*)safe_emalloc(string_len, sizeof(OLECHAR), 0);
00049               ok = MultiByteToWideChar(codepage, flags, string, string_len, olestring, string_len);
00050        } else {
00051               ok = FALSE;
00052               olestring = (OLECHAR*)emalloc(sizeof(OLECHAR));
00053               *olestring = 0;
00054        }
00055 
00056        if (!ok) {
00057               char *msg = php_win_err(GetLastError());
00058 
00059               php_error_docref(NULL TSRMLS_CC, E_WARNING,
00060                      "Could not convert string to unicode: `%s'", msg);
00061 
00062               LocalFree(msg);
00063        }
00064 
00065        return olestring;
00066 }
00067 
00068 PHPAPI char *php_com_olestring_to_string(OLECHAR *olestring, uint *string_len, int codepage TSRMLS_DC)
00069 {
00070        char *string;
00071        uint length = 0;
00072        BOOL ok;
00073 
00074        length = WideCharToMultiByte(codepage, 0, olestring, -1, NULL, 0, NULL, NULL);
00075 
00076        if (length) {
00077               string = (char*)safe_emalloc(length, sizeof(char), 0);
00078               length = WideCharToMultiByte(codepage, 0, olestring, -1, string, length, NULL, NULL);
00079               ok = length > 0;
00080        } else {
00081               string = (char*)emalloc(sizeof(char));
00082               *string = '\0';
00083               ok = FALSE;
00084               length = 0;
00085        }
00086 
00087        if (!ok) {
00088               char *msg = php_win_err(GetLastError());
00089 
00090               php_error_docref(NULL TSRMLS_CC, E_WARNING,
00091                      "Could not convert string from unicode: `%s'", msg);
00092 
00093               LocalFree(msg);
00094        }
00095 
00096        if (string_len) {
00097               *string_len = length-1;
00098        }
00099 
00100        return string;
00101 }