00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00032 #include <stdlib.h>
00033 #include <string.h>
00034
00035 #include "duifilter.h"
00036 #include "perr.h"
00037
00038 struct DuiFilter_s
00039 {
00040 gchar * name;
00041 DuiFilter *rev_filter;
00042 const gchar * (*filter) (const gchar *);
00043
00044 GHashTable *lookup_table;
00045 };
00046
00047 static void filters_init (void);
00048
00049 static GHashTable * filter_table = NULL;
00050
00051
00052
00053 DuiFilter *
00054 dui_filter_new (const gchar * name, const gchar * revname)
00055 {
00056 DuiFilter *filt;
00057
00058 filt = g_new (DuiFilter, 1);
00059 filt->name = g_strdup (name);
00060 filt->rev_filter = NULL;
00061 filt->filter = NULL;
00062 filt->lookup_table = NULL;
00063
00064 if (!filter_table)
00065 {
00066 filters_init();
00067 }
00068
00069 g_hash_table_insert (filter_table, filt->name, filt);
00070 if (revname)
00071 {
00072 filt->rev_filter = dui_filter_new (revname, NULL);
00073 }
00074
00075 return filt;
00076 }
00077
00078
00079
00080
00081
00082
00083 static const gchar *
00084 is_null (const gchar * val)
00085 {
00086 if (!val) return "null";
00087 return NULL;
00088 }
00089
00090
00091
00092 static const gchar *
00093 is_whitespace_or_null (const gchar * val)
00094 {
00095 val = whitespace_filter (val);
00096 if (!val) return "null";
00097 return NULL;
00098 }
00099
00100
00101
00102
00103 const gchar *
00104 whitespace_filter (const gchar * val)
00105 {
00106 size_t len;
00107 if (!val) return NULL;
00108
00109 len = strspn (val, "\a\b\t\n\v\f\r ");
00110 if (0 == val[len]) return NULL;
00111 return val+len;
00112 }
00113
00114
00115
00116
00117 static const gchar *
00118 to_int_filter (const gchar * val)
00119 {
00120 gint ival;
00121 static gchar intbuff[30];
00124 if (!val) return "0";
00125 ival = atoi (val);
00126 snprintf (intbuff, 30, "%d", ival);
00127 return intbuff;
00128 }
00129
00130
00131
00132
00133
00134 gint
00135 dui_util_bool_to_int (const gchar * val)
00136 {
00137 const gchar * p = whitespace_filter (val);
00138 if (!p) return 0;
00139 if ('t' == p[0]) return 1;
00140 if ('T' == p[0]) return 1;
00141 if ('y' == p[0]) return 1;
00142 if ('Y' == p[0]) return 1;
00143 if (strstr (p, "true")) return 1;
00144 if (strstr (p, "TRUE")) return 1;
00145 if (strstr (p, "yes")) return 1;
00146 if (strstr (p, "YES")) return 1;
00147 return atoi (val);
00148 }
00149
00150
00151
00152
00153 void
00154 dui_filter_add_lookup (DuiFilter *filt, const gchar * key, const gchar * val)
00155 {
00156 gchar *keyc, *valc;
00157 if (!filt) return;
00158
00159 if (!filt->lookup_table)
00160 {
00161 filt->lookup_table = g_hash_table_new (g_str_hash, g_str_equal);
00162 }
00163
00164 keyc = g_strdup (key);
00165 valc = g_strdup (val);
00166 g_hash_table_insert (filt->lookup_table, keyc, valc);
00167
00168
00169 if (filt->rev_filter)
00170 {
00171 dui_filter_add_lookup (filt->rev_filter, val, key);
00172 }
00173 }
00174
00175
00176
00177 #define ADD_FILT(name,func) { \
00178 DuiFilter *filt = dui_filter_new(name, NULL); \
00179 filt->filter = func; \
00180 }
00181
00182 static void
00183 filters_init (void)
00184 {
00185 filter_table = g_hash_table_new (g_str_hash, g_str_equal);
00186
00187
00188 ADD_FILT ("is_null", is_null);
00189 ADD_FILT ("is_whitespace_or_null", is_whitespace_or_null);
00190 ADD_FILT ("to_int", to_int_filter);
00191 ADD_FILT ("whitespace", whitespace_filter);
00192 }
00193
00194
00195
00196 const gchar *
00197 dui_filter_apply (DuiFilter *filt, const gchar *str)
00198 {
00199 if (!filt) return str;
00200
00201 if (filt->filter)
00202 {
00203 return (filt->filter) (str);
00204 }
00205
00206 if (!str) return NULL;
00207 if (filt->lookup_table)
00208 {
00209 return g_hash_table_lookup (filt->lookup_table, str);
00210 }
00211 PWARN ("filter \"%s\" can't be found", filt->name);
00212 return str;
00213 }
00214
00215
00216
00217 DuiFilter *
00218 dui_filter_find_by_name (const gchar *filtername)
00219 {
00220 DuiFilter *filt;
00221 if (!filtername) return NULL;
00222
00223 if (!filter_table)
00224 {
00225 filters_init();
00226 }
00227
00228 filt = g_hash_table_lookup (filter_table, filtername);
00229 if (filt) return filt;
00230
00231 SYNTAX ("unknown filter \"%s\"", filtername);
00232 return NULL;
00233 }
00234
00235