1 | /*! @file Explanations.cc |
---|
2 | * @brief Explanations Implementation |
---|
3 | * |
---|
4 | * This file contains the implementation of the functions |
---|
5 | * which provide a base level of functionality of a Explanations. |
---|
6 | */ |
---|
7 | |
---|
8 | #include <stdio.h> |
---|
9 | #include <stdlib.h> |
---|
10 | #include <string.h> |
---|
11 | #include <unistd.h> |
---|
12 | |
---|
13 | #include "Explanations.h" |
---|
14 | #include "app_common.h" |
---|
15 | |
---|
16 | namespace Coverage { |
---|
17 | |
---|
18 | Explanations::Explanations() |
---|
19 | { |
---|
20 | } |
---|
21 | |
---|
22 | Explanations::~Explanations() |
---|
23 | { |
---|
24 | } |
---|
25 | |
---|
26 | void Explanations::load( |
---|
27 | const char* const explanations |
---|
28 | ) |
---|
29 | { |
---|
30 | #define MAX_LINE_LENGTH 512 |
---|
31 | FILE *explain; |
---|
32 | char *cStatus; |
---|
33 | Explanation *e; |
---|
34 | int line = 1; |
---|
35 | |
---|
36 | if (!explanations) |
---|
37 | return; |
---|
38 | |
---|
39 | explain = fopen( explanations, "r" ); |
---|
40 | if (!explain) { |
---|
41 | fprintf( |
---|
42 | stderr, |
---|
43 | "ERROR: Explanations::load - unable to open explanations file %s\n", |
---|
44 | explanations |
---|
45 | ); |
---|
46 | exit(-1); |
---|
47 | } |
---|
48 | |
---|
49 | while ( 1 ) { |
---|
50 | e = new Explanation; |
---|
51 | // Read the starting line of this explanation and |
---|
52 | // skip blank lines between |
---|
53 | do { |
---|
54 | inputBuffer[0] = '\0'; |
---|
55 | cStatus = fgets( inputBuffer, MAX_LINE_LENGTH, explain ); |
---|
56 | if (cStatus == NULL) { |
---|
57 | goto done; |
---|
58 | } |
---|
59 | inputBuffer[ strlen(inputBuffer) - 1] = '\0'; |
---|
60 | line++; |
---|
61 | } while ( inputBuffer[0] == '\0' ); |
---|
62 | |
---|
63 | // Have we already seen this one? |
---|
64 | if (set.find( inputBuffer ) != set.end()) { |
---|
65 | fprintf( |
---|
66 | stderr, |
---|
67 | "ERROR: Explanations::load - line %d " |
---|
68 | "contains a duplicate explanation (%s)\n", |
---|
69 | line, |
---|
70 | inputBuffer |
---|
71 | ); |
---|
72 | exit( -1 ); |
---|
73 | } |
---|
74 | |
---|
75 | // Add the starting line and file |
---|
76 | e->startingPoint = std::string(inputBuffer); |
---|
77 | e->found = false; |
---|
78 | |
---|
79 | // Get the classification |
---|
80 | cStatus = fgets( inputBuffer, MAX_LINE_LENGTH, explain ); |
---|
81 | if (cStatus == NULL) { |
---|
82 | fprintf( |
---|
83 | stderr, |
---|
84 | "ERROR: Explanations::load - line %d " |
---|
85 | "out of sync at the classification\n", |
---|
86 | line |
---|
87 | ); |
---|
88 | exit( -1 ); |
---|
89 | } |
---|
90 | inputBuffer[ strlen(inputBuffer) - 1] = '\0'; |
---|
91 | e->classification = inputBuffer; |
---|
92 | line++; |
---|
93 | |
---|
94 | // Get the explanation |
---|
95 | while (1) { |
---|
96 | cStatus = fgets( inputBuffer, MAX_LINE_LENGTH, explain ); |
---|
97 | // fprintf( stderr, "%d - %s\n", line, inputBuffer ); |
---|
98 | if (cStatus == NULL) { |
---|
99 | fprintf( |
---|
100 | stderr, |
---|
101 | "ERROR: Explanations::load - line %d " |
---|
102 | "out of sync at the explanation\n", |
---|
103 | line |
---|
104 | ); |
---|
105 | exit( -1 ); |
---|
106 | } |
---|
107 | inputBuffer[ strlen(inputBuffer) - 1] = '\0'; |
---|
108 | line++; |
---|
109 | |
---|
110 | const char delimiter[4] = "+++"; |
---|
111 | if (!strncmp( inputBuffer, delimiter, 3 )) { |
---|
112 | break; |
---|
113 | } |
---|
114 | // XXX only taking last line. Needs to be a vector |
---|
115 | e->explanation.push_back( inputBuffer ); |
---|
116 | } |
---|
117 | |
---|
118 | // Add this to the set of Explanations |
---|
119 | set[ e->startingPoint ] = *e; |
---|
120 | } |
---|
121 | done: |
---|
122 | ; |
---|
123 | |
---|
124 | #undef MAX_LINE_LENGTH |
---|
125 | } |
---|
126 | |
---|
127 | const Explanation *Explanations::lookupExplanation( |
---|
128 | std::string& start |
---|
129 | ) |
---|
130 | { |
---|
131 | if (set.find( start ) == set.end()) { |
---|
132 | #if 0 |
---|
133 | fprintf( stderr, |
---|
134 | "Warning: Unable to find explanation for %s\n", |
---|
135 | start.c_str() |
---|
136 | ); |
---|
137 | #endif |
---|
138 | return NULL; |
---|
139 | } |
---|
140 | set[ start ].found = true; |
---|
141 | return &set[ start ]; |
---|
142 | } |
---|
143 | |
---|
144 | void Explanations::writeNotFound( |
---|
145 | const char* const fileName |
---|
146 | ) |
---|
147 | { |
---|
148 | FILE* notFoundFile; |
---|
149 | bool notFoundOccurred = false; |
---|
150 | |
---|
151 | if (!fileName) |
---|
152 | return; |
---|
153 | |
---|
154 | notFoundFile = fopen( fileName, "w" ); |
---|
155 | if (!fileName) { |
---|
156 | fprintf( |
---|
157 | stderr, |
---|
158 | "ERROR: Explanations::writeNotFound - unable to open file %s\n", |
---|
159 | fileName |
---|
160 | ); |
---|
161 | exit( -1 ); |
---|
162 | } |
---|
163 | |
---|
164 | for (std::map<std::string, Explanation>::iterator itr = set.begin(); |
---|
165 | itr != set.end(); |
---|
166 | itr++) { |
---|
167 | Explanation e = (*itr).second; |
---|
168 | std::string key = (*itr).first; |
---|
169 | |
---|
170 | if (!e.found) { |
---|
171 | notFoundOccurred = true; |
---|
172 | fprintf( |
---|
173 | notFoundFile, |
---|
174 | "%s\n", |
---|
175 | e.startingPoint.c_str() |
---|
176 | ); |
---|
177 | } |
---|
178 | } |
---|
179 | fclose( notFoundFile ); |
---|
180 | |
---|
181 | if (!notFoundOccurred) { |
---|
182 | if (!unlink( fileName )) { |
---|
183 | fprintf( stderr, |
---|
184 | "Warning: Unable to unlink %s\n\n", |
---|
185 | fileName |
---|
186 | ); |
---|
187 | } |
---|
188 | } |
---|
189 | } |
---|
190 | |
---|
191 | } |
---|