00001 #ifndef _URLHANDLER_H_
00002 #define _URLHANDLER_H_
00003
00004
00005
00006 #include "Object.h"
00007 #include <string>
00008 #include <map>
00009 #include "BaseException.h"
00010 #include "net_types.h"
00011
00012 namespace FD {
00013
00014 typedef ObjectRef (*url_func)(const std::string &url, int flags);
00015
00016 class URLHandler {
00017
00018 private:
00019 static std::map<std::string,url_func> &url_table();
00020
00021 public:
00022 enum {
00023 URL_READ,
00024 URL_WRITE,
00025 URL_READWRITE
00026 };
00027
00028 static int RegisterURLHandler(const std::string& name, url_func func)
00029 {
00030 url_table()[name] = func;
00031 return 0;
00032 }
00033
00034 static ObjectRef openStream(const std::string &inputURL, int flags)
00035 {
00036
00037 try
00038 {
00039 int pos = inputURL.find(":");
00040
00041 if (pos == std::string::npos)
00042 {
00043
00044 return url_table()["file"](inputURL, flags);
00045 }
00046 else
00047 {
00048
00049
00050 if (url_table().find(inputURL.substr(0,pos)) != url_table().end())
00051 {
00052 return url_table()[inputURL.substr(0,pos)](inputURL, flags);
00053 }
00054 else
00055 {
00056 throw new GeneralException(std::string("Unable to create URL of type : ") + inputURL,__FILE__,__LINE__);
00057 }
00058 }
00059 }
00060 catch(BaseException *e)
00061 {
00062 throw e->add(new GeneralException(std::string("Unable to create URL of type : ") + inputURL,__FILE__,__LINE__));
00063 }
00064 }
00065 };
00066
00067
00068 #define REGISTER_URL_HANDLER(name, func) \
00069 int dummy_url_handler_for ## _ ## name = \
00070 URLHandler::RegisterURLHandler(# name,func);
00071
00072 }
00073
00074 #endif