# load-url.c

Go to the documentation of this file.

    00001
    00006 /* $Progeny$
    00007  *
    00008  * Copyright 2003 Hewlett-Packard Company
    00009  *
    00010  * Permission is hereby granted, free of charge, to any person obtaining a
    00011  * copy of this software and associated documentation files (the "Software"),
    00012  * to deal in the Software without restriction, including without limitation
    00013  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
    00014  * and/or sell copies of the Software, and to permit persons to whom the
    00015  * Software is furnished to do so, subject to the following conditions:
    00016  *
    00017  * The above copyright notice and this permission notice shall be included in
    00018  * all copies or substantial portions of the Software.
    00019  *
    00020  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    00021  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    00022  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
    00023  * THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    00024  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    00025  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    00026  * DEALINGS IN THE SOFTWARE.
    00027  */
    00028
    00029 #include "config.h"
    00030
    00031 #ifndef HAVE_LIBCURL
    00032
    00033 #include <assert.h>
    00034 #include <expat.h>
    00035
    00036 #include <discover/load-url.h>
    00037
    00038 #ifdef HAVE_ZLIB
    00039 #include <zlib.h>
    00040 #define stdio_file gzFile
    00041 #define fopen gzopen
    00042 #define STDIO_READ fread
    00043 #define fclose gzclose
    00044 #else
    00045 #include <stdio.h>
    00046 typedef FILE *stdio_file;
    00047 #define STDIO_READ(f, buf, len) fread(buf, 1, len, f)
    00048 #endif
    00049
    00050 #include <string.h>
    00051
    00052 int
    00053 _discover_load_url(const char *url, XML_Parser parser)
    00054 {
    00055     const char *file_path;
    00056     stdio_file url_file;
    00057     char buf[4096];
    00058     char *tmp;
    00059     size_t len;
    00060     int parse_result;
    00061
    00062     assert(url != NULL);
    00063
    00064     if (strncmp(url, "file:", 5))
    00065         return 0;
    00066
    00067     /* We have three accepted formats for the url: file://host/path,
    00068      * file:///path, and file://path.  The third is not actually
    00069      * legal, but curl allows it to refer to a file in the current
    00070      * directory.  Since we use that format in the test suite, we need
    00071      * to support it here.
    00072      *
    00073      * For file://host/path and file:///path, the strchr call takes us
    00074      * directly to the correct absolute path.  For file://path, url +
    00075      * 7 is just "path" so we use that relative path. */
    00076     file_path = strchr(url + 7, '/');
    00077     if (!file_path) {
    00078         file_path = url + 7;
    00079     }
    00080
    00081     if (*file_path == '\0')
    00082         return 0;
    00083
    00084     url_file = fopen(file_path, "r");
    00085     if (url_file == NULL)
    00086         return 0;
    00087
    00088     do
    00089     {
    00090         len = STDIO_READ(url_file, buf, sizeof(buf));
    00091         if (len > 0)
    00092             parse_result = XML_Parse(parser, buf, len, 0);
    00093     } while ((len > 0) && (parse_result != 0));
    00094
    00095     fclose(url_file);
    00096
    00097     return (parse_result != 0);
    00098 }
    00099
    00100 #endif