Package list ohcount / lintian-fixes/main src / loc.c
lintian-fixes/main

Tree @lintian-fixes/main (Download .tar.gz)

loc.c @lintian-fixes/mainraw · history · blame

  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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// loc.c written by Mitchell Foral. mitchell<att>caladbolg.net.
// See COPYING for license information.

#include <stdlib.h>
#include <string.h>

#include "loc.h"

// Loc

Loc *ohcount_loc_new(const char *language, int code, int comments, int blanks,
                     int filecount) {
  Loc *loc = malloc(sizeof(Loc));
  loc->language = language;
  loc->code = code;
  loc->comments = comments;
  loc->blanks = blanks;
  loc->filecount = filecount;
  return loc;
}

int ohcount_loc_total(Loc *loc) {
  return loc->code + loc->comments + loc->blanks;
}

void ohcount_loc_add_loc(Loc *loc, Loc *other) {
  if (strcmp(loc->language, other->language) == 0) {
    loc->code += other->code;
    loc->comments += other->comments;
    loc->blanks += other->blanks;
    loc->filecount += other->filecount;
  }
}

int ohcount_loc_is_equal(Loc *loc, Loc *other) {
  return strcmp(loc->language, other->language) == 0 &&
         loc->code == other->code && loc->comments == other->comments &&
         loc->blanks == other->blanks && loc->filecount == other->filecount;
}

void ohcount_loc_free(Loc *loc) {
  free(loc);
}

// LocList

LocList *ohcount_loc_list_new() {
  LocList *list = malloc(sizeof(LocList));
  list->loc = NULL;
  list->next = NULL;
  list->head = NULL;
  list->tail = NULL;
  return list;
}

void ohcount_loc_list_add_loc(LocList *list, Loc *loc) {
  if (list->head == NULL) { // empty list
    list->head = list;
    list->tail = list;
    list->head->loc = ohcount_loc_new(loc->language, loc->code, loc->comments,
                                      loc->blanks, loc->filecount);
    list->head->next = NULL;
  } else {
    LocList *iter = list->head;
    while (iter) {
      if (iter->loc && strcmp(iter->loc->language, loc->language) == 0) break;
      iter = iter->next;
    }
    if (iter == NULL) { // new language
      LocList *item = ohcount_loc_list_new();
      item->loc = ohcount_loc_new(loc->language, loc->code, loc->comments,
                                  loc->blanks, loc->filecount);
      list->tail->next = item;
      list->tail = item;
    } else ohcount_loc_add_loc(iter->loc, loc); // existing language
  }
}

void ohcount_loc_list_add_loc_list(LocList *list, LocList *other) {
  LocList *iter = other->head;
  while (iter) {
    ohcount_loc_list_add_loc(list, iter->loc);
    iter = iter->next;
  }
}

Loc *ohcount_loc_list_get_loc(LocList *list, const char *language) {
  LocList *iter = list->head;
  while (iter) {
    if (strcmp(iter->loc->language, language) == 0) return iter->loc;
    iter = iter->next;
  }
  return NULL;
}

int ohcount_loc_list_code(LocList *list) {
  int sum = 0;
  LocList *iter = list->head;
  while (iter) {
    sum += iter->loc->code;
    iter = iter->next;
  }
  return sum;
}

int ohcount_loc_list_comments(LocList *list) {
  int sum = 0;
  LocList *iter = list->head;
  while (iter) {
    sum += iter->loc->comments;
    iter = iter->next;
  }
  return sum;
}

int ohcount_loc_list_blanks(LocList *list) {
  int sum = 0;
  LocList *iter = list->head;
  while (iter) {
    sum += iter->loc->blanks;
    iter = iter->next;
  }
  return sum;
}

int ohcount_loc_list_total(LocList *list) {
  int sum = 0;
  LocList *iter = list->head;
  while (iter) {
    sum += ohcount_loc_total(iter->loc);
    iter = iter->next;
  }
  return sum;
}

int ohcount_loc_list_filecount(LocList *list) {
  int sum = 0;
  LocList *iter = list->head;
  while (iter) {
    sum += iter->loc->filecount;
    iter = iter->next;
  }
  return sum;
}

