-
Notifications
You must be signed in to change notification settings - Fork 2
/
loc.fb
202 lines (147 loc) · 3.35 KB
/
loc.fb
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
struct Data {
extension *u8
files i32
lines i32
}
func main() i32 {
var data = Data {
extension: ".go",
files: 0,
lines: 0
};
directory(&data, ".");
printf("Found %d files with the extension '%s'\n", data.files, data.extension);
printf("Line count: %d\n", data.lines);
return 0;
}
func directory(data *Data, path *u8) {
var dir = opendir(path);
for {
var entry = readdir(dir);
if (entry == nil) {
break;
}
var child = combine(path, &entry.name[0]);
if (entry.type == 4) {
// Directory
if (!equals(&entry.name[0], ".") && !equals(&entry.name[0], "..")) {
directory(data, child);
}
}
else if (entry.type == 8) {
// File
file(data, child);
}
free(child);
}
closedir(dir);
}
func file(data *Data, path *u8) {
if (endsWith(path, data.extension)) {
data.files += 1;
data.lines += getLineCount(path);
}
}
func getLineCount(path *u8) i32 {
var lines = 0;
var stream = fopen(path, "rb");
var str [1024]u8;
for {
var read = fread(&str[0], 1 as u64, 1024 as u64, stream) as i32;
if (read == 0) {
break;
}
lines += count(&str[0], read, '\n');
}
fclose(stream);
return lines;
}
func count(str *u8, len i32, char u8) i32 {
var i = 0;
var count = 0;
for {
if (i >= len) {
break;
}
if (str[i] == char) {
count += 1;
}
i += 1;
}
return count;
}
func combine(left *u8, right *u8) *u8 {
var leftLen = length(left);
var rightLen = length(right);
var len = leftLen + 1 + rightLen + 1;
var str = malloc(len as u64) as *u8;
memcpy(str, left, leftLen as u64);
str[leftLen] = '/';
memcpy(&str[leftLen + 1], right, rightLen as u64);
str[len - 1] = '\0';
return str;
}
func length(str *u8) i32 {
var i = 0;
for {
var ch = str[i];
if (ch == '\0') {
break;
}
i += 1;
}
return i;
}
func equals(str1 *u8, str2 *u8) bool {
var i = 0;
for {
var ch1 = str1[i];
var ch2 = str2[i];
if (ch1 != ch2) {
return false;
}
if (ch1 == '\0' && ch2 == '\0') {
break;
}
i += 1;
}
return true;
}
func endsWith(str *u8, end *u8) bool {
var strLen = length(str);
var endLen = length(end);
if (endLen > strLen) {
return false;
}
var i = 0;
for {
if (i >= endLen) {
break;
}
if (str[strLen - 1 - i] != end[endLen - 1 - i]) {
return false;
}
i += 1;
}
return true;
}
// LIBC
struct Dir {}
struct DirEntry {
ino i64
off i64
reclen i16
type i8
name [256]u8
}
struct File {}
extern func printf(format *u8, ...) void
extern func malloc(size u64) *void
extern func free(ptr *void) void
extern func memcpy(dest *void, src *void, size u64) void
extern func opendir(name *u8) *Dir
extern func closedir(dir *Dir) i32
extern func readdir(dir *Dir) *DirEntry
extern func fopen(path *u8, mode *u8) *File
extern func fclose(stream *File) i32
extern func fread(ptr *void, size u64, nmemb u64, stream *File) u64