<commit_before>#include "rvmparser.h"

#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdio>

#include "rvmreader.h"

using namespace std;

string trim(const string& s) {
    size_t si = s.find_first_not_of(" \n\r\t");
    if (si == string::npos) {
        return "";
    }
    size_t ei = s.find_last_not_of(" \n\r\t");
    return s.substr(si, ei - si + 1);
}

RVMParser::RVMParser(RVMReader* reader) :
    m_reader(reader),
    m_headerFound(false),
    m_objectName(""),
    m_objectFound(0),
    m_forcedColor(-1),
    m_nbGroups(0),
    m_nbPyramids(0),
    m_nbBoxes(0),
    m_nbRectangularToruses(0),
    m_nbCircularToruses(0),
    m_nbEllipticalDishes(0),
    m_nbSphericalDishes(0),
    m_nbSnouts(0),
    m_nbCylinders(0),
    m_nbSpheres(0),
    m_nbLines(0),
    m_nbFacetGroups(0),
    m_attributes(0),
    m_cd(0),
    m_aggregation(false) {
}

bool RVMParser::readFile(const string& filename, bool ignoreAttributes) {
    m_lastError = "";

    // Open RVM file
    filebuf fb;
    if (!fb.open(filename.data(), ios::in)) {
        m_lastError = "Could not open file";
        return false;
    }
    istream is(&fb);

    // Try to find ATT companion file
    m_attributeStream = 0;
    filebuf afb;
    if (!ignoreAttributes) {
        string attfilename = filename.substr(0, filename.find_last_of(".")) + ".att";
        if (afb.open(attfilename.data(), ios::in)) {
            cout << "Found attribute companion file: " << attfilename << endl;
            m_attributeStream = new istream(&afb);
        } else {
            attfilename = filename.substr(0, filename.find_last_of(".")) + ".ATT";
            if (afb.open(attfilename.data(), ios::in)) {
                cout << "Found attribute companion file: " << attfilename << endl;
                m_attributeStream = new istream(&afb);
            }
        }
        if (m_attributeStream && !m_attributeStream->eof()) {
            std::getline(*m_attributeStream, m_currentAttributeLine, '\n');
        }
    }

    bool success = readStream(is);

    fb.close();
    afb.close();

    return success;
}

bool RVMParser::readFiles(const vector<string>& filenames, const string& name, bool ignoreAttributes) {
    bool success = true;

    m_reader->startDocument();
    m_reader->startHeader("PMUC - Plant Mock-Up Converter", "Aggregation file", "", "", "");
    m_reader->endHeader();
    m_reader->startModel(name, "Aggreagation");

    m_aggregation = true;
    for (int i = 0; i < filenames.size(); i++) {
        success = readFile(filenames[i], ignoreAttributes);
        if (!success) {
            break;
        }
    }
<commit_msg>Added one enclosing group per sub file.<commit_after>#include "rvmparser.h"

#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdio>

#include "rvmreader.h"

using namespace std;

#ifdef _WIN32
#define PATHSEP '\\'
#else
#define PATHSEP '/'
#endif

string trim(const string& s) {
    size_t si = s.find_first_not_of(" \n\r\t");
    if (si == string::npos) {
        return "";
    }
    size_t ei = s.find_last_not_of(" \n\r\t");
    return s.substr(si, ei - si + 1);
}

RVMParser::RVMParser(RVMReader* reader) :
    m_reader(reader),
    m_headerFound(false),
    m_objectName(""),
    m_objectFound(0),
    m_forcedColor(-1),
    m_nbGroups(0),
    m_nbPyramids(0),
    m_nbBoxes(0),
    m_nbRectangularToruses(0),
    m_nbCircularToruses(0),
    m_nbEllipticalDishes(0),
    m_nbSphericalDishes(0),
    m_nbSnouts(0),
    m_nbCylinders(0),
    m_nbSpheres(0),
    m_nbLines(0),
    m_nbFacetGroups(0),
    m_attributes(0),
    m_cd(0),
    m_aggregation(false) {
}

bool RVMParser::readFile(const string& filename, bool ignoreAttributes) {
    m_lastError = "";

    // Open RVM file
    filebuf fb;
    if (!fb.open(filename.data(), ios::in)) {
        m_lastError = "Could not open file";
        return false;
    }
    istream is(&fb);

    // Try to find ATT companion file
    m_attributeStream = 0;
    filebuf afb;
    if (!ignoreAttributes) {
        string attfilename = filename.substr(0, filename.find_last_of(".")) + ".att";
        if (afb.open(attfilename.data(), ios::in)) {
            cout << "Found attribute companion file: " << attfilename << endl;
            m_attributeStream = new istream(&afb);
        } else {
            attfilename = filename.substr(0, filename.find_last_of(".")) + ".ATT";
            if (afb.open(attfilename.data(), ios::in)) {
                cout << "Found attribute companion file: " << attfilename << endl;
                m_attributeStream = new istream(&afb);
            }
        }
        if (m_attributeStream && !m_attributeStream->eof()) {
            std::getline(*m_attributeStream, m_currentAttributeLine, '\n');
        }
    }

    bool success = readStream(is);

    fb.close();
    afb.close();

    return success;
}

bool RVMParser::readFiles(const vector<string>& filenames, const string& name, bool ignoreAttributes) {
    bool success = true;

    m_reader->startDocument();
    m_reader->startHeader("PMUC - Plant Mock-Up Converter", "Aggregation file", "", "", "");
    m_reader->endHeader();
    m_reader->startModel(name, "Aggregation");

    m_aggregation = true;
    for (int i = 0; i < filenames.size(); i++) {
        string groupname = filenames[i].substr(filenames[i].rfind(PATHSEP) + 1, filenames[i].find_last_of("."));
        m_reader->startGroup(groupname, vector<float>(3, 0), 0);
        success = readFile(filenames[i], ignoreAttributes);
        if (!success) {
            break;
        }
        m_reader->endGroup();
    }

{'fraction_non_alphanumeric': 0.14905051952705123, 'fraction_numerical': 0.008599068434252956, 'mean_word_length': 2.7979591836734694, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4785049', 'n_tokens_mistral': 1970, 'n_tokens_neox': 1910, 'n_words': 384}