summaryrefslogtreecommitdiff
path: root/src/base/director.c
blob: 49522de5a4bd698a1dab25e9363977d2bea15dfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

#include <dirent.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>

#include "main.h"
#include "list.h"

typedef struct director_struct {
	char *path;
	list_t *list;
} director_t;

director_t *loadDirector(char *s)
{
	director_t *new;
	DIR *dir;
	struct dirent *item;
	char path[STR_PATH_SIZE];

	assert(s != 0);

	new = malloc(sizeof(director_t));
	memset(new, 0, sizeof(director_t));

	new->list = newList();

#ifndef __WIN32__
	if (s[0] == '/')
#else
	if (s[1] == ':')
#endif
		strcpy(path, s);

	else {
		// this is really correct aproach
		getcwd(path, STR_PATH_SIZE);
		strcat(path, "/");
		strcat(path, s);
	}

	new->path = strdup(path);
	dir = opendir(new->path);

	if (dir == NULL) {
		free(new->path);
		free(new);
		return NULL;
	}

	while ((item = readdir(dir)) != NULL)
		addList(new->list, strdup(item->d_name));

	closedir(dir);

	return new;
}

void destroyDirector(director_t * p)
{
	assert(p != NULL);

	destroyListItem(p->list, free);
	free(p->path);
	free(p);
}