MICO Platform
 All Classes Namespaces Functions Variables Friends
URLStream.hpp
1 #ifndef HAVE_URL_STREAM_H
2 #define HAVE_URL_STREAM_H 1
3 
4 #include <istream>
5 #include <ostream>
6 #include <streambuf>
7 
8 namespace mico {
9  namespace io {
10 
11  /*
12  * Internal data types and functions.
13  */
14  namespace {
15  enum URLType {
16  URL_TYPE_UNKNOWN = 0,
17  URL_TYPE_FILE = 1,
18  URL_TYPE_HTTP = 2,
19  URL_TYPE_FTP = 3,
20  URL_TYPE_HDFS = 4
21  };
22 
23  /*
24  * Parsed URL components
25  *
26  * @param port The port number. 0 means not specified.
27  */
28  struct url_components {
29  std::string scheme;
30  std::string host;
31  uint16_t port;
32  std::string path;
33  std::string username;
34  std::string password;
35  };
36 
37  /*
38  * Takes an URL or absolute path (with UNIX style delimiters) to parse it. It does no validation of the
39  * URL/path, therefore the result of malformed (or maybe even unusual) input can lead to incorrect results.
40  *
41  * @param url The URL string to parse
42  * @return The identified URL components. If everything is empty (and port == 0) the URL could not be parsed.
43  */
44  static url_components getURLComponents(const char* url);
45 
46  /*
47  * Returns the URL type.
48  *
49  * @param url The parsed URL.
50  * @return URL type.
51  */
52  static URLType getType(url_components url);
53 
54  /*
55  * Removes double occurrences of slashes from string
56  *
57  * @param path The path to fix
58  * @return Fixed path
59  */
60  static std::string fixPath(const std::string& path);
61  }
62 
67  class url_ostream : public std::ostream {
68  public:
69  url_ostream(const char* url);
70  url_ostream(std::string url) : url_ostream(url.c_str()) {};
71  ~url_ostream();
72 
73  private:
74  static std::streambuf* getStreamHandler(const char* url);
75  };
76 
77 
82  class url_istream : public std::istream {
83  public:
84  url_istream(const char* url);
85  url_istream(std::string url) : url_istream(url.c_str()) {};
86  ~url_istream();
87 
88  private:
89  static std::streambuf* getStreamHandler(const char* url);
90  };
91 
96  int remove(const char* url);
97  int remove(std::string url);
98  }
99 }
100 
101 #endif
102 
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in c...
Definition: http_client.cpp:23
Main type for opening an output stream to an URL for writing.
Definition: URLStream.hpp:67
Main type for opening an input stream to an URL for reading.
Definition: URLStream.hpp:82