| iMatix home page
| << | < | > | >>
SFL Logo SFL
Version 1.91

 

file_slurp

#include "sflfile.h"
DESCR *
file_slurp (
    const char *filename)

Synopsis

Reads an entire file, and returns a DESCR containing the file data. The file is read as binary data. The returned DESCR should be freed using the mem_free() call. if the file is > 65000, only the first 65000 bytes are read into memory. This is to stop really silly things from happening. Returns NULL if the file cannot be found. Appends a null byte to the data in any case.

Source Code - (sflfile.c)

{
    DESCR
        *buffer;
    long
        file_size;
    int
        rc;
    FILE
        *file_stream;

    ASSERT (filename);

    file_size = get file size (filename);
    if (file_size == -1)
        return (NULL);
    else
    if (file_size > 65000L)
        file_size = 65000L;

    buffer = mem_descr (NULL, (word) file_size + 1);
    if (buffer == NULL)
        return (NULL);

    file_stream = fopen (filename, FOPEN_READ_BINARY);
    if (file_stream == NULL)
      {
        mem_free (buffer);
        return (NULL);
      }
    rc = fread (buffer-> data, (word) file_size, 1, file_stream);
    fclose (file_stream);
    if (rc != 1)
      {
        mem_free (buffer);
        return (NULL);
      }
    buffer-> data [(word) file_size] = '\0';
    return (buffer);
}

| << | < | > | >> iMatix Copyright © 1996-98 iMatix