#include "gromacs/utility/exceptions.h"
#include "gromacs/utility/fileredirector.h"
#include "gromacs/utility/gmxassert.h"
+#include "gromacs/utility/path.h"
#include "gromacs/utility/programcontext.h"
+#include "gromacs/utility/stringstream.h"
#include "gromacs/utility/stringutil.h"
#include "gromacs/utility/textreader.h"
+#include "gromacs/utility/textstream.h"
#include "gromacs/utility/textwriter.h"
#include "shellcompletions.h"
{
public:
//! Initializes reST exporter.
- explicit HelpExportReStructuredText(
- const CommandLineHelpModuleImpl &helpModule);
+ HelpExportReStructuredText(
+ const CommandLineHelpModuleImpl &helpModule,
+ FileOutputRedirectorInterface *outputRedirector);
virtual void startModuleExport();
virtual void exportModuleHelp(
};
HelpExportReStructuredText::HelpExportReStructuredText(
- const CommandLineHelpModuleImpl &helpModule)
- : outputRedirector_(helpModule.outputRedirector_),
+ const CommandLineHelpModuleImpl &helpModule,
+ FileOutputRedirectorInterface *outputRedirector)
+ : outputRedirector_(outputRedirector),
binaryName_(helpModule.binaryName_),
links_(eHelpOutputFormat_Rst)
{
const std::string &tag,
const std::string &displayName)
{
- // TODO: Ideally, the file would only be touched if it really changes.
- // This would make Sphinx reruns much faster.
TextOutputStreamPointer file
= outputRedirector_->openTextOutputFile("onlinehelp/" + tag + ".rst");
TextWriter writer(file);
rootTopic_->exportHelp(exporter);
}
+namespace
+{
+
+/********************************************************************
+ * ModificationCheckingFileOutputStream
+ */
+
+class ModificationCheckingFileOutputStream : public TextOutputStream
+{
+ public:
+ ModificationCheckingFileOutputStream(
+ const char *path,
+ FileOutputRedirectorInterface *redirector)
+ : path_(path), redirector_(redirector)
+ {
+ }
+
+ virtual void write(const char *str) { contents_.write(str); }
+ virtual void close()
+ {
+ const std::string &newContents = contents_.toString();
+ // TODO: Redirect these for unit tests.
+ if (File::exists(path_))
+ {
+ const std::string originalContents_
+ = TextReader::readFileToString(path_);
+ if (originalContents_ == newContents)
+ {
+ return;
+ }
+ }
+ TextWriter writer(redirector_->openTextOutputFile(path_));
+ writer.writeString(newContents);
+ }
+
+ private:
+ std::string path_;
+ StringOutputStream contents_;
+ FileOutputRedirectorInterface *redirector_;
+};
+
+/********************************************************************
+ * ModificationCheckingFileOutputRedirector
+ */
+
+class ModificationCheckingFileOutputRedirector : public FileOutputRedirectorInterface
+{
+ public:
+ explicit ModificationCheckingFileOutputRedirector(
+ FileOutputRedirectorInterface *redirector)
+ : redirector_(redirector)
+ {
+ }
+
+ virtual TextOutputStream &standardOutput()
+ {
+ return redirector_->standardOutput();
+ }
+ virtual TextOutputStreamPointer openTextOutputFile(const char *filename)
+ {
+ return TextOutputStreamPointer(
+ new ModificationCheckingFileOutputStream(filename, redirector_));
+ }
+
+ private:
+ FileOutputRedirectorInterface *redirector_;
+};
+
+} // namespace
+
/********************************************************************
* CommandLineHelpModule
*/
CommandLineParser(&options).parse(&argc, argv);
if (!exportFormat.empty())
{
- boost::scoped_ptr<HelpExportInterface> exporter;
+ ModificationCheckingFileOutputRedirector redirector(impl_->outputRedirector_);
+ boost::scoped_ptr<HelpExportInterface> exporter;
if (exportFormat == "rst")
{
- exporter.reset(new HelpExportReStructuredText(*impl_));
+ exporter.reset(new HelpExportReStructuredText(*impl_, &redirector));
}
else if (exportFormat == "completion")
{