Logo Search packages:      
Sourcecode: rapple version File versions  Download package

rpl_str_t rpl_fs_f2str ( rpl_c_str_t  filename  ) 

Returns the contents of file filename as a string.

Parameters:
filename is the relative (to the cwd) name of the file.
Returns:
the contents of file filename as a string.

Definition at line 49 of file fsutil.c.

References rpl_log_fatal(), rpl_me_malloc(), and rpl_message_get().

Referenced by rpl_mod_link_cleanup(), rpl_mod_link_process(), and rpl_mod_parse_process().

                                   {
    rpl_str_t f_cnts, f_buf;
    struct stat f_stat;
    size_t f_size;
    FILE *fp;

    assert(filename != NULL);

    /* open the file (and acquire a descriptor for it) */
    /* binary "b" is req'd for ANSI C portability but has no effect on POSIX platforms */
    errno = 0;
    if((fp=fopen(filename, "rb")) == NULL)
        rpl_log_fatal(rpl_message_get("FS_OPEN_FAILED", filename, " (", strerror(errno), ")", RPL_EOM));

    /* determine file size */
    errno = 0;
    if(stat(filename, &f_stat) == -1)
        rpl_log_fatal(rpl_message_get("FS_LSTAT_FAILED", strerror(errno), RPL_EOM));

    f_size = f_stat.st_size;
    /* assign sufficient memory to store file contents */
    f_cnts = (rpl_str_t)rpl_me_malloc(f_size + 1);
    /* buffer needs to be sufficiently large in case entire file is a single string */
    f_buf = (rpl_str_t)rpl_me_malloc(f_size + 1);

    /* read file contents into string (fgets retains \n on each line and appends \0 at the end) */
    (void)fgets(f_cnts,f_size,fp);
    while(fgets(f_buf,f_size,fp) != NULL)
        strcat(f_cnts,f_buf);

    /* tidy up */
    fclose(fp);
    /* free(f_buf); */

    return f_cnts;
}


Generated by  Doxygen 1.6.0   Back to index