<commit_before>/*
  MusicXML Library
  Copyright (C) Grame 2006-2013

  This Source Code Form is subject to the terms of the Mozilla Public
  License, v. 2.0. If a copy of the MPL was not distributed with this
  file, You can obtain one at http://mozilla.org/MPL/2.0/.

  Grame Research Laboratory, 11, cours de Verdun Gensoul 69002 Lyon - France
  research@<EMAIL>.fr
*/

#ifdef VC6
# pragma warning (disable : 4786)
#endif

#include <iomanip> // for setw()

#include "xml.h"
#include "xmlfile.h"
#include "xmlreader.h"

#include "traceOptions.h"
#include "musicXMLOptions.h"

#include "musicXML2mxmlTreeInterface.h"


using namespace std;

namespace MusicXML2
{

//_______________________________________________________________________________
EXP Sxmlelement musicXMLFile2mxmlTree (
  const char*       fileName,
  S_musicXMLOptions mxmlOpts,
  indentedOstream&  logIOstream) 
{
  clock_t startClock = clock ();

  if (gTraceOptions->fTraceBasic) {
    string separator =
      "%--------------------------------------------------------------";
    
    gLogIOstream <<
      endl <<
      separator <<
      endl <<
      gTab <<
      "Pass 1: building the xmlelement tree from \"" << fileName << "\"" <<
      endl <<
      separator <<
      endl <<
      endl;
  }

  xmlreader r;
  
  SXMLFile xmlFile = r.read (fileName);
  if (! xmlFile)
    return Sxmlelement (0);

  if (false) { // JMI
    gLogIOstream <<
      "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" <<
      endl;
      
    xmlFile->print (gLogIOstream);

    gLogIOstream <<
      endl;
  }
  
  // get the xmlDecl
  TXMLDecl * xmlDecl = xmlFile->getXMLDecl ();
  
  if (false) { // JMI
    gLogIOstream <<
      "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" <<
      "getXMLDecl =" <<
      endl;
    xmlDecl->print (gLogIOstream);
  }
  
  string fXMLVersion = xmlDecl->getVersion ();
  string fXMLEncoding = xmlDecl->getEncoding ();
  int     fXMLStandalone = xmlDecl->getStandalone ();

  const int fieldWidth = 17;
  
  gLogIOstream <<
    "XML Declaration:" <<
    endl;

  gIndenter++;
  
  gLogIOstream << left <<
    setw (fieldWidth) <<
    "fXMLVersion" << " = \"" << fXMLVersion << "\"" <<
    endl <<
    setw (fieldWidth) <<
    "fXMLEncoding" << " = \"" << fXMLEncoding << "\"" <<
    endl  <<
    setw (fieldWidth) <<
    "fXMLStandalone" << " = \"" << fXMLStandalone << "\"" <<
    endl <<
    endl;

  gIndenter--;

  // get the docType
  TDocType * docType = xmlFile->getDocType ();
  
  if (false) { // JMI
    gLogIOstream <<
      "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" <<
      "getDocType =" <<
      endl;
    docType->print (gLogIOstream);
  }

  gLogIOstream <<
    "Document Type:" <<
    endl;

  gIndenter++;
  
  std::string fXMLStartElement = docType->getStartElement ();
  bool    fXMLPublic = docType->getPublic ();
  std::string fXMLPubLitteral = docType->getPubLitteral ();
  std::string fXMLSysLitteral = docType->getSysLitteral ();

  gLogIOstream << left <<
    setw (fieldWidth) <<
    "fXMLStartElement" << " = \"" << fXMLStartElement << "\"" <<
    endl <<
    setw (fieldWidth) <<
    "fXMLPublic" << " = \"" << fXMLPublic << "\"" <<
    endl  <<
    setw (fieldWidth) <<
    "fXMLPubLitteral" << " = \"" << fXMLPubLitteral << "\"" <<
    endl  <<
    setw (fieldWidth) <<
    "fXMLSysLitteral" << " = \"" << fXMLSysLitteral << "\"" <<
    endl;
    
  gIndenter--;
    
  clock_t endClock = clock ();

  // register time spent
  timing::gTiming.appendTimingItem (
    "Pass 1",
    "build xmlelement tree from file",
    timingItem::kMandatory,
    startClock,
    endClock);
  
  // fetch mxmlTree
  Sxmlelement mxmlTree = xmlFile->elements ();

  return mxmlTree;
}

//_______________________________________________________________________________
EXP Sxmlelement musicXMLFd2mxmlTree (
  FILE*             fd,
  S_musicXMLOptions mxmlOpts,
  indentedOstream&  logIOstream) 
{
  clock_t startClock = clock ();
  
  if (gTraceOptions->fTraceBasic) {
    string separator =
      "%--------------------------------------------------------------";
    
    logIOstream <<
      endl <<
      separator <<
      endl <<
      gTab <<
      "Pass 1: building the xmlelement tree from standard input" <<
      endl <<
      separator <<
      endl;
  }

  xmlreader r;
  
  SXMLFile xmlFile = r.read (fd);

  clock_t endClock = clock ();

  // register time spent
  timing::gTiming.appendTimingItem (
    "Pass 1",
    "build xmlelement tree from stdin",
    timingItem::kMandatory,
    startClock,
    endClock);

  // fetch mxmlTree<commit_msg>Preparing conversion to UTF-8 encoding<commit_after>/*
  MusicXML Library
  Copyright (C) Grame 2006-2013

  This Source Code Form is subject to the terms of the Mozilla Public
  License, v. 2.0. If a copy of the MPL was not distributed with this
  file, You can obtain one at http://mozilla.org/MPL/2.0/.

  Grame Research Laboratory, 11, cours de Verdun Gensoul 69002 Lyon - France
  <EMAIL>
*/

#ifdef VC6
# pragma warning (disable : 4786)
#endif

#include <iomanip> // for setw()

#include "xml.h"
#include "xmlfile.h"
#include "xmlreader.h"

#include "traceOptions.h"
#include "musicXMLOptions.h"

#include "musicXML2mxmlTreeInterface.h"


using namespace std;

namespace MusicXML2
{

//_______________________________________________________________________________
EXP Sxmlelement musicXMLFile2mxmlTree (
  const char*       fileName,
  S_musicXMLOptions mxmlOpts,
  indentedOstream&  logIOstream) 
{
  clock_t startClock = clock ();

  if (gTraceOptions->fTraceBasic) {
    string separator =
      "%--------------------------------------------------------------";
    
    gLogIOstream <<
      endl <<
      separator <<
      endl <<
      gTab <<
      "Pass 1: building the xmlelement tree from \"" << fileName << "\"" <<
      endl <<
      separator <<
      endl <<
      endl;
  }

  xmlreader r;
  
  SXMLFile xmlFile = r.read (fileName);
  if (! xmlFile)
    return Sxmlelement (0);

  if (false) { // JMI
    gLogIOstream <<
      "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" <<
      endl;
      
    xmlFile->print (gLogIOstream);

    gLogIOstream <<
      endl;
  }
  
  // get the xmlDecl
  TXMLDecl * xmlDecl = xmlFile->getXMLDecl ();
  
  if (false) { // JMI
    gLogIOstream <<
      "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" <<
      "getXMLDecl =" <<
      endl;
    xmlDecl->print (gLogIOstream);
  }
  
  string fXMLVersion = xmlDecl->getVersion ();
  string fXMLEncoding = xmlDecl->getEncoding ();
  int     fXMLStandalone = xmlDecl->getStandalone ();

  const int fieldWidth = 17;
  
  gLogIOstream <<
    "XML Declaration:" <<
    endl;

  gIndenter++;
  
  gLogIOstream << left <<
    setw (fieldWidth) <<
    "fXMLVersion" << " = \"" << fXMLVersion << "\"" <<
    endl <<
    setw (fieldWidth) <<
    "fXMLEncoding" << " = \"" << fXMLEncoding << "\"" <<
    endl  <<
    setw (fieldWidth) <<
    "fXMLStandalone" << " = \"" << fXMLStandalone << "\"" <<
    endl <<
    endl;

  gIndenter--;

  // get the docType
  TDocType * docType = xmlFile->getDocType ();
  
  if (false) { // JMI
    gLogIOstream <<
      "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" <<
      "getDocType =" <<
      endl;
    docType->print (gLogIOstream);
  }

  gLogIOstream <<
    "Document Type:" <<
    endl;

  gIndenter++;
  
  std::string fXMLStartElement = docType->getStartElement ();
  bool    fXMLPublic = docType->getPublic ();
  std::string fXMLPubLitteral = docType->getPubLitteral ();
  std::string fXMLSysLitteral = docType->getSysLitteral ();

  gLogIOstream << left <<
    setw (fieldWidth) <<
    "fXMLStartElement" << " = \"" << fXMLStartElement << "\"" <<
    endl <<
    setw (fieldWidth) <<
    "fXMLPublic" << " = \"" << fXMLPublic << "\"" <<
    endl  <<
    setw (fieldWidth) <<
    "fXMLPubLitteral" << " = \"" << fXMLPubLitteral << "\"" <<
    endl  <<
    setw (fieldWidth) <<
    "fXMLSysLitteral" << " = \"" << fXMLSysLitteral << "\"" <<
    endl <<
    endl;
    
  gIndenter--;
    
  // should the encoding be converted to UTF-8?
  string desiredEncoding = "UTF-8";
  
  if (fXMLEncoding != desiredEncoding) {
    gLogIOstream <<
      "Converting MusicXML data from \"" <<
      fXMLEncoding <<
      "\" to " <<
      desiredEncoding <<
      "\"" <<
      endl;
  }
    
  clock_t endClock = clock ();

  // register time spent
  timing::gTiming.appendTimingItem (
    "Pass 1",
    "build xmlelement tree from file",
    timingItem::kMandatory,
    startClock,
    endClock);
  
  // fetch mxmlTree
  Sxmlelement mxmlTree = xmlFile->elements ();

  return mxmlTree;
}

//_______________________________________________________________________________
EXP Sxmlelement musicXMLFd2mxmlTree (
  FILE*             fd,
  S_musicXMLOptions mxmlOpts,
  indentedOstream&  logIOstream) 
{
  clock_t startClock = clock ();
  
  if (gTraceOptions->fTraceBasic) {
    string separator =
      "%--------------------------------------------------------------";
    
    logIOstream <<
      endl <<
      separator <<
      endl <<
      gTab <<
      "Pass 1: building the xmlelement tree from standard input" <<
      endl <<
      separator <<
      endl;
  }

  xmlreader r;
  
  SXMLFile xmlFile = r.read (fd);
  if (! xmlFile)
    return Sxmlelement (0);

  if (false) { // JMI
    gLogIOstream <<
      "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" <<
      endl;
      
    xmlFile->print (gLogIOstream);

    gLogIOstream <<
      endl;
  }
  
  // get the xmlDecl
  TXMLDecl * xmlDecl = xmlFile->getXMLDecl ();
  
  if (false) { // JMI
    gLogIOstream <<
      "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" <<
      "getXMLDecl =" <<
      endl;
    xmlDecl->print (gLogIOstream);
  }
  
  string fXMLVersion = xmlDecl->getVersion ();
  string fXMLEncoding = xmlDecl->getEncoding ();
  int     fXMLStandalone = xmlDecl->getStandalone ();

  const int fieldWidth = 17;
  
  gLogIOstream <<
    "XML Declaration:" <<
    endl;

  gIndenter++;
  
  gLogIOstream << left <<
    setw (fieldWidth) <<
    "fXMLVersion" << " = \"" << fXMLVersion << "\"" <<
    endl <<
    setw (fieldWidth) <<
    "fXMLEncoding" << " = \"" << fXMLEncoding << "\"" <<
    endl  <<
    setw (fieldWidth) <<
    "fXMLStandalone" << " = \"" << fXMLStandalone << "\"" <<
    endl <<
    endl;

  gIndenter--;

  // get the docType
  TDocType * docType = xmlFile->getDocType ();
  
  if (false) { // JMI
    gLogIOstream <<
      "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" <<
      "getDocType =" <<
      endl;
    docType->print (gLogIOstream);
  }

  gLogIOstream <<
    "Document Type:" <<
    endl;

  gIndenter++;
  
  std::string fXMLStartElement = docType->getStartElement ();
  bool    fXMLPublic = docType->getPublic ();
  std::string fXMLPubLitteral = docType->getPubLitteral ();
  std::string fXMLSysLitteral = docType->getSysLitteral ();

  gLogIOstream << left <<
    setw (fieldWidth) <<
    "fXMLStartElement" << " = \"" << fXMLStartElement << "\"" <<
    endl <<
    setw (fieldWidth) <<
    "fXMLPublic" << " = \"" << fXMLPublic << "\"" <<
    endl  <<
    setw (fieldWidth) <<
    "fXMLPubLitteral" << " = \"" << fXMLPubLitteral << "\"" <<
    endl  <<
    setw (fieldWidth) <<
    "fXMLSysLitteral" << " = \"" << fXMLSysLitteral << "\"" <<
    endl <<
    endl;
    
  gIndenter--;

  // should the encoding be converted to UTF-8?
  string desiredEncoding = "UTF-8";
  
  if (fXMLEncoding != desiredEncoding) {
    gLogIOstream <<
      "Converting MusicXML data from \"" <<
      fXMLEncoding <<
      "\" to \"" <<
      desiredEncoding <<
      "\"" <<
      endl;
  }
    
  clock_t endClock = clock ();

  // register time spent
  timing::gTiming.appendTimingItem (
    "Pass 1",
    "build xmlelement tree from stdin",
    timingItem::kMandatory,
    startClock,
    endClock);

  // fetch mxmlTree
{'fraction_non_alphanumeric': 0.18243185507989404, 'fraction_numerical': 0.0066649577031530375, 'mean_word_length': 2.415231981324774, 'pattern_counts': {'":': 0, '<': 459, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 81}, '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': '243503', 'n_tokens_mistral': 4004, 'n_tokens_neox': 3860, 'n_words': 812}