TableWriterBothImpl< Holder > Class Template Reference

#include <kaldi-table-inl.h>

Inheritance diagram for TableWriterBothImpl< Holder >:
Collaboration diagram for TableWriterBothImpl< Holder >:

Public Types

typedef Holder::T T
 
- Public Types inherited from TableWriterImplBase< Holder >
typedef Holder::T T
 

Public Member Functions

virtual bool Open (const std::string &wspecifier)
 
virtual bool IsOpen () const
 
void MakeFilename (typename std::ostream::pos_type streampos, std::string *output) const
 
virtual bool Write (const std::string &key, const T &value)
 
virtual void Flush ()
 
virtual bool Close ()
 
 TableWriterBothImpl ()
 
virtual ~TableWriterBothImpl ()
 
- Public Member Functions inherited from TableWriterImplBase< Holder >
virtual ~TableWriterImplBase ()
 
 TableWriterImplBase ()
 

Private Types

enum  { kUninitialized, kOpen, kWriteError }
 

Private Attributes

Output archive_output_
 
Output script_output_
 
WspecifierOptions opts_
 
std::string archive_wxfilename_
 
std::string script_wxfilename_
 
std::string wspecifier_
 
enum kaldi::TableWriterBothImpl:: { ... }  state_
 

Detailed Description

template<class Holder>
class kaldi::TableWriterBothImpl< Holder >

Definition at line 1289 of file kaldi-table-inl.h.

Member Typedef Documentation

◆ T

typedef Holder::T T

Definition at line 1291 of file kaldi-table-inl.h.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
private
Enumerator
kUninitialized 
kOpen 
kWriteError 

Definition at line 1458 of file kaldi-table-inl.h.

1458  { // is stream open?
1459  kUninitialized, // no
1460  kOpen, // yes
1461  kWriteError, // yes
1462  } state_;
enum kaldi::TableWriterBothImpl::@3 state_

Constructor & Destructor Documentation

◆ TableWriterBothImpl()

TableWriterBothImpl ( )
inline

Definition at line 1440 of file kaldi-table-inl.h.

◆ ~TableWriterBothImpl()

virtual ~TableWriterBothImpl ( )
inlinevirtual

Definition at line 1444 of file kaldi-table-inl.h.

References SequentialTableReader< Holder >::Close(), SequentialTableReader< Holder >::IsOpen(), and KALDI_ERR.

1444  {
1445  if (!IsOpen()) return;
1446  else if (!Close())
1447  KALDI_ERR << "Write failed or stream close failed: "
1448  << wspecifier_;
1449  }
virtual bool IsOpen() const
#define KALDI_ERR
Definition: kaldi-error.h:147

Member Function Documentation

◆ Close()

virtual bool Close ( )
inlinevirtual

Implements TableWriterImplBase< Holder >.

Definition at line 1427 of file kaldi-table-inl.h.

References SequentialTableReader< Holder >::IsOpen(), and KALDI_ERR.

1427  {
1428  if (!this->IsOpen())
1429  KALDI_ERR << "Close called on a stream that was not open.";
1430  bool close_success = true;
1431  if (archive_output_.IsOpen())
1432  if (!archive_output_.Close()) close_success = false;
1433  if (script_output_.IsOpen())
1434  if (!script_output_.Close()) close_success = false;
1435  bool ans = close_success && (state_ != kWriteError);
1437  return ans;
1438  }
virtual bool IsOpen() const
enum kaldi::TableWriterBothImpl::@3 state_
#define KALDI_ERR
Definition: kaldi-error.h:147
bool Close()
Definition: kaldi-io.cc:677

◆ Flush()

virtual void Flush ( )
inlinevirtual

Implements TableWriterImplBase< Holder >.

Definition at line 1416 of file kaldi-table-inl.h.

References KALDI_WARN.

