Initial Commit
[Projects.git] / pkgbuilds / wiifuse / src / wiifuse-0.2.0 / tree.c
blob6a2c94293b7e2a8132de5e70f5ddf86457d2df57
1 /*
2 * Copyright (C) 2008 dhewg, #wiidev efnet
3 * based on code by:
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>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <string.h>
30 #include "io.h"
31 #include "tree.h"
32 #include "image.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)));
39 node->part = part;
40 node->type = type;
41 node->name = strdup (name);
42 node->next = root->sub;
43 root->sub = node;
44 node->v1 = v1;
45 node->v2 = v2;
47 if (type == TREE_DIR)
48 root->nsubdirs++;
50 return node;
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,
77 const u64 value) {
78 char str[22];
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,
85 const u8 value) {
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,
90 const u16 value) {
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,
95 const u32 value) {
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,
100 const u64 value) {
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,
105 const char c) {
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,
110 const u8 b) {
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,
121 const char *dst) {
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;
128 const char *next;
130 if (!root)
131 return NULL;
133 if (!strcmp (path, root->name))
134 return root;
136 if (strncmp (path, root->name, strlen (root->name)))
137 return NULL;
139 next = path + strlen (root->name);
140 if (*next != '/')
141 return NULL;
142 next++;
144 if (!*next)
145 return root;
147 node = root->sub;
148 while (node) {
149 ret = tree_find_entry (node, next);
150 if (ret)
151 return ret;
153 node = node->next;
156 return NULL;
159 void tree_free (struct tree *root) {
160 struct tree *node, *next;
162 node = root->sub;
163 while (node) {
164 next = node->next;
165 tree_free (node);
166 node = next;
169 if (root->type == TREE_SYMLINK)
170 free ((char *) ((size_t) root->v1));
172 free (root->name);
173 free (root);
176 struct tree *tree_empty (void) {
177 struct tree *ret;
179 ret = (struct tree *) malloc (sizeof (struct tree));
180 memset (ret, 0, (sizeof (struct tree)));
182 ret->name = strdup ("");
183 ret->type = TREE_DIR;
185 return ret;