lean4-htt/src/api/ios.cpp
2015-08-23 08:19:25 -07:00

88 lines
2.7 KiB
C++

/*
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
*/
#include "frontends/lean/pp.h"
#include "api/string.h"
#include "api/exception.h"
#include "api/ios.h"
using namespace lean; // NOLINT
lean_bool lean_ios_mk_std(lean_options o, lean_ios * r, lean_exception * ex) {
LEAN_TRY;
check_nonnull(o);
io_state ios(mk_pretty_formatter_factory(), to_options_ref(o));
*r = of_io_state(new io_state(ios));
LEAN_CATCH;
}
lean_bool lean_ios_mk_buffered(lean_options o, lean_ios * r, lean_exception * ex) {
LEAN_TRY;
check_nonnull(o);
io_state ios(mk_pretty_formatter_factory(), to_options_ref(o));
ios.set_regular_channel(std::make_shared<string_output_channel>());
ios.set_diagnostic_channel(std::make_shared<string_output_channel>());
*r = of_io_state(new io_state(ios));
LEAN_CATCH;
}
void lean_ios_del(lean_ios ios) {
delete to_io_state(ios);
}
lean_bool lean_ios_is_std(lean_ios ios) {
if (!ios)
return lean_false;
return dynamic_cast<string_output_channel*>(&to_io_state_ref(ios).get_regular_channel()) != nullptr;
}
lean_bool lean_ios_set_options(lean_ios ios, lean_options o, lean_exception * ex) {
LEAN_TRY;
check_nonnull(ios);
check_nonnull(o);
to_io_state(ios)->set_options(to_options_ref(o));
LEAN_CATCH;
}
lean_bool lean_ios_get_options(lean_ios ios, lean_options * r, lean_exception * ex) {
LEAN_TRY;
check_nonnull(ios);
*r = of_options(new options(to_io_state_ref(ios).get_options()));
LEAN_CATCH;
}
static void check_nonstd(lean_ios ios) {
check_nonnull(ios);
if (lean_ios_is_std(ios))
throw exception("invalid argument, buffered IO state expected");
}
lean_bool lean_ios_get_regular(lean_ios ios, char const ** r, lean_exception * ex) {
LEAN_TRY;
check_nonstd(ios);
*r = mk_string(static_cast<string_output_channel&>(to_io_state_ref(ios).get_regular_channel()).str());
LEAN_CATCH;
}
lean_bool lean_ios_get_diagnostic(lean_ios ios, char const ** r, lean_exception * ex) {
LEAN_TRY;
check_nonstd(ios);
*r = mk_string(static_cast<string_output_channel&>(to_io_state_ref(ios).get_diagnostic_channel()).str());
LEAN_CATCH;
}
lean_bool lean_ios_reset_regular(lean_ios ios, lean_exception * ex) {
LEAN_TRY;
check_nonstd(ios);
to_io_state(ios)->set_regular_channel(std::make_shared<string_output_channel>());
LEAN_CATCH;
}
lean_bool lean_ios_reset_diagnostic(lean_ios ios, lean_exception * ex) {
LEAN_TRY;
check_nonstd(ios);
to_io_state(ios)->set_diagnostic_channel(std::make_shared<string_output_channel>());
LEAN_CATCH;
}