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.Pattern;
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  * A GPatternSpec struct is the 'compiled' form of a pattern. This
37  * structure is opaque and its fields cannot be accessed directly.
38  */
39 public class Pattern
40 {
41 	/** the main Gtk struct */
42 	protected GPatternSpec* gPatternSpec;
43 	protected bool ownedRef;
44 
45 	/** Get the main Gtk struct */
46 	public GPatternSpec* getPatternStruct(bool transferOwnership = false)
47 	{
48 		if (transferOwnership)
49 			ownedRef = false;
50 		return gPatternSpec;
51 	}
52 
53 	/** the main Gtk struct as a void* */
54 	protected void* getStruct()
55 	{
56 		return cast(void*)gPatternSpec;
57 	}
58 
59 	/**
60 	 * Sets our main struct and passes it to the parent class.
61 	 */
62 	public this (GPatternSpec* gPatternSpec, bool ownedRef = false)
63 	{
64 		this.gPatternSpec = gPatternSpec;
65 		this.ownedRef = ownedRef;
66 	}
67 
68 	~this ()
69 	{
70 		if ( Linker.isLoaded(LIBRARY_GLIB) && ownedRef )
71 			g_pattern_spec_free(gPatternSpec);
72 	}
73 
74 
75 	/**
76 	 * Compares two compiled pattern specs and returns whether they will
77 	 * match the same set of strings.
78 	 *
79 	 * Params:
80 	 *     pspec2 = another #GPatternSpec
81 	 *
82 	 * Returns: Whether the compiled patterns are equal
83 	 */
84 	public bool equal(Pattern pspec2)
85 	{
86 		return g_pattern_spec_equal(gPatternSpec, (pspec2 is null) ? null : pspec2.getPatternStruct()) != 0;
87 	}
88 
89 	/**
90 	 * Frees the memory allocated for the #GPatternSpec.
91 	 */
92 	public void free()
93 	{
94 		g_pattern_spec_free(gPatternSpec);
95 		ownedRef = false;
96 	}
97 
98 	/**
99 	 * Compiles a pattern to a #GPatternSpec.
100 	 *
101 	 * Params:
102 	 *     pattern = a zero-terminated UTF-8 encoded string
103 	 *
104 	 * Returns: a newly-allocated #GPatternSpec
105 	 *
106 	 * Throws: ConstructionException GTK+ fails to create the object.
107 	 */
108 	public this(string pattern)
109 	{
110 		auto p = g_pattern_spec_new(Str.toStringz(pattern));
111 
112 		if(p is null)
113 		{
114 			throw new ConstructionException("null returned by new");
115 		}
116 
117 		this(cast(GPatternSpec*) p);
118 	}
119 
120 	/**
121 	 * Matches a string against a compiled pattern. Passing the correct
122 	 * length of the string given is mandatory. The reversed string can be
123 	 * omitted by passing %NULL, this is more efficient if the reversed
124 	 * version of the string to be matched is not at hand, as
125 	 * g_pattern_match() will only construct it if the compiled pattern
126 	 * requires reverse matches.
127 	 *
128 	 * Note that, if the user code will (possibly) match a string against a
129 	 * multitude of patterns containing wildcards, chances are high that
130 	 * some patterns will require a reversed string. In this case, it's
131 	 * more efficient to provide the reversed string to avoid multiple
132 	 * constructions thereof in the various calls to g_pattern_match().
133 	 *
134 	 * Note also that the reverse of a UTF-8 encoded string can in general
135 	 * not be obtained by g_strreverse(). This works only if the string
136 	 * does not contain any multibyte characters. GLib offers the
137 	 * g_utf8_strreverse() function to reverse UTF-8 encoded strings.
138 	 *
139 	 * Params:
140 	 *     pspec = a #GPatternSpec
141 	 *     stringLength = the length of @string (in bytes, i.e. strlen(),
142 	 *         not g_utf8_strlen())
143 	 *     str = the UTF-8 encoded string to match
144 	 *     stringReversed = the reverse of @string or %NULL
145 	 *
146 	 * Returns: %TRUE if @string matches @pspec
147 	 */
148 	public static bool patternMatch(Pattern pspec, uint stringLength, string str, string stringReversed)
149 	{
150 		return g_pattern_match((pspec is null) ? null : pspec.getPatternStruct(), stringLength, Str.toStringz(str), Str.toStringz(stringReversed)) != 0;
151 	}
152 
153 	/**
154 	 * Matches a string against a pattern given as a string. If this
155 	 * function is to be called in a loop, it's more efficient to compile
156 	 * the pattern once with g_pattern_spec_new() and call
157 	 * g_pattern_match_string() repeatedly.
158 	 *
159 	 * Params:
160 	 *     pattern = the UTF-8 encoded pattern
161 	 *     str = the UTF-8 encoded string to match
162 	 *
163 	 * Returns: %TRUE if @string matches @pspec
164 	 */
165 	public static bool patternMatchSimple(string pattern, string str)
166 	{
167 		return g_pattern_match_simple(Str.toStringz(pattern), Str.toStringz(str)) != 0;
168 	}
169 
170 	/**
171 	 * Matches a string against a compiled pattern. If the string is to be
172 	 * matched against more than one pattern, consider using
173 	 * g_pattern_match() instead while supplying the reversed string.
174 	 *
175 	 * Params:
176 	 *     pspec = a #GPatternSpec
177 	 *     str = the UTF-8 encoded string to match
178 	 *
179 	 * Returns: %TRUE if @string matches @pspec
180 	 */
181 	public static bool patternMatchString(Pattern pspec, string str)
182 	{
183 		return g_pattern_match_string((pspec is null) ? null : pspec.getPatternStruct(), Str.toStringz(str)) != 0;
184 	}
185 }