32 #ifdef KALDI_CYGWIN_COMPAT 34 #define MapOsPath(x) MapCygwinPath(x) 35 #else // KALDI_CYGWIN_COMPAT 36 #define MapOsPath(x) x 37 #endif // KALDI_CYGWIN_COMPAT 41 static FILE *popen(
const char* command,
const char* mode) {
42 #ifdef KALDI_CYGWIN_COMPAT 44 #else // KALDI_CYGWIN_COMPAT 45 return _popen(command, mode);
46 #endif // KALDI_CYGWIN_COMPAT 52 #ifndef _MSC_VER // on VS, we don't need this type. 62 if (rxfilename ==
"" || rxfilename ==
"-") {
63 return "standard input";
74 if (wxfilename ==
"" || wxfilename ==
"-") {
75 return "standard output";
86 const char *c = filename.c_str();
87 size_t length = filename.length();
88 char first_char = c[0],
89 last_char = (length == 0 ?
'\0' : c[filename.length()-1]);
92 if (length == 0 || (length == 1 && first_char ==
'-'))
95 else if (isspace(first_char) || isspace(last_char) || last_char ==
'|') {
99 }
else if ((first_char ==
'a' || first_char ==
's') &&
100 strchr(c,
':') != NULL &&
112 }
else if (isdigit(last_char)) {
118 const char *
d = c + length - 1;
119 while (isdigit(*d) && d > c) d--;
128 if (strchr(c,
'|') != NULL) {
129 KALDI_WARN <<
"Trying to classify wxfilename with pipe symbol in the" 130 " wrong place (pipe without | at the beginning?): " <<
139 const char *c = filename.c_str();
140 size_t length = filename.length();
141 char first_char = c[0],
142 last_char = (length == 0 ?
'\0' : c[filename.length()-1]);
145 if (length == 0 || (length == 1 && first_char ==
'-')) {
147 }
else if (first_char ==
'|') {
150 }
else if (last_char ==
'|') {
152 }
else if (isspace(first_char) || isspace(last_char)) {
154 }
else if ((first_char ==
'a' || first_char ==
's') &&
155 strchr(c,
':') != NULL &&
167 }
else if (isdigit(last_char)) {
168 const char *
d = c + length - 1;
169 while (isdigit(*d) && d > c) d--;
180 if (strchr(c,
'|') != NULL) {
181 KALDI_WARN <<
"Trying to classify rxfilename with pipe symbol in the" 182 " wrong place (pipe without | at the end?): " << filename;
192 virtual bool Open(
const std::string &filename,
bool binary) = 0;
193 virtual std::ostream &
Stream() = 0;
194 virtual bool Close() = 0;
201 virtual bool Open(
const std::string &filename,
bool binary) {
202 if (os_.is_open())
KALDI_ERR <<
"FileOutputImpl::Open(), " 203 <<
"open called on already open file.";
204 filename_ = filename;
206 binary ? std::ios_base::out | std::ios_base::binary
207 : std::ios_base::out);
208 return os_.is_open();
213 KALDI_ERR <<
"FileOutputImpl::Stream(), file is not open.";
220 KALDI_ERR <<
"FileOutputImpl::Close(), file is not open.";
223 return !(os_.fail());
229 KALDI_ERR <<
"Error closing output file " << filename_;
241 virtual bool Open(
const std::string &filename,
bool binary) {
242 if (is_open_)
KALDI_ERR <<
"StandardOutputImpl::Open(), " 243 "open called on already open file.";
245 _setmode(_fileno(stdout), binary ? _O_BINARY : _O_TEXT);
247 is_open_ = std::cout.good();
253 KALDI_ERR <<
"StandardOutputImpl::Stream(), object not initialized.";
260 KALDI_ERR <<
"StandardOutputImpl::Close(), file is not open.";
262 std::cout << std::flush;
263 return !(std::cout.fail());
267 std::cout << std::flush;
268 if (std::cout.fail())
269 KALDI_ERR <<
"Error writing to standard output";
280 virtual bool Open(
const std::string &wxfilename,
bool binary) {
281 filename_ = wxfilename;
283 KALDI_ASSERT(wxfilename.length() != 0 && wxfilename[0] ==
'|');
285 std::string cmd_name(wxfilename, 1);
286 #if defined(_MSC_VER) || defined(__CYGWIN__) 287 f_ = popen(cmd_name.c_str(), (binary ?
"wb" :
"w"));
289 f_ = popen(cmd_name.c_str(),
"w");
292 KALDI_WARN <<
"Failed opening pipe for writing, command is: " 293 << cmd_name <<
", errno is " << strerror(errno);
300 (binary ? std::ios_base::out|
301 std::ios_base::binary
302 :std::ios_base::out));
304 os_ =
new std::ostream(fb_);
306 os_ =
new std::ofstream(f_);
313 if (os_ == NULL)
KALDI_ERR <<
"PipeOutputImpl::Stream()," 314 " object not initialized.";
320 if (os_ == NULL)
KALDI_ERR <<
"PipeOutputImpl::Close(), file is not open.";
323 if (os_->fail()) ok =
false;
328 status = _pclose(f_);
333 KALDI_WARN <<
"Pipe " << filename_ <<
" had nonzero return status " 369 virtual bool Open(
const std::string &filename,
bool binary) = 0;
370 virtual std::istream &
Stream() = 0;
383 virtual bool Open(
const std::string &filename,
bool binary) {
384 if (is_.is_open())
KALDI_ERR <<
"FileInputImpl::Open(), " 385 <<
"open called on already open file.";
387 binary ? std::ios_base::in | std::ios_base::binary
388 : std::ios_base::in);
389 return is_.is_open();
394 KALDI_ERR <<
"FileInputImpl::Stream(), file is not open.";
401 KALDI_ERR <<
"FileInputImpl::Close(), file is not open.";
423 virtual bool Open(
const std::string &filename,
bool binary) {
424 if (is_open_)
KALDI_ERR <<
"StandardInputImpl::Open(), " 425 "open called on already open file.";
428 _setmode(_fileno(stdin), binary ? _O_BINARY : _O_TEXT);
436 KALDI_ERR <<
"StandardInputImpl::Stream(), object not initialized.";
444 if (!is_open_)
KALDI_ERR <<
"StandardInputImpl::Close(), file is not open.";
457 virtual bool Open(
const std::string &rxfilename,
bool binary) {
458 filename_ = rxfilename;
461 rxfilename[rxfilename.length()-1] ==
'|');
462 std::string cmd_name(rxfilename, 0, rxfilename.length()-1);
463 #if defined(_MSC_VER) || defined(__CYGWIN__) 464 f_ = popen(cmd_name.c_str(), (binary ?
"rb" :
"r"));
466 f_ = popen(cmd_name.c_str(),
"r");
470 KALDI_WARN <<
"Failed opening pipe for reading, command is: " 471 << cmd_name <<
", errno is " << strerror(errno);
477 (binary ? std::ios_base::in|
478 std::ios_base::binary
479 :std::ios_base::in));
481 is_ =
new std::istream(fb_);
483 is_ =
new std::ifstream(f_);
485 if (is_->fail() || is_->bad())
return false;
498 KALDI_ERR <<
"PipeInputImpl::Stream(), object not initialized.";
505 KALDI_ERR <<
"PipeInputImpl::Close(), file is not open.";
510 status = _pclose(f_);
515 KALDI_WARN <<
"Pipe " << filename_ <<
" had nonzero return status " 563 std::string *filename,
565 size_t pos = rxfilename.find_last_of(
':');
569 *filename = std::string(rxfilename, 0, pos);
570 std::string number(rxfilename, pos+1);
573 KALDI_ERR <<
"Cannot get offset from filename " << rxfilename
574 <<
" (possibly you compiled in 32-bit and have a >32-bit" 575 <<
" byte offset into a file; you'll have to compile 64-bit.";
579 size_t cur_pos = is_.tellg();
580 if (cur_pos == offset)
return true;
581 else if (cur_pos<offset && cur_pos+100 > offset) {
584 for (
size_t i = cur_pos;
i < offset;
i++)
586 return (is_.tellg() == std::streampos(offset));
589 is_.seekg(offset, std::ios_base::beg);
602 virtual bool Open(
const std::string &rxfilename,
bool binary) {
607 std::string tmp_filename;
609 SplitFilename(rxfilename, &tmp_filename, &offset);
610 if (tmp_filename == filename_ && binary == binary_) {
615 filename_ = tmp_filename;
617 binary ? std::ios_base::in | std::ios_base::binary
618 : std::ios_base::in);
619 if (!is_.is_open())
return false;
625 SplitFilename(rxfilename, &filename_, &offset);
628 binary ? std::ios_base::in | std::ios_base::binary
629 : std::ios_base::in);
630 if (!is_.is_open())
return false;
638 KALDI_ERR <<
"FileInputImpl::Stream(), file is not open.";
645 KALDI_ERR <<
"FileInputImpl::Close(), file is not open.";
666 bool write_header):impl_(NULL) {
667 if (!
Open(wxfilename, binary, write_header)) {
672 KALDI_ERR <<
"Error opening output stream " <<
694 KALDI_ERR <<
"Error closing output file " 697 " (disk full?)" :
"");
712 KALDI_ERR <<
"Output::Open(), failed to close output stream: " 729 KALDI_WARN <<
"Invalid output filename format "<<
755 if (!
Open(rxfilename, binary)) {
756 KALDI_ERR <<
"Error opening input stream " 774 bool *contents_binary) {
781 if (!
impl_->
Open(rxfilename, file_binary)) {
788 if (contents_binary != NULL)
806 KALDI_WARN <<
"Invalid input filename format "<<
810 if (!
impl_->
Open(rxfilename, file_binary)) {
816 if (contents_binary != NULL)
834 if (!filename.empty() && filename[filename.size() - 1] ==
']') {
837 std::string rxfilename, range;
839 KALDI_ERR <<
"Could not make sense of possible range specifier in filename " 840 <<
"while reading matrix: " << filename;
844 Input ki(rxfilename, &binary_in);
847 KALDI_ERR <<
"Error extracting range of object: " << filename;
852 Input ki(filename, &binary_in);
859 if (!filename.empty() && filename[filename.size() - 1] ==
']') {
862 std::string rxfilename, range;
864 KALDI_ERR <<
"Could not make sense of possible range specifier in filename " 865 <<
"while reading matrix: " << filename;
869 Input ki(rxfilename, &binary_in);
872 KALDI_ERR <<
"Error extracting range of object: " << filename;
877 Input ki(filename, &binary_in);
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
bool ConvertStringToInteger(const std::string &str, Int *out)
Converts a string into an integer via strtoll and returns false if there was any kind of problem (i...
InputType ClassifyRxfilename(const std::string &filename)
ClassifyRxfilenames interprets filenames for reading as follows:
bool InitKaldiInputStream(std::istream &is, bool *binary)
Initialize an opened stream for reading by detecting the binary header and.
virtual ~FileOutputImpl()
virtual std::ostream & Stream()=0
virtual std::ostream & Stream()
static std::string Escape(const std::string &str)
The following function will return a possibly quoted and escaped version of "str", according to the current shell.
virtual ~PipeOutputImpl()
basic_pipebuf< char > PipebufType
RspecifierType ClassifyRspecifier(const std::string &rspecifier, std::string *rxfilename, RspecifierOptions *opts)
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
void Read(std::istream &in, bool binary, bool add=false)
read from stream.
bool ExtractObjectRange(const GeneralMatrix &input, const std::string &range, GeneralMatrix *output)
GeneralMatrix is always of type BaseFloat.
virtual bool Open(const std::string &wxfilename, bool binary)
virtual bool Open(const std::string &filename, bool binary)
virtual bool Open(const std::string &filename, bool binary)=0
virtual ~StandardOutputImpl()
static FILE * CygwinCompatPopen(const char *command, const char *mode)
WspecifierType ClassifyWspecifier(const std::string &wspecifier, std::string *archive_wxfilename, std::string *script_wxfilename, WspecifierOptions *opts)
bool Open(const std::string &wxfilename, bool binary, bool write_header)
This opens the stream, with the given mode (binary or text).
virtual std::ostream & Stream()
#define KALDI_ASSERT(cond)
OutputType ClassifyWxfilename(const std::string &filename)
ClassifyWxfilename interprets filenames as follows:
virtual std::ostream & Stream()
virtual ~OutputImplBase()
std::string PrintableRxfilename(const std::string &rxfilename)
PrintableRxfilename turns the rxfilename into a more human-readable form for error reporting...
std::string PrintableWxfilename(const std::string &wxfilename)
PrintableWxfilename turns the wxfilename into a more human-readable form for error reporting...
void InitKaldiOutputStream(std::ostream &os, bool binary)
InitKaldiOutputStream initializes an opened stream for writing by writing an optional binary header a...
bool ExtractRangeSpecifier(const std::string &rxfilename_with_range, std::string *data_rxfilename, std::string *range)
virtual bool Open(const std::string &filename, bool binary)
This is an Kaldi C++ Library header.