Heraia  0.1.8
treatments.c
Go to the documentation of this file.
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 - 2008 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 <libheraia.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 {
45  value_t *a_value = NULL;
46 
47  a_value = (value_t*) g_malloc0(sizeof(value_t));
48 
49  a_value->length = length;
50 
51  a_value->bytes = (guchar *) g_malloc0(length * sizeof(guchar));
52 
53  memcpy(a_value->bytes, bin_data, length);
54 
55  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 {
64  treatment_t *new_tment = NULL;
65 
66  new_tment = (treatment_t *) g_malloc0(sizeof(treatment_t));
67 
68  if (tment != NULL)
69  {
70  if (tment->name != NULL)
71  {
72  new_tment->name = g_strdup(tment->name);
73  }
74  else
75  {
76  new_tment->name = NULL;
77  }
78  new_tment->do_it = tment->do_it;
79  new_tment->init = tment->init;
80  new_tment->kill = tment->kill;
81  new_tment->copy = tment->copy;
82 
83  if (tment->copy != NULL)
84  {
85  new_tment->data = tment->copy(tment->data);
86  }
87 
88  return new_tment;
89  }
90  else
91  {
92  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 {
102  treatment_t *tment = NULL;
103 
104  while (tment_list != NULL)
105  {
106  tment = (treatment_t *) tment_list->data;
107  if (g_ascii_strcasecmp(tment->name, tment_name) == 0)
108  {
109  return tment;
110  }
111  else
112  {
113  tment_list = g_list_next(tment_list);
114  }
115  }
116 
117  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 {
126  treatment_t *new_tment = NULL;
127  GList *list = NULL;
128 
129  /* reverse treatment */
130  new_tment = (treatment_t *) g_malloc0(sizeof(treatment_t));
131  new_tment->name = g_strdup("reverse");
132  new_tment->init = treatment_reverse_init;
133  new_tment->do_it = treatment_reverse_do_it;
134  new_tment->kill = treatment_reverse_kill;
135  new_tment->copy = treatment_reverse_copy;
136  list = g_list_prepend(list, new_tment);
137 
138  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 {
148  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  tment->data = NULL;
156 }
157 
158 /**
159  * Deletes treatment (itself)
160  */
161 static void treatment_reverse_kill(gpointer data)
162 {
163  treatment_t *tment = (treatment_t *) data;
164  /**
165  * As there is no data nor structure, there is nothing to free !!
166  */
167 
168  if (tment != NULL)
169  {
170  g_free(tment);
171  }
172 }
173 
174 /**
175  * Copy treatment's datas
176  */
177 static gpointer treatment_reverse_copy(gpointer data)
178 {
179  /* As there is no data, there is nothing to do ! */
180 
181  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 {
189  GList *head = NULL;
190  value_t *extracted = NULL;
191  guchar *reversed = NULL;
192  guint i = 0;
193 
194  head = values_list;
195 
196  while (values_list != NULL)
197  {
198  /* here we reverse the bytes in the values_t * structure */
199  extracted = (value_t *) values_list->data;
200  reversed = (guchar *) g_malloc0(sizeof(guchar) * extracted->length);
201 
202  for (i=0; i<extracted->length; i++)
203  {
204  reversed[i] = extracted->bytes[extracted->length-1-i];
205  }
206  g_free(extracted->bytes);
207  extracted->bytes = reversed;
208 
209  values_list = g_list_next(values_list);
210  }
211 
212  return head;
213 }
214 
215 
216 
GList * init_treatments(void)
Here we do init the available treatment list Should only be called once at init time.
Definition: treatments.c:124
static gpointer treatment_reverse_copy(gpointer data)
Copy treatment's datas.
Definition: treatments.c:177
static void treatment_reverse_kill(gpointer data)
Deletes treatment (itself)
Definition: treatments.c:161
guchar * bytes
Definition: treatments.h:31
guint length
Definition: treatments.h:30
treatment_t * find_treatment(GList *tment_list, gchar *tment_name)
Finds a treatment, by name, in the treatment list returns the treatment if found, NULL otherwise...
Definition: treatments.c:100
static void treatment_reverse_init(gpointer data)
Treatment initialisation (called at runtime when selected by the user)
Definition: treatments.c:146
treatment_t * copy_treatment(treatment_t *tment)
Copies the treatment and calls the treatment copy function that copies the data of the treatment itse...
Definition: treatments.c:62
static GList * treatment_reverse_do_it(GList *gchar_list)
General background : Each treatment has to provide 4 functions :
Definition: treatments.c:187
value_t * new_value_t(guint length, guchar *bin_data)
Definition: treatments.c:43
This file contains all the definitions and includes all other .h files.