Commit 77c44a68 authored by Sebastian Ramacher's avatar Sebastian Ramacher

improve girara_list_position

parent e302e007
......@@ -113,4 +113,6 @@ uninstall:
.PHONY: all options clean debug test dist install uninstall ${PROJECT} ${PROJECT}-debug
-include $(wildcard .depend/*.dep)
TDEPENDS = ${OBJECTS:.o=.o.dep}
DEPENDS = ${TDEPENDS:^=.depend/}
-include ${DEPENDS}
......@@ -276,7 +276,7 @@ girara_list_size(girara_list_t* list)
return g_list_length(list->start);
}
int
ssize_t
girara_list_position(girara_list_t* list, void* data)
{
g_return_val_if_fail(list != NULL, -1);
......@@ -285,13 +285,14 @@ girara_list_position(girara_list_t* list, void* data)
return -1;
}
for (unsigned int i = 0; i < g_list_length(list->start); i++) {
GList* tmp = g_list_nth(list->start, i);
if (data == tmp->data) {
return i;
size_t pos = 0;
GIRARA_LIST_FOREACH(list, void*, iter, tmp)
if (tmp == data) {
girara_list_iterator_free(iter);
return pos;
}
}
++pos;
GIRARA_LIST_FOREACH_END(list, void*, iter, tmp);
return -1;
}
......
......@@ -5,6 +5,7 @@
#include <stddef.h>
#include <stdbool.h>
#include <sys/types.h>
#include "types.h"
/**
......@@ -112,7 +113,7 @@ size_t girara_list_size(girara_list_t* list);
* @param data The element
* @return The position or -1 if the data is not found
*/
int girara_list_position(girara_list_t* list, void* data);
ssize_t girara_list_position(girara_list_t* list, void* data);
/**
* Sort a list
......
......@@ -14,6 +14,7 @@
#include <unistd.h>
#include "utils.h"
#include "datastructures.h"
#define BLOCK_SIZE 64
......@@ -98,6 +99,23 @@ girara_get_xdg_path(girara_xdg_path_t path)
return NULL;
}
girara_list_t*
girara_split_path_array(const gchar* patharray)
{
if (patharray == NULL) {
return NULL;
}
girara_list_t* res = girara_list_new2(g_free);
gchar** paths = g_strsplit(patharray, ":", 0);
for (unsigned int i = 0; paths[i] != '\0'; ++i) {
girara_list_append(res, g_strdup(paths[i]));
}
g_strfreev(paths);
return res;
}
FILE*
girara_file_open(const char* path, char* mode)
{
......
......@@ -33,6 +33,13 @@ gchar* girara_get_home_directory(const gchar* user);
*/
gchar* girara_get_xdg_path(girara_xdg_path_t path);
/**
* Splits paths seperated by : (as in $PATH) into a list.
* @param patharray $PATH like string to split
* @return a list of paths and NULL on failure.
*/
girara_list_t* girara_split_path_array(const gchar* patharray);
/**
* Returns a "fixed" version of path. Which means, it will be an absolute path
* and fully expanded. ~ and ~user will be replaced by the current user's home
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment