src/utils/net.cpp

00001 /* 
00002  * wsdlpull- A C++ parser  for WSDL  (Web services description language)
00003  * Copyright (C) 2003 Vivek Krishna
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Library General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Library General Public
00016  * License along with this library; if not, write to the Free
00017  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018  *
00019  *
00020  */
00021 #include <utils/utils.h>
00022 #include <fstream>
00023 #ifdef WITH_CURL
00024 #include <curl/curl.h>
00025 #endif
00026 #include <stdio.h>
00027 
00028 //All network related util functions
00029 
00030 /*
00031  * Fetch a document at the given URI  and store it in a file
00032  */
00033 bool
00034 fetchUri(string uri,string& filename)
00035 {
00036   if(uri.find("http://")!=string::npos || 
00037      uri.find("ftp://") !=string::npos)
00038     {  
00039 #ifdef WITH_CURL
00040       CURL * curl;
00041       CURLcode res;
00042       curl=curl_easy_init();
00043       FILE * file;
00044       filename=uri.substr(uri.rfind('/')+1);
00045       if(curl){
00046         file=fopen(filename.c_str(),"w");
00047 
00048         curl_easy_setopt(curl, CURLOPT_URL,uri.c_str());
00049 
00050         curl_easy_setopt(curl,CURLOPT_FILE,(void*)file);
00051 
00052         res = curl_easy_perform(curl);
00053 
00054         curl_easy_cleanup(curl);
00055         fclose(file);
00056         if(res)
00057           return false;
00058         else 
00059           return true;
00060       }
00061 #endif
00062       return false;
00063     }else 
00064       { 
00065         /*
00066          * assume its a file on the disk
00067          */
00068         filename=uri;
00069         ifstream ifs;
00070         ifs.open(filename.c_str(),ios::in);
00071         if(ifs.fail()) {
00072           ifs.close();
00073           return false;
00074         }
00075         else {
00076           ifs.close();
00077           return true;
00078         }
00079 
00080       }
00081 }
00082 
00083 
00084 

Generated on Sat May 3 16:29:00 2008 for wsdlpull by  doxygen 1.4.6