LocList *ohcount_loc_list_new_compact(LocList *list) {
  LocList *new_list = ohcount_loc_list_new();
  LocList *iter = list->head;
  while (iter) {
    if (ohcount_loc_total(iter->loc) != 0)
      ohcount_loc_list_add_loc(new_list, iter->loc);
    iter = iter->next;
  }
  return new_list;
}

void ohcount_loc_list_free(LocList *list) {
  if (list->head) {
    LocList *iter = list->head;
    while (iter) {
      LocList *next = iter->next;
      ohcount_loc_free(iter->loc);
      free(iter);
      iter = next;
    }
  } else free(list);
}

// LocDelta

LocDelta *ohcount_loc_delta_new(const char *language, int code_added,
                                int code_removed, int comments_added,
                                int comments_removed, int blanks_added,
                                int blanks_removed) {
  LocDelta *delta = malloc(sizeof(LocDelta));
  delta->language = language;
  delta->code_added = code_added;
  delta->code_removed = code_removed;
  delta->comments_added = comments_added;
  delta->comments_removed = comments_removed;
  delta->blanks_added = blanks_added;
  delta->blanks_removed = blanks_removed;
  return delta;
}

int ohcount_loc_delta_net_code(LocDelta *delta) {
  return delta->code_added - delta->code_removed;
}

int ohcount_loc_delta_net_comments(LocDelta *delta) {
  return delta->comments_added - delta->comments_removed;
}

int ohcount_loc_delta_net_blanks(LocDelta *delta) {
  return delta->blanks_added - delta->blanks_removed;
}

int ohcount_loc_delta_net_total(LocDelta *delta) {
  return ohcount_loc_delta_net_code(delta) +
         ohcount_loc_delta_net_comments(delta) +
         ohcount_loc_delta_net_blanks(delta);
}

void ohcount_loc_delta_add_loc_delta(LocDelta *delta, LocDelta *other) {
  if (strcmp(delta->language, other->language) == 0) {
    delta->code_added += other->code_added;
    delta->code_removed += other->code_removed;
    delta->comments_added += other->comments_added;
    delta->comments_removed += other->comments_removed;
    delta->blanks_added += other->blanks_added;
    delta->blanks_removed += other->blanks_removed;
  }
}

int ohcount_loc_delta_is_changed(LocDelta *delta) {
  return delta->code_added != 0 || delta->code_removed != 0 ||
         delta->comments_added != 0 || delta->comments_removed != 0 ||
         delta->blanks_added != 0 || delta->blanks_removed != 0;
}

int ohcount_loc_delta_is_equal(LocDelta *delta, LocDelta *other) {
  return strcmp(delta->language, other->language) == 0 &&
         delta->code_added == other->code_added &&
         delta->code_removed == other->code_removed &&
         delta->comments_added == other->comments_added &&
         delta->comments_removed == other->comments_removed &&
         delta->blanks_added == other->blanks_added &&
         delta->blanks_removed == other->blanks_removed;
}

void ohcount_loc_delta_free(LocDelta *delta) {
  free(delta);
}

// LocDeltaList

LocDeltaList *ohcount_loc_delta_list_new() {
  LocDeltaList *list = malloc(sizeof(LocDeltaList));
  list->delta = NULL;
  list->next = NULL;
  list->head = NULL;
  list->tail = NULL;
  return list;
}

void ohcount_loc_delta_list_add_loc_delta(LocDeltaList *list, LocDelta *delta) {
  if (list->head == NULL) { // empty list
    list->head = list;
    list->tail = list;
    list->head->delta = ohcount_loc_delta_new(delta->language,
                                              delta->code_added,
                                              delta->code_removed,
                                              delta->comments_added,
                                              delta->comments_removed,
                                              delta->blanks_added,
                                              delta->blanks_removed);
    list->head->next = NULL;
  } else {
    LocDeltaList *iter = list->head;
    while (iter) {
      if (list->delta && strcmp(list->delta->language, delta->language) == 0) break;
      iter = iter->next;
    }
    if (iter == NULL) { // new language
      LocDeltaList *item = ohcount_loc_delta_list_new();
      item->delta = ohcount_loc_delta_new(delta->language,
                                          delta->code_added,
                                          delta->code_removed,
                                          delta->comments_added,
                                          delta->comments_removed,
                                          delta->blanks_added,
                                          delta->blanks_removed);
      list->tail->next = item;
      list->tail = item;
    } else ohcount_loc_delta_add_loc_delta(iter->delta, delta); // existing
  }
}

