1 /*
2 	Copyright (C) 2005-2007 Feeling Software Inc.
3 	Portions of the code are:
4 	Copyright (C) 2005-2007 Sony Computer Entertainment America
5 
6 	MIT License: http://www.opensource.org/licenses/mit-license.php
7 */
8 /*
9 	Based on the FS Import classes:
10 	Copyright (C) 2005-2006 Feeling Software Inc
11 	Copyright (C) 2005-2006 Autodesk Media Entertainment
12 	MIT License: http://www.opensource.org/licenses/mit-license.php
13 */
14 
15 #ifndef _FU_URI_H_
16 #define _FU_URI_H_
17 
18 /**
19 	A simple URI structure.
20 
21 	This structure is quite incomplete but covers all the necessary cases for now.
22 	Possible upgrades to support all five parts:
23 	SCHEME://HOSTNAME/FILENAME@@ARGUMENTS@#DAE_ID
24 
25 	[scheme:][schemeDelimiter][userInfo@][host][:port][/path][?query][#fragment]
26 
27 	Right now, SCHEME must always be "file://".
28 	HOSTNAME, on Windows, can be a UNC computer name. No other
29 	hostname types are supported. ARGUMENTS are not supported.
30 
31 	@ingroup FUtils
32 */
33 class FCOLLADA_EXPORT FUUri
34 {
35 public:
36 	enum Scheme
37 	{
38 		NONE,
39 		FILE,
40 		FTP,
41 		HTTP,
42 		HTTPS
43 	};
44 
45 private:
46 	/** The URI scheme */
47 	Scheme scheme;
48 	/** The URI scheme specific part */
49 	fstring schemeDelimiter;
50 	/** The URI user used to connect to the host */
51 	fstring username;
52 	/** The URI password used to connect to the host */
53 	fstring password;
54 	/** The URI the host */
55 	fstring hostname;
56 	/** The URI port used to connect to the host */
57 	uint32 port;
58 	/** The URI path represent the name of the filename. */
59 	fstring path;
60 	/** The URI query */
61 	fstring query;
62 	/** The URI fragment represent the COLLADA id of the element targeted. */
63 	fstring fragment;
64 
65 	static bool IsAlpha(fchar fc);
66 	static bool IsDigit(fchar fc);
67 	static bool IsAlphaNumeric(fchar fc);
68 	static bool IsMark(fchar fc);
69 	static bool IsHex(fchar fc);
70 	static bool IsReserved(fchar fc);
71 
72 	/** For a relative path, extract the list of the individual paths that must be traversed to get to the file.
73 		@param filename A file path.
74 		@param list The returned list of paths to traverse.
75 		@param includeFilename Whether the filename should be pushed at the back of the returned list. */
76 	void ExtractPathStack(const fstring& name, FStringList& list, bool includeFilename = false) const;
77 public:
78 	/** Constructor. */
79 	FUUri();
80 
81 	/** Constructor.
82 		@param uri The string value for the URI.
83 		@param escape Whether to escape the strings.*/
84 	FUUri(const fstring& uri, bool escape = false);
85 
86 	/** Constructor.
87 		@param scheme The scheme to use in the construction of the URI.
88 		@param username The username to use in the construction of the URI.
89 		@param passwd The password to use in the construction of the URI.
90 		@param host The host to use in the construction of the URI.
91 		@param port The port to use in the construction of the URI.
92 		@param path The path to use in the construction of the URI.
93 		@param query The query to use in the construction of the URI.
94 		@param fragment The fragment to use in the construction of the URI. */
95 	FUUri(Scheme scheme, const fstring& username, const fstring& passwd, const fstring& host, uint32 port, const fstring& path = FC(""), const fstring& query = FC(""), const fstring& fragment = FC(""));
96 
97 	/** Constructor.
98 		@param scheme The scheme to use in the construction of the URI.
99 		@param host The host to use in the construction of the URI.
100 		@param path The path to use in the construction of the URI.
101 		@param fragment The fragment to use in the construction of the URI. */
102 	FUUri(Scheme scheme, const fstring& host, const fstring& path = FC(""), const fstring& fragment = FC(""));
103 
104 	/** Constructor.
105 		@param path The path to use in the construction of the URI.
106 		@param fragment The fragment to use in the construction of the URI. */
107 	FUUri(const fstring& path, const fstring& fragment);
108 
109 	/** Retrieves the scheme from the URI.
110 		@return The URI scheme. */
GetScheme()111 	inline Scheme GetScheme() const { return scheme; }
112 
113 	/** Retrieves the scheme delimiter from the URI.
114 		@return The URI scheme delimiter. */
GetSchemeDelimiter()115 	inline const fstring& GetSchemeDelimiter() const { return schemeDelimiter; }
116 
117 	/** Retrieves the user information from the URI.
118 		@return The URI user information. */
119 	fstring GetUserInformations() const;
120 
121 	/** Retrieves the host information from the URI.
122 		@return The URI host. */
GetHostname()123 	inline const fstring& GetHostname() const { return hostname; }
124 
125 	/** Retrieves the port number from the URI.
126 		@return The URI port number. */
GetPort()127 	inline uint32 GetPort() const { return port; }
128 
129 	/** Sets the port number of the URI.
130 		@param _port A valid port number. */
SetPort(uint32 _port)131 	inline void SetPort(uint32 _port) { port = _port; }
132 
133 	/** Retrieves the path from the URI.
134 		@return The URI path. */
GetPath()135 	inline const fstring& GetPath() const { return path; }
136 
137 	/** Retrieves the query from the URI.
138 		@return The URI query. */
GetQuery()139 	inline const fstring& GetQuery() const { return query; }
140 
141 	/** Sets the query of the URI.
142 		@param _query A URI fragment. */
SetQuery(const fstring & _query)143 	inline void SetQuery(const fstring& _query) { query = _query; }
144 
145 	/** Retrieves the fragment from the URI.
146 		@return The URI query. */
GetFragment()147 	inline const fstring& GetFragment() const { return fragment; }
148 
149 	/** Sets the fragment of the URI.
150 		@param _fragment A URI fragment. */
SetFragment(const fstring & _fragment)151 	inline void SetFragment(const fstring& _fragment) { fragment = _fragment; }
152 
153 	/** Retrieves the authority string from the URI. ("[userInfo@]host[:port]")
154 		@return The URI authority string. */
155 	fstring GetAuthority() const;
156 
157 	/** Retrieves an absolute path from the URI.
158 		@return The URI absolute path. */
159 	fstring GetAbsolutePath() const;
160 
161 	/** Retrieves an absolute URI string from the URI.
162 		@param fragment Whether to return a string with the fragment
163 		@return The URI string. */
164 	fstring GetAbsoluteUri(bool fragment = true) const;
165 
166 	/** Retrieves an relative URI string from the URI.
167 		@return The URI string. */
168 	fstring GetRelativeUri(const FUUri& uri) const;
169 
170 	/** Makes a relative path from a uri
171 		@param	uri	The uri representing the path.
172 		@return The relative path. */
173 	fstring MakeRelative(const fstring& path) const;
174 
175 	/** Makes an absolute path from a relative path and this URI
176 		@param	relpath	The relative path
177 		@return The absolute path. */
178 	fstring MakeAbsolute(const fstring& relativePath) const;
179 
180 	/** Makes the passed in URI relative to this URI
181 		@param uri The relative or absolute URI */
182 	void MakeAbsolute(FUUri& uri) const;
183 
184 	/** Resolves a URI from a relative path against this URI
185 		@param	relpath	The relative path.
186 		@return The newly contructed URI. */
187 	FUUri Resolve(const fstring& relativePath) const;
188 
189 	/** Retrieves whether this URI points to a file.
190 		@return Whether this URI points to a file. */
191 	bool IsFile() const;
192 
193 	/** Escapes a path
194 		@param path A path.
195 		@return The escaped path. */
196 	static fstring Escape(const fstring& path);
197 };
198 
199 typedef fm::vector<FUUri> FUUriList; /**< A dynamically-sized array of URIs. */
200 
201 #endif // _FU_URI_H_
202 
203