1416  {
1417  switch (state_) {
1418  case kWriteError: case kOpen:
1419  archive_output_.Stream().flush(); // Don't check error status.
1420  script_output_.Stream().flush(); // Don't check error status.
1421  return;
1422  default:
1423  KALDI_WARN << "Flush called on not-open writer.";
1424  }
1425  }
enum kaldi::TableWriterBothImpl::@3 state_
std::ostream & Stream()
Definition: kaldi-io.cc:701
#define KALDI_WARN
Definition: kaldi-error.h:150

◆ IsOpen()

virtual bool IsOpen ( ) const
inlinevirtual

Implements TableWriterImplBase< Holder >.

Definition at line 1331 of file kaldi-table-inl.h.

References KALDI_ERR.

1331  {
1332  switch (state_) {
1333  case kUninitialized: return false;
1334  case kOpen: case kWriteError: return true;
1335  default: KALDI_ERR << "IsOpen() called on TableWriter in invalid state.";
1336  }
1337  return false;
1338  }
enum kaldi::TableWriterBothImpl::@3 state_
#define KALDI_ERR
Definition: kaldi-error.h:147

◆ MakeFilename()

void MakeFilename ( typename std::ostream::pos_type  streampos,
std::string *  output 
) const
inline

Definition at line 1340 of file kaldi-table-inl.h.

References KALDI_ASSERT.

1341  {
1342  std::ostringstream ss;
1343  ss << ':' << streampos;
1344  KALDI_ASSERT(ss.str() != ":-1");
1345  *output = archive_wxfilename_ + ss.str();
1346 
1347  // e.g. /some/file:12302.
1348  // Note that we warned if archive_wxfilename_ is not an actual filename;
1349  // the philosophy is we give the user rope and if they want to hang
1350  // themselves, with it, fine.
1351  }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Open()

virtual bool Open ( const std::string &  wspecifier)
inlinevirtual

Implements TableWriterImplBase< Holder >.

Definition at line 1293 of file kaldi-table-inl.h.

References kaldi::ClassifyWspecifier(), kaldi::ClassifyWxfilename(), SequentialTableReader< Holder >::Close(), KALDI_ASSERT, KALDI_ERR, KALDI_WARN, kaldi::kBothWspecifier, and kaldi::kFileOutput.

1293  {
1294  switch (state_) {
1295  case kUninitialized:
1296  break;
1297  case kWriteError:
1298  KALDI_ERR << "Opening stream, already open with write error.";
1299  case kOpen: default:
1300  if (!Close()) // throw because this error may not have been previously
1301  // detected by user.
1302  KALDI_ERR << "Opening stream, error closing previously open stream.";
1303  }
1304  wspecifier_ = wspecifier;
1305  WspecifierType ws = ClassifyWspecifier(wspecifier,
1308  &opts_);
1309  KALDI_ASSERT(ws == kBothWspecifier); // or wrongly called.
1311  KALDI_WARN << "When writing to both archive and script, the script file "
1312  "will generally not be interpreted correctly unless the archive is "
1313  "an actual file: wspecifier = " << wspecifier;
1314 
1316  // false means no binary header.
1318  return false;
1319  }
1320  if (!script_output_.Open(script_wxfilename_, false, false)) { // first
1321  // false means text mode: script files always text-mode. second false
1322  // means don't write header (doesn't matter for text mode).
1323  archive_output_.Close(); // Don't care about status: error anyway.
1325  return false;
1326  }
1327  state_ = kOpen;
1328  return true;
1329  }
enum kaldi::TableWriterBothImpl::@3 state_
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
WspecifierType
Definition: kaldi-table.h:106
WspecifierType ClassifyWspecifier(const std::string &wspecifier, std::string *archive_wxfilename, std::string *script_wxfilename, WspecifierOptions *opts)
Definition: kaldi-table.cc:135
bool Open(const std::string &wxfilename, bool binary, bool write_header)
This opens the stream, with the given mode (binary or text).
Definition: kaldi-io.cc:707
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
OutputType ClassifyWxfilename(const std::string &filename)
ClassifyWxfilename interprets filenames as follows:
Definition: kaldi-io.cc:85
bool Close()
Definition: kaldi-io.cc:677

