1 /* GIMP - The GNU Image Manipulation Program
2  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3  *
4  * gimppluginmanager-data.c
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #include "config.h"
21 
22 #include <string.h>
23 
24 #include <gio/gio.h>
25 
26 #include "plug-in-types.h"
27 
28 #include "gimppluginmanager.h"
29 #include "gimppluginmanager-data.h"
30 
31 
32 typedef struct _GimpPlugInData GimpPlugInData;
33 
34 struct _GimpPlugInData
35 {
36   gchar  *identifier;
37   gint32  bytes;
38   guint8 *data;
39 };
40 
41 
42 /*  public functions  */
43 
44 void
gimp_plug_in_manager_data_free(GimpPlugInManager * manager)45 gimp_plug_in_manager_data_free (GimpPlugInManager *manager)
46 {
47   g_return_if_fail (GIMP_IS_PLUG_IN_MANAGER (manager));
48 
49   if (manager->data_list)
50     {
51       GList *list;
52 
53       for (list = manager->data_list;
54            list;
55            list = g_list_next (list))
56         {
57           GimpPlugInData *data = list->data;
58 
59           g_free (data->identifier);
60           g_free (data->data);
61           g_slice_free (GimpPlugInData, data);
62         }
63 
64       g_list_free (manager->data_list);
65       manager->data_list = NULL;
66     }
67 }
68 
69 void
gimp_plug_in_manager_set_data(GimpPlugInManager * manager,const gchar * identifier,gint32 bytes,const guint8 * data)70 gimp_plug_in_manager_set_data (GimpPlugInManager *manager,
71                                const gchar       *identifier,
72                                gint32             bytes,
73                                const guint8      *data)
74 {
75   GimpPlugInData *plug_in_data;
76   GList          *list;
77 
78   g_return_if_fail (GIMP_IS_PLUG_IN_MANAGER (manager));
79   g_return_if_fail (identifier != NULL);
80   g_return_if_fail (bytes > 0);
81   g_return_if_fail (data != NULL);
82 
83   for (list = manager->data_list; list; list = g_list_next (list))
84     {
85       plug_in_data = list->data;
86 
87       if (! strcmp (plug_in_data->identifier, identifier))
88         break;
89     }
90 
91   /* If there isn't already data with the specified identifier, create one */
92   if (list == NULL)
93     {
94       plug_in_data = g_slice_new0 (GimpPlugInData);
95       plug_in_data->identifier = g_strdup (identifier);
96 
97       manager->data_list = g_list_prepend (manager->data_list, plug_in_data);
98     }
99   else
100     {
101       g_free (plug_in_data->data);
102     }
103 
104   plug_in_data->bytes = bytes;
105   plug_in_data->data  = g_memdup (data, bytes);
106 }
107 
108 const guint8 *
gimp_plug_in_manager_get_data(GimpPlugInManager * manager,const gchar * identifier,gint32 * bytes)109 gimp_plug_in_manager_get_data (GimpPlugInManager *manager,
110                                const gchar       *identifier,
111                                gint32            *bytes)
112 {
113   GList *list;
114 
115   g_return_val_if_fail (GIMP_IS_PLUG_IN_MANAGER (manager), NULL);
116   g_return_val_if_fail (identifier != NULL, NULL);
117   g_return_val_if_fail (bytes != NULL, NULL);
118 
119   *bytes = 0;
120 
121   for (list = manager->data_list; list; list = g_list_next (list))
122     {
123       GimpPlugInData *plug_in_data = list->data;
124 
125       if (! strcmp (plug_in_data->identifier, identifier))
126         {
127           *bytes = plug_in_data->bytes;
128           return plug_in_data->data;
129         }
130     }
131 
132   return NULL;
133 }
134