16#include <boost/tokenizer.hpp>
23using namespace Kernel;
32 "The filename of the output TBL file.");
35 "The name of the workspace containing the data you want to "
36 "save to a TBL file.");
44 size_t rowCount = ws->rowCount();
45 for (
size_t i = 0; i < rowCount; ++i) {
47 if (row.
cell<
int>(ws->columnCount() - 2) != 0) {
64 throw std::runtime_error(
"Please provide an input workspace to be saved.");
67 std::ofstream file(filename.c_str());
73 std::vector<std::string> columnHeadings = ws->getColumnNames();
75 for (
size_t rowIndex = 0; rowIndex < ws->rowCount(); ++rowIndex) {
77 for (
size_t columnIndex = 0; columnIndex < columnHeadings.size(); columnIndex++) {
78 if (ws->getColumn(columnIndex)->type() !=
"str") {
80 int error = remove(filename.c_str());
82 throw std::runtime_error(columnHeadings[columnIndex] +
" column must be of type \"str\". The TBL "
83 "file will not be saved");
85 throw std::runtime_error(
"Saving TBL unsuccessful, please check the " + columnHeadings[columnIndex] +
86 " column is of type \"str\".");
88 if (columnIndex == columnHeadings.size() - 1)
89 writeVal<std::string>(row.
String(columnIndex), file,
false,
true);
91 writeVal<std::string>(row.
String(columnIndex), file);
100 auto it = columnHeadings.begin();
101 for (; it != columnHeadings.end() - 1; ++it) {
102 auto const &heading = *it;
103 writeVal<std::string>(heading, file);
105 auto const &lastHeading = *it;
106 writeVal<std::string>(lastHeading, file,
false,
true);
117template <
class T>
void SaveTBL::writeVal(
const T &val, std::ofstream &file,
bool endsep,
bool endline) {
118 std::string valStr = boost::lexical_cast<std::string>(val);
119 size_t comPos = valStr.find(
',');
120 if (comPos != std::string::npos) {
121 file <<
'"' << val <<
'"';
123 file << boost::lexical_cast<T>(val);
#define DECLARE_ALGORITHM(classname)
static char constexpr m_sep
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
@ Save
to specify a file to write to, the file may or may not exist
TableRow represents a row in a TableWorkspace.
std::string & String(size_t col)
Returns a reference to the element in position col if its type is std::string.
T & cell(size_t col)
Templated method to access the element col in the row.
A property class for workspaces.
void writeColumnNames(std::ofstream &file, std::vector< std::string > const &columnHeadings)
const char m_sep
the separator
void writeVal(const T &val, std::ofstream &file, bool endsep=true, bool endline=false)
Writes a value to the file.
std::map< int, std::vector< size_t > > m_stichgroups
Map the separator options to their string equivalents.
std::vector< size_t > m_nogroup
void findGroups(const API::ITableWorkspace_sptr &ws)
Finds the stitch groups that need to be on the same line.
void init() override
Overwrites Algorithm method.
SaveTBL()
Default constructor.
void exec() override
Overwrites Algorithm method.
Records the filename and the description of failure.
std::shared_ptr< ITableWorkspace > ITableWorkspace_sptr
shared pointer to Mantid::API::ITableWorkspace
@ Input
An input workspace.