Ported code to use tinyxml2 instead of tinyxml

This commit is contained in:
Ferdinand Thiessen 2016-05-13 14:25:08 +02:00
parent f0f670845e
commit c83f6294ab
6 changed files with 92 additions and 94 deletions

View File

@ -16,7 +16,7 @@ find_package(CURL 7.32.0 REQUIRED)
find_package(OAuth REQUIRED) find_package(OAuth REQUIRED)
find_package(Jsoncpp REQUIRED) find_package(Jsoncpp REQUIRED)
find_package(Htmlcxx REQUIRED) find_package(Htmlcxx REQUIRED)
find_package(Tinyxml REQUIRED) find_package(Tinyxml2 REQUIRED)
find_package(Rhash REQUIRED) find_package(Rhash REQUIRED)
file(GLOB SRC_FILES file(GLOB SRC_FILES
@ -83,7 +83,7 @@ target_include_directories(${PROJECT_NAME}
PRIVATE ${OAuth_INCLUDE_DIRS} PRIVATE ${OAuth_INCLUDE_DIRS}
PRIVATE ${Jsoncpp_INCLUDE_DIRS} PRIVATE ${Jsoncpp_INCLUDE_DIRS}
PRIVATE ${Htmlcxx_INCLUDE_DIRS} PRIVATE ${Htmlcxx_INCLUDE_DIRS}
PRIVATE ${Tinyxml_INCLUDE_DIRS} PRIVATE ${Tinyxml2_INCLUDE_DIRS}
PRIVATE ${Rhash_INCLUDE_DIRS} PRIVATE ${Rhash_INCLUDE_DIRS}
) )
@ -93,7 +93,7 @@ target_link_libraries(${PROJECT_NAME}
PRIVATE ${OAuth_LIBRARIES} PRIVATE ${OAuth_LIBRARIES}
PRIVATE ${Jsoncpp_LIBRARIES} PRIVATE ${Jsoncpp_LIBRARIES}
PRIVATE ${Htmlcxx_LIBRARIES} PRIVATE ${Htmlcxx_LIBRARIES}
PRIVATE ${Tinyxml_LIBRARIES} PRIVATE ${Tinyxml2_LIBRARIES}
PRIVATE ${Rhash_LIBRARIES} PRIVATE ${Rhash_LIBRARIES}
) )
@ -106,7 +106,7 @@ if(MSVC)
endif() endif()
elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
# Update if necessary # Update if necessary
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wno-long-long -fexceptions") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wextra -Wno-long-long -fexceptions")
endif() endif()
set(INSTALL_BIN_DIR bin CACHE PATH "Installation directory for executables") set(INSTALL_BIN_DIR bin CACHE PATH "Installation directory for executables")

View File

