1 /* EINA - EFL data type library
2  * Copyright (C) 2002-2008 Carsten Haitzler,
3  *                         Jorge Luis Zapata Muga,
4  *                         Cedric Bail,
5  *                         Gustavo Sverzut Barbieri
6  *                         Tom Hacohen
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library;
20  * if not, see <http://www.gnu.org/licenses/>.
21 
22  */
23 /**
24  * @page tutorial_ustringshare_page UStringshare Tutorial
25  *
26  * to be written...
27  *
28  */
29 
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33 
34 #include "eina_config.h"
35 #include "eina_private.h"
36 #include "eina_unicode.h"
37 #include "eina_log.h"
38 #include "eina_share_common.h"
39 
40 /* undefs EINA_ARG_NONULL() so NULL checks are not compiled out! */
41 #include "eina_safety_checks.h"
42 #include "eina_ustringshare.h"
43 
44 
45 #ifdef CRI
46 #undef CRI
47 #endif
48 #define CRI(...) EINA_LOG_DOM_CRIT(_eina_share_ustringshare_log_dom, __VA_ARGS__)
49 
50 #ifdef ERR
51 #undef ERR
52 #endif
53 #define ERR(...) EINA_LOG_DOM_ERR(_eina_share_ustringshare_log_dom, __VA_ARGS__)
54 
55 #ifdef DBG
56 #undef DBG
57 #endif
58 #define DBG(...) EINA_LOG_DOM_DBG(_eina_share_ustringshare_log_dom, __VA_ARGS__)
59 
60 static int _eina_share_ustringshare_log_dom = -1;
61 
62 /* The actual share */
63 static Eina_Share *ustringshare_share;
64 static const char EINA_MAGIC_USTRINGSHARE_NODE_STR[] = "Eina UStringshare Node";
65 
66 /*============================================================================*
67 *                                 Global                                     *
68 *============================================================================*/
69 
70 /**
71  * @internal
72  * @brief Initialize the share_common module.
73  *
74  * @return #EINA_TRUE on success, #EINA_FALSE on failure.
75  *
76  * This function sets up the share_common module of Eina. It is called by
77  * eina_init().
78  *
79  * @see eina_init()
80  */
81 Eina_Bool
eina_ustringshare_init(void)82 eina_ustringshare_init(void)
83 {
84    Eina_Bool ret;
85 
86    if (_eina_share_ustringshare_log_dom < 0)
87      {
88         _eina_share_ustringshare_log_dom = eina_log_domain_register
89           ("eina_ustringshare", EINA_LOG_COLOR_DEFAULT);
90 
91         if (_eina_share_ustringshare_log_dom < 0)
92           {
93              EINA_LOG_ERR("Could not register log domain: eina_ustringshare");
94              return EINA_FALSE;
95           }
96      }
97 
98    ret = eina_share_common_init(&ustringshare_share,
99                                 EINA_MAGIC_USTRINGSHARE_NODE,
100                                 EINA_MAGIC_USTRINGSHARE_NODE_STR);
101 
102    if (!ret)
103      {
104         eina_log_domain_unregister(_eina_share_ustringshare_log_dom);
105         _eina_share_ustringshare_log_dom = -1;
106      }
107 
108    return ret;
109 }
110 
111 /**
112  * @internal
113  * @brief Shut down the share_common module.
114  *
115  * @return #EINA_TRUE on success, #EINA_FALSE on failure.
116  *
117  * This function shuts down the share_common module set up by
118  * eina_share_common_init(). It is called by eina_shutdown().
119  *
120  * @see eina_shutdown()
121  */
122 Eina_Bool
eina_ustringshare_shutdown(void)123 eina_ustringshare_shutdown(void)
124 {
125    Eina_Bool ret;
126    ret = eina_share_common_shutdown(&ustringshare_share);
127 
128    if (_eina_share_ustringshare_log_dom >= 0)
129      {
130         eina_log_domain_unregister(_eina_share_ustringshare_log_dom);
131         _eina_share_ustringshare_log_dom = -1;
132      }
133 
134    return ret;
135 }
136 
137 /*============================================================================*
138 *                                   API                                      *
139 *============================================================================*/
140 
141 EAPI void
eina_ustringshare_del(const Eina_Unicode * str)142 eina_ustringshare_del(const Eina_Unicode *str)
143 {
144    if (!str)
145       return;
146 
147    if (!eina_share_common_del(ustringshare_share, (const char *)str))
148      CRI("EEEK trying to del non-shared ustringshare \"%s\"", (const char *)str);
149 }
150 
151 EAPI const Eina_Unicode *
eina_ustringshare_add_length(const Eina_Unicode * str,unsigned int slen)152 eina_ustringshare_add_length(const Eina_Unicode *str, unsigned int slen)
153 {
154    return (const Eina_Unicode *)eina_share_common_add_length(ustringshare_share,
155                                                              (const char *)str,
156                                                              slen *
157                                                              sizeof(
158                                                                 Eina_Unicode),
159                                                              sizeof(
160                                                                 Eina_Unicode));
161 }
162 
163 EAPI const Eina_Unicode *
eina_ustringshare_add(const Eina_Unicode * str)164 eina_ustringshare_add(const Eina_Unicode *str)
165 {
166    int slen = (str) ? (int)eina_unicode_strlen(str) : -1;
167    return eina_ustringshare_add_length(str, slen);
168 }
169 
170 EAPI const Eina_Unicode *
eina_ustringshare_ref(const Eina_Unicode * str)171 eina_ustringshare_ref(const Eina_Unicode *str)
172 {
173    return (const Eina_Unicode *)eina_share_common_ref(ustringshare_share,
174                                                       (const char *)str);
175 }
176 
177 EAPI int
eina_ustringshare_strlen(const Eina_Unicode * str)178 eina_ustringshare_strlen(const Eina_Unicode *str)
179 {
180    int len = eina_share_common_length(ustringshare_share, (const char *)str);
181    len = (len > 0) ? len / (int)sizeof(Eina_Unicode) : -1;
182    return len;
183 }
184 
185 EAPI void
eina_ustringshare_dump(void)186 eina_ustringshare_dump(void)
187 {
188    eina_share_common_dump(ustringshare_share, NULL, 0);
189 }
190