make libzip optional

This commit is contained in:
Patrick Brosi 2022-10-12 12:08:52 +02:00
parent 0699fccb1e
commit 57545ced30
6 changed files with 65 additions and 3 deletions

View file

@ -47,6 +47,12 @@ endif ()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DPFAEDLE_PRECISION=${PFAEDLE_PRECISION}") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DPFAEDLE_PRECISION=${PFAEDLE_PRECISION}")
find_package(LibZip)
if (LIBZIP_FOUND)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DLIBZIP_FOUND=1")
endif()
# http://brianmilco.blogspot.de/2012/11/cmake-automatically-use-git-tags-as.html # http://brianmilco.blogspot.de/2012/11/cmake-automatically-use-git-tags-as.html
include(GetGitRevisionDescription) include(GetGitRevisionDescription)
git_get_tag(VERSION_GIT) git_get_tag(VERSION_GIT)

View file

@ -17,6 +17,7 @@ For a quick visual inspection of the shape quality, see for example the schedule
* `cmake` * `cmake`
* `gcc >= 5.0` (or `clang >= 3.9`) * `gcc >= 5.0` (or `clang >= 3.9`)
* `libzip` (optional, for ZIP support)
## Building and Installation ## Building and Installation

View file

@ -44,6 +44,6 @@ MARK_AS_ADVANCED(LIBZIP_LIBRARY LIBZIP_INCLUDE_DIR LIBZIP_CONF_INCLUDE_DIR LIBZI
IF (LIBZIP_FOUND) IF (LIBZIP_FOUND)
MESSAGE(STATUS "Found libzip: ${LIBZIP_LIBRARY}") MESSAGE(STATUS "Found libzip: ${LIBZIP_LIBRARY}")
ELSE (LIPZIP_FOUND) ELSE (LIBZIP_FOUND)
MESSAGE(FATAL_ERROR "Could not find libzip") MESSAGE(FATAL_ERROR "Could not find libzip")
ENDIF (LIBZIP_FOUND) ENDIF (LIBZIP_FOUND)

@ -1 +1 @@
Subproject commit 35ab19c313f96fec649fd0cb31c42d281f2c3b6e Subproject commit 354fc1d68dd8a1cff43ae2cec9a4d294eabcb0c9

View file

@ -3,7 +3,10 @@
// Authors: Patrick Brosi <brosi@informatik.uni-freiburg.de> // Authors: Patrick Brosi <brosi@informatik.uni-freiburg.de>
#include <stdio.h> #include <stdio.h>
#ifdef LIBZIP_FOUND
#include <zip.h> #include <zip.h>
#endif
#include <cstdio> #include <cstdio>
#include <fstream> #include <fstream>
@ -20,7 +23,9 @@
using ad::cppgtfs::Parser; using ad::cppgtfs::Parser;
using ad::util::CsvWriter; using ad::util::CsvWriter;
#ifdef LIBZIP_FOUND
using ad::util::ZipCsvParser; using ad::util::ZipCsvParser;
#endif
using pfaedle::gtfs::Writer; using pfaedle::gtfs::Writer;
using util::getTmpFName; using util::getTmpFName;
@ -39,6 +44,7 @@ void Writer::write(gtfs::Feed* sourceFeed, const std::string& path) const {
if (gtfsPath.size() == 0) gtfsPath = "."; if (gtfsPath.size() == 0) gtfsPath = ".";
#ifdef LIBZIP_FOUND
zip* za = 0; zip* za = 0;
if (toZip) { if (toZip) {
@ -65,6 +71,11 @@ void Writer::write(gtfs::Feed* sourceFeed, const std::string& path) const {
<< ") Could not open ZIP file, reason was: " << errBuf; << ") Could not open ZIP file, reason was: " << errBuf;
throw ad::cppgtfs::WriterException(ss.str(), tmpZip); throw ad::cppgtfs::WriterException(ss.str(), tmpZip);
} }
#else
if (toZip) {
throw ad::cppgtfs::WriterException(
"Could not output ZIP file, pfaedle was compiled without libzip", path);
#endif
} else { } else {
mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
} }
@ -80,7 +91,9 @@ void Writer::write(gtfs::Feed* sourceFeed, const std::string& path) const {
fs.close(); fs.close();
if (toZip) { if (toZip) {
#ifdef LIBZIP_FOUND
moveIntoZip(za, curFile, "agency.txt"); moveIntoZip(za, curFile, "agency.txt");
#endif
} else { } else {
if (std::rename(curFile.c_str(), curFileTg.c_str())) if (std::rename(curFile.c_str(), curFileTg.c_str()))
cannotWrite(curFileTg); cannotWrite(curFileTg);
@ -94,7 +107,9 @@ void Writer::write(gtfs::Feed* sourceFeed, const std::string& path) const {
fs.close(); fs.close();
if (toZip) { if (toZip) {
#ifdef LIBZIP_FOUND
moveIntoZip(za, curFile, "stops.txt"); moveIntoZip(za, curFile, "stops.txt");
#endif
} else { } else {
if (std::rename(curFile.c_str(), curFileTg.c_str())) if (std::rename(curFile.c_str(), curFileTg.c_str()))
cannotWrite(curFileTg); cannotWrite(curFileTg);
@ -108,7 +123,9 @@ void Writer::write(gtfs::Feed* sourceFeed, const std::string& path) const {
fs.close(); fs.close();
if (toZip) { if (toZip) {
#ifdef LIBZIP_FOUND
moveIntoZip(za, curFile, "routes.txt"); moveIntoZip(za, curFile, "routes.txt");
#endif
} else { } else {
if (std::rename(curFile.c_str(), curFileTg.c_str())) if (std::rename(curFile.c_str(), curFileTg.c_str()))
cannotWrite(curFileTg); cannotWrite(curFileTg);
@ -123,7 +140,9 @@ void Writer::write(gtfs::Feed* sourceFeed, const std::string& path) const {
writeCalendar(sourceFeed, &fs); writeCalendar(sourceFeed, &fs);
fs.close(); fs.close();
if (toZip) { if (toZip) {
#ifdef LIBZIP_FOUND
moveIntoZip(za, curFile, "calendar.txt"); moveIntoZip(za, curFile, "calendar.txt");
#endif
} else { } else {
if (std::rename(curFile.c_str(), curFileTg.c_str())) if (std::rename(curFile.c_str(), curFileTg.c_str()))
cannotWrite(curFileTg); cannotWrite(curFileTg);
@ -139,7 +158,9 @@ void Writer::write(gtfs::Feed* sourceFeed, const std::string& path) const {
writeCalendarDates(sourceFeed, &fs); writeCalendarDates(sourceFeed, &fs);
fs.close(); fs.close();
if (toZip) { if (toZip) {
#ifdef LIBZIP_FOUND
moveIntoZip(za, curFile, "calendar_dates.txt"); moveIntoZip(za, curFile, "calendar_dates.txt");
#endif
} else { } else {
if (std::rename(curFile.c_str(), curFileTg.c_str())) if (std::rename(curFile.c_str(), curFileTg.c_str()))
cannotWrite(curFileTg); cannotWrite(curFileTg);
@ -155,7 +176,9 @@ void Writer::write(gtfs::Feed* sourceFeed, const std::string& path) const {
writeTransfers(sourceFeed, &fs); writeTransfers(sourceFeed, &fs);
fs.close(); fs.close();
if (toZip) { if (toZip) {
#ifdef LIBZIP_FOUND
moveIntoZip(za, curFile, "transfers.txt"); moveIntoZip(za, curFile, "transfers.txt");
#endif
} else { } else {
if (std::rename(curFile.c_str(), curFileTg.c_str())) if (std::rename(curFile.c_str(), curFileTg.c_str()))
cannotWrite(curFileTg); cannotWrite(curFileTg);
@ -171,7 +194,9 @@ void Writer::write(gtfs::Feed* sourceFeed, const std::string& path) const {
writeFares(sourceFeed, &fs); writeFares(sourceFeed, &fs);
fs.close(); fs.close();
if (toZip) { if (toZip) {
#ifdef LIBZIP_FOUND
moveIntoZip(za, curFile, "fare_attributes.txt"); moveIntoZip(za, curFile, "fare_attributes.txt");
#endif
} else { } else {
if (std::rename(curFile.c_str(), curFileTg.c_str())) if (std::rename(curFile.c_str(), curFileTg.c_str()))
cannotWrite(curFileTg); cannotWrite(curFileTg);
@ -187,7 +212,9 @@ void Writer::write(gtfs::Feed* sourceFeed, const std::string& path) const {
writeFareRules(sourceFeed, &fs); writeFareRules(sourceFeed, &fs);
fs.close(); fs.close();
if (toZip) { if (toZip) {
#ifdef LIBZIP_FOUND
moveIntoZip(za, curFile, "fare_rules.txt"); moveIntoZip(za, curFile, "fare_rules.txt");
#endif
} else { } else {
if (std::rename(curFile.c_str(), curFileTg.c_str())) if (std::rename(curFile.c_str(), curFileTg.c_str()))
cannotWrite(curFileTg); cannotWrite(curFileTg);
@ -204,7 +231,9 @@ void Writer::write(gtfs::Feed* sourceFeed, const std::string& path) const {
fs.close(); fs.close();
if (toZip) { if (toZip) {
#ifdef LIBZIP_FOUND
moveIntoZip(za, curFile, "pathways.txt"); moveIntoZip(za, curFile, "pathways.txt");
#endif
} else { } else {
if (std::rename(curFile.c_str(), curFileTg.c_str())) if (std::rename(curFile.c_str(), curFileTg.c_str()))
cannotWrite(curFileTg); cannotWrite(curFileTg);
@ -221,7 +250,9 @@ void Writer::write(gtfs::Feed* sourceFeed, const std::string& path) const {
fs.close(); fs.close();
if (toZip) { if (toZip) {
#ifdef LIBZIP_FOUND
moveIntoZip(za, curFile, "levels.txt"); moveIntoZip(za, curFile, "levels.txt");
#endif
} else { } else {
if (std::rename(curFile.c_str(), curFileTg.c_str())) if (std::rename(curFile.c_str(), curFileTg.c_str()))
cannotWrite(curFileTg); cannotWrite(curFileTg);
@ -236,7 +267,9 @@ void Writer::write(gtfs::Feed* sourceFeed, const std::string& path) const {
fs.close(); fs.close();
if (toZip) { if (toZip) {
#ifdef LIBZIP_FOUND
moveIntoZip(za, curFile, "shapes.txt"); moveIntoZip(za, curFile, "shapes.txt");
#endif
} else { } else {
if (std::rename(curFile.c_str(), curFileTg.c_str())) if (std::rename(curFile.c_str(), curFileTg.c_str()))
cannotWrite(curFileTg); cannotWrite(curFileTg);
@ -250,7 +283,9 @@ void Writer::write(gtfs::Feed* sourceFeed, const std::string& path) const {
fs.close(); fs.close();
if (toZip) { if (toZip) {
#ifdef LIBZIP_FOUND
moveIntoZip(za, curFile, "trips.txt"); moveIntoZip(za, curFile, "trips.txt");
#endif
} else { } else {
if (std::rename(curFile.c_str(), curFileTg.c_str())) if (std::rename(curFile.c_str(), curFileTg.c_str()))
cannotWrite(curFileTg); cannotWrite(curFileTg);
@ -266,7 +301,9 @@ void Writer::write(gtfs::Feed* sourceFeed, const std::string& path) const {
fs.close(); fs.close();
if (toZip) { if (toZip) {
#ifdef LIBZIP_FOUND
moveIntoZip(za, curFile, "frequencies.txt"); moveIntoZip(za, curFile, "frequencies.txt");
#endif
} else { } else {
if (std::rename(curFile.c_str(), curFileTg.c_str())) if (std::rename(curFile.c_str(), curFileTg.c_str()))
cannotWrite(curFileTg); cannotWrite(curFileTg);
@ -282,7 +319,9 @@ void Writer::write(gtfs::Feed* sourceFeed, const std::string& path) const {
fs.close(); fs.close();
if (toZip) { if (toZip) {
#ifdef LIBZIP_FOUND
moveIntoZip(za, curFile, "stop_times.txt"); moveIntoZip(za, curFile, "stop_times.txt");
#endif
} else { } else {
if (std::rename(curFile.c_str(), curFileTg.c_str())) if (std::rename(curFile.c_str(), curFileTg.c_str()))
cannotWrite(curFileTg); cannotWrite(curFileTg);
@ -298,23 +337,29 @@ void Writer::write(gtfs::Feed* sourceFeed, const std::string& path) const {
fs.close(); fs.close();
if (toZip) { if (toZip) {
#ifdef LIBZIP_FOUND
moveIntoZip(za, curFile, "feed_info.txt"); moveIntoZip(za, curFile, "feed_info.txt");
#endif
} else { } else {
if (std::rename(curFile.c_str(), curFileTg.c_str())) if (std::rename(curFile.c_str(), curFileTg.c_str()))
cannotWrite(curFileTg); cannotWrite(curFileTg);
} }
} }
} catch (...) { } catch (...) {
#ifdef LIBZIP_FOUND
zip_discard(za); zip_discard(za);
#endif
throw; throw;
} }
if (toZip) { if (toZip) {
#ifdef LIBZIP_FOUND
std::string targetZipPath = gtfsPath + "/" + zipFileName; std::string targetZipPath = gtfsPath + "/" + zipFileName;
if (!za) cannotWrite(targetZipPath); if (!za) cannotWrite(targetZipPath);
zip_close(za); zip_close(za);
if (std::rename(tmpZip.c_str(), targetZipPath.c_str())) if (std::rename(tmpZip.c_str(), targetZipPath.c_str()))
cannotWrite(targetZipPath); cannotWrite(targetZipPath);
#endif
} }
} }
@ -334,6 +379,9 @@ void Writer::writeFeedInfo(gtfs::Feed* f, std::ostream* os) const {
else else
csvw->skip(); csvw->skip();
csvw->writeString(f->getVersion()); csvw->writeString(f->getVersion());
csvw->writeString(f->getContactEmail());
csvw->writeString(f->getContactUrl());
csvw->writeString(f->getDefaultLang());
csvw->flushLine(); csvw->flushLine();
} }
@ -619,6 +667,7 @@ void Writer::cannotWrite(const std::string& file, const std::string& file2) {
} }
// ___________________________________________________________________________ // ___________________________________________________________________________
#ifdef LIBZIP_FOUND
void Writer::moveIntoZip(zip* za, const std::string& sourcePath, void Writer::moveIntoZip(zip* za, const std::string& sourcePath,
const std::string& targetPath) { const std::string& targetPath) {
zip_source_t* s; zip_source_t* s;
@ -638,3 +687,4 @@ void Writer::moveIntoZip(zip* za, const std::string& sourcePath,
cannotWrite(targetPath); cannotWrite(targetPath);
} }
} }
#endif

View file

@ -7,6 +7,9 @@
#include <memory> #include <memory>
#include <string> #include <string>
#ifdef LIBZIP_FOUND
#include <zip.h>
#endif
#include "Feed.h" #include "Feed.h"
#include "ad/cppgtfs/Parser.h" #include "ad/cppgtfs/Parser.h"
@ -41,8 +44,10 @@ class Writer {
static void cannotWrite(const std::string& file, const std::string& file2); static void cannotWrite(const std::string& file, const std::string& file2);
static void cannotWrite(const std::string& file); static void cannotWrite(const std::string& file);
#ifdef LIBZIP_FOUND
static void moveIntoZip(zip* za, const std::string& sourcePath, static void moveIntoZip(zip* za, const std::string& sourcePath,
const std::string& targetPath); const std::string& targetPath);
#endif
mutable std::ifstream _ifs; mutable std::ifstream _ifs;
}; };