@ -9,7 +9,7 @@ This repository contains the code of unofficial [GOG](http://www.gog.com/) downl
* librhash * librhash
* jsoncpp * jsoncpp
* htmlcxx * htmlcxx
* tinyxml * tinyxml2
* boost (regex, date-time, system, filesystem, program-options) * boost (regex, date-time, system, filesystem, program-options)
* help2man * help2man

View File

@ -1,33 +0,0 @@
# - Try to find tinyxml
#
# Once done this will define
# Tinyxml_FOUND - System has tinyxml
# Tinyxml_INCLUDE_DIRS - The tinyxml include directories
# Tinyxml_LIBRARIES - The libraries needed to use tinyxml
find_package(PkgConfig)
pkg_check_modules(PC_TINYXML tinyxml)
find_path(TINYXML_INCLUDE_DIR tinyxml.h
HINTS
${PC_TINYXML_INCLUDEDIR}
${PC_TINYXML_INCLUDE_DIRS}
PATHS
${PC_TINYXML_INCLUDE_DIRS}
)
find_library(TINYXML_LIBRARY tinyxml
HINTS
${PC_TINYXML_LIBDIR}
${PC_TINYXML_LIBRARY_DIRS}
PATHS
${PC_TINYXML_LIBRARY_DIRS}
)
mark_as_advanced(TINYXML_INCLUDE_DIR TINYXML_LIBRARY)
if(TINYXML_INCLUDE_DIR)
set(Tinyxml_FOUND ON)
set(Tinyxml_INCLUDE_DIRS ${TINYXML_INCLUDE_DIR})
set(Tinyxml_LIBRARIES ${TINYXML_LIBRARY})
endif(TINYXML_INCLUDE_DIR)

33
cmake/FindTinyxml2.cmake Normal file
View File

@ -0,0 +1,33 @@
# - Try to find tinyxml2
#
# Once done this will define
# Tinyxml2_FOUND - System has tinyxml2
# Tinyxml2_INCLUDE_DIRS - The tinyxml2 include directories
# Tinyxml2_LIBRARIES - The libraries needed to use tinyxml
find_package(PkgConfig)
pkg_check_modules(PC_TINYXML2 tinyxml2)
find_path(TINYXML2_INCLUDE_DIR tinyxml2.h
HINTS
${PC_TINYXML2_INCLUDEDIR}
${PC_TINYXML2_INCLUDE_DIRS}
PATHS
${PC_TINYXML2_INCLUDE_DIRS}
)
find_library(TINYXML2_LIBRARY tinyxml2
HINTS
${PC_TINYXML2_LIBDIR}
${PC_TINYXML2_LIBRARY_DIRS}
PATHS
${PC_TINYXML2_LIBRARY_DIRS}
)
mark_as_advanced(TINYXML2_INCLUDE_DIR TINYXML2_LIBRARY)
if(TINYXML2_INCLUDE_DIR)
set(Tinyxml2_FOUND ON)
set(Tinyxml2_INCLUDE_DIRS ${TINYXML2_INCLUDE_DIR})
set(Tinyxml2_LIBRARIES ${TINYXML2_LIBRARY})
endif(TINYXML2_INCLUDE_DIR)

View File

@ -19,7 +19,7 @@
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include <boost/regex.hpp> #include <boost/regex.hpp>
#include <boost/date_time/posix_time/posix_time.hpp> #include <boost/date_time/posix_time/posix_time.hpp>
#include <tinyxml.h> #include <tinyxml2.h>
#include <json/json.h> #include <json/json.h>
#include <htmlcxx/html/ParserDom.h> #include <htmlcxx/html/ParserDom.h>
#include <htmlcxx/html/Uri.h> #include <htmlcxx/html/Uri.h>
@ -1293,12 +1293,11 @@ CURLcode Downloader::downloadFile(const std::string& url, const std::string& fil
// Do version check if local hash exists // Do version check if local hash exists
if (!localHash.empty()) if (!localHash.empty())
{ {
TiXmlDocument remote_xml; tinyxml2::XMLDocument remote_xml;
remote_xml.Parse(xml_data.c_str()); remote_xml.Parse(xml_data.c_str());
TiXmlNode *fileNodeRemote = remote_xml.FirstChild("file"); tinyxml2::XMLElement *fileElemRemote = remote_xml.FirstChildElement("file");
if (fileNodeRemote) if (fileElemRemote)
{ {
TiXmlElement *fileElemRemote = fileNodeRemote->ToElement();
std::string remoteHash = fileElemRemote->Attribute("md5"); std::string remoteHash = fileElemRemote->Attribute("md5");
if (remoteHash != localHash) if (remoteHash != localHash)
bSameVersion = false; bSameVersion = false;
@ -1496,7 +1495,7 @@ int Downloader::repairFile(const std::string& url, const std::string& filepath,
bool bFileExists = boost::filesystem::exists(pathname); bool bFileExists = boost::filesystem::exists(pathname);
bool bLocalXMLExists = boost::filesystem::exists(xml_file); bool bLocalXMLExists = boost::filesystem::exists(xml_file);
TiXmlDocument xml; tinyxml2::XMLDocument xml;
if (!xml_data.empty()) // Parse remote XML data if (!xml_data.empty()) // Parse remote XML data
{ {
std::cout << "XML: Using remote file" << std::endl; std::cout << "XML: Using remote file" << std::endl;
@ -1507,12 +1506,12 @@ int Downloader::repairFile(const std::string& url, const std::string& filepath,
std::cout << "XML: Using local file" << std::endl; std::cout << "XML: Using local file" << std::endl;
if (!bLocalXMLExists) if (!bLocalXMLExists)
std::cout << "XML: File doesn't exist (" << xml_file << ")" << std::endl; std::cout << "XML: File doesn't exist (" << xml_file << ")" << std::endl;
xml.LoadFile(xml_file); xml.LoadFile(xml_file.c_str());
} }
// Check if file node exists in XML data // Check if file node exists in XML data
TiXmlNode *fileNode = xml.FirstChild("file"); tinyxml2::XMLElement *fileElem = xml.FirstChildElement("file");
if (!fileNode) if (!fileElem)
{ // File node doesn't exist { // File node doesn't exist
std::cout << "XML: Parsing failed / not valid XML" << std::endl; std::cout << "XML: Parsing failed / not valid XML" << std::endl;
if (config.bDownload) if (config.bDownload)
@ -1523,23 +1522,22 @@ int Downloader::repairFile(const std::string& url, const std::string& filepath,
else else
{ // File node exists --> valid XML { // File node exists --> valid XML
std::cout << "XML: Valid XML" << std::endl; std::cout << "XML: Valid XML" << std::endl;
TiXmlElement *fileElem = fileNode->ToElement();
filename = fileElem->Attribute("name"); filename = fileElem->Attribute("name");
filehash = fileElem->Attribute("md5"); filehash = fileElem->Attribute("md5");
std::stringstream(fileElem->Attribute("chunks")) >> chunks; std::stringstream(fileElem->Attribute("chunks")) >> chunks;
std::stringstream(fileElem->Attribute("total_size")) >> filesize; std::stringstream(fileElem->Attribute("total_size")) >> filesize;
//Iterate through all chunk nodes //Iterate through all chunk nodes
TiXmlNode *chunkNode = fileNode->FirstChild(); tinyxml2::XMLNode *chunkNode = fileElem->FirstChild();
while (chunkNode) while (chunkNode)
{ {
TiXmlElement *chunkElem = chunkNode->ToElement(); tinyxml2::XMLElement *chunkElem = chunkNode->ToElement();
std::stringstream(chunkElem->Attribute("from")) >> from_offset; std::stringstream(chunkElem->Attribute("from")) >> from_offset;
std::stringstream(chunkElem->Attribute("to")) >> to_offset; std::stringstream(chunkElem->Attribute("to")) >> to_offset;
chunk_from.push_back(from_offset); chunk_from.push_back(from_offset);
chunk_to.push_back(to_offset); chunk_to.push_back(to_offset);
chunk_hash.push_back(chunkElem->GetText()); chunk_hash.push_back(chunkElem->GetText());
chunkNode = fileNode->IterateChildren(chunkNode); chunkNode = chunkNode->NextSibling();
} }
std::cout << "XML: Parsing finished" << std::endl << std::endl std::cout << "XML: Parsing finished" << std::endl << std::endl
@ -1735,7 +1733,7 @@ int Downloader::repairFile(const std::string& url, const std::string& filepath,
int Downloader::downloadCovers(const std::string& gamename, const std::string& directory, const std::string& cover_xml_data) int Downloader::downloadCovers(const std::string& gamename, const std::string& directory, const std::string& cover_xml_data)
{ {
int res = 0; int res = 0;
TiXmlDocument xml; tinyxml2::XMLDocument xml;
// Check that directory exists and create subdirectories // Check that directory exists and create subdirectories
boost::filesystem::path path = directory; boost::filesystem::path path = directory;
@ -1758,7 +1756,7 @@ int Downloader::downloadCovers(const std::string& gamename, const std::string& d
} }
xml.Parse(cover_xml_data.c_str()); xml.Parse(cover_xml_data.c_str());
TiXmlElement *rootNode = xml.RootElement(); tinyxml2::XMLElement *rootNode = xml.RootElement();
if (!rootNode) if (!rootNode)
{ {
std::cout << "Not valid XML" << std::endl; std::cout << "Not valid XML" << std::endl;
@ -1766,19 +1764,19 @@ int Downloader::downloadCovers(const std::string& gamename, const std::string& d
} }
else else
{ {
TiXmlNode *gameNode = rootNode->FirstChild(); tinyxml2::XMLNode *gameNode = rootNode->FirstChild();
while (gameNode) while (gameNode)
{ {
TiXmlElement *gameElem = gameNode->ToElement(); tinyxml2::XMLElement *gameElem = gameNode->ToElement();
std::string game_name = gameElem->Attribute("name"); std::string game_name = gameElem->Attribute("name");
if (game_name == gamename) if (game_name == gamename)
{ {
boost::match_results<std::string::const_iterator> what; boost::match_results<std::string::const_iterator> what;
TiXmlNode *coverNode = gameNode->FirstChild(); tinyxml2::XMLNode *coverNode = gameNode->FirstChild();
while (coverNode) while (coverNode)
{ {
TiXmlElement *coverElem = coverNode->ToElement(); tinyxml2::XMLElement *coverElem = coverNode->ToElement();
std::string cover_url = coverElem->GetText(); std::string cover_url = coverElem->GetText();
// Get file extension for the image // Get file extension for the image
boost::regex e1(".*(\\.\\w+)$", boost::regex::perl | boost::regex::icase); boost::regex e1(".*(\\.\\w+)$", boost::regex::perl | boost::regex::icase);
@ -1806,11 +1804,11 @@ int Downloader::downloadCovers(const std::string& gamename, const std::string& d
std::cout << "failed to get error code: " << curl_easy_strerror(result) << " (" << cover_url << ")" << std::endl; std::cout << "failed to get error code: " << curl_easy_strerror(result) << " (" << cover_url << ")" << std::endl;
} }
coverNode = gameNode->IterateChildren(coverNode); coverNode = coverNode->NextSibling();
} }
break; // Found cover for game, no need to go through rest of the game nodes break; // Found cover for game, no need to go through rest of the game nodes
} }
gameNode = rootNode->IterateChildren(gameNode); gameNode = gameNode->NextSibling();
} }
} }
@ -1871,6 +1869,10 @@ std::string Downloader::getResponse(const std::string& url)
int Downloader::progressCallback(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) int Downloader::progressCallback(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
{ {
// unused so lets prevent warnings and be more pedantic
(void) ulnow;
(void) ultotal;
// on entry: dltotal - how much remains to download till the end of the file (bytes) // on entry: dltotal - how much remains to download till the end of the file (bytes)
// dlnow - how much was downloaded from the start of the program (bytes) // dlnow - how much was downloaded from the start of the program (bytes)
int bar_length = 26; int bar_length = 26;
@ -2326,12 +2328,11 @@ void Downloader::checkStatus()
if (boost::filesystem::exists(local_xml_file)) if (boost::filesystem::exists(local_xml_file))
{ {
TiXmlDocument local_xml; tinyxml2::XMLDocument local_xml;
local_xml.LoadFile(local_xml_file.string()); local_xml.LoadFile(local_xml_file.string().c_str());
TiXmlNode *fileNodeLocal = local_xml.FirstChild("file"); tinyxml2::XMLElement *fileElemLocal = local_xml.FirstChildElement("file");
if (fileNodeLocal) if (fileElemLocal)
{ {
TiXmlElement *fileElemLocal = fileNodeLocal->ToElement();
std::string filesize_xml_str = fileElemLocal->Attribute("total_size"); std::string filesize_xml_str = fileElemLocal->Attribute("total_size");
filesize_xml = std::stoull(filesize_xml_str); filesize_xml = std::stoull(filesize_xml_str);
} }
@ -2463,12 +2464,11 @@ void Downloader::checkStatus()
if (boost::filesystem::exists(local_xml_file)) if (boost::filesystem::exists(local_xml_file))
{ {
TiXmlDocument local_xml; tinyxml2::XMLDocument local_xml;
local_xml.LoadFile(local_xml_file.string()); local_xml.LoadFile(local_xml_file.string().c_str());
TiXmlNode *fileNodeLocal = local_xml.FirstChild("file"); tinyxml2::XMLElement *fileElemLocal = local_xml.FirstChildElement("file");
if (fileNodeLocal) if (fileElemLocal)
{ {
TiXmlElement *fileElemLocal = fileNodeLocal->ToElement();
std::string filesize_xml_str = fileElemLocal->Attribute("total_size"); std::string filesize_xml_str = fileElemLocal->Attribute("total_size");
filesize_xml = std::stoull(filesize_xml_str); filesize_xml = std::stoull(filesize_xml_str);
} }
@ -2561,19 +2561,18 @@ std::string Downloader::getLocalFileHash(const std::string& filepath, const std:
if (boost::filesystem::exists(local_xml_file)) if (boost::filesystem::exists(local_xml_file))
{ {
TiXmlDocument local_xml; tinyxml2::XMLDocument local_xml;
local_xml.LoadFile(local_xml_file.string()); local_xml.LoadFile(local_xml_file.string().c_str());
TiXmlNode *fileNodeLocal = local_xml.FirstChild("file"); tinyxml2::XMLElement *fileElemLocal = local_xml.FirstChildElement("file");
if (!fileNodeLocal && config.bAutomaticXMLCreation) if (!fileElemLocal && config.bAutomaticXMLCreation)
{ {
std::string xml_directory = config.sXMLDirectory + "/" + gamename; std::string xml_directory = config.sXMLDirectory + "/" + gamename;
Util::createXML(filepath, config.iChunkSize, xml_directory); Util::createXML(filepath, config.iChunkSize, xml_directory);
local_xml.LoadFile(local_xml_file.string()); local_xml.LoadFile(local_xml_file.string().c_str());
fileNodeLocal = local_xml.FirstChild("file"); fileElemLocal = local_xml.FirstChildElement("file");
} }
if (fileNodeLocal) if (fileElemLocal)
{ {
TiXmlElement *fileElemLocal = fileNodeLocal->ToElement();
localHash = fileElemLocal->Attribute("md5"); localHash = fileElemLocal->Attribute("md5");
return localHash; return localHash;
} }
@ -2597,12 +2596,11 @@ std::string Downloader::getRemoteFileHash(const std::string& gamename, const std
} }
if (!xml_data.empty()) if (!xml_data.empty())
{ {
TiXmlDocument remote_xml; tinyxml2::XMLDocument remote_xml;
remote_xml.Parse(xml_data.c_str()); remote_xml.Parse(xml_data.c_str());
TiXmlNode *fileNodeRemote = remote_xml.FirstChild("file"); tinyxml2::XMLElement *fileElemRemote = remote_xml.FirstChildElement("file");
if (fileNodeRemote) if (fileElemRemote)
{ {
TiXmlElement *fileElemRemote = fileNodeRemote->ToElement();
remoteHash = fileElemRemote->Attribute("md5"); remoteHash = fileElemRemote->Attribute("md5");
} }
} }

View File

@ -8,7 +8,7 @@
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include <boost/algorithm/string/case_conv.hpp> #include <boost/algorithm/string/case_conv.hpp>
#include <tinyxml.h> #include <tinyxml2.h>
#include <json/json.h> #include <json/json.h>
#include <fstream> #include <fstream>
#include <sys/ioctl.h> #include <sys/ioctl.h>
@ -132,11 +132,11 @@ int Util::createXML(std::string filepath, uintmax_t chunk_size, std::string xml_
<< "Chunks: " << chunks << std::endl << "Chunks: " << chunks << std::endl
<< "Chunk size: " << (chunk_size >> 20) << " MB" << std::endl; << "Chunk size: " << (chunk_size >> 20) << " MB" << std::endl;
TiXmlDocument xml; tinyxml2::XMLDocument xml;
TiXmlElement *fileElem = new TiXmlElement("file"); tinyxml2::XMLElement *fileElem = xml.NewElement("file");
fileElem->SetAttribute("name", filename); fileElem->SetAttribute("name", filename.c_str());
fileElem->SetAttribute("chunks", chunks); fileElem->SetAttribute("chunks", chunks);
fileElem->SetAttribute("total_size", std::to_string(filesize)); fileElem->SetAttribute("total_size", std::to_string(filesize).c_str());
std::cout << "Getting MD5 for chunks" << std::endl; std::cout << "Getting MD5 for chunks" << std::endl;
@ -175,12 +175,12 @@ int Util::createXML(std::string filepath, uintmax_t chunk_size, std::string xml_
free(chunk); free(chunk);
TiXmlElement *chunkElem = new TiXmlElement("chunk"); tinyxml2::XMLElement *chunkElem = xml.NewElement("chunk");
chunkElem->SetAttribute("id", i); chunkElem->SetAttribute("id", i);
chunkElem->SetAttribute("from", std::to_string(range_begin)); chunkElem->SetAttribute("from", std::to_string(range_begin).c_str());
chunkElem->SetAttribute("to", std::to_string(range_end)); chunkElem->SetAttribute("to", std::to_string(range_end).c_str());
chunkElem->SetAttribute("method", "md5"); chunkElem->SetAttribute("method", "md5");
TiXmlText *text = new TiXmlText(hash); tinyxml2::XMLText *text = xml.NewText(hash.c_str());
chunkElem->LinkEndChild(text); chunkElem->LinkEndChild(text);
fileElem->LinkEndChild(chunkElem); fileElem->LinkEndChild(chunkElem);
@ -192,15 +192,15 @@ int Util::createXML(std::string filepath, uintmax_t chunk_size, std::string xml_
rhash_print(rhash_result, rhash_context, RHASH_MD5, RHPR_HEX); rhash_print(rhash_result, rhash_context, RHASH_MD5, RHPR_HEX);
rhash_free(rhash_context); rhash_free(rhash_context);
std::string file_md5 = rhash_result; std::cout << std::endl << "MD5: " << rhash_result << std::endl;
std::cout << std::endl << "MD5: " << file_md5 << std::endl; fileElem->SetAttribute("md5", rhash_result);
fileElem->SetAttribute("md5", file_md5);
xml.LinkEndChild(fileElem); xml.LinkEndChild(fileElem);
std::cout << "Writing XML: " << filenameXML << std::endl; std::cout << "Writing XML: " << filenameXML << std::endl;
if ((xmlfile=fopen(filenameXML.c_str(), "w"))!=NULL) { if ((xmlfile=fopen(filenameXML.c_str(), "w"))!=NULL) {
xml.Print(xmlfile); tinyxml2::XMLPrinter printer(xmlfile);
xml.Print(&printer);
fclose(xmlfile); fclose(xmlfile);
res = 1; res = 1;
} else { } else {