valijson/thirdparty/urdl-2013-08-15/example/get1.cpp
Tristan Penman 4c9864de73 Initial commit.
This commit contains the third major design of a C++ library for JSON Schema validation.

It is definitely not what I would consider production-ready, but I do think that the overall design of the library is robust.
2013-10-30 07:51:11 +11:00

58 lines
1.2 KiB
C++

//
// get1.cpp
// ~~~~~~~~
//
// Copyright (c) 2009-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include <urdl/istream.hpp>
#include <boost/progress.hpp>
#include <iostream>
#include <fstream>
int main(int argc, char* argv[])
{
try
{
if (argc != 3)
{
std::cerr << "Usage: get1 <url> <outputfile>\n";
return 1;
}
urdl::istream is(argv[1]);
if (!is)
{
std::cout << is.error().message() << std::endl;
return 1;
}
std::ofstream os(argv[2], std::ios_base::out | std::ios_base::binary);
if (is.content_length() != std::numeric_limits<std::size_t>::max())
{
boost::progress_display progress(is.content_length());
while (is && os)
{
char buffer[1024] = "";
is.read(buffer, sizeof(buffer));
os.write(buffer, is.gcount());
progress += is.gcount();
}
std::cout << std::endl;
}
else
{
os << is.rdbuf();
}
std::cout << is.error().message() << std::endl;
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << std::endl;
}
}