Annotation of libwww/Library/Examples/LoadToFile.c, revision 1.6

1.4       frystyk     1: /*
1.6     ! frystyk     2: **     @(#) $Id: LoadToFile.c,v 1.5 1999/02/23 17:53:30 frystyk Exp $
1.4       frystyk     3: **     
                      4: **     More libwww samples can be found at "http://www.w3.org/Library/Examples/"
                      5: **     
                      6: **     Copyright © 1995-1998 World Wide Web Consortium, (Massachusetts
                      7: **     Institute of Technology, Institut National de Recherche en
                      8: **     Informatique et en Automatique, Keio University). All Rights
                      9: **     Reserved. This program is distributed under the W3C's Software
                     10: **     Intellectual Property License. This program is distributed in the hope
                     11: **     that it will be useful, but WITHOUT ANY WARRANTY; without even the
                     12: **     implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
                     13: **     PURPOSE. See W3C License http://www.w3.org/Consortium/Legal/ for more
                     14: **     details.
                     15: **
                     16: **     Sample showing how to load a document and save it to local file
                     17: */
1.1       frystyk    18: 
                     19: #include "WWWLib.h"                          /* Global Library Include file */
                     20: #include "WWWMIME.h"                               /* MIME parser/generator */
                     21: #include "WWWNews.h"                                  /* News access module */
                     22: #include "WWWHTTP.h"                                  /* HTTP access module */
                     23: #include "WWWFTP.h"
                     24: #include "WWWFile.h"
                     25: #include "WWWGophe.h"
                     26: #include "WWWInit.h"
                     27: 
                     28: #define APP_NAME               "GETTOOL"
                     29: #define APP_VERSION            "1.0"
1.2       frystyk    30: #define DEFAULT_OUTPUT_FILE     "get.out"
                     31: 
1.5       frystyk    32: PRIVATE int printer (const char * fmt, va_list pArgs)
                     33: {
                     34:     return (vfprintf(stdout, fmt, pArgs));
                     35: }
                     36: 
                     37: PRIVATE int tracer (const char * fmt, va_list pArgs)
                     38: {
                     39:     return (vfprintf(stderr, fmt, pArgs));
                     40: }
                     41: 
1.2       frystyk    42: /*
                     43: **  We get called here from the event loop when we are done
                     44: **  loading. Here we terminate the program as we have nothing
                     45: **  better to do.
                     46: */
                     47: int terminate_handler (HTRequest * request, HTResponse * response,
                     48:                       void * param, int status)
                     49: {
                     50:     /* Delete our request again */
                     51:     HTRequest_delete(request);
                     52: 
                     53:     /* Delete our profile */
                     54:     HTProfile_delete();
                     55: 
                     56:     exit(status ? status : 0);
                     57: }
1.1       frystyk    58: 
                     59: int main (int argc, char ** argv)
                     60: {
                     61:     int                        arg = 0;
1.2       frystyk    62:     char *              outputfile = NULL;
                     63:     char *              getme = NULL;
                     64:     HTRequest *         request = NULL;
1.1       frystyk    65: 
                     66:     /* Initiate W3C Reference Library with a client profile */
                     67:     HTProfile_newNoCacheClient(APP_NAME, APP_VERSION);
1.2       frystyk    68: 
1.6     ! frystyk    69:     /* And the traces... */
        !            70: #if 0
        !            71:     HTSetTraceMessageMask("sop");
        !            72: #endif
        !            73: 
1.5       frystyk    74:     /* Need our own trace and print functions */
                     75:     HTPrint_setCallback(printer);
                     76:     HTTrace_setCallback(tracer);
                     77: 
1.2       frystyk    78:     /* Add our own filter to terminate the application */
                     79:     HTNet_addAfter(terminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST);
                     80: 
1.4       frystyk    81:     /* Set the timeout for long we are going to wait for a response */
                     82:     HTHost_setEventTimeout(10000);
                     83: 
1.1       frystyk    84:     /* Scan command line for parameters */
                     85:     for (arg=1; arg<argc; arg++) {
                     86:         if (!strcmp(argv[arg], "-o")) { 
                     87:             outputfile = (arg+1 < argc && *argv[arg+1] != '-') ?
                     88:                 argv[++arg] : DEFAULT_OUTPUT_FILE;
                     89:             
                     90:         } else {
                     91:             getme = argv[arg];
                     92:         }
                     93:     }
1.2       frystyk    94: 
                     95:     /* Make sure we have an output */
                     96:     if (!outputfile) outputfile = DEFAULT_OUTPUT_FILE;
                     97: 
                     98:     if (getme && *getme) {
                     99:         request = HTRequest_new();
                    100: 
                    101:         /* Start the load */
1.6     ! frystyk   102:         if (HTLoadToFile(getme, request, outputfile) != YES) {
        !           103:            HTPrint("Can't open output file\n");
        !           104:            HTProfile_delete();
        !           105:            return 0;
        !           106:        }
1.2       frystyk   107: 
                    108:         /* Go into the event loop... */
                    109:         HTEventList_loop(request);
                    110: 
                    111:     } else {
1.5       frystyk   112:        HTPrint("Type the URI of document you want to load and the name of the local file.\n");
                    113:        HTPrint("\t%s <address> -o <localfile>\n", argv[0]);
                    114:        HTPrint("For example, %s http://www.w3.org -o w3chome.html\n", argv[0]);
1.2       frystyk   115: 
                    116:         /* Delete our profile if no load */
                    117:         HTProfile_delete();
1.1       frystyk   118:     }
                    119: 
                    120:     return 0;
                    121: }

Webmaster