Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members | Related Pages | Examples

vos/corelibs/typechain/handlers/type_wininet.cc

Go to the documentation of this file.
00001 
00002 /* URL handler for Windows */
00003 
00004 /*
00005 
00006     Copyright (C) 2001, 2002 Frank Richter <resqu@gmx.ch>
00007 
00008     This library is free software; you can redistribute it and/or
00009     modify it under the terms of the GNU Lesser General Public
00010     License as published by the Free Software Foundation; either
00011     version 2 of the License, or (at your option) any later version.
00012 
00013     This library is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016     Lesser General Public License for more details.
00017 
00018     You should have received a copy of the GNU Lesser General Public
00019     License along with this library; if not, write to the Free Software
00020     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00021 */
00022 
00023 
00024 #include "type_wininet.hh"
00025 
00026 #include <stdio.h>
00027 
00028 
00029 /* Called when loaded as a plugin */
00030 
00031 extern "C" void plugin_init(void *registerHandler){
00032 #ifdef HAVE_LIBWININET
00033    ( (void (*)(TypeHandler*)) registerHandler )(new TypeWinInet("url") );
00034    ( (void (*)(TypeHandler*)) registerHandler )(new TypeWinInet("URL") );
00035 #endif
00036 }
00037 
00038 
00039 /* Constructor */
00040 
00041 TypeWinInet::TypeWinInet(string name) : TypeHandler(name, "url") {
00042 #ifdef HAVE_LIBWININET
00043     internet = InternetOpen("URLTypeHandler", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
00044     if (!internet)
00045     {
00046        fprintf(stderr, "WinInet: InternetOpen() failed with %.8x\n", GetLastError());
00047     }
00048 #endif
00049 }
00050 
00051 TypeWinInet::~TypeWinInet() {
00052 #ifdef HAVE_LIBWININET
00053     if (internet) InternetCloseHandle(internet);
00054 #endif
00055 }
00056 
00057 #ifndef HAVE_LIBWININET
00058 
00059 void TypeWinInet::decode(string& data, TypeParams& params)
00060 {
00061 }
00062 
00063 #else
00064 
00065 void TypeWinInet::decode(string& data, TypeParams& params)
00066 {
00067     if (!internet) return;
00068 
00069     fprintf(stderr, "WinInet URL Handler: retrieving %s ...\n", data.c_str());
00070 
00071     HINTERNET conn =
00072      InternetOpenUrl(internet, data.c_str(), NULL, 0,
00073        INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_HYPERLINK,
00074        0);
00075     if (!conn)
00076     {
00077        DWORD error = GetLastError();
00078        if (error == ERROR_INTERNET_EXTENDED_ERROR)
00079        {
00080            char exterror[255];
00081            DWORD exterrorlen = sizeof(exterror);
00082            DWORD dummy;
00083            if (InternetGetLastResponseInfo(&dummy, &exterror[0], &exterrorlen))
00084            {
00085                throw TypeChain::DecodeError(exterror, "url");
00086            }
00087        }
00088        throw TypeChain::DecodeError("Something happened", "url");
00089     }
00090 
00091     char buffer[4096];
00092     DWORD bytesread;
00093 
00094     data = "";
00095     while (InternetReadFile(conn, &buffer[0], sizeof(buffer), &bytesread) && bytesread)
00096     {
00097        data += string(buffer, bytesread);
00098     }
00099 
00100     InternetCloseHandle(conn);
00101 
00102     fprintf(stderr, "URL Handler: done.\n");
00103 }
00104 #endif

Generated on Tue Aug 12 03:55:38 2003 for Interreality Project - VOS by doxygen 1.3.2