I have this program I am writing that cleans up a xml file and adds new lines to a settings section from a txt file. Part of it I have a section labeled // <settings> Part in my code. It is during or after that section, either is fine, I would like to compare the lines to make sure they are not duplicated but ignore their setting in this case True and False and consider them identical if one is set to true and the other set to false and only keep the second line and discard the first. Here is an example of how the settings look:
<setting1>true</setting1>
<setting2>false</setting2>
<setting3>true</setting3>
<setting1>false</setting1>
<setting4>true</setting4>
<setting2>true</setting2>
So in the end I would like the first setting 1 to be removed and the second setting 1 to stay and same thing for setting 2. Keep in mind this is an example as the settings have different names and sometimes contain the same words.
I appreciate any help.
Thanks,
V
Code:
//
#include <stdio.h>
#include <fstream>
#include <sstream>
#include <iostream>
#include <string>
#include <cctype>
#include <cstdlib>
#include <set>
#include <vector>
#include <algorithm>
#include <cassert>
#include <Windows.h>
using namespace std;
bool isSpace(unsigned char c) {
return ( c == '\r' ||
c == '\t' || c == '\v' || c == '\f');
}
int main()
{
const string Dir{ "C:/synergyii/config/" };
ifstream in_config{ Dir + "clientconfig.xml" },
in_newlines{ Dir + "newlines.txt" };
ofstream out{ Dir + "cltesting.txt" };
vector<string> vlines31;
vector<string> vlines32;
set<string> slines31;
set<string> slines32;
for (string line31; getline(in_config, line31); vlines31.push_back(line31))
if (line31.find("<settings>") != string::npos) {
vlines31.push_back(line31);
break;
}
for (const auto& v : vlines31)
out << v << '\n';
// <settings> Part
for (string line32; getline(in_config, line32) && line32.find("</settings>") == string::npos; ) {
line32.erase(remove_if(line32.begin(), line32.end(), isSpace), line32.end());
line32.erase(line32.find_last_not_of(" ") + 1);
const auto& result = slines32.insert(line32);
if (result.second)
vlines32.push_back(line32);
}
for (string line32; getline(in_newlines, line32);) {
line32.erase(remove_if(line32.begin(), line32.end(), isSpace), line32.end());
const auto& result = slines32.insert(line32);
if (result.second)
vlines32.push_back(line32);
}
vlines32.erase(unique(vlines32.begin(), vlines32.end()), vlines32.end() );
for (auto it = vlines32.cbegin(); it != vlines32.cend(); ++it)
out << '\t' << '\t' << *it << '\n';
out << '\t' << "</settings>\n";
out << "</config>\n";
in_config.close();
out.close();
}