xref: /PHP-7.4/ext/intl/msgformat/msgformat_data.c (revision 92ac598a)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | http://www.php.net/license/3_01.txt                                  |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Authors: Stanislav Malyshev <stas@zend.com>                          |
14    +----------------------------------------------------------------------+
15  */
16 
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20 
21 #include <unicode/ustring.h>
22 #include "msgformat_data.h"
23 
24 #include "msgformat_class.h"
25 
26 /* {{{ void msgformat_data_init( msgformat_data* mf_data )
27  * Initialize internals of msgformat_data.
28  */
msgformat_data_init(msgformat_data * mf_data)29 void msgformat_data_init( msgformat_data* mf_data )
30 {
31 	if( !mf_data )
32 		return;
33 
34 	mf_data->umsgf			= NULL;
35 	mf_data->orig_format	= NULL;
36 	mf_data->arg_types		= NULL;
37 	mf_data->tz_set			= 0;
38 	intl_error_reset( &mf_data->error );
39 }
40 /* }}} */
41 
42 /* {{{ void msgformat_data_free( msgformat_data* mf_data )
43  * Clean up memory allocated for msgformat_data
44  */
msgformat_data_free(msgformat_data * mf_data)45 void msgformat_data_free(msgformat_data* mf_data)
46 {
47 	if (!mf_data)
48 		return;
49 
50 	if (mf_data->umsgf)
51 		umsg_close(mf_data->umsgf);
52 
53 	if (mf_data->orig_format) {
54 		efree(mf_data->orig_format);
55 		mf_data->orig_format = NULL;
56 	}
57 
58 	if (mf_data->arg_types) {
59 		zend_hash_destroy(mf_data->arg_types);
60 		efree(mf_data->arg_types);
61 		mf_data->arg_types = NULL;
62 	}
63 
64 	mf_data->umsgf = NULL;
65 	intl_error_reset(&mf_data->error);
66 }
67 /* }}} */
68 
69 /* {{{ msgformat_data* msgformat_data_create()
70  * Allocate memory for msgformat_data and initialize it with default values.
71  */
msgformat_data_create(void)72 msgformat_data* msgformat_data_create( void )
73 {
74 	msgformat_data* mf_data = ecalloc( 1, sizeof(msgformat_data) );
75 
76 	msgformat_data_init( mf_data );
77 
78 	return mf_data;
79 }
80 /* }}} */
81 
82 #ifdef MSG_FORMAT_QUOTE_APOS
msgformat_fix_quotes(UChar ** spattern,uint32_t * spattern_len,UErrorCode * ec)83 int msgformat_fix_quotes(UChar **spattern, uint32_t *spattern_len, UErrorCode *ec)
84 {
85 	if(*spattern && *spattern_len && u_strchr(*spattern, (UChar)'\'')) {
86 		UChar *npattern = safe_emalloc(sizeof(UChar)*2, *spattern_len, sizeof(UChar));
87 		uint32_t npattern_len;
88 		npattern_len = umsg_autoQuoteApostrophe(*spattern, *spattern_len, npattern, 2*(*spattern_len)+1, ec);
89 		efree(*spattern);
90 		if( U_FAILURE(*ec) )
91 		{
92 			return FAILURE;
93 		}
94 		npattern = erealloc(npattern, sizeof(UChar)*(npattern_len+1));
95 		*spattern = npattern;
96 		*spattern_len = npattern_len;
97 	}
98 	return SUCCESS;
99 }
100 #endif
101