1 : /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 : /*
3 : treatments.c
4 : Treatments that may be applied to make a new data type
5 :
6 : (C) Copyright 2007 - 2007 Olivier Delhomme
7 : e-mail : heraia@delhomme.org
8 : URL : http://heraia.tuxfamily.org
9 :
10 : This program is free software; you can redistribute it and/or modify
11 : it under the terms of the GNU General Public License as published by
12 : the Free Software Foundation; either version 2, or (at your option)
13 : any later version.
14 :
15 : This program is distributed in the hope that it will be useful,
16 : but WITHOUT ANY WARRANTY; without even the implied warranty of
17 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 : GNU General Public License for more details.
19 :
20 : You should have received a copy of the GNU General Public License
21 : along with this program; if not, write to the Free Software
22 : Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 : */
24 :
25 : #include "heraia_types.h"
26 :
27 : /**
28 : * General background :
29 : * Each treatment has to provide 4 functions :
30 : * - one that will do the treatment (execute it) with the treatment parameters (if any) [do_it]
31 : * take a GList of values_t * and return a GList of values_t *
32 : * - one that will init the treatment with defaults values (widget creation included, if any) [init]
33 : * - one that will delete the treatment, widgets included (if any) [kill]
34 : * - one that will copy the internal structures (widgets will be created by the init function) [copy]
35 : */
36 :
37 : static GList *treatment_reverse_do_it(GList *gchar_list);
38 : static gpointer treatment_reverse_copy(gpointer data);
39 : static void treatment_reverse_init(gpointer data);
40 : static void treatment_reverse_kill(gpointer data);
41 :
42 :
43 : value_t *new_value_t(guint length, guchar *bin_data)
44 0 : {
45 0 : value_t *a_value = NULL;
46 :
47 0 : a_value = (value_t*) g_malloc0(sizeof(value_t));
48 :
49 0 : a_value->length = length;
50 :
51 0 : a_value->bytes = (guchar *) g_malloc0(length * sizeof(guchar));
52 :
53 0 : memcpy(a_value->bytes, bin_data, length);
54 :
55 0 : return a_value;
56 : }
57 :
58 : /**
59 : * Copies the treatment and calls the treatment copy function that
60 : * copies the data of the treatment itself
61 : */
62 : treatment_t *copy_treatment(treatment_t *tment)
63 0 : {
64 0 : treatment_t *new_tment = NULL;
65 :
66 0 : new_tment = (treatment_t *) g_malloc0(sizeof(treatment_t));
67 :
68 0 : if (tment != NULL)
69 : {
70 0 : if (tment->name != NULL)
71 : {
72 0 : new_tment->name = g_strdup(tment->name);
73 : }
74 : else
75 : {
76 0 : new_tment->name = NULL;
77 : }
78 0 : new_tment->do_it = tment->do_it;
79 0 : new_tment->init = tment->init;
80 0 : new_tment->kill = tment->kill;
81 0 : new_tment->copy = tment->copy;
82 :
83 0 : if (tment->copy != NULL)
84 : {
85 0 : new_tment->data = tment->copy(tment->data);
86 : }
87 :
88 0 : return new_tment;
89 : }
90 : else
91 : {
92 0 : return NULL;
93 : }
94 : }
95 :
96 : /**
97 : * Finds a treatment, by name, in the treatment list
98 : * returns the treatment if found, NULL otherwise
99 : */
100 : treatment_t *find_treatment(GList *tment_list, gchar *tment_name)
101 0 : {
102 0 : treatment_t *tment = NULL;
103 :
104 0 : while (tment_list != NULL)
105 : {
106 0 : tment = (treatment_t *) tment_list->data;
107 0 : if (g_ascii_strcasecmp(tment->name, tment_name) == 0)
108 : {
109 0 : return tment;
110 : }
111 : else
112 : {
113 0 : tment_list = g_list_next(tment_list);
114 : }
115 : }
116 :
117 0 : return NULL;
118 : }
119 :
120 : /**
121 : * Here we do init the available treatment list
122 : * Should only be called once at init time
123 : */
124 : GList *init_treatments(void)
125 2 : {
126 2 : treatment_t *new_tment = NULL;
127 2 : GList *list = NULL;
128 :
129 : /* reverse treatment */
130 2 : new_tment = (treatment_t *) g_malloc0(sizeof(treatment_t));
131 2 : new_tment->name = g_strdup("reverse");
132 2 : new_tment->init = treatment_reverse_init;
133 2 : new_tment->do_it = treatment_reverse_do_it;
134 2 : new_tment->kill = treatment_reverse_kill;
135 2 : new_tment->copy = treatment_reverse_copy;
136 2 : list = g_list_prepend(list, new_tment);
137 :
138 2 : return list;
139 : }
140 :
141 : /******************* Here begins the treatments definitions *******************/
142 :
143 : /**
144 : * Treatment initialisation (called at runtime when selected by the user)
145 : */
146 : static void treatment_reverse_init(gpointer data)
147 0 : {
148 0 : treatment_t *tment = (treatment_t *) data;
149 : /**
150 : * We may init :
151 : * . structures and widgets (if any) an put all this in 'data'
152 : */
153 :
154 : /* In this treatment there is no need for any widget nor structures */
155 0 : tment->data = NULL;
156 0 : }
157 :
158 : /**
159 : * Deletes treatment (itself)
160 : */
161 : static void treatment_reverse_kill(gpointer data)
162 0 : {
163 0 : treatment_t *tment = (treatment_t *) data;
164 : /**
165 : * As there is no data nor structure, there is nothing to free !!
166 : */
167 :
168 0 : if (tment != NULL)
169 : {
170 0 : g_free(tment);
171 : }
172 0 : }
173 :
174 : /**
175 : * Copy treatment's datas
176 : */
177 : static gpointer treatment_reverse_copy(gpointer data)
178 0 : {
179 : /* As there is no data, there is nothing to do ! */
180 :
181 0 : return data;
182 : }
183 :
184 : /**
185 : * Reverse every lists that contains bytes from the hexwidget
186 : */
187 : static GList *treatment_reverse_do_it(GList *values_list)
188 0 : {
189 0 : GList *head = NULL;
190 0 : value_t *extracted = NULL;
191 0 : guchar *reversed = NULL;
192 0 : guint i = 0;
193 :
194 0 : head = values_list;
195 :
196 0 : while (values_list != NULL)
197 : {
198 : /* here we reverse the bytes in the values_t * structure */
199 0 : extracted = (value_t *) values_list->data;
200 0 : reversed = (guchar *) g_malloc0(sizeof(guchar) * extracted->length);
201 :
202 0 : for (i=0; i<extracted->length; i++)
203 : {
204 0 : reversed[i] = extracted->bytes[extracted->length-1-i];
205 : }
206 0 : g_free(extracted->bytes);
207 0 : extracted->bytes = reversed;
208 :
209 0 : values_list = g_list_next(values_list);
210 : }
211 :
212 0 : return head;
213 : }
214 :
215 :
216 :
|