Merge commit 'git-svn'
[anjuta-git-plugin.git] / plugins / project-wizard / file.c
blob98dfca05044e1e0848c63eade53cf0420768728f
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 file.c
4 Copyright (C) 2004 Sebastien Granjoux
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 2 of the License, or
9 (at your option) any later version.
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.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 * File data read in .wiz file
24 *---------------------------------------------------------------------------*/
26 #include <config.h>
28 #include "file.h"
30 #include <glib/gdir.h>
32 /*---------------------------------------------------------------------------*/
34 #define STRING_CHUNK_SIZE 256
36 /*---------------------------------------------------------------------------*/
38 struct _NPWFileList
40 GNode* list;
41 GStringChunk* string_pool;
42 GMemChunk* data_pool;
45 struct _NPWFile {
46 NPWFileType type;
47 gchar* source;
48 gchar* destination;
49 gint attribute;
50 NPWFileList* owner;
51 GNode* node;
54 typedef enum {
55 NPW_EXECUTE_FILE = 1 << 0,
56 NPW_PROJECT_FILE = 1 << 1,
57 NPW_AUTOGEN_SET = 1 << 2,
58 NPW_AUTOGEN_FILE = 1 << 3
59 } NPWFileAttributes;
61 /* File object
62 *---------------------------------------------------------------------------*/
64 NPWFile*
65 npw_file_new (NPWFileList* owner)
67 NPWFile* this;
69 g_return_val_if_fail (owner, NULL);
71 this = g_chunk_new0(NPWFile, owner->data_pool);
72 this->owner = owner;
73 this->node = g_node_append_data (owner->list, this);
74 this->attribute = 0;
76 return this;
79 void
80 npw_file_free (NPWFile* this)
82 GNode* node;
84 node = g_node_find_child (this->owner->list, G_TRAVERSE_ALL, this);
85 if (node != NULL)
87 g_node_destroy (node);
88 /* Memory allocated in string pool and project pool is not free */
92 void
93 npw_file_set_type (NPWFile* this, NPWFileType type)
95 this->type = type;
98 NPWFileType
99 npw_file_get_type (const NPWFile* this)
101 return this->type;
104 void
105 npw_file_set_destination (NPWFile* this, const gchar* destination)
107 this->destination = g_string_chunk_insert (this->owner->string_pool, destination);
110 const gchar*
111 npw_file_get_destination (const NPWFile* this)
113 return this->destination;
116 void
117 npw_file_set_source (NPWFile* this, const gchar* source)
119 this->source = g_string_chunk_insert (this->owner->string_pool, source);
122 const gchar*
123 npw_file_get_source (const NPWFile* this)
125 return this->source;
128 void
129 npw_file_set_execute (NPWFile* this, gboolean value)
131 if (value)
133 this->attribute |= NPW_EXECUTE_FILE;
135 else
137 this->attribute &= ~NPW_EXECUTE_FILE;
141 gboolean
142 npw_file_get_execute (const NPWFile* this)
144 return this->attribute & NPW_EXECUTE_FILE;
147 void
148 npw_file_set_project (NPWFile* this, gboolean value)
150 if (value)
152 this->attribute |= NPW_PROJECT_FILE;
154 else
156 this->attribute &= ~NPW_PROJECT_FILE;
160 gboolean
161 npw_file_get_project (const NPWFile* this)
163 return this->attribute & NPW_PROJECT_FILE;
166 void
167 npw_file_set_autogen (NPWFile* this, NPWFileBooleanValue value)
169 switch (value)
171 case NPW_FILE_TRUE:
172 this->attribute |= NPW_AUTOGEN_FILE | NPW_AUTOGEN_SET;
173 break;
174 case NPW_FILE_FALSE:
175 this->attribute |= NPW_AUTOGEN_SET;
176 this->attribute &= ~NPW_AUTOGEN_FILE;
177 break;
178 case NPW_FILE_DEFAULT:
179 this->attribute &= ~(NPW_AUTOGEN_SET | NPW_AUTOGEN_FILE);
180 break;
184 NPWFileBooleanValue
185 npw_file_get_autogen (const NPWFile* this)
187 return this->attribute & NPW_AUTOGEN_SET ? (this->attribute & NPW_AUTOGEN_FILE ? NPW_FILE_TRUE : NPW_FILE_FALSE) : NPW_FILE_DEFAULT;
190 const NPWFile*
191 npw_file_next (const NPWFile* this)
193 GNode* node = this->node->next;
195 return node == NULL ? NULL : (NPWFile *)node->data;
198 /* File list object
199 *---------------------------------------------------------------------------*/
201 NPWFileList*
202 npw_file_list_new (void)
204 NPWFileList* this;
206 this = g_new (NPWFileList, 1);
207 this->string_pool = g_string_chunk_new (STRING_CHUNK_SIZE);
208 this->data_pool = g_mem_chunk_new ("file pool", sizeof (NPWFile), STRING_CHUNK_SIZE * sizeof (NPWFile) / 4, G_ALLOC_ONLY);
209 this->list = g_node_new (NULL);
211 return this;
214 void
215 npw_file_list_free (NPWFileList* this)
217 g_return_if_fail (this != NULL);
219 g_string_chunk_free (this->string_pool);
220 g_mem_chunk_destroy (this->data_pool);
221 g_node_destroy (this->list);
222 g_free (this);
225 static void
226 cb_file_list_foreach_file (GNode* node, gpointer data)
228 ((NPWFileForeachFunc)data)((NPWFile*)node->data);
231 void
232 npw_file_list_foreach_file (const NPWFileList* this, NPWFileForeachFunc func)
234 g_node_children_foreach (this->list, G_TRAVERSE_LEAFS, cb_file_list_foreach_file, (gpointer)func);
237 const NPWFile*
238 npw_file_list_first (const NPWFileList* this)
240 /* Should work even if first child is NULL (empty list) */
241 GNode* node = g_node_first_child (this->list);
243 return node == NULL ? NULL : (NPWFile *)node->data;