treatments.c

Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
00002 /*
00003   treatments.c
00004   Treatments that may be applied to make a new data type
00005   
00006   (C) Copyright 2007 - 2008 Olivier Delhomme
00007   e-mail : heraia@delhomme.org
00008   URL    : http://heraia.tuxfamily.org
00009  
00010   This program is free software; you can redistribute it and/or modify
00011   it under the terms of the GNU General Public License as published by
00012   the Free Software Foundation; either version 2, or  (at your option) 
00013   any later version.
00014  
00015   This program is distributed in the hope that it will be useful,
00016   but WITHOUT ANY WARRANTY;  without even the implied warranty of
00017   MERCHANTABILITY  or  FITNESS FOR A PARTICULAR PURPOSE.  See the
00018   GNU General Public License for more details.
00019  
00020   You should have received a copy of the GNU General Public License
00021   along with this program; if not, write to the Free Software
00022   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
00023 */
00024 
00025 #include <libheraia.h>
00026 
00027 /**
00028  *  General background : 
00029  *  Each treatment has to provide 4 functions :
00030  *   - one that will do the treatment (execute it) with the treatment parameters (if any) [do_it]
00031  *     take a GList of values_t * and return a GList of values_t *
00032  *   - one that will init the treatment with defaults values (widget creation included, if any) [init]
00033  *   - one that will delete the treatment, widgets included (if any) [kill] 
00034  *   - one that will copy the internal structures (widgets will be created by the init function) [copy]
00035  */
00036 
00037 static GList *treatment_reverse_do_it(GList *gchar_list);
00038 static gpointer treatment_reverse_copy(gpointer data);
00039 static void treatment_reverse_init(gpointer data);
00040 static void treatment_reverse_kill(gpointer data);
00041 
00042 
00043 value_t *new_value_t(guint length, guchar *bin_data)
00044 {
00045         value_t *a_value = NULL;
00046 
00047         a_value = (value_t*) g_malloc0(sizeof(value_t));
00048         
00049         a_value->length = length;
00050 
00051         a_value->bytes = (guchar *) g_malloc0(length * sizeof(guchar));
00052 
00053         memcpy(a_value->bytes, bin_data, length);
00054 
00055         return a_value;
00056 }
00057 
00058 /**
00059  *  Copies the treatment and calls the treatment copy function that
00060  *  copies the data of the treatment itself
00061  */
00062 treatment_t *copy_treatment(treatment_t *tment)
00063 {
00064         treatment_t *new_tment = NULL;
00065 
00066         new_tment = (treatment_t *) g_malloc0(sizeof(treatment_t));
00067 
00068         if (tment != NULL)
00069                 {
00070                         if (tment->name != NULL)
00071                                 {
00072                                         new_tment->name = g_strdup(tment->name);
00073                                 }
00074                         else
00075                                 {
00076                                         new_tment->name = NULL;
00077                                 }
00078                         new_tment->do_it = tment->do_it;
00079                         new_tment->init = tment->init;
00080                         new_tment->kill = tment->kill;
00081                         new_tment->copy = tment->copy;
00082 
00083                         if (tment->copy != NULL)
00084                                 {
00085                                         new_tment->data = tment->copy(tment->data);
00086                                 }
00087     
00088                         return new_tment;
00089                 }
00090         else 
00091                 {
00092                         return NULL;
00093                 }
00094 }
00095 
00096 /**
00097  *  Finds a treatment, by name, in the treatment list
00098  *  returns the treatment if found, NULL otherwise
00099  */
00100 treatment_t *find_treatment(GList *tment_list, gchar *tment_name) 
00101 {
00102         treatment_t *tment = NULL;
00103 
00104         while (tment_list != NULL)
00105                 {
00106                         tment = (treatment_t *) tment_list->data;
00107                         if (g_ascii_strcasecmp(tment->name, tment_name) == 0)
00108                                 {
00109                                         return tment;
00110                                 }
00111                         else
00112                                 {
00113                                         tment_list = g_list_next(tment_list);
00114                                 }
00115                 }
00116 
00117         return NULL;
00118 }
00119 
00120 /**
00121  *  Here we do init the available treatment list
00122  *  Should only be called once at init time
00123  */
00124 GList *init_treatments(void)
00125 {
00126         treatment_t *new_tment = NULL;
00127         GList *list = NULL;
00128   
00129         /* reverse treatment */
00130         new_tment = (treatment_t *) g_malloc0(sizeof(treatment_t));
00131         new_tment->name = g_strdup("reverse");
00132         new_tment->init = treatment_reverse_init;
00133         new_tment->do_it = treatment_reverse_do_it;
00134         new_tment->kill = treatment_reverse_kill;
00135         new_tment->copy = treatment_reverse_copy;
00136         list = g_list_prepend(list, new_tment);
00137  
00138         return list;
00139 }
00140 
00141 /******************* Here begins the treatments definitions *******************/
00142 
00143 /**
00144  *  Treatment initialisation (called at runtime when selected by the user)
00145  */
00146 static void treatment_reverse_init(gpointer data)
00147 {
00148         treatment_t *tment = (treatment_t *) data;
00149         /**
00150          *  We may init :
00151          *  . structures and widgets (if any) an put all this in 'data'
00152          */
00153   
00154         /* In this treatment there is no need for any widget nor structures */
00155         tment->data = NULL;
00156 }
00157 
00158 /**
00159  *  Deletes treatment (itself)
00160  */
00161 static void treatment_reverse_kill(gpointer data)
00162 {
00163         treatment_t *tment = (treatment_t *) data;
00164         /**
00165          *  As there is no data nor structure, there is nothing to free !!
00166          */
00167   
00168         if (tment != NULL)
00169                 {
00170                         g_free(tment);
00171                 }
00172 }
00173 
00174 /**
00175  *  Copy treatment's datas 
00176  */
00177 static gpointer treatment_reverse_copy(gpointer data)
00178 {
00179         /* As there is no data, there is nothing to do ! */
00180 
00181         return data;
00182 }
00183 
00184 /**
00185  *  Reverse every lists that contains bytes from the hexwidget
00186  */
00187 static GList *treatment_reverse_do_it(GList *values_list)
00188 {
00189         GList *head = NULL;
00190         value_t *extracted = NULL;
00191         guchar *reversed = NULL;
00192         guint i = 0;
00193 
00194         head = values_list;
00195 
00196         while (values_list != NULL)
00197                 {
00198                         /* here we reverse the bytes in the values_t * structure */
00199                         extracted = (value_t *) values_list->data;
00200                         reversed = (guchar *) g_malloc0(sizeof(guchar) * extracted->length);
00201 
00202                         for (i=0; i<extracted->length; i++)
00203                                 {
00204                                         reversed[i] = extracted->bytes[extracted->length-1-i];
00205                                 }
00206                         g_free(extracted->bytes);
00207                         extracted->bytes = reversed;
00208 
00209                         values_list = g_list_next(values_list);
00210                 }
00211 
00212         return head;
00213 }
00214 
00215 
00216 

Generated on Tue Jun 30 23:18:17 2009 for Heraia by  doxygen 1.5.8