block: don't put spaces around :
[ironout.git] / project.c
blobb71f8ac30ec4b02f2885a7846206eabee7dfc278
1 #include <dirent.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "project.h"
5 #include "strutils.h"
6 #include "utils.h"
8 #define MAXFILES 128
10 static int cfile_cmp(void *o1, void *o2)
12 struct cfile *c1 = *(struct cfile **) o1;
13 struct cfile *c2 = *(struct cfile **) o2;
14 return -strcmp(c1->name, c2->name);
17 struct project *project_init(char *root)
19 struct project *project;
20 struct dirent *dirent;
21 DIR *dir = opendir(root);
22 project = xmalloc(sizeof(*project));
23 project->files = xmalloc(sizeof(*project->files) * MAXFILES);
24 project->count = 0;
25 while ((dirent = readdir(dir))) {
26 struct cfile *cfile;
27 char *name = dirent->d_name;
28 if (startswith(name, "."))
29 continue;
30 if (endswith(name, ".h") || endswith(name, ".c")) {
31 cfile = cfile_init(name);
32 if (cfile)
33 project->files[project->count++] = cfile;
36 qsort(project->files, project->count,
37 sizeof(*project->files), cfile_cmp);
38 return project;
41 void project_free(struct project *project)
43 free(project->files);
44 free(project);
47 struct cfile *project_find(struct project *project, char *filename)
49 int i;
50 for (i = 0; i < project->count; i++)
51 if (!strcmp(project->files[i]->name, filename))
52 return project->files[i];
53 return NULL;