void ohcount_loc_delta_list_add_loc_delta_list(LocDeltaList *list,
                                               LocDeltaList *loc_delta_list) {
  LocDeltaList *iter = loc_delta_list->head;
  while (iter) {
    ohcount_loc_delta_list_add_loc_delta(list, iter->delta);
    iter = iter->next;
  }
}

LocDelta *ohcount_loc_delta_list_get_loc_delta(LocDeltaList *list,
                                               const char *language) {
  LocDeltaList *iter = list->head;
  while (iter) {
    if (strcmp(iter->delta->language, language) == 0) return iter->delta;
    iter = iter->next;
  }
  return NULL;
}

int ohcount_loc_delta_list_code_added(LocDeltaList *list) {
  int sum = 0;
  LocDeltaList *iter = list->head;
  while (iter) {
    sum += iter->delta->code_added;
    iter = iter->next;
  }
  return sum;
}

int ohcount_loc_delta_list_code_removed(LocDeltaList *list) {
  int sum = 0;
  LocDeltaList *iter = list->head;
  while (iter) {
    sum += iter->delta->code_removed;
    iter = iter->next;
  }
  return sum;
}

int ohcount_loc_delta_list_comments_added(LocDeltaList *list) {
  int sum = 0;
  LocDeltaList *iter = list->head;
  while (iter) {
    sum += iter->delta->comments_added;
    iter = iter->next;
  }
  return sum;
}

int ohcount_loc_delta_list_comments_removed(LocDeltaList *list) {
  int sum = 0;
  LocDeltaList *iter = list->head;
  while (iter) {
    sum += iter->delta->comments_removed;
    iter = iter->next;
  }
  return sum;
}

int ohcount_loc_delta_list_blanks_added(LocDeltaList *list) {
  int sum = 0;
  LocDeltaList *iter = list->head;
  while (iter) {
    sum += iter->delta->blanks_added;
    iter = iter->next;
  }
  return sum;
}

int ohcount_loc_delta_list_blanks_removed(LocDeltaList *list) {
  int sum = 0;
  LocDeltaList *iter = list->head;
  while (iter) {
    sum += iter->delta->blanks_removed;
    iter = iter->next;
  }
  return sum;
}

int ohcount_loc_delta_list_net_code(LocDeltaList *list) {
  int sum = 0;
  LocDeltaList *iter = list->head;
  while (iter) {
    sum += ohcount_loc_delta_net_code(iter->delta);
    iter = iter->next;
  }
  return sum;
}

int ohcount_loc_delta_list_net_comments(LocDeltaList *list) {
  int sum = 0;
  LocDeltaList *iter = list->head;
  while (iter) {
    sum += ohcount_loc_delta_net_comments(iter->delta);
    iter = iter->next;
  }
  return sum;
}

int ohcount_loc_delta_list_net_blanks(LocDeltaList *list) {
  int sum = 0;
  LocDeltaList *iter = list->head;
  while (iter) {
    sum += ohcount_loc_delta_net_blanks(iter->delta);
    iter = iter->next;
  }
  return sum;
}

int ohcount_loc_delta_list_net_total(LocDeltaList *list) {
  int sum = 0;
  LocDeltaList *iter = list->head;
  while (iter) {
    sum += ohcount_loc_delta_net_total(iter->delta);
    iter = iter->next;
  }
  return sum;
}

LocDeltaList *ohcount_loc_delta_list_new_compact(LocDeltaList *list) {
  LocDeltaList *new_list = ohcount_loc_delta_list_new();
  LocDeltaList *iter = list->head;
  while (iter) {
    if (ohcount_loc_delta_is_changed(iter->delta))
      ohcount_loc_delta_list_add_loc_delta(new_list, iter->delta);
    iter = iter->next;
  }
  return new_list;
}

void ohcount_loc_delta_list_free(LocDeltaList *list) {
  if (list->head) {
    LocDeltaList *iter = list->head;
    while (iter) {
      LocDeltaList *next = iter->next;
      ohcount_loc_delta_free(iter->delta);
      free(iter);
      iter = next;
    }
  } else free(list);
}