With the number of sourcelines for each clause being variable.
I had to check the sanity of the file then compare it to another edited version of the same file.
I stored each file in a vector<vector<string>> essentially containing each clause and it's source lines into a seperate vector.
From there I took each vector in turn in a for loop, compared it to each clause in the vector for the second file and if there was a match, did essentially the same thing for the source lines. Using if statements etc to report the findings.
This all works fine but feels somewhat messy when seeing it on the screen, is there a more elegant way of doing this? Is this how you'd have done it?
ifstream file1 = ..., file2 = ...;
void checkNextClauseInFile2(string clause, vector<string> sourceLines)
{
// checks only until the next clause in file2 and "report findings"
for (...)
{
string line;
file2 >> line;
}
}
void processNextClausInFile1()
{
vector<string> souceLines;
string clause;
file1 >> clause;
for (...)
{
string l; file2 >> l;
sourceLines.push_back(l);
}
checkNextClauseInFile2(clause, sourceLines);
}
But I guess it's just personal preferences. Depending whether you have inter-clause dependencies (then my way of doing every clause separately won't work). If you don't have this dependency, I don't see a reason to first completely read in the first file.
And I don't see a reason why you couldn't have both files open simultanously.
If speed or memory size doesn't matter, your solution is as good as mine. If it's readability you are concerned with, then maybe defining some structs and classes instead of using "vector<string>" or even a simple typedef can do wonders...
You're right with your assumption with regards to the clause and sourceline.
Given the spec of the task I was given, that I had to check the files were in the correct format first before comparing them. There are no inter-clause dependancies, but if I have to check the format of the first file, then the second file, then diff them - storing them is essential I think else I'd be reading them twice and refering back to which clause the source lines are under when I notice a difference would be tricky.
struct Section
{
Section(ifstream& in); // fill itself from the stream and reports syntactic mistakes (e.g. via exception)
bool compare(const Section& fromOtherFile); // does comparing with the reference file
private:
string clause;
vector<string> sourcelines;
};
That would be a neater looking way to store it, but would it speed it up?
The only real difference between methods from what I can gather is that I have a vector<vector<string>> and you have a vector<Section> - are there other benefits I'm missing?