Skip to content

Commit

Permalink
Fixes #29 (check for invalid stream in CliFileSession)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniele77 committed Feb 16, 2019
1 parent 55b7885 commit 8fd6b23
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Current Version: 1.0-alpha
- Fix Clang 3.6 - 7.0 undefined reference bug
- Fix infinite loop on EoF
- Fix issue #27 (odd history behaviour): the history now works like in bash
- Fix issue #28 (check for invalid stream in CliFileSession class)
- Add doxygen basic configuration
- Add session recordings in README file
- Add CHANGELOG.md, CODE_OF_CONDUCT.md, CONTRIBUTING.md files
8 changes: 7 additions & 1 deletion cli/clifilesession.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include <string>
#include <iostream>
#include <stdexcept> // std::invalid_argument
#include "cli.h" // CliSession

namespace cli
Expand All @@ -40,12 +41,15 @@ namespace cli
class CliFileSession : public CliSession
{
public:
/// @throw std::invalid_argument if @c _in or @c out are invalid streams
CliFileSession(Cli& cli, std::istream& _in=std::cin, std::ostream& out=std::cout) :
CliSession(cli, out, 1),
exit(false),
in(_in)
{
ExitAction(
if (!in.good()) throw std::invalid_argument("istream invalid");
if (!out.good()) throw std::invalid_argument("ostream invalid");
ExitAction(
[this](std::ostream&)
{
exit = true;
Expand All @@ -58,6 +62,8 @@ class CliFileSession : public CliSession
{
Prompt();
std::string line;
if (!in.good())
Exit();
std::getline(in, line);
if (in.eof())
Exit();
Expand Down
2 changes: 1 addition & 1 deletion samples/complete_vsprj/complete.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<VCProjectVersion>15.0</VCProjectVersion>
<ProjectGuid>{4CEAC87B-D068-4A56-B0E9-5B85EE17EB70}</ProjectGuid>
<RootNamespace>complete</RootNamespace>
<WindowsTargetPlatformVersion>10.0.16299.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion>10.0.17763.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
Expand Down
12 changes: 11 additions & 1 deletion samples/filesession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,18 @@ int main()
cli.ExitAction( [](auto& out){ out << "Goodbye and thanks for all the fish.\n"; } );

std::ifstream infile("input.txt");
if (!infile)
{
std::cerr << "File input.txt not found in current directory!\n";
return 1;
}
std::ofstream outfile("output.txt");
CliFileSession input(cli, infile, outfile);
if (!outfile)
{
std::cerr << "Can't write file output.txt in the current directory!\n";
return 1;
}
CliFileSession input(cli, infile, outfile);
input.Start();

return 0;
Expand Down
2 changes: 1 addition & 1 deletion samples/filesession_vsprj/filesession.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<VCProjectVersion>15.0</VCProjectVersion>
<ProjectGuid>{35EAF199-0396-4370-9939-09B9D2714969}</ProjectGuid>
<RootNamespace>filesession</RootNamespace>
<WindowsTargetPlatformVersion>10.0.16299.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion>10.0.17763.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<VCProjectVersion>15.0</VCProjectVersion>
<ProjectGuid>{F3ADB3BD-FE8D-4A00-A7EC-543BA3DE3D5E}</ProjectGuid>
<RootNamespace>simplelocalsession</RootNamespace>
<WindowsTargetPlatformVersion>10.0.16299.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion>10.0.17763.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
Expand Down

0 comments on commit 8fd6b23

Please sign in to comment.