htils 1
A small set of utilities for C programming.
Loading...
Searching...
No Matches
file.h File Reference
#include <stdio.h>
#include <htils/arena.h>
#include <htils/basictypes.h>
#include <htils/string.h>

Go to the source code of this file.

Functions

u64 file_size_stream (FILE *stream)
 Gets the size of a file from a file stream.
 
u64 file_size (const string *path)
 Gets the size of a file at path.
 
stringread_file_from_stream (arena_t *arena, FILE *stream)
 Reads the contents of a file from stream into a string.
 
stringread_file (arena_t *arena, const string *path)
 Reads the contents of a file at path into a string.
 
stringread_file_from_stream_bytes (arena_t *arena, FILE *stream, const u64 bytes)
 Read bytes from file stream to string.
 
stringread_file_bytes (arena_t *arena, const string *path, const u64 bytes)
 Read bytes from a file path to string.
 
u64 write_to_file_stream (FILE *stream, const string *contents)
 Write contents to file stream.
 
u64 write_to_file (const string *path, const string *contents)
 Write contents to file path.
 
u64 write_to_file_stream_bytes (FILE *stream, const string *contents, const u64 bytes)
 Write bytes from contents to stream.
 
u64 write_to_file_bytes (const string *path, const string *contents, const u64 bytes)
 Write bytes from @contents to a file at path.
 

Function Documentation

◆ file_size()

u64 file_size ( const string path)

Gets the size of a file at path.

Through opening the file and then running file_size_stream().

Precondition
path must be valid and cannot be null.
Parameters
pathThe path to the file to get the size of.
Returns
The size of the file.
See also
file_size_stream()

◆ file_size_stream()

u64 file_size_stream ( FILE *  stream)

Gets the size of a file from a file stream.

Safe macros for fseek() and ftell() thats compatible across platforms.

By reading the size through fseek64() and ftell64(). Which are aliases:

Precondition
stream must be valid, preferably open with "rb" and cannot be null.
Parameters
streamThe file stream to get the size of.
Returns
The size of the file.
See also
fseeko(), ftello(), _fseeki64(), _ftelli64().

◆ read_file()

string * read_file ( arena_t arena,
const string path 
)

Reads the contents of a file at path into a string.

Opens the file, reads the entire file 'til EOF with read_file_from_stream(), then returns the result.

Precondition
  • arena and path must be valid and cannot be null.
Parameters
arenaFor allocating the contents of the file.
pathThe path to the file to read.
Returns
A string containing the contents of the file.
See also
read_file_from_stream()

◆ read_file_bytes()

string * read_file_bytes ( arena_t arena,
const string path,
const u64  bytes 
)

Read bytes from a file path to string.

By opening the file, reading bytes into a block using read_file_from_stream_bytes(), then returning the result.

Precondition
arena, path, and bytes must be valid and cannot be null.
Parameters
arenaFor allocating the contents of the file.
pathThe path to the file to read.
bytesThe number of bytes to read.
Returns
A string containing the contents of the file.
See also
read_file_from_stream_bytes()

◆ read_file_from_stream()

string * read_file_from_stream ( arena_t arena,
FILE *  stream 
)

Reads the contents of a file from stream into a string.

Reads the entire file 'til EOF and stores it in a string, with the file content length in the len param using fread().

Precondition
  • arena must be valid antd cannot be null.
  • stream must be valid and cannot be null preferably open with "rb".
Parameters
arenaFor allocating the contents of the file.
streamThe file stream to read from.
Returns
A string containing the contents of the file.
See also
fread()

◆ read_file_from_stream_bytes()

string * read_file_from_stream_bytes ( arena_t arena,
FILE *  stream,
const u64  bytes 
)

Read bytes from file stream to string.

By allocating a block using the arena that is bytes big and reading bytes into that block using fread().

Precondition
  • arena, stream, and bytes must be valid and cannot be null.
  • stream should be preferably open with "rb".
Parameters
arenaFor allocating the contents of the file.
streamThe file stream to read from.
bytesThe number of bytes to read.
Returns
A string containing the contents of the file.
See also
fread()

◆ write_to_file()

u64 write_to_file ( const string path,
const string contents 
)

Write contents to file path.

By writing len characters from contents to the file at path using write_to_file_stream().

Precondition
path and contents must be valid, and cannot be null.
Parameters
pathThe path to the file to write to.
contentsThe string to write to the file.
Returns
The number of bytes written.
See also
write_to_file_stream()

◆ write_to_file_bytes()

u64 write_to_file_bytes ( const string path,
const string contents,
const u64  bytes 
)

Write bytes from @contents to a file at path.

By writing bytes from contents to the file at path using write_to_file_stream_bytes(), and then returning the result.

Precondition
path and contents must be valid, and cannot be null.
Parameters
pathThe path to the file to write to.
contentsThe string to write to the file.
Returns
The number of bytes written.
See also
write_to_file_stream_bytes()

◆ write_to_file_stream()

u64 write_to_file_stream ( FILE *  stream,
const string contents 
)

Write contents to file stream.

By writing len characters from contents to that file thats associated with stream using fwrite().

Precondition
  • stream and contents must be valid, and cannot be null.
  • stream should be preferably open with "wb".
Parameters
streamThe file stream to write to.
contentsThe string to write to the file.
Returns
The number of bytes written.
See also
fwrite()

◆ write_to_file_stream_bytes()

u64 write_to_file_stream_bytes ( FILE *  stream,
const string contents,
const u64  bytes 
)

Write bytes from contents to stream.

By writing bytes from contents to the file stream provided by stream using fwrite() .

Precondition
  • stream, contents, and bytes must be valid, and cannot be null nor 0.
  • stream should be preferably open with "wb".
Parameters
streamThe file stream to write to.
contentsThe string to write to the file.
bytesThe number of bytes to write.
Returns
The number of bytes written.
See also
fwrite()