GPAK  1.0.0
A general-purpose archive library

◆ filesystem_tree_find_file()

GPAK_API filesystem_tree_file_t* filesystem_tree_find_file ( filesystem_tree_node_t _root,
const char *  _path 
)
Brief Description:\n Finds a file in the filesystem tree.

This function searches for a file node with the specified path in the filesystem tree.

Parameters
_rootA pointer to the root filesystem_tree_node_t.
_pathThe path of the file to find.
Returns
A pointer to the found file node or NULL if not found.

Definition at line 185 of file filesystem_tree.c.

185  {
186  if (!_root || !_path || !*_path)
187  return NULL;
188 
189  char* dir_path = strdup(_path);
190  char* dir_name = strtok(dir_path, "/");
191  char* file_name = NULL;
192 
193  filesystem_tree_node_t* current = _root;
194  while (dir_name)
195  {
196  file_name = strtok(NULL, "/");
197  if (file_name)
198  {
199  filesystem_tree_node_t* found = _find_directory(current, dir_name);
200  if (!found)
201  {
202  free(dir_path);
203  return NULL;
204  }
205 
206  current = found;
207  dir_name = file_name;
208  }
209  else
210  break;
211  }
212 
213  filesystem_tree_file_t* file = NULL;
214  if (dir_name && !file_name)
215  file = _find_file(current, dir_name);
216 
217  free(dir_path);
218  return file;
219 }

Referenced by gpak_find_file(), and gpak_fopen().