1 // loc.c written by Mitchell Foral. mitchell<att>caladbolg.net.
2 // See COPYING for license information.
11 Loc *ohcount_loc_new(const char *language, int code, int comments, int blanks,
13 Loc *loc = malloc(sizeof(Loc));
14 loc->language = language;
16 loc->comments = comments;
18 loc->filecount = filecount;
22 int ohcount_loc_total(Loc *loc) {
23 return loc->code + loc->comments + loc->blanks;
26 void ohcount_loc_add_loc(Loc *loc, Loc *other) {
27 if (strcmp(loc->language, other->language) == 0) {
28 loc->code += other->code;
29 loc->comments += other->comments;
30 loc->blanks += other->blanks;
31 loc->filecount += other->filecount;
35 int ohcount_loc_is_equal(Loc *loc, Loc *other) {
36 return strcmp(loc->language, other->language) == 0 &&
37 loc->code == other->code && loc->comments == other->comments &&
38 loc->blanks == other->blanks && loc->filecount == other->filecount;
41 void ohcount_loc_free(Loc *loc) {
47 LocList *ohcount_loc_list_new() {
48 LocList *list = malloc(sizeof(LocList));
56 void ohcount_loc_list_add_loc(LocList *list, Loc *loc) {
57 if (list->head == NULL) { // empty list
60 list->head->loc = ohcount_loc_new(loc->language, loc->code, loc->comments,
61 loc->blanks, loc->filecount);
62 list->head->next = NULL;
64 LocList *iter = list->head;
66 if (iter->loc && strcmp(iter->loc->language, loc->language) == 0) break;
69 if (iter == NULL) { // new language
70 LocList *item = ohcount_loc_list_new();
71 item->loc = ohcount_loc_new(loc->language, loc->code, loc->comments,
72 loc->blanks, loc->filecount);
73 list->tail->next = item;
75 } else ohcount_loc_add_loc(iter->loc, loc); // existing language
79 void ohcount_loc_list_add_loc_list(LocList *list, LocList *other) {
80 LocList *iter = other->head;
82 ohcount_loc_list_add_loc(list, iter->loc);
87 Loc *ohcount_loc_list_get_loc(LocList *list, const char *language) {
88 LocList *iter = list->head;
90 if (strcmp(iter->loc->language, language) == 0) return iter->loc;
96 int ohcount_loc_list_code(LocList *list) {
98 LocList *iter = list->head;
100 sum += iter->loc->code;
106 int ohcount_loc_list_comments(LocList *list) {
108 LocList *iter = list->head;
110 sum += iter->loc->comments;
116 int ohcount_loc_list_blanks(LocList *list) {
118 LocList *iter = list->head;
120 sum += iter->loc->blanks;
126 int ohcount_loc_list_total(LocList *list) {
128 LocList *iter = list->head;
130 sum += ohcount_loc_total(iter->loc);
136 int ohcount_loc_list_filecount(LocList *list) {
138 LocList *iter = list->head;
140 sum += iter->loc->filecount;
146 LocList *ohcount_loc_list_new_compact(LocList *list) {
147 LocList *new_list = ohcount_loc_list_new();
148 LocList *iter = list->head;
150 if (ohcount_loc_total(iter->loc) != 0)
151 ohcount_loc_list_add_loc(new_list, iter->loc);
157 void ohcount_loc_list_free(LocList *list) {
159 LocList *iter = list->head;
161 LocList *next = iter->next;
162 ohcount_loc_free(iter->loc);
171 LocDelta *ohcount_loc_delta_new(const char *language, int code_added,
172 int code_removed, int comments_added,
173 int comments_removed, int blanks_added,
174 int blanks_removed) {
175 LocDelta *delta = malloc(sizeof(LocDelta));
176 delta->language = language;
177 delta->code_added = code_added;
178 delta->code_removed = code_removed;
179 delta->comments_added = comments_added;
180 delta->comments_removed = comments_removed;
181 delta->blanks_added = blanks_added;
182 delta->blanks_removed = blanks_removed;
186 int ohcount_loc_delta_net_code(LocDelta *delta) {
187 return delta->code_added - delta->code_removed;
190 int ohcount_loc_delta_net_comments(LocDelta *delta) {
191 return delta->comments_added - delta->comments_removed;
194 int ohcount_loc_delta_net_blanks(LocDelta *delta) {
195 return delta->blanks_added - delta->blanks_removed;
198 int ohcount_loc_delta_net_total(LocDelta *delta) {
199 return ohcount_loc_delta_net_code(delta) +
200 ohcount_loc_delta_net_comments(delta) +
201 ohcount_loc_delta_net_blanks(delta);
204 void ohcount_loc_delta_add_loc_delta(LocDelta *delta, LocDelta *other) {
205 if (strcmp(delta->language, other->language) == 0) {
206 delta->code_added += other->code_added;
207 delta->code_removed += other->code_removed;
208 delta->comments_added += other->comments_added;
209 delta->comments_removed += other->comments_removed;
210 delta->blanks_added += other->blanks_added;
211 delta->blanks_removed += other->blanks_removed;
215 int ohcount_loc_delta_is_changed(LocDelta *delta) {
216 return delta->code_added != 0 || delta->code_removed != 0 ||
217 delta->comments_added != 0 || delta->comments_removed != 0 ||
218 delta->blanks_added != 0 || delta->blanks_removed != 0;
221 int ohcount_loc_delta_is_equal(LocDelta *delta, LocDelta *other) {
222 return strcmp(delta->language, other->language) == 0 &&
223 delta->code_added == other->code_added &&
224 delta->code_removed == other->code_removed &&
225 delta->comments_added == other->comments_added &&
226 delta->comments_removed == other->comments_removed &&
227 delta->blanks_added == other->blanks_added &&
228 delta->blanks_removed == other->blanks_removed;
231 void ohcount_loc_delta_free(LocDelta *delta) {
237 LocDeltaList *ohcount_loc_delta_list_new() {
238 LocDeltaList *list = malloc(sizeof(LocDeltaList));
246 void ohcount_loc_delta_list_add_loc_delta(LocDeltaList *list, LocDelta *delta) {
247 if (list->head == NULL) { // empty list
250 list->head->delta = ohcount_loc_delta_new(delta->language,
253 delta->comments_added,
254 delta->comments_removed,
256 delta->blanks_removed);
257 list->head->next = NULL;
259 LocDeltaList *iter = list->head;
261 if (list->delta && strcmp(list->delta->language, delta->language) == 0) break;
264 if (iter == NULL) { // new language
265 LocDeltaList *item = ohcount_loc_delta_list_new();
266 item->delta = ohcount_loc_delta_new(delta->language,
269 delta->comments_added,
270 delta->comments_removed,
272 delta->blanks_removed);
273 list->tail->next = item;
275 } else ohcount_loc_delta_add_loc_delta(iter->delta, delta); // existing
279 void ohcount_loc_delta_list_add_loc_delta_list(LocDeltaList *list,
280 LocDeltaList *loc_delta_list) {
281 LocDeltaList *iter = loc_delta_list->head;
283 ohcount_loc_delta_list_add_loc_delta(list, iter->delta);
288 LocDelta *ohcount_loc_delta_list_get_loc_delta(LocDeltaList *list,
289 const char *language) {
290 LocDeltaList *iter = list->head;
292 if (strcmp(iter->delta->language, language) == 0) return iter->delta;
298 int ohcount_loc_delta_list_code_added(LocDeltaList *list) {
300 LocDeltaList *iter = list->head;
302 sum += iter->delta->code_added;
308 int ohcount_loc_delta_list_code_removed(LocDeltaList *list) {
310 LocDeltaList *iter = list->head;
312 sum += iter->delta->code_removed;
318 int ohcount_loc_delta_list_comments_added(LocDeltaList *list) {
320 LocDeltaList *iter = list->head;
322 sum += iter->delta->comments_added;
328 int ohcount_loc_delta_list_comments_removed(LocDeltaList *list) {
330 LocDeltaList *iter = list->head;
332 sum += iter->delta->comments_removed;
338 int ohcount_loc_delta_list_blanks_added(LocDeltaList *list) {
340 LocDeltaList *iter = list->head;
342 sum += iter->delta->blanks_added;
348 int ohcount_loc_delta_list_blanks_removed(LocDeltaList *list) {
350 LocDeltaList *iter = list->head;
352 sum += iter->delta->blanks_removed;
358 int ohcount_loc_delta_list_net_code(LocDeltaList *list) {
360 LocDeltaList *iter = list->head;
362 sum += ohcount_loc_delta_net_code(iter->delta);
368 int ohcount_loc_delta_list_net_comments(LocDeltaList *list) {
370 LocDeltaList *iter = list->head;
372 sum += ohcount_loc_delta_net_comments(iter->delta);
378 int ohcount_loc_delta_list_net_blanks(LocDeltaList *list) {
380 LocDeltaList *iter = list->head;
382 sum += ohcount_loc_delta_net_blanks(iter->delta);
388 int ohcount_loc_delta_list_net_total(LocDeltaList *list) {
390 LocDeltaList *iter = list->head;
392 sum += ohcount_loc_delta_net_total(iter->delta);
398 LocDeltaList *ohcount_loc_delta_list_new_compact(LocDeltaList *list) {
399 LocDeltaList *new_list = ohcount_loc_delta_list_new();
400 LocDeltaList *iter = list->head;
402 if (ohcount_loc_delta_is_changed(iter->delta))
403 ohcount_loc_delta_list_add_loc_delta(new_list, iter->delta);
409 void ohcount_loc_delta_list_free(LocDeltaList *list) {
411 LocDeltaList *iter = list->head;
413 LocDeltaList *next = iter->next;
414 ohcount_loc_delta_free(iter->delta);