Initial Revision
[ohcount] / ext / ohcount_native / language_breakdown.c
1 /*
2  *  language_breakdown.c
3  *  Ohcount
4  *
5  *  Created by Jason Allen on 6/26/06.
6  *  Copyright 2006 Ohloh. All rights reserved.
7  *
8  */
9
10 #include "common.h"
11
12 /*
13  * language_breakdown_initialize
14  *
15  * sets up the blank buffers...
16  *
17  */
18 void language_breakdown_initialize(LanguageBreakdown *lb, char *name, int buffer_size) {
19         strcpy(lb->name, name);
20         lb->code    = lb->code_cur    = malloc(buffer_size);
21         lb->code[0] = 0;
22         lb->comment = lb->comment_cur = malloc(buffer_size);
23         lb->comment[0] = 0;
24         lb->blank_count = 0;
25 }
26
27 /*
28  * language_breakdown_free
29  *
30  * frees the buffers allocated by a language_breakdown
31  *
32  */
33 void language_breakdown_free(LanguageBreakdown *lb) {
34 #ifndef NDEBUG
35         if (lb->code == NULL || lb->comment == NULL) {
36                 log0("freeing language_breakdown twice!");
37         }
38 #endif
39         free(lb->code);
40         free(lb->comment);
41         lb->code = NULL;
42         lb->comment = NULL;
43 }
44
45
46 char * first_non_blank(char *from, char *to) {
47         while (from < to && (*from == ' ' || *from == '\t')) {
48                 from++;
49         }
50         return from;
51 }
52
53 /*
54  * language_breakdown_copy_code
55  *
56  * copies the passed in string (via delimiters) to the code buffer
57  *
58  */
59 void language_breakdown_copy_code(LanguageBreakdown *lb, char *from, char *to) {
60         from = first_non_blank(from, to);
61         strncpy(lb->code_cur, from, to - from);
62         lb->code_cur += to - from;
63         *lb->code_cur = 0;
64 }
65
66 /*
67  * language_breakdown_copy_comment
68  *
69  * copies the passed in string (via delimiters) to the comment buffer
70  *
71  */
72 void language_breakdown_copy_comment(LanguageBreakdown *lb, char *from, char *to) {
73         from = first_non_blank(from, to);
74         strncpy(lb->comment_cur, from, to - from);
75         lb->comment_cur += to - from;
76         *lb->comment_cur = 0;
77 }