◆ Write()

virtual bool Write ( const std::string &  key,
const T value 
)
inlinevirtual

Implements TableWriterImplBase< Holder >.

Definition at line 1355 of file kaldi-table-inl.h.

References kaldi::IsToken(), KALDI_ERR, KALDI_WARN, and kaldi::PrintableWxfilename().

1355  {
1356  switch (state_) {
1357  case kOpen: break;
1358  case kWriteError:
1359  // user should have known from the last
1360  // call to Write that there was a problem. Warn about it.
1361  KALDI_WARN << "Writing to non-open TableWriter object.";
1362  return false;
1363  case kUninitialized: default:
1364  KALDI_ERR << "Write called on invalid stream";
1365  }
1366  // state is now kOpen or kWriteError.
1367  if (!IsToken(key)) // e.g. empty string or has spaces...
1368  KALDI_ERR << "Using invalid key " << key;
1369  std::ostream &archive_os = archive_output_.Stream();
1370  archive_os << key << ' ';
1371  typename std::ostream::pos_type archive_os_pos = archive_os.tellp();
1372  // position at start of Write() to archive. We will record this in the
1373  // script file.
1374  std::string offset_rxfilename; // rxfilename with offset into the archive,
1375  // e.g. some_archive_name.ark:431541423
1376  MakeFilename(archive_os_pos, &offset_rxfilename);
1377 
1378  // Write to the script file first.
1379  // The idea is that we want to get all the information possible into the
1380  // script file, to make it easier to unwind errors later.
1381  std::ostream &script_os = script_output_.Stream();
1382  script_output_.Stream() << key << ' ' << offset_rxfilename << '\n';
1383 
1384  if (!Holder::Write(archive_output_.Stream(), opts_.binary, value)) {
1385  KALDI_WARN << "Write failure to"
1387  state_ = kWriteError;
1388  return false;
1389  }
1390 
1391  if (script_os.fail()) {
1392  KALDI_WARN << "Write failure to script file detected: "
1394  state_ = kWriteError;
1395  return false;
1396  }
1397 
1398  if (archive_os.fail()) {
1399  KALDI_WARN << "Write failure to archive file detected: "
1401  state_ = kWriteError;
1402  return false;
1403  }
1404 
1405  if (state_ == kWriteError) return false; // Even if this Write seems to
1406  // have succeeded, we fail because a previous Write failed and the archive
1407  // may be corrupted and unreadable.
1408 
1409  if (opts_.flush)
1410  Flush();
1411  return true;
1412  }
enum kaldi::TableWriterBothImpl::@3 state_
void MakeFilename(typename std::ostream::pos_type streampos, std::string *output) const
bool IsToken(const std::string &token)
Returns true if "token" is nonempty, and all characters are printable and whitespace-free.
Definition: text-utils.cc:105
std::ostream & Stream()
Definition: kaldi-io.cc:701
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
std::string PrintableWxfilename(const std::string &wxfilename)
PrintableWxfilename turns the wxfilename into a more human-readable form for error reporting...
Definition: kaldi-io.cc:73

Member Data Documentation

◆ archive_output_

Output archive_output_
private

Definition at line 1452 of file kaldi-table-inl.h.

◆ archive_wxfilename_

std::string archive_wxfilename_
private

Definition at line 1455 of file kaldi-table-inl.h.

◆ opts_

WspecifierOptions opts_
private

Definition at line 1454 of file kaldi-table-inl.h.

◆ script_output_

Output script_output_
private

Definition at line 1453 of file kaldi-table-inl.h.

◆ script_wxfilename_

std::string script_wxfilename_
private

Definition at line 1456 of file kaldi-table-inl.h.

◆ state_

enum { ... } state_

◆ wspecifier_

std::string wspecifier_
private

Definition at line 1457 of file kaldi-table-inl.h.


The documentation for this class was generated from the following file: