Skip to content

Commit

Permalink
improved nul device handling fix eclipse-sumo#3400 refs eclipse-sumo#…
Browse files Browse the repository at this point in the history
  • Loading branch information
behrisch committed Nov 24, 2021
1 parent f657250 commit d1cadd5
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 20 deletions.
26 changes: 19 additions & 7 deletions src/router/RONet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,19 +271,31 @@ void
RONet::openOutput(const OptionsCont& options) {
if (options.isSet("output-file") && options.getString("output-file") != "") {
myRoutesOutput = &OutputDevice::getDevice(options.getString("output-file"));
myRoutesOutput->writeHeader<ROEdge>(SUMO_TAG_ROUTES);
myRoutesOutput->writeAttr("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance").writeAttr("xsi:noNamespaceSchemaLocation", "http://sumo.dlr.de/xsd/routes_file.xsd");
if (myRoutesOutput->isNull()) {
myRoutesOutput = nullptr;
} else {
myRoutesOutput->writeHeader<ROEdge>(SUMO_TAG_ROUTES);
myRoutesOutput->writeAttr("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance").writeAttr("xsi:noNamespaceSchemaLocation", "http://sumo.dlr.de/xsd/routes_file.xsd");
}
}
if (options.exists("alternatives-output") && options.isSet("alternatives-output")
&& !(options.exists("write-trips") && options.getBool("write-trips"))) {
myRouteAlternativesOutput = &OutputDevice::getDevice(options.getString("alternatives-output"));
myRouteAlternativesOutput->writeHeader<ROEdge>(SUMO_TAG_ROUTES);
myRouteAlternativesOutput->writeAttr("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance").writeAttr("xsi:noNamespaceSchemaLocation", "http://sumo.dlr.de/xsd/routes_file.xsd");
if (myRouteAlternativesOutput->isNull()) {
myRouteAlternativesOutput = nullptr;
} else {
myRouteAlternativesOutput->writeHeader<ROEdge>(SUMO_TAG_ROUTES);
myRouteAlternativesOutput->writeAttr("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance").writeAttr("xsi:noNamespaceSchemaLocation", "http://sumo.dlr.de/xsd/routes_file.xsd");
}
}
if (options.isSet("vtype-output")) {
myTypesOutput = &OutputDevice::getDevice(options.getString("vtype-output"));
myTypesOutput->writeHeader<ROEdge>(SUMO_TAG_ROUTES);
myTypesOutput->writeAttr("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance").writeAttr("xsi:noNamespaceSchemaLocation", "http://sumo.dlr.de/xsd/routes_file.xsd");
if (myTypesOutput->isNull()) {
myTypesOutput = nullptr;
} else {
myTypesOutput->writeHeader<ROEdge>(SUMO_TAG_ROUTES);
myTypesOutput->writeAttr("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance").writeAttr("xsi:noNamespaceSchemaLocation", "http://sumo.dlr.de/xsd/routes_file.xsd");
}
}
}

Expand Down Expand Up @@ -692,7 +704,7 @@ RONet::saveAndRemoveRoutesUntil(OptionsCont& options, const RORouterProvider& pr
// ok, check whether it has been routed
if (r->getRoutingSuccess()) {
// write the route
r->write(*myRoutesOutput, myRouteAlternativesOutput, myTypesOutput, options);
r->write(myRoutesOutput, myRouteAlternativesOutput, myTypesOutput, options);
myWrittenRouteNo++;
} else {
myDiscardedRouteNo++;
Expand Down
14 changes: 8 additions & 6 deletions src/router/RORoutable.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,16 @@ class RORoutable {
* @param[in] typeos The types - output device to store the vehicle types into
* @exception IOError If something fails (not yet implemented)
*/
void write(OutputDevice& os, OutputDevice* const altos,
void write(OutputDevice* os, OutputDevice* const altos,
OutputDevice* const typeos, OptionsCont& options) const {
if (altos == 0 && typeos == 0) {
saveAsXML(os, &os, false, options);
} else {
saveAsXML(os, typeos, false, options);
if (os != nullptr) {
if (altos == nullptr && typeos == nullptr) {
saveAsXML(*os, os, false, options);
} else {
saveAsXML(*os, typeos, false, options);
}
}
if (altos != 0) {
if (altos != nullptr) {
saveAsXML(*altos, typeos, true, options);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/utils/iodevices/OutputDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,8 @@ OutputDevice::getDevice(const std::string& name) {
throw IOError("No port number given.");
}
} else {
const int len = (int)name.length();
std::string name2 = name;
if (OptionsCont::getOptions().isSet("output-prefix") && name != "/dev/null") {
std::string name2 = (name == "nul" || name == "NUL") ? "/dev/null" : name;
if (OptionsCont::getOptions().isSet("output-prefix") && name2 != "/dev/null") {
std::string prefix = OptionsCont::getOptions().getString("output-prefix");
const std::string::size_type metaTimeIndex = prefix.find("TIME");
if (metaTimeIndex != std::string::npos) {
Expand All @@ -100,6 +99,7 @@ OutputDevice::getDevice(const std::string& name) {
}
name2 = FileHelpers::prependToLastPathComponent(prefix, name);
}
const int len = (int)name.length();
dev = new OutputDevice_File(name2, len > 3 && name.substr(len - 3) == ".gz");
}
dev->setPrecision();
Expand Down
7 changes: 7 additions & 0 deletions src/utils/iodevices/OutputDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ class OutputDevice {
*/
virtual bool ok();

/** @brief returns the information whether the device will discard all output
* @return Whether the device redirects to /dev/null
*/
virtual bool isNull() {
return false;
}

/// @brief get filename or suitable description of this device
const std::string& getFilename();

Expand Down
7 changes: 4 additions & 3 deletions src/utils/iodevices/OutputDevice_File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,18 @@
// method definitions
// ===========================================================================
OutputDevice_File::OutputDevice_File(const std::string& fullName, const bool compressed)
: OutputDevice(0, fullName), myFileStream(nullptr) {
#ifdef WIN32
: OutputDevice(0, fullName) {
if (fullName == "/dev/null") {
myAmNull = true;
#ifdef WIN32
myFileStream = new std::ofstream("NUL");
if (!myFileStream->good()) {
delete myFileStream;
throw IOError("Could not redirect to NUL device (" + std::string(std::strerror(errno)) + ").");
}
return;
}
#endif
}
const std::string& localName = StringUtils::transcodeToLocal(fullName);
#ifdef HAVE_ZLIB
if (compressed) {
Expand Down
12 changes: 11 additions & 1 deletion src/utils/iodevices/OutputDevice_File.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ class OutputDevice_File : public OutputDevice {
/// @brief Destructor
~OutputDevice_File();

/** @brief returns the information whether the device will discard all output
* @return Whether the device redirects to /dev/null
*/
bool isNull() {
return myAmNull;
}


protected:
/// @name Methods that override/implement OutputDevice-methods
Expand All @@ -62,6 +69,9 @@ class OutputDevice_File : public OutputDevice {

private:
/// The wrapped ofstream
std::ostream* myFileStream;
std::ostream* myFileStream = nullptr;

/// am I redirecting to /dev/null
bool myAmNull = false;

};

0 comments on commit d1cadd5

Please sign in to comment.