Add support for Coq .v files (based on the OCaml parser).
[ohcount] / src / sourcefile.h
1 // sourcefile.h written by Mitchell Foral. mitchell<att>caladbolg.net.
2 // See COPYING for license information.
3
4 #ifndef OHCOUNT_SOURCEFILE_H
5 #define OHCOUNT_SOURCEFILE_H
6
7 #include "loc.h"
8 #include "parsed_language.h"
9
10 /**
11  * Creates and returns a new SourceFile from a given filepath.
12  * The given filepath is copied and may be 'free'd immediately.
13  * @param filepath The path to a file on disk.
14  * @return SourceFile
15  */
16 SourceFile *ohcount_sourcefile_new(const char *filepath);
17
18 /**
19  * Sets the filepath on the disk of the given SourceFile.
20  * This is only used if the SourceFile's filepath field is not accurate,
21  * typically only in language detection.
22  * @param sourcefile A SourceFile created by ohcount_sourcefile_new().
23  * @param diskpath The real path to the file on disk.
24  */
25 void ohcount_sourcefile_set_diskpath(SourceFile *sourcefile,
26                                      const char *diskpath);
27
28 /**
29  * Sets the contents of the given SourceFile.
30  * The given contents are copied and may be 'free'd immediately.
31  * @param sourcefile A SourceFile created by ohcount_sourcefile_new().
32  * @param contents The contents to set for sourcefile.
33  */
34 void ohcount_sourcefile_set_contents(SourceFile *sourcefile,
35                                      const char *contents);
36
37 /**
38  * Returns the file contents of a given SourceFile.
39  * The returned pointer is used internally and may not be 'free'd.
40  * @param sourcefile A SourceFile created by ohcount_sourcefile_new().
41  * @return pointer to string file contents.
42  */
43 char *ohcount_sourcefile_get_contents(SourceFile *sourcefile);
44
45 /**
46  * Returns the size of the file contents of a given SourceFile.
47  * @param sourcefile A SourceFile created by ohcount_sourcefile_new().
48  * @return size of the file's contents.
49  */
50 int ohcount_sourcefile_get_contents_size(SourceFile *sourcefile);
51
52 /**
53  * Sets the language of a given SourceFile.
54  * The given language copied and may be 'free'd immediately.
55  * @param sourcefile A SourceFile created by ohcount_sourcefile_new().
56  * @param language The language to set the SourceFile to.
57  */
58 void ohcount_sourcefile_set_language(SourceFile *sourcefile,
59                                      const char *language);
60
61 /**
62  * Returns the detected language of a given SourceFile.
63  * The returned pointer is used internally and may not be 'free'd.
64  * @param sourcefile A SourceFile created by ohcount_sourcefile_new().
65  * @return string language name.
66  */
67 const char *ohcount_sourcefile_get_language(SourceFile *sourcefile);
68
69 /**
70  * Parses the given SourceFile with the default callback that keeps track of the
71  * number of lines of code, comments, and blank lines.
72  * @param sourcefile A SourceFile created by ohcount_sourcefile_new().
73  */
74 void ohcount_sourcefile_parse(SourceFile *sourcefile);
75
76 /**
77  * Returns the ParsedLanguageList parsed out of the given SourceFile.
78  * @param sourcefile A SourceFile created by ohcount_sourcefile_new().
79  * @return ParsedLanguageList
80  */
81 ParsedLanguageList *ohcount_sourcefile_get_parsed_language_list(SourceFile
82                                                                 *sourcefile);
83
84 /**
85  * Parses the given SourceFile with a specific callback.
86  * The callback is called for each line parsed, not entity.
87  * @param sourcefile A SourceFile created by ohcount_sourcefile_new().
88  * @param callback The callback function to call for every line parsed.
89  * @param userdata Userdata to pass to the callback function.
90  */
91 void ohcount_sourcefile_parse_with_callback(SourceFile *sourcefile,
92                                             void (*callback) (const char *,
93                                                               const char *, int,
94                                                               int, void *),
95                                             void *userdata);
96
97 /**
98  * Parses the given SourceFile with a specific callback.
99  * The callback is called for each entity parsed, not line.
100  * @param sourcefile A SourceFile created by ohcount_sourcefile_new().
101  * @param callback The callback function to call for every entity parsed.
102  * @param userdata Userdata to pass to the callback function.
103  */
104 void ohcount_sourcefile_parse_entities_with_callback(SourceFile *sourcefile,
105                                                      void (*callback)
106                                                        (const char *,
107                                                         const char *, int,
108                                                         int, void *),
109                                                      void *userdata);
110
111 /**
112  * Returns a LicenseList of detected licenses in the given SourceFile.
113  * The returned list and its contents are used internally and may not be
114  * 'free'd.
115  * @param sourcefile A SourceFile created by ohcount_sourcefile_new().
116  * @return LicenseList
117  */
118 LicenseList *ohcount_sourcefile_get_license_list(SourceFile *sourcefile);
119
120 /**
121  * Returns a LocList of total lines of code in each language in the given
122  * SourceFile.
123  * The returned list and its contents are used internally and may not be
124  * 'free'd.
125  * @param sourcefile A SourceFile created by ohcount_sourcefile_new().
126  * @return LocList
127  */
128 LocList *ohcount_sourcefile_get_loc_list(SourceFile *sourcefile);
129
130 /**
131  * Returns a LocDeltaList reflecting the changes from one revision of a
132  * SourceFile to another for all languages.
133  * The returned pointer must be 'free'd.
134  * @param from The reference SourceFile created by ohcount_sourcefile_new().
135  * @param to The SourceFile to compare the reference SourceFile to (typically a
136  *   later revision instead of a completely different SourceFile).
137  * @return LocDeltaList
138  */
139 LocDeltaList *ohcount_sourcefile_diff(SourceFile *from, SourceFile *to);
140
141 /**
142  * Returns a LocDelta reflecting the changes from one revision of a SourceFile
143  * to another for a given language.
144  * The given language is not copied and may not be 'free'd. Use a language
145  * defined in src/languages.h.
146  * The returned pointer must be 'free'd.
147  * @param from The reference SourceFile created by ohcount_sourcefile_new().
148  * @param language The language to calculate the LocDelta from.
149  * @param to The SourceFile to compare the reference SourceFile to (typically a
150  *   later revision instead of a completely different SourceFile).
151  * @return LocDelta
152  */
153 LocDelta *ohcount_sourcefile_calc_loc_delta(SourceFile *from,
154                                             const char *language,
155                                             SourceFile *to);
156
157 /**
158  * Frees a SourceFile created by ohcount_sourcefile_new().
159  * @param sourcefile A SourceFile created by ohcount_sourcefile_new().
160  */
161 void ohcount_sourcefile_free(SourceFile *sourcefile);
162
163 /**
164  * Creates a new SourceFileList that is initially empty.
165  * Files can be added using ohcount_sourcefile_list_add_file().
166  * Directories can be added using ohcount_sourcefile_list_add_directory().
167  * @return SourceFileList
168  */
169 SourceFileList *ohcount_sourcefile_list_new();
170
171 /**
172  * Adds a given file to a SourceFileList.
173  * The given filepath is copied and may be 'free'd immediately.
174  * @param list a SourceFileList created by ohcount_sourcefile_list_new().
175  * @param filepath The full path to the file to be added to the list.
176  */
177 void ohcount_sourcefile_list_add_file(SourceFileList *list,
178                                           const char *filepath);
179
180 /**
181  * Adds the contents of a given directory to a SourceFileList.
182  * The given directory may be 'free'd immediately.
183  * @param list A SourceFileList created by ohcount_sourcefile_list_new().
184  * @param directory The directory whose contents are to be added to the list.
185  */
186 void ohcount_sourcefile_list_add_directory(SourceFileList *list,
187                                            const char *directory);
188
189 /**
190  * Returns a new LocList for all files in the given SourceFileList.
191  * @param list A SourceFileList created by ohcount_sourcefile_list_new().
192  * @return LocList
193  */
194 LocList *ohcount_sourcefile_list_analyze_languages(SourceFileList *list);
195
196 /**
197  * Frees the memory allocated for a given SourceFileList.
198  * @param list A SourceFileList created by ohcount_sourcefile_list_new().
199  */
200 void ohcount_sourcefile_list_free(SourceFileList *list);
201
202 #endif