-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilemanager.c
64 lines (47 loc) · 1.48 KB
/
filemanager.c
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
#include "filemanager.h"
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "io.h"
#define FILE_TABLE_SIZE 1000
static bool endsWith(const char *fileName, const char *ending) {
if (strlen(ending) > strlen(fileName))
return false;
return strcmp(&fileName[strlen(fileName)-strlen(ending)], ending) == 0;
}
unsigned fileTableLength(FileTable fileTable) {
unsigned length = 0;
for (FileItem *item = fileTable; item->fileName != NULL; item++)
length++;
return length;
}
FileItem fileTableElement(FileTable fileTable, unsigned index) {
return fileTable[index];
}
static void freeFileNames(char **fileNames) {
for (int i = 0; fileNames[i] != NULL; i++)
free(fileNames[i]);
free(fileNames);
}
FileTable getTranslationUnitsFromCurrentDirectory(void) {
char **fileNames = getFilesInCurrentDirectory();
int length = 0;
for (unsigned i = 0; fileNames[i] != NULL; i++)
length++;
FileTable fileTable = (FileTable)malloc((length+1)*sizeof(FileItem));
fileTable[0].fileName = NULL;
int i = 0;
for (char **f = fileNames; *f != NULL; f++) {
if (endsWith(*f, ".c")) {
fileTable[i].fileName = strdup(*f);
fileTable[++i].fileName = NULL;
}
}
freeFileNames(fileNames);
return fileTable;
}
void freeFileTable(FileTable fileTable) {
for (int i = 0; fileTable[i].fileName != NULL; i++)
free((char *)fileTable[i].fileName);
free(fileTable);
}