FileStreamFactory.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // FileStreamFactory.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/FileStreamFactory.cpp#1 $
  5. //
  6. // Library: Foundation
  7. // Package: URI
  8. // Module: FileStreamFactory
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  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. namespace Poco {
  22. FileStreamFactory::FileStreamFactory()
  23. {
  24. }
  25. FileStreamFactory::~FileStreamFactory()
  26. {
  27. }
  28. std::istream* FileStreamFactory::open(const URI& uri)
  29. {
  30. poco_assert (uri.isRelative() || uri.getScheme() == "file");
  31. std::string uriPath = uri.getPath();
  32. if (uriPath.substr(0, 2) == "./")
  33. uriPath.erase(0, 2);
  34. Path p(uriPath, Path::PATH_UNIX);
  35. p.setNode(uri.getHost());
  36. return open(p);
  37. }
  38. std::istream* FileStreamFactory::open(const Path& path)
  39. {
  40. File file(path);
  41. if (!file.exists()) throw FileNotFoundException(path.toString());
  42. FileInputStream* istr = new FileInputStream(path.toString(), std::ios::binary);
  43. if (!istr->good())
  44. {
  45. delete istr;
  46. throw OpenFileException(path.toString());
  47. }
  48. return istr;
  49. }
  50. } // namespace Poco