1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module glib.StringChunk;
26 
27 private import glib.ConstructionException;
28 private import glib.Str;
29 private import glib.c.functions;
30 public  import glib.c.types;
31 public  import gtkc.glibtypes;
32 private import gtkd.Loader;
33 
34 
35 /**
36  * An opaque data structure representing String Chunks.
37  * It should only be accessed by using the following functions.
38  */
39 public class StringChunk
40 {
41 	/** the main Gtk struct */
42 	protected GStringChunk* gStringChunk;
43 	protected bool ownedRef;
44 
45 	/** Get the main Gtk struct */
46 	public GStringChunk* getStringChunkStruct(bool transferOwnership = false)
47 	{
48 		if (transferOwnership)
49 			ownedRef = false;
50 		return gStringChunk;
51 	}
52 
53 	/** the main Gtk struct as a void* */
54 	protected void* getStruct()
55 	{
56 		return cast(void*)gStringChunk;
57 	}
58 
59 	/**
60 	 * Sets our main struct and passes it to the parent class.
61 	 */
62 	public this (GStringChunk* gStringChunk, bool ownedRef = false)
63 	{
64 		this.gStringChunk = gStringChunk;
65 		this.ownedRef = ownedRef;
66 	}
67 
68 	~this ()
69 	{
70 		if ( Linker.isLoaded(LIBRARY_GLIB) && ownedRef )
71 			g_string_chunk_free(gStringChunk);
72 	}
73 
74 
75 	/**
76 	 * Frees all strings contained within the #GStringChunk.
77 	 * After calling g_string_chunk_clear() it is not safe to
78 	 * access any of the strings which were contained within it.
79 	 *
80 	 * Since: 2.14
81 	 */
82 	public void clear()
83 	{
84 		g_string_chunk_clear(gStringChunk);
85 	}
86 
87 	/**
88 	 * Frees all memory allocated by the #GStringChunk.
89 	 * After calling g_string_chunk_free() it is not safe to
90 	 * access any of the strings which were contained within it.
91 	 */
92 	public void free()
93 	{
94 		g_string_chunk_free(gStringChunk);
95 		ownedRef = false;
96 	}
97 
98 	/**
99 	 * Adds a copy of @string to the #GStringChunk.
100 	 * It returns a pointer to the new copy of the string
101 	 * in the #GStringChunk. The characters in the string
102 	 * can be changed, if necessary, though you should not
103 	 * change anything after the end of the string.
104 	 *
105 	 * Unlike g_string_chunk_insert_const(), this function
106 	 * does not check for duplicates. Also strings added
107 	 * with g_string_chunk_insert() will not be searched
108 	 * by g_string_chunk_insert_const() when looking for
109 	 * duplicates.
110 	 *
111 	 * Params:
112 	 *     str = the string to add
113 	 *
114 	 * Returns: a pointer to the copy of @string within
115 	 *     the #GStringChunk
116 	 */
117 	public string insert(string str)
118 	{
119 		auto retStr = g_string_chunk_insert(gStringChunk, Str.toStringz(str));
120 
121 		scope(exit) Str.freeString(retStr);
122 		return Str.toString(retStr);
123 	}
124 
125 	/**
126 	 * Adds a copy of @string to the #GStringChunk, unless the same
127 	 * string has already been added to the #GStringChunk with
128 	 * g_string_chunk_insert_const().
129 	 *
130 	 * This function is useful if you need to copy a large number
131 	 * of strings but do not want to waste space storing duplicates.
132 	 * But you must remember that there may be several pointers to
133 	 * the same string, and so any changes made to the strings
134 	 * should be done very carefully.
135 	 *
136 	 * Note that g_string_chunk_insert_const() will not return a
137 	 * pointer to a string added with g_string_chunk_insert(), even
138 	 * if they do match.
139 	 *
140 	 * Params:
141 	 *     str = the string to add
142 	 *
143 	 * Returns: a pointer to the new or existing copy of @string
144 	 *     within the #GStringChunk
145 	 */
146 	public string insertConst(string str)
147 	{
148 		auto retStr = g_string_chunk_insert_const(gStringChunk, Str.toStringz(str));
149 
150 		scope(exit) Str.freeString(retStr);
151 		return Str.toString(retStr);
152 	}
153 
154 	/**
155 	 * Adds a copy of the first @len bytes of @string to the #GStringChunk.
156 	 * The copy is nul-terminated.
157 	 *
158 	 * Since this function does not stop at nul bytes, it is the caller's
159 	 * responsibility to ensure that @string has at least @len addressable
160 	 * bytes.
161 	 *
162 	 * The characters in the returned string can be changed, if necessary,
163 	 * though you should not change anything after the end of the string.
164 	 *
165 	 * Params:
166 	 *     str = bytes to insert
167 	 *     len = number of bytes of @string to insert, or -1 to insert a
168 	 *         nul-terminated string
169 	 *
170 	 * Returns: a pointer to the copy of @string within the #GStringChunk
171 	 *
172 	 * Since: 2.4
173 	 */
174 	public string insertLen(string str, ptrdiff_t len)
175 	{
176 		auto retStr = g_string_chunk_insert_len(gStringChunk, Str.toStringz(str), len);
177 
178 		scope(exit) Str.freeString(retStr);
179 		return Str.toString(retStr);
180 	}
181 
182 	/**
183 	 * Creates a new #GStringChunk.
184 	 *
185 	 * Params:
186 	 *     size = the default size of the blocks of memory which are
187 	 *         allocated to store the strings. If a particular string
188 	 *         is larger than this default size, a larger block of
189 	 *         memory will be allocated for it.
190 	 *
191 	 * Returns: a new #GStringChunk
192 	 *
193 	 * Throws: ConstructionException GTK+ fails to create the object.
194 	 */
195 	public this(size_t size)
196 	{
197 		auto p = g_string_chunk_new(size);
198 
199 		if(p is null)
200 		{
201 			throw new ConstructionException("null returned by new");
202 		}
203 
204 		this(cast(GStringChunk*) p);
205 	}
206 }