[CHANGE] diff large files from the shell
[ohcount] / src / parsed_language.c
1 // parsed_language.c written by Mitchell Foral. mitchell<att>caladbolg.net.
2 // See COPYING for license information.
3
4 #include <stdlib.h>
5 #include <string.h>
6
7 #include "parsed_language.h"
8
9 ParsedLanguage *ohcount_parsed_language_new(const char *name,
10                                             int buffer_size) {
11   ParsedLanguage *pl = malloc(sizeof(ParsedLanguage));
12   pl->name = name;
13   pl->buffer_size = buffer_size;
14   pl->code = malloc(buffer_size + 5);
15   pl->code_p = pl->code;
16   *pl->code_p = '\0';
17   pl->code_count = 0;
18   pl->comments = malloc(buffer_size + 5);
19   pl->comments_p = pl->comments;
20   *pl->comments_p = '\0';
21   pl->comments_count = 0;
22   pl->blanks_count = 0;
23   return pl;
24 }
25
26 void ohcount_parsed_language_add_code(ParsedLanguage *parsed_language,
27                                       char *p, int length) {
28   if (parsed_language->code_p + length <
29       parsed_language->code + parsed_language->buffer_size + 5) {
30     strncpy(parsed_language->code_p, p, length);
31     parsed_language->code_p += length;
32     *parsed_language->code_p = '\0';
33     parsed_language->code_count++;
34   }
35 }
36
37 void ohcount_parsed_language_add_comment(ParsedLanguage *parsed_language,
38                                          char *p, int length) {
39   if (parsed_language->comments_p + length <
40       parsed_language->comments + parsed_language->buffer_size + 5) {
41     strncpy(parsed_language->comments_p, p, length);
42     parsed_language->comments_p += length;
43     *parsed_language->comments_p = '\0';
44     parsed_language->comments_count++;
45   }
46 }
47
48 void ohcount_parsed_language_free(ParsedLanguage *parsed_language) {
49   free(parsed_language->code);
50   free(parsed_language->comments);
51   free(parsed_language);
52 }
53
54 ParsedLanguageList *ohcount_parsed_language_list_new() {
55   ParsedLanguageList *list = malloc(sizeof(ParsedLanguageList));
56   list->pl = NULL;
57   list->next = NULL;
58   list->head = NULL;
59   list->tail = NULL;
60   return list;
61 }
62
63 void ohcount_parsed_language_list_free(ParsedLanguageList *list) {
64   if (list->head) {
65     ParsedLanguageList *iter = list->head;
66     while (iter) {
67       ParsedLanguageList *next = iter->next;
68       ohcount_parsed_language_free(iter->pl);
69       free(iter);
70       iter = next;
71     }
72   } else free(list);
73 }