summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarsten Haitzler <raster@rasterman.com>2012-10-17 10:24:03 +0000
committerCarsten Haitzler <raster@rasterman.com>2012-10-17 10:24:03 +0000
commitafdd3f4a398489568a08b50b0b004b8ff810d2d4 (patch)
tree591c1f6c03730a4ef1c74173f59bbd5b910c113c
parentb8297e68236830adc0704ac0551db86dd02305ea (diff)
add tmpstr to efl eina tree... this is for the future so not going
into eina tree. SVN revision: 78112
-rw-r--r--ChangeLog1
-rw-r--r--NEWS1
-rw-r--r--src/lib/eina/Makefile.am6
-rw-r--r--src/lib/eina/eina_main.c2
-rw-r--r--src/lib/eina/eina_tmpstr.c106
-rw-r--r--src/lib/eina/eina_tmpstr.h190
6 files changed, 304 insertions, 2 deletions
diff --git a/ChangeLog b/ChangeLog
index 34cdb8b60a..282140a37f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -43,3 +43,4 @@
432012-10-17 Carsten Haitzler (The Rasterman) 432012-10-17 Carsten Haitzler (The Rasterman)
44 44
45 * Add eina_barrier API to cover pthread_barrier functionality 45 * Add eina_barrier API to cover pthread_barrier functionality
46 * Add eina_tmpstr_add() and eina_tmpstr_del().
diff --git a/NEWS b/NEWS
index 3abbe99475..f976ac0f53 100644
--- a/NEWS
+++ b/NEWS
@@ -8,6 +8,7 @@ Changes since 1.7.0:
8Additions: 8Additions:
9 * Add DOCTYPE children parsing in eina_simple_xml 9 * Add DOCTYPE children parsing in eina_simple_xml
10 * Add eina_barrier thread API 10 * Add eina_barrier thread API
11 * Add eina_tmpstr_add() and eina_tmpstr_del()
11 12
12Improvements: 13Improvements:
13 * Single EFL tree now covring all EFL library components. 14 * Single EFL tree now covring all EFL library components.
diff --git a/src/lib/eina/Makefile.am b/src/lib/eina/Makefile.am
index 287b29aa8c..d290892206 100644
--- a/src/lib/eina/Makefile.am
+++ b/src/lib/eina/Makefile.am
@@ -82,7 +82,8 @@ eina_mmap.h \
82eina_xattr.h \ 82eina_xattr.h \
83eina_value.h \ 83eina_value.h \
84eina_inline_value.x \ 84eina_inline_value.x \
85eina_inline_lock_barrier.x 85eina_inline_lock_barrier.x \
86eina_tmpstr.h
86 87
87# Will be back for developper after 1.2. 88# Will be back for developper after 1.2.
88# eina_model.h 89# eina_model.h
@@ -151,7 +152,8 @@ eina_value.c \
151eina_xattr.c \ 152eina_xattr.c \
152eina_share_common.h \ 153eina_share_common.h \
153eina_private.h \ 154eina_private.h \
154eina_strbuf_common.h 155eina_strbuf_common.h \
156eina_tmpstr.c
155 157
156# Will be back for developper after 1.2 158# Will be back for developper after 1.2
157# eina_model.c \ 159# eina_model.c \
diff --git a/src/lib/eina/eina_main.c b/src/lib/eina/eina_main.c
index e6236b91f6..fb4241eb1b 100644
--- a/src/lib/eina/eina_main.c
+++ b/src/lib/eina/eina_main.c
@@ -156,6 +156,7 @@ EAPI Eina_Inlist *_eina_tracking = NULL;
156 S(file); 156 S(file);
157 S(prefix); 157 S(prefix);
158 S(value); 158 S(value);
159 S(tmpstr);
159/* no model for now 160/* no model for now
160 S(model); 161 S(model);
161 */ 162 */
@@ -196,6 +197,7 @@ static const struct eina_desc_setup _eina_desc_setup[] = {
196 S(file), 197 S(file),
197 S(prefix), 198 S(prefix),
198 S(value), 199 S(value),
200 S(tmpstr),
199/* no model for now 201/* no model for now
200 S(model) 202 S(model)
201 */ 203 */
diff --git a/src/lib/eina/eina_tmpstr.c b/src/lib/eina/eina_tmpstr.c
new file mode 100644
index 0000000000..849d6c618d
--- /dev/null
+++ b/src/lib/eina/eina_tmpstr.c
@@ -0,0 +1,106 @@
1/* EINA - EFL data type library
2 * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2010
3 * Carsten Haitzler,
4 * Jorge Luis Zapata Muga,
5 * Cedric Bail,
6 * Gustavo Sverzut Barbieri
7 * Tom Hacohen
8 * Brett Nash
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library;
22 * if not, see <http://www.gnu.org/licenses/>.
23 */
24
25#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
28
29#ifdef HAVE_EVIL
30# include <Evil.h>
31#endif
32
33#include "eina_config.h"
34#include "eina_private.h"
35#include "eina_error.h"
36#include "eina_log.h"
37#include "eina_lock.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_tmpstr.h"
43
44typedef struct _Str Str;
45
46struct _Str
47{
48 Str *next;
49 char *str;
50};
51
52static Eina_Lock _mutex;
53static Str *strs = NULL;
54
55Eina_Bool
56eina_tmpstr_init(void)
57{
58 if (!eina_lock_new(&_mutex)) return EINA_FALSE;
59 return EINA_TRUE;
60}
61
62Eina_Bool
63eina_tmpstr_shutdown(void)
64{
65 eina_lock_free(&_mutex);
66 return EINA_TRUE;
67}
68
69EAPI Eina_Tmpstr *
70eina_tmpstr_add(const char *str)
71{
72 Str *s;
73 int len;
74
75 if (!str) return NULL;
76 len = strlen(str);
77 s = malloc(sizeof(Str) + len + 1);
78 if (!s) return NULL;
79 s->str = ((char *)s) + sizeof(Str);
80 strcpy(s->str, str);
81 eina_lock_take(&_mutex);
82 s->next = strs;
83 strs = s;
84 eina_lock_release(&_mutex);
85 return s->str;
86}
87
88EAPI void
89eina_tmpstr_del(Eina_Tmpstr *tmpstr)
90{
91 Str *s, *sp;
92
93 if ((!strs) || (!tmpstr)) return;
94 eina_lock_take(&_mutex);
95 for (sp = NULL, s = strs; s; sp = s, s = s->next)
96 {
97 if (s->str == tmpstr)
98 {
99 if (sp) sp->next = s->next;
100 else strs = s->next;
101 free(s);
102 break;
103 }
104 }
105 eina_lock_release(&_mutex);
106}
diff --git a/src/lib/eina/eina_tmpstr.h b/src/lib/eina/eina_tmpstr.h
new file mode 100644
index 0000000000..aab682dd8f
--- /dev/null
+++ b/src/lib/eina/eina_tmpstr.h
@@ -0,0 +1,190 @@
1/* EINA - EFL data type library
2 * Copyright (C) 2002-2012 Carsten Haitzler, Jorge Luis Zapata Muga, Cedric Bail
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library;
16 * if not, see <http://www.gnu.org/licenses/>.
17 *
18 * This file incorporates work covered by the following copyright and
19 * permission notice:
20 *
21 * Copyright (C) 2008 Peter Wehrfritz
22 *
23 * Permission is hereby granted, free of charge, to any person obtaining a copy
24 * of this software and associated documentation files (the "Software"), to
25 * deal in the Software without restriction, including without limitation the
26 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
27 * sell copies of the Software, and to permit persons to whom the Software is
28 * furnished to do so, subject to the following conditions:
29 *
30 * The above copyright notice and this permission notice shall be included in
31 * all copies of the Software and its Copyright notices. In addition publicly
32 * documented acknowledgment must be given that this software has been used if no
33 * source code of this software is made available publicly. This includes
34 * acknowledgments in either Copyright notices, Manuals, Publicity and Marketing
35 * documents or any documentation provided with any product containing this
36 * software. This License does not apply to any software that links to the
37 * libraries provided by this software (statically or dynamically), but only to
38 * the software provided.
39 *
40 * Please see the OLD-COPYING.PLAIN for a plain-english explanation of this notice
41 * and it's intent.
42 *
43 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
44 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
45 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
46 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
47 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
48 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
49 */
50
51#ifndef EINA_TMPSTR_H_
52#define EINA_TMPSTR_H_
53
54#include "eina_types.h"
55
56/**
57 * @page eina_tmpstr_ppage
58 *
59 * Eina tmpstr is intended for being able to conveniently pass strings back
60 * to a calling parent without having to use single static buffers (which
61 * don't work with multiple threads or when returning multilpe times as
62 * parameters to a single function.
63 *
64 * The traditional way to "return" a string in C is either to provide a buffer
65 * as a paramater to return it in, return a pointer to a single static buffer,
66 * which has issues, or return a duplicated string. All cases are inconvenient
67 * and return special handling. This is intended to make this easier. Now you
68 * can do something like this:
69 *
70 * @code
71 * Eina_Tmpstr *my_homedir(void) {
72 * return eina_tmpstr_add(getenv("HOME"));
73 * }
74 *
75 * Eina_Tmpstr *my_tmpdir(void) {
76 * return eina_tmpstr_add(getenv("TMP"));
77 * }
78 *
79 * void my_movefile(Eina_Tmpstr *src, Eina_Tmpstr *dst) {
80 * rename(src, dst);
81 * eina_tmpstr_del(src);
82 * eina_tmpstr_del(dst);
83 * }
84 *
85 * char buf[500];
86 * my_movefile(my_homedir(), my_tmpdir());
87 * my_movefile("/tmp/file", "/tmp/newname");
88 * my_movefile(my_homedir(), "/var/tmp");
89 * snprintf(buf, sizeof(buf), "/tmp/%i.file", rand());
90 * my_movefile("/tmp.file", buf);
91 * @endcode
92 *
93 * Notice that you can interchange standard C strings (static ones or even
94 * generated buffers) with tmpstrings. The Eina_Tmpstr type is merely a
95 * type marker letting you know that the function will clean up those
96 * strings after use, and it is totally interchangeable with const char.
97 */
98
99/**
100 * @addtogroup Eina_Data_Types_Group Data Types
101 *
102 * @{
103 */
104
105/**
106 * @defgroup Eina_Stringshare_Group Stringshare
107 *
108 * @{
109 */
110
111/**
112 * @typedef Eina_Tmpstr
113 *
114 * Interchangeable with "const char *" but still a good visual hint for the
115 * purpose. This indicates the string is temporary and should be freed after
116 * use.
117 *
118 * @since 1.8.0
119 */
120
121typedef const char Eina_Tmpstr;
122
123/**
124 * @brief Add a new temporary string based on the input string.
125 *
126 * @param str This is the input stringthat is copied into the temp string.
127 * @return A pointer to the tmp string that is a standard C string.
128 *
129 * When you add a temporary string (tmpstr) it is expected to have a very
130 * short lifespan, and at any one time only a few of these are intended to
131 * exist. This is not intended for longer term storage of strings. The
132 * intended use is the ability to safely pass strings as return values from
133 * functions directly into parameters of new functions and then have the
134 * string be cleaned up automatically by the caller.
135 *
136 * If @p str is NULL, or no memory space exists to store the tmpstr, then
137 * NULL will be returned, otherwise a valid string pointer will be returned
138 * that you can treat as any other C string (eg strdup(tmpstr) or
139 * printf("%s\n", tmpstr) etc.). This string should be considered read-only
140 * and immutable, and when youa re done with the string yo should delete it
141 * with eina_tmpstr_del().
142 *
143 * Example usage:
144 *
145 * @code
146 * Eina_Tmpstr *my_homedir(void) {
147 * return eina_tmpstr_add(getenv("HOME"));
148 * }
149 *
150 * void my_rmfile(Eina_Tmpstr *str) {
151 * if (!str) return;
152 * unlink(str);
153 * eina_tmpstr_del(str);
154 * }
155 *
156 * my_rmfile(my_homedir());
157 * my_rmfile("/tmp/file");
158 * @endcode
159 *
160 * @see eina_tmpstr_del()
161 *
162 * @since 1.8.0
163 */
164EAPI Eina_Tmpstr *eina_tmpstr_add(const char *str) EINA_WARN_UNUSED_RESULT;
165
166/**
167 * @brief Delete the temporary string if it is one, or ignore it if it is not.
168 *
169 * @param tmpstr This is any C string pointer, but if it is a tmp string
170 * it is freed.
171 *
172 * This will delete the given temporary string @p tmpstr if it is a valid
173 * temporary string, or otherwise it will ignore it and do nothing so this
174 * can be used safely with non-temporary strings.
175 *
176 * @see eina_tmpstr_add()
177 *
178 * @since 1.8.0
179 */
180EAPI void eina_tmpstr_del(Eina_Tmpstr *tmpstr) EINA_ARG_NONNULL(1);
181
182/**
183 * @}
184 */
185
186/**
187 * @}
188 */
189
190#endif