?? cha-glib.html
字號:
</p> </div> <p> <a href="cha-glib.html#FL-STRFORMATS">Figure 9</a> shows a few more semi-standard functions glib wraps. <tt class="FUNCTION">g_strtod</tt> is like <tt class= "FUNCTION">strtod()</tt>---it converts string <span class="STRUCTNAME">nptr</span> to a double---with the exception that it will also attempt to convert the double in the <tt class="APPLICATION">"C"</tt> locale if it fails to convert it in the user's default locale. <span class="STRUCTNAME">*endptr</span> is set to the first unconverted character, i.e. any text after the number representation. If conversion fails, <span class="STRUCTNAME">*endptr</span> is set to <span class="STRUCTNAME">nptr</span>. <span class= "STRUCTNAME">endptr</span> may be <span class= "STRUCTNAME">NULL</span>, causing it to be ignored. </p> <p> <tt class="FUNCTION">g_strerror()</tt> and <tt class= "FUNCTION">g_strsignal()</tt> are like their non-<span class="STRUCTNAME">g_</span> equivalents, but portable. (They return a string representation for an <span class="STRUCTNAME">errno</span> or a signal number.) </p> <div class="FIGURE"> <a name="FL-STRFORMATS"></a> <div class="FUNCSYNOPSIS"> <a name="FL-STRFORMATS.SYNOPSIS"></a> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="FUNCSYNOPSISINFO">#include <glib.h></pre> </td> </tr> </table> <p> <code><code class="FUNCDEF">gdouble <tt class= "FUNCTION">g_strtod</tt></code>(const gchar* <tt class="PARAMETER"><i>nptr</i></tt>, gchar** <tt class="PARAMETER"><i>endptr</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">gchar* <tt class= "FUNCTION">g_strerror</tt></code>(gint <tt class= "PARAMETER"><i>errnum</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">gchar* <tt class= "FUNCTION">g_strsignal</tt></code>(gint <tt class= "PARAMETER"><i>signum</i></tt>);</code> </p> </div> <p> <b>Figure 9. String Conversions</b> </p> </div> <p> <a href="cha-glib.html#FL-STRDUP">Figure 10</a> shows glib's rich array of functions for allocating strings. Unsurprisingly, <tt class="FUNCTION">g_strdup()</tt> and <tt class="FUNCTION">g_strndup()</tt> produce an allocated copy of <span class="STRUCTNAME">str</span> or the first <span class="STRUCTNAME">n</span> characters of <span class="STRUCTNAME">str</span>. For consistency with the glib memory allocation functions, they return <span class="STRUCTNAME">NULL</span> if passed a <span class="STRUCTNAME">NULL</span> pointer. The <tt class="FUNCTION">printf()</tt> variants return a formatted string. <tt class="FUNCTION"> g_strescape</tt> escapes any <span class="STRUCTNAME"> \</span> characters in its argument by inserting another <span class="STRUCTNAME">\</span> before them, returning the escaped string. <tt class="FUNCTION"> g_strnfill()</tt>returns a string of size <span class= "STRUCTNAME">length</span> filled with <span class= "STRUCTNAME">fill_char</span>. </p> <p> <tt class="FUNCTION">g_strdup_printf()</tt> deserves a special mention; it is a simpler way to handle this common piece of code: </p> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="PROGRAMLISTING"> gchar* str = g_malloc(256); g_snprintf(str, 256, "%d printf-style %s", 1, "format"); </pre> </td> </tr> </table> <p> Instead you could say this, and avoid having to figure out the proper length of the buffer to boot: </p> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="PROGRAMLISTING"> gchar* str = g_strdup_printf("%d printf-style %s", 1, "format"); </pre> </td> </tr> </table> <div class="FIGURE"> <a name="FL-STRDUP"></a> <div class="FUNCSYNOPSIS"> <a name="FL-STRDUP.SYNOPSIS"></a> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="FUNCSYNOPSISINFO">#include <glib.h></pre> </td> </tr> </table> <p> <code><code class="FUNCDEF">gchar* <tt class= "FUNCTION">g_strdup</tt></code>(const gchar* <tt class="PARAMETER"><i>str</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">gchar* <tt class= "FUNCTION">g_strndup</tt></code>(const gchar* <tt class="PARAMETER"><i>format</i></tt>, guint <tt class="PARAMETER"><i>n</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">gchar* <tt class= "FUNCTION">g_strdup_printf</tt></code>(const gchar* <tt class="PARAMETER"><i>format</i></tt>, <tt class="PARAMETER"><i>...</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">gchar* <tt class= "FUNCTION">g_strdup_vprintf</tt></code>(const gchar* <tt class="PARAMETER"><i>format</i></tt>, va_list <tt class="PARAMETER"><i> args</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">gchar* <tt class= "FUNCTION">g_strescape</tt></code>(gchar* <tt class="PARAMETER"><i>string</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">gchar* <tt class= "FUNCTION">g_strnfill</tt></code>(guint <tt class= "PARAMETER"><i>length</i></tt>, gchar <tt class= "PARAMETER"><i>fill_char</i></tt>);</code> </p> </div> <p> <b>Figure 10. Allocating Strings</b> </p> </div> <p> glib provides some convenient functions for concatenating strings, shown in <a href= "cha-glib.html#FL-STRCONCAT">Figure 11</a>. <tt class= "FUNCTION">g_strconcat()</tt> returns a newly-allocated string created by concatenating each of the strings in the argument list. The last argument must be <span class="STRUCTNAME">NULL</span>, so <tt class= "FUNCTION">g_strconcat()</tt> knows when to stop. <tt class="FUNCTION">g_strjoin()</tt> is similar, but <span class="STRUCTNAME">separator</span> is inserted between each string. If <span class="STRUCTNAME"> separator</span> is <span class="STRUCTNAME"> NULL</span>, no separator is used. </p> <div class="FIGURE"> <a name="FL-STRCONCAT"></a> <div class="FUNCSYNOPSIS"> <a name="FL-STRCONCAT.SYNOPSIS"></a> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="FUNCSYNOPSISINFO">#include <glib.h></pre> </td> </tr> </table> <p> <code><code class="FUNCDEF">gchar* <tt class= "FUNCTION">g_strconcat</tt></code>(const gchar* <tt class="PARAMETER"><i>string1</i></tt>, <tt class= "PARAMETER"><i>...</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">gchar* <tt class= "FUNCTION">g_strjoin</tt></code>(const gchar* <tt class="PARAMETER"><i>separator</i></tt>, <tt class= "PARAMETER"><i>...</i></tt>);</code> </p> </div> <p> <b>Figure 11. Concatenating strings</b> </p> </div> <p> Finally, <a href="cha-glib.html#FL-STRVECTOR">Figure 12</a> summarizes a few routines which manipulate <span class="STRUCTNAME">NULL</span>-terminated arrays of strings. <tt class="FUNCTION">g_strsplit()</tt> breaks <span class="STRUCTNAME">string</span> at each <span class="STRUCTNAME">delimiter</span>, returning a newly-allocated array. <tt class="FUNCTION"> g_strjoinv()</tt> concatenates each string in the array with an optional <span class="STRUCTNAME"> separator</span>, returning an allocated string. <tt class="FUNCTION">g_strfreev()</tt> frees each string in the array and then the array itself. </p> <div class="FIGURE"> <a name="FL-STRVECTOR"></a> <div class="FUNCSYNOPSIS"> <a name="FL-STRVECTOR.SYNOPSIS"></a> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="FUNCSYNOPSISINFO">#include <glib.h></pre> </td> </tr> </table> <p> <code><code class="FUNCDEF">gchar** <tt class= "FUNCTION">g_strsplit</tt></code>(const gchar* <tt class="PARAMETER"><i>string</i></tt>, const gchar* <tt class="PARAMETER"><i>delimiter</i></tt>, gint <tt class="PARAMETER"><i> max_tokens</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">gchar* <tt class= "FUNCTION">g_strjoinv</tt></code>(const gchar* <tt class="PARAMETER"><i>separator</i></tt>, gchar** <tt class="PARAMETER"><i> str_array</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">void <tt class= "FUNCTION">g_strfreev</tt></code>(gchar** <tt class="PARAMETER"><i>str_array</i></tt>);</code> </p> </div> <p> <b>Figure 12. Manipulating <span class="STRUCTNAME"> NULL</span>-terminated string vectors</b> </p> </div> </div> </div> </div> <div class="NAVFOOTER"> <br> <br> <table width="100%" border="0" bgcolor="#ffffff" cellpadding= "1" cellspacing="0"> <tr> <td width="25%" bgcolor="#ffffff" align="left"> <a href="z22.html"><font color="#0000ff" size="2"><b> <<< Previous</b></font></a> </td> <td width="25%" colspan="2" bgcolor="#ffffff" align= "center"> <font color="#0000ff" size="2"><b><a href="ggad.html"> <font color="#0000ff" size="2"><b> Home</b></font></a></b></font> </td> <td width="25%" bgcolor="#ffffff" align="right"> <a href="z29.html"><font color="#0000ff" size="2"><b> Next >>></b></font></a> </td> </tr> <tr> <td colspan="2" align="left"> <font color="#000000" size="2"><b>Structure of the Book</b></font> </td> <td colspan="2" align="right"> <font color="#000000" size="2"><b>Data Structures</b></font> </td> </tr> </table> </div> </body></html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -