2 * Copyright (C) 2008 dhewg, #wiidev efnet
4 * Copyright (C) 2005 Janusz Dziemidowicz (rraptorr@nails.eu.org)
6 * this file is part of wiifuse
7 * http://wiibrew.org/index.php?title=Wiifuse
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <sys/types.h>
34 struct tree
*tree_add_entry (u32 part
, struct tree
*root
, enum tree_type type
,
35 const char *name
, u64 v1
, u64 v2
) {
36 struct tree
*node
= (struct tree
*) malloc (sizeof (struct tree
));
37 memset (node
, 0, (sizeof (struct tree
)));
41 node
->name
= strdup (name
);
42 node
->next
= root
->sub
;
53 struct tree
*tree_add_filler (u32 part
, struct tree
*root
, const char *name
) {
54 return tree_add_entry (part
, root
, TREE_FILE
, name
, 0, 0);
57 struct tree
*tree_add_dir (u32 part
, struct tree
*root
, const char *name
) {
58 return tree_add_entry (part
, root
, TREE_DIR
, name
, 0, 0);
61 struct tree
*tree_add_file (u32 part
, struct tree
*root
, const char *name
,
62 u64 offset
, u64 size
) {
63 return tree_add_entry (part
, root
, TREE_FILE
, name
, offset
, size
);
66 struct tree
*tree_add_raw_file (u32 part
, struct tree
*root
, const char *name
,
67 u64 offset
, u64 size
) {
68 return tree_add_entry (part
, root
, TREE_RAW_FILE
, name
, offset
, size
);
71 struct tree
*tree_add_mem (u32 part
, struct tree
*root
, const char *name
,
72 void *ptr
, size_t size
) {
73 return tree_add_entry (part
, root
, TREE_MEM
, name
, ptr
, size
);
76 struct tree
*tree_add_uint (u32 part
, struct tree
*root
, const char *name
,
79 sprintf (str
, "%llu", value
);
80 return tree_add_entry (part
, root
, TREE_UINT
, name
, value
,
81 g_strnlen (str
, 22) + 1);
84 struct tree
*tree_add_hex_uint8 (u32 part
, struct tree
*root
, const char *name
,
86 return tree_add_entry (part
, root
, TREE_HEX_UINT8
, name
, value
, 5);
89 struct tree
*tree_add_hex_uint16 (u32 part
, struct tree
*root
, const char *name
,
91 return tree_add_entry (part
, root
, TREE_HEX_UINT16
, name
, value
, 7);
94 struct tree
*tree_add_hex_uint32 (u32 part
, struct tree
*root
, const char *name
,
96 return tree_add_entry (part
, root
, TREE_HEX_UINT32
, name
, value
, 11);
99 struct tree
*tree_add_hex_uint64 (u32 part
, struct tree
*root
, const char *name
,
101 return tree_add_entry (part
, root
, TREE_HEX_UINT64
, name
, value
, 19);
104 struct tree
*tree_add_char (u32 part
, struct tree
*root
, const char *name
,
106 return tree_add_entry (part
, root
, TREE_CHAR
, name
, c
, 2);
109 struct tree
*tree_add_bool (u32 part
, struct tree
*root
, const char *name
,
111 return tree_add_char (part
, root
, name
, b
? '1' : '0');
114 struct tree
*tree_add_string (u32 part
, struct tree
*root
, const char *name
,
115 const char *str
, size_t size
) {
116 return tree_add_entry (part
, root
, TREE_STRING
, name
,
117 str
, g_strnlen (str
, size
) + 1);
120 struct tree
*tree_add_symlink (u32 part
, struct tree
*root
, const char *name
,
122 return tree_add_entry (part
, root
, TREE_SYMLINK
, name
,
123 strdup (dst
), strlen (dst
));
126 struct tree
*tree_find_entry (struct tree
*root
, const char *path
) {
127 struct tree
*node
, *ret
;
133 if (!strcmp (path
, root
->name
))
136 if (strncmp (path
, root
->name
, strlen (root
->name
)))
139 next
= path
+ strlen (root
->name
);
149 ret
= tree_find_entry (node
, next
);
159 void tree_free (struct tree
*root
) {
160 struct tree
*node
, *next
;
169 if (root
->type
== TREE_SYMLINK
)
170 free ((char *) ((size_t) root
->v1
));
176 struct tree
*tree_empty (void) {
179 ret
= (struct tree
*) malloc (sizeof (struct tree
));
180 memset (ret
, 0, (sizeof (struct tree
)));
182 ret
->name
= strdup ("");
183 ret
->type
= TREE_DIR
;