2009-05-29 08:12:09 +00:00
|
|
|
#include <qse/sed/StdSed.hpp>
|
2009-06-04 15:50:32 +00:00
|
|
|
#include <qse/cmn/main.h>
|
2019-06-25 02:36:32 +00:00
|
|
|
#include <qse/Exception.hpp>
|
2009-05-30 00:21:41 +00:00
|
|
|
#include <iostream>
|
2013-01-13 09:04:54 +00:00
|
|
|
#include "sed00.h"
|
2009-05-30 00:21:41 +00:00
|
|
|
|
2013-01-13 09:04:54 +00:00
|
|
|
#if defined(QSE_CHAR_IS_MCHAR)
|
2009-05-31 07:33:08 +00:00
|
|
|
# define xcout std::cout
|
|
|
|
#else
|
|
|
|
# define xcout std::wcout
|
|
|
|
#endif
|
|
|
|
|
2009-12-19 06:34:42 +00:00
|
|
|
//
|
|
|
|
// The MySed class simplifies QSE::StdSed by utilizing exception handling.
|
|
|
|
//
|
|
|
|
class MySed: protected QSE::StdSed
|
|
|
|
{
|
|
|
|
public:
|
2019-06-25 02:36:32 +00:00
|
|
|
QSE_EXCEPTION(Error);
|
2009-12-19 06:34:42 +00:00
|
|
|
|
2019-06-25 02:36:32 +00:00
|
|
|
MySed () { if (this->open() <= -1) QSE_THROW_WITH_MSG (Error, QSE_T("cannot open sed")); }
|
|
|
|
~MySed () { this->close (); }
|
2009-12-19 06:34:42 +00:00
|
|
|
|
|
|
|
void compile (const char_t* sptr)
|
|
|
|
{
|
2011-10-03 01:25:23 +00:00
|
|
|
QSE::StdSed::StringStream stream(sptr);
|
|
|
|
if (QSE::StdSed::compile (stream) <= -1)
|
2019-06-25 02:36:32 +00:00
|
|
|
QSE_THROW_WITH_MSG(Error, this->getErrorMessage());
|
2009-12-19 06:34:42 +00:00
|
|
|
}
|
|
|
|
|
2009-12-19 21:06:28 +00:00
|
|
|
void execute (Stream& stream)
|
2009-12-19 06:34:42 +00:00
|
|
|
{
|
|
|
|
if (QSE::StdSed::execute (stream) <= -1)
|
2019-06-25 02:36:32 +00:00
|
|
|
QSE_THROW_WITH_MSG(Error, this->getErrorMessage());
|
2009-12-19 06:34:42 +00:00
|
|
|
}
|
2009-05-29 08:12:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
int sed_main (int argc, qse_char_t* argv[])
|
|
|
|
{
|
2009-05-30 00:21:41 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
MySed sed;
|
2009-05-29 08:12:09 +00:00
|
|
|
|
2009-12-19 06:34:42 +00:00
|
|
|
sed.compile (QSE_T("y/ABC/abc/;s/abc/def/g"));
|
|
|
|
|
2009-12-19 21:06:28 +00:00
|
|
|
QSE::StdSed::StringStream stream (QSE_T("ABCDEFabcdef"));
|
2009-12-19 06:34:42 +00:00
|
|
|
sed.execute (stream);
|
|
|
|
|
|
|
|
xcout << QSE_T("INPUT: ") << stream.getInput() << std::endl;
|
|
|
|
xcout << QSE_T("OUTPUT: ") << stream.getOutput() << std::endl;
|
2009-05-30 00:21:41 +00:00
|
|
|
}
|
|
|
|
catch (MySed::Error& err)
|
2009-05-29 08:12:09 +00:00
|
|
|
{
|
2019-06-25 02:36:32 +00:00
|
|
|
xcout << QSE_T("ERROR: ") << QSE_EXCEPTION_MSG(err) << std::endl;
|
2009-05-29 08:12:09 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-12-19 06:34:42 +00:00
|
|
|
int qse_main (int argc, qse_achar_t* argv[])
|
2009-05-29 08:12:09 +00:00
|
|
|
{
|
2013-11-03 16:48:20 +00:00
|
|
|
int x;
|
2013-01-13 09:04:54 +00:00
|
|
|
init_sed_sample_locale ();
|
2018-09-13 03:16:23 +00:00
|
|
|
x = qse_run_main (argc, argv, sed_main);
|
2013-11-03 16:48:20 +00:00
|
|
|
return x;
|
2009-05-29 08:12:09 +00:00
|
|
|
}
|