Module file

Extended file handling.

Mostly wrappers around familar functions; file_gets is a fgets that strips the line feed; The other functions return a refcounted string, or array of strings (like with file_getlines)

FILE *f = fopen(file,"r");
printf("size was %d bytes\n",file_size(file));
char **lines = file_getlines(f);
printf("no of lines %d \n",array_len(lines));
fclose(f);

There are functions that work with parts of filenames which don’t have the limitations and gotchas of the libc equivalents, like file_basename.

Finally, file_fopen provides a file wrapper FILE**. It returns an actual error string if it fails, and disposing this file object will close the underlying stream.

See test-file.c

Functions

file_exists (path, rw) does the path exist and is accessible?
file_gets (f, buff, bufsize) like fgets, except trims (\r)\n.
file_getline (f) like file_gets but returns a refcounted string.
file_size_stream (fp) size of an opened file sream.
file_size (file) size of a file by name.
file_read_all (file, text) read the contents of a file.
file_getlines (f) all the files from a file.
file_fopen (file, how) Open a file, wrapping FILE* in an llib object.
file_command (cmd) output of a command as text.
file_command_lines (cmd) output of a command as lines.
file_files_in_dir (mask, recursive) all the files matching a mask.

Operations on File paths

file_basename (path) file part of a path.
file_dirname (path) file part of a path.
file_extension (path) extension of a path.
file_replace_extension (path, ext) replace existing extension of path.
file_expand_user (path) expand initial tilde into user’s home directory.

file_fmt Functions

file_popen_fmt (fmt, how, ...) popen using varargs.
file_command_fmt (fmt, ...) output of a command as text, varags version.
file_write_fmt (file, fmt, ...) create a file with given format and args.


Functions

file_exists (path, rw)
does the path exist and is accessible? rw is a string containing any of ‘r’, ‘w’ and ‘x’

Parameters:

  • path const char *
  • rw const char *

Returns:

    bool
file_gets (f, buff, bufsize)
like fgets, except trims (\r)\n.

Parameters:

  • f FILE *
  • buff char *
  • bufsize int

Returns:

    char *
file_getline (f)
like file_gets but returns a refcounted string.

Parameters:

  • f FILE *

Returns:

    char *
file_size_stream (fp)
size of an opened file sream.

Parameters:

  • fp FILE *

Returns:

    long
file_size (file)
size of a file by name. Will return -1 if the file cannot be opened for reading.

Parameters:

  • file const char *

Returns:

    long
file_read_all (file, text)
read the contents of a file. If text is true, will also strip any \r\n at the end.

Parameters:

  • file const char *
  • text bool

Returns:

    char *
file_getlines (f)
all the files from a file.

Parameters:

  • f FILE *

Returns:

    char * *
file_fopen (file, how)
Open a file, wrapping FILE* in an llib object. On error, will return an error value, not NULL!

Parameters:

  • file char* the file path
  • how char* same as fopen

Returns:

    FILE** a pointer to the stream
file_command (cmd)
output of a command as text. Will return “” if the command does not return anything Only first line! Use file_command_lines for the rest!

Parameters:

  • cmd const char *

Returns:

    char *
file_command_lines (cmd)
output of a command as lines. Will capture stderr as well.

Parameters:

  • cmd const char *

Returns:

    char * *
file_files_in_dir (mask, recursive)
all the files matching a mask.

Parameters:

  • mask const char *
  • recursive int

Returns:

    char * *

Operations on File paths

Unlike the POSIX functions basename and dirname, these functions are guaranteed not to touch the passed string, and will always return a refcounted string.
file_basename (path)
file part of a path. E.g. ‘/my/path/bonzo.dog’ => ‘bonzo.dog’

Parameters:

  • path const char *

Returns:

    char *
file_dirname (path)
file part of a path. E.g. ‘/my/path/bonzo.dog’ => ‘/my/path’

Parameters:

  • path const char *

Returns:

    char *
file_extension (path)
extension of a path. Note: this will ignore any periods in the path itself.

Parameters:

  • path const char *

Returns:

    char *
file_replace_extension (path, ext)
replace existing extension of path. ext may be the empty string, and path may not have a extension.

Parameters:

  • path const char *
  • ext const char *

Returns:

    char *
file_expand_user (path)
expand initial tilde into user’s home directory. Always returns a ref-counted string, even if path doesn’t begin with ‘~’

Parameters:

  • path const char *

Returns:

    char *

file_fmt Functions

file_popen_fmt (fmt, how, ...)
popen using varargs.

Parameters:

  • fmt const char *
  • how const char *
  • ...

Returns:

    FILE *
file_command_fmt (fmt, ...)
output of a command as text, varags version. Will return “” if the command does not return anything Only first line! Use file_command_lines for the rest!

Parameters:

  • fmt const char *
  • ...

Returns:

    char *
file_write_fmt (file, fmt, ...)
create a file with given format and args. Replaces the old fopen/fprintf/fclose combination.

Parameters:

  • file const char *
  • fmt const char *
  • ...

Returns:

    bool

Usage:

    flle_write_fmt("out.txt","%s %d\n",name,age);
generated by LDoc 1.4.3 Last updated 2015-04-27 12:35:55