-
Notifications
You must be signed in to change notification settings - Fork 0
/
haiti.c
214 lines (196 loc) · 4.32 KB
/
haiti.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#define _DEFAULT_SOURCE
#define _POSIX_C_SOURCE 200809L // c99 warning
#define _ISOC99_SOURCE
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include "filetype.h"
#ifdef USE_GITHUB
extern void haiti_github(char** file, int n);
#endif
#include "haiti.h"
uint n_dirs = 0;
uint n_file = 0;
uint n_tags = 0;
pstr dirs = NULL;
pstr file = NULL;
pstr tags = NULL;
static bool sync = 0;
static bool by_tag = 0;
// [string] find a string inside another
bool find(cstr str, pstr src, uint size)
{
uint i;
if(!str || !src)
return 0;
for(i = 0; i < size; i++)
if(!strcmp(str, src[i]))
return 1;
return 0;
}
// [string] get file extension
const cstr file_ext(cstr s)
{
uint i;
cstr c;
if(s[0] == '.')
return NULL;
if(!(c = strchr(s, '.')) || !(c++))
return NULL;
for(i = 0; i < sizeof(filetype)/sizeof(filetype[0]); i++)
if(!strcmp(c, filetype[i][0]))
return filetype[i][1];
return NULL;
}
// [string] append to a string array
static pstr append_cstr(pstr array, cstr str, uint* i)
{
if(!array) {
array = malloc(sizeof(pstr));
array[*i] = strdup(str);
} else {
array = realloc(array, (*i+1) * sizeof(pstr));
array[*i] = strdup(str);
}
(*i)++;
return array;
}
// [string] concatenate two strings arrays
static pstr append_pstr(pstr dest, pstr src, uint* dest_size, uint src_size)
{
uint i, size;
for(i = 0; i < src_size; i++) {
size = *dest_size + i;
dest = append_cstr(dest, src[i], &size);
}
*dest_size += src_size;
return dest;
}
// [string] free strings array
static void free_array(pstr a, uint size)
{
uint i;
if(!a)
return;
for(i = 0; i < size; i++)
free(a[i]);
free(a);
}
// [string] remove duplicates in a string array
static pstr remove_dup(pstr src, uint* size)
{
uint i, count = 0;
pstr a = NULL;
for(i = 0; i < *size; i++)
if(!find(src[i], a, count))
a = append_cstr(a, src[i], &count);
free_array(src, *size);
*size = count;
return a;
}
// [string] remove duplicates in a string array
static pstr list_dir(cstr dirname, uint* n)
{
uint i, count = 0;
pstr ret = NULL;
cstr path = realpath(dirname, NULL);
struct dirent** namelist;
*n = scandir(dirname, &namelist, NULL, NULL);
for(i = 0; i < *n; i++)
{
char c[strlen(path) + 1 + strlen(namelist[i]->d_name)];
if(file_ext(namelist[i]->d_name)) {
memset(c, 0, sizeof(c));
strcat(strcat(strcat(c, path), "/"), namelist[i]->d_name);
ret = append_cstr(ret, c, &count);
}
free(namelist[i]);
}
free(namelist);
free(path);
*n = count;
return ret;
}
static int sort(const void* a, const void* b)
{
return strcmp(*(pstr)a, *(pstr)b);
}
static void haiti_sort()
{
uint i;
for(i = 0; i < n_dirs; i++) {
uint size = 0;
pstr list = list_dir(dirs[i], &size);
file = append_pstr(file, list, &n_file, size);
free_array(list, size);
}
file = remove_dup(file, &n_file);
qsort(file, n_file, sizeof(pstr), sort);
tags = remove_dup(tags, &n_tags);
qsort(tags, n_tags, sizeof(pstr), sort);
}
static void haiti_parse(int argc, char** argv)
{
uint i;
for(i = 1; i < argc; i++) {
struct stat s;
if(!strcmp(argv[i], "-sync")) {
sync = 1;
continue;
}
else if(!strcmp(argv[i], "-tags")) {
by_tag = 1;
continue;
}
if(stat(argv[i],&s) == 0) {
cstr path = realpath(argv[i], NULL);
if(s.st_mode & S_IFDIR)
dirs = append_cstr(dirs, path, &n_dirs);
else if(s.st_mode & S_IFREG)
file = append_cstr(file, path, &n_file);
else
printf("'%s' is not a regular file\n", argv[i]);
if(path)
free(path);
}
else
tags = append_cstr(tags, argv[i], &n_tags);
}
}
static void haiti_sync()
{
if(sync)
#ifdef USE_GITHUB
haiti_github(file, n_file);
#else
fprintf(stderr, "-sync option not enabled. please recompile with '-DUSE_GITHUB'.\n");
#endif
}
// [haiti] print
static void haiti_print()
{
if(by_tag)
haiti_print_tags();
else
haiti_print_file();
}
// [haiti] clean
static void haiti_clean()
{
free_array(dirs, n_dirs);
free_array(tags, n_tags);
free_array(file, n_file);
}
// [main] this is the main function
int main(int argc, pstr argv)
{
haiti_parse(argc, argv);
haiti_sort();
haiti_sync();
haiti_print();
haiti_clean();
return 0;
}