summaryrefslogtreecommitdiff
path: root/tester/covoar/Explanations.cc
blob: 2050b84814f3adb11b1c23885f33b65aff91c159 (plain)
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
/*! @file Explanations.cc
 *  @brief Explanations Implementation
 *
 *  This file contains the implementation of the functions
 *  which provide a base level of functionality of a Explanations.
 */

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

#include <rld.h>

#include "Explanations.h"
#include "app_common.h"

namespace Coverage {

  Explanations::Explanations()
  {
  }

  Explanations::~Explanations()
  {
  }

  void Explanations::load(
    const char* const explanations
  )
  {
    #define MAX_LINE_LENGTH 512
    FILE       *explain;
    char        *cStatus;
    Explanation *e;
    int          line = 1;

    if (!explanations)
      return;

    explain = ::fopen( explanations, "r" );
    if (!explain) {
      std::ostringstream what;
      what << "Unable to open " << explanations;
      throw rld::error( what, "Explanations::load" );
    }

    while ( 1 ) {
      e = new Explanation;
      // Read the starting line of this explanation and
      // skip blank lines between
      do {
        inputBuffer[0] = '\0';
        cStatus = fgets( inputBuffer, MAX_LINE_LENGTH, explain );
        if (cStatus == NULL) {
          goto done;
        }
        inputBuffer[ strlen(inputBuffer) - 1] = '\0';
        line++;
      } while ( inputBuffer[0] == '\0' );

      // Have we already seen this one?
      if (set.find( inputBuffer ) != set.end()) {
        std::ostringstream what;
        what << "line " << line
             << "contains a duplicate explanation ("
             << inputBuffer << ")";
        throw rld::error( what, "Explanations::load" );
      }

      // Add the starting line and file
      e->startingPoint = std::string(inputBuffer);
      e->found = false;

      // Get the classification
      cStatus = fgets( inputBuffer, MAX_LINE_LENGTH, explain );
      if (cStatus == NULL) {
        std::ostringstream what;
        what << "line " << line
             << "out of sync at the classification";
        throw rld::error( what, "Explanations::load" );
      }
      inputBuffer[ strlen(inputBuffer) - 1] = '\0';
      e->classification = inputBuffer;
      line++;

      // Get the explanation
      while (1) {
        cStatus = fgets( inputBuffer, MAX_LINE_LENGTH, explain );
        // fprintf( stderr, "%d - %s\n", line, inputBuffer );
        if (cStatus == NULL) {
          std::ostringstream what;
          what << "line " << line
               << "out of sync at the explanation";
          throw rld::error( what, "Explanations::load" );
        }
        inputBuffer[ strlen(inputBuffer) - 1] = '\0';
        line++;

        const char delimiter[4] = "+++";
        if (!strncmp( inputBuffer, delimiter, 3 )) {
          break;
        }
        // XXX only taking last line.  Needs to be a vector
        e->explanation.push_back( inputBuffer );
      }

      // Add this to the set of Explanations
      set[ e->startingPoint ] = *e;
    }
done:
    ;

    #undef MAX_LINE_LENGTH
  }

  const Explanation *Explanations::lookupExplanation(
    std::string& start
  )
  {
    if (set.find( start ) == set.end()) {
      #if 0
        std::cerr << "Warning: Unable to find explanation for "
                  << start << std::endl;
      #endif
      return NULL;
    }
    set[ start ].found = true;
    return &set[ start ];
  }

  void Explanations::writeNotFound(
    const char* const fileName
  )
  {
    FILE* notFoundFile;
    bool  notFoundOccurred = false;

    if (!fileName)
      return;

    notFoundFile = fopen( fileName, "w" );
    if (!fileName) {
      std::ostringstream what;
      what << "Unable to open " << fileName
           << "out of sync at the explanation";
      throw rld::error( what, "Explanations::writeNotFound" );
    }

    for (std::map<std::string, Explanation>::iterator itr = set.begin();
         itr != set.end();
         itr++) {
      Explanation e = (*itr).second;
      std::string key = (*itr).first;

      if (!e.found) {
        notFoundOccurred = true;
        fprintf(
          notFoundFile,
          "%s\n",
          e.startingPoint.c_str()
        );
      }
    }
    fclose( notFoundFile );

    if (!notFoundOccurred) {
      if (!unlink( fileName )) {
        std::cerr << "Warning: Unable to unlink " << fileName
                  << std::endl
                  << std::endl;
      }
    }
  }

}