1//
2// FileStreamFactory.cpp
3//
4// Library: Foundation
5// Package: URI
6// Module: FileStreamFactory
7//
8// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
9// and Contributors.
10//
11// SPDX-License-Identifier: BSL-1.0
12//
13
14
15#include "Poco/FileStreamFactory.h"
16#include "Poco/URI.h"
17#include "Poco/Path.h"
18#include "Poco/File.h"
19#include "Poco/Exception.h"
20#include "Poco/FileStream.h"
21
22
23namespace Poco {
24
25
26FileStreamFactory::FileStreamFactory()
27{
28}
29
30
31FileStreamFactory::~FileStreamFactory()
32{
33}
34
35
36std::istream* FileStreamFactory::open(const URI& uri)
37{
38 poco_assert (uri.isRelative() || uri.getScheme() == "file");
39
40 std::string uriPath = uri.getPath();
41 if (uriPath.substr(0, 2) == "./")
42 uriPath.erase(0, 2);
43 Path p(uriPath, Path::PATH_UNIX);
44 p.setNode(uri.getHost());
45 return open(p);
46}
47
48
49std::istream* FileStreamFactory::open(const Path& path)
50{
51 File file(path);
52 if (!file.exists()) throw FileNotFoundException(path.toString());
53
54 FileInputStream* istr = new FileInputStream(path.toString(), std::ios::binary);
55 if (!istr->good())
56 {
57 delete istr;
58 throw OpenFileException(path.toString());
59 }
60 return istr;
61}
62
63
64} // namespace Poco
65