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 gtkc.glib;
30 public  import gtkc.glibtypes;
31 private import gtkd.Loader;
32 
33 
34 /**
35  * An opaque data structure representing String Chunks.
36  * It should only be accessed by using the following functions.
37  */
38 public class StringChunk
39 {
40 	/** the main Gtk struct */
41 	protected GStringChunk* gStringChunk;
42 	protected bool ownedRef;
43 
44 	/** Get the main Gtk struct */
45 	public GStringChunk* getStringChunkStruct(bool transferOwnership = false)
46 	{
47 		if (transferOwnership)
48 			ownedRef = false;
49 		return gStringChunk;
50 	}
51 
52 	/** the main Gtk struct as a void* */
53 	protected void* getStruct()
54 	{
55 		return cast(void*)gStringChunk;
56 	}
57 
58 	/**
59 	 * Sets our main struct and passes it to the parent class.
60 	 */
61 	public this (GStringChunk* gStringChunk, bool ownedRef = false)
62 	{
63 		this.gStringChunk = gStringChunk;
64 		this.ownedRef = ownedRef;
65 	}
66 
67 	~this ()
68 	{
69 		if (  Linker.isLoaded(LIBRARY_GLIB) && ownedRef )
70 			g_string_chunk_free(gStringChunk);
71 	}
72 
73 
74 	/**
75 	 * Frees all strings contained within the #GStringChunk.
76 	 * After calling g_string_chunk_clear() it is not safe to
77 	 * access any of the strings which were contained within it.
78 	 *
79 	 * Since: 2.14
80 	 */
81 	public void clear()
82 	{
83 		g_string_chunk_clear(gStringChunk);
84 	}
85 
86 	/**
87 	 * Frees all memory allocated by the #GStringChunk.
88 	 * After calling g_string_chunk_free() it is not safe to
89 	 * access any of the strings which were contained within it.
90 	 */
91 	public void free()
92 	{
93 		g_string_chunk_free(gStringChunk);
94 		ownedRef = false;
95 	}
96 
97 	/**
98 	 * Adds a copy of @string to the #GStringChunk.
99 	 * It returns a pointer to the new copy of the string
100 	 * in the #GStringChunk. The characters in the string
101 	 * can be changed, if necessary, though you should not
102 	 * change anything after the end of the string.
103 	 *
104 	 * Unlike g_string_chunk_insert_const(), this function
105 	 * does not check for duplicates. Also strings added
106 	 * with g_string_chunk_insert() will not be searched
107 	 * by g_string_chunk_insert_const() when looking for
108 	 * duplicates.
109 	 *
110 	 * Params:
111 	 *     str = the string to add
112 	 *
113 	 * Returns: a pointer to the copy of @string within
114 	 *     the #GStringChunk
115 	 */
116 	public string insert(string str)
117 	{
118 		auto retStr = g_string_chunk_insert(gStringChunk, Str.toStringz(str));
119 		
120 		scope(exit) Str.freeString(retStr);
121 		return Str.toString(retStr);
122 	}
123 
124 	/**
125 	 * Adds a copy of @string to the #GStringChunk, unless the same
126 	 * string has already been added to the #GStringChunk with
127 	 * g_string_chunk_insert_const().
128 	 *
129 	 * This function is useful if you need to copy a large number
130 	 * of strings but do not want to waste space storing duplicates.
131 	 * But you must remember that there may be several pointers to
132 	 * the same string, and so any changes made to the strings
133 	 * should be done very carefully.
134 	 *
135 	 * Note that g_string_chunk_insert_const() will not return a
136 	 * pointer to a string added with g_string_chunk_insert(), even
137 	 * if they do match.
138 	 *
139 	 * Params:
140 	 *     str = the string to add
141 	 *
142 	 * Returns: a pointer to the new or existing copy of @string
143 	 *     within the #GStringChunk
144 	 */
145 	public string insertConst(string str)
146 	{
147 		auto retStr = g_string_chunk_insert_const(gStringChunk, Str.toStringz(str));
148 		
149 		scope(exit) Str.freeString(retStr);
150 		return Str.toString(retStr);
151 	}
152 
153 	/**
154 	 * Adds a copy of the first @len bytes of @string to the #GStringChunk.
155 	 * The copy is nul-terminated.
156 	 *
157 	 * Since this function does not stop at nul bytes, it is the caller's
158 	 * responsibility to ensure that @string has at least @len addressable
159 	 * bytes.
160 	 *
161 	 * The characters in the returned string can be changed, if necessary,
162 	 * though you should not change anything after the end of the string.
163 	 *
164 	 * Params:
165 	 *     str = bytes to insert
166 	 *     len = number of bytes of @string to insert, or -1 to insert a
167 	 *         nul-terminated string
168 	 *
169 	 * Returns: a pointer to the copy of @string within the #GStringChunk
170 	 *
171 	 * Since: 2.4
172 	 */
173 	public string insertLen(string str, ptrdiff_t len)
174 	{
175 		auto retStr = g_string_chunk_insert_len(gStringChunk, Str.toStringz(str), len);
176 		
177 		scope(exit) Str.freeString(retStr);
178 		return Str.toString(retStr);
179 	}
180 
181 	/**
182 	 * Creates a new #GStringChunk.
183 	 *
184 	 * Params:
185 	 *     size = the default size of the blocks of memory which are
186 	 *         allocated to store the strings. If a particular string
187 	 *         is larger than this default size, a larger block of
188 	 *         memory will be allocated for it.
189 	 *
190 	 * Returns: a new #GStringChunk
191 	 *
192 	 * Throws: ConstructionException GTK+ fails to create the object.
193 	 */
194 	public this(size_t size)
195 	{
196 		auto p = g_string_chunk_new(size);
197 		
198 		if(p is null)
199 		{
200 			throw new ConstructionException("null returned by new");
201 		}
202 		
203 		this(cast(GStringChunk*) p);
204 	}
205 }