1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  *  Copyright (C) 2008-2010  Kouhei Sutou <kou@clear-code.com>
4  *
5  *  This library is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU Lesser General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This library 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 this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 #  include <config.h>
22 #endif /* HAVE_CONFIG_H */
23 
24 #include "gcut-inspect.h"
25 #include "gcut-enum.h"
26 
27 void
gcut_inspect_direct(GString * string,gconstpointer data,gpointer user_data)28 gcut_inspect_direct (GString *string, gconstpointer data, gpointer user_data)
29 {
30     g_string_append_printf(string, "%u", GPOINTER_TO_UINT(data));
31 }
32 
33 void
gcut_inspect_int(GString * string,gconstpointer data,gpointer user_data)34 gcut_inspect_int (GString *string, gconstpointer data, gpointer user_data)
35 {
36     const gint *int_value = data;
37 
38     g_string_append_printf(string, "%d", *int_value);
39 }
40 
41 void
gcut_inspect_uint(GString * string,gconstpointer data,gpointer user_data)42 gcut_inspect_uint (GString *string, gconstpointer data, gpointer user_data)
43 {
44     const guint *uint_value = data;
45 
46     g_string_append_printf(string, "%u", *uint_value);
47 }
48 
49 void
gcut_inspect_int64(GString * string,gconstpointer data,gpointer user_data)50 gcut_inspect_int64 (GString *string, gconstpointer data, gpointer user_data)
51 {
52     const gint64 *int64_value = data;
53 
54     g_string_append_printf(string, "%" G_GINT64_FORMAT, *int64_value);
55 }
56 
57 void
gcut_inspect_uint64(GString * string,gconstpointer data,gpointer user_data)58 gcut_inspect_uint64 (GString *string, gconstpointer data, gpointer user_data)
59 {
60     const guint64 *uint64_value = data;
61 
62     g_string_append_printf(string, "%" G_GUINT64_FORMAT, *uint64_value);
63 }
64 
65 void
gcut_inspect_size(GString * string,gconstpointer data,gpointer user_data)66 gcut_inspect_size (GString *string, gconstpointer data, gpointer user_data)
67 {
68     const gsize *size_value = data;
69 
70     g_string_append_printf(string, "%" G_GSIZE_FORMAT, *size_value);
71 }
72 
73 void
gcut_inspect_char(GString * string,gconstpointer data,gpointer user_data)74 gcut_inspect_char (GString *string, gconstpointer data, gpointer user_data)
75 {
76     const gchar *value = data;
77 
78     switch (value[0]) {
79     case '\a':
80         g_string_append(string, "'\\a'");
81         break;
82     case '\b':
83         g_string_append(string, "'\\b'");
84         break;
85     case '\t':
86         g_string_append(string, "'\\t'");
87         break;
88     case '\n':
89         g_string_append(string, "'\\n'");
90         break;
91     case '\v':
92         g_string_append(string, "'\\v'");
93         break;
94     case '\f':
95         g_string_append(string, "'\\f'");
96         break;
97     case '\r':
98         g_string_append(string, "'\\r'");
99         break;
100 #ifndef _MSC_VER
101     case '\e':
102         g_string_append(string, "'\\e'");
103         break;
104 #endif
105     case '\'':
106         g_string_append(string, "'\\''");
107         break;
108     case '\\':
109         g_string_append(string, "'\\\\'");
110         break;
111     default:
112         if (0x20 <= value[0] && value[0] <= 0x7e) {
113             g_string_append_printf(string, "\'%c\'", value[0]);
114         } else {
115             g_string_append_printf(string, "'\\%d'", value[0]);
116         }
117         break;
118     }
119 }
120 
121 void
gcut_inspect_string(GString * string,gconstpointer data,gpointer user_data)122 gcut_inspect_string (GString *string, gconstpointer data, gpointer user_data)
123 {
124     const gchar *value = data;
125 
126     if (value)
127         g_string_append_printf(string, "\"%s\"", value);
128     else
129         g_string_append(string, "NULL");
130 }
131 
132 void
gcut_inspect_type(GString * string,gconstpointer data,gpointer user_data)133 gcut_inspect_type (GString *string, gconstpointer data, gpointer user_data)
134 {
135     const GType *type = data;
136 
137     g_string_append_printf(string, "<%s>", g_type_name(*type));
138 }
139 
140 void
gcut_inspect_flags(GString * string,gconstpointer data,gpointer user_data)141 gcut_inspect_flags (GString *string, gconstpointer data, gpointer user_data)
142 {
143     const guint *flags = data;
144     GType *flags_type = user_data;
145     gchar *inspected_flags;
146 
147     inspected_flags = gcut_flags_inspect(*flags_type, *flags);
148     g_string_append(string, inspected_flags);
149     g_free(inspected_flags);
150 }
151 
152 void
gcut_inspect_enum(GString * string,gconstpointer data,gpointer user_data)153 gcut_inspect_enum (GString *string, gconstpointer data, gpointer user_data)
154 {
155     const gint *enum_value = data;
156     GType *enum_type = user_data;
157     gchar *inspected_enum;
158 
159     inspected_enum = gcut_enum_inspect(*enum_type, *enum_value);
160     g_string_append(string, inspected_enum);
161     g_free(inspected_enum);
162 }
163 
164 void
gcut_inspect_pointer(GString * string,gconstpointer data,gpointer user_data)165 gcut_inspect_pointer (GString *string, gconstpointer data, gpointer user_data)
166 {
167     g_string_append_printf(string, "#<%p>", data);
168 }
169 
170 void
gcut_inspect_boolean(GString * string,gconstpointer data,gpointer user_data)171 gcut_inspect_boolean (GString *string, gconstpointer data, gpointer user_data)
172 {
173     const gboolean *boolean_value = data;
174 
175     if (*boolean_value) {
176         g_string_append(string, "TRUE");
177     } else {
178         g_string_append(string, "FALSE");
179     }
180 }
181 
182 void
gcut_inspect_double(GString * string,gconstpointer data,gpointer user_data)183 gcut_inspect_double (GString *string, gconstpointer data, gpointer user_data)
184 {
185     const gdouble *double_value = data;
186 
187     g_string_append_printf(string, "%g", *double_value);
188 }
189 
190 /*
191 vi:nowrap:ai:expandtab:sw=4:ts=4
192 */
193 
194