#include <stdint.h>#include <stdlib.h>#include <assert.h>#include <stdio.h>#include <string.h>#include <errno.h>#include <stdbool.h>#include "list.h"#include "log.h"Go to the source code of this file.
Classes | |
| struct | SafeStr |
| Safe String type. More... | |
| struct | Buffer |
| Byte buffer. More... | |
Macros | |
| #define | try(a) |
| Macro para probar si un numero es negativo. | |
| #define | exists(a) |
| Macro para detectar si un pointer es NULL. | |
| #define | TODO |
| Macro for marking parts that have not been implemented. | |
| #define | expect(cond, ...) |
| Macro para crashear el programa si una condicion no se cumple. | |
| #define | KB4 0x1000 |
| cuatro kilobytes | |
| #define | CHUNK_SIZE 0x400 |
| un solo kilobyte | |
Typedefs | |
| typedef uint8_t | Byte |
| Simple Byte type. Guaranteed to be exactly 8 bits. | |
| typedef const char * | Str |
| String type. | |
Functions | |
| Byte * | copy (Byte *src, size_t nbytes) |
| Byte by Byte copy. | |
| DefList (Buffer) typedef struct | |
| This is the buffer that lazily opens the file. | |
| bool | mkdir_p (SafeStr fpath, mode_t mode) |
| Recursively create directories. | |
| LazyBuffer | open (Str fpath) |
| Open a LazyBuffer. | |
| LazyBuffer | next_chunk (LazyBuffer b) |
| Reads the next chunk of the file, if it can. | |
| Buffer | Buffer_serialize (Buffer buf) |
| Serialize a Buffer to a Buffer. | |
| Buffer | Buffer_deserialize (Buffer buf) |
| Deserialize a Buffer from a Buffer. | |
| Buffer | ListBuffer_serialize (ListBuffer list) |
| Serialize a List of Buffer's to a Buffer. | |
| ListBuffer | ListBuffer_deserialize (Buffer buf) |
| Deserialize a ListBuffer from a Buffer. | |
| Buffer | to_buffer (const void *thing, size_t nbytes) |
| Convert a thing to a buffer. | |
| Buffer | uint8_t_serialize (uint8_t n) |
| Serialize a uint8_t to a Buffer. | |
| uint8_t | uint8_t_deserialize (Buffer buf) |
| Deserialize a uint8_t from a Buffer. | |
| Buffer | uint16_t_serialize (uint16_t n) |
| Serialize a uint16_t to a Buffer. | |
| uint16_t | uint16_t_deserialize (Buffer buf) |
| Deserialize a uint16_t from a Buffer. | |
| Buffer | uint32_t_serialize (uint32_t n) |
| Serialize a uint32_t to a Buffer. | |
| uint32_t | uint32_t_deserialize (Buffer buf) |
| Deserialize a uint32_t from a Buffer. | |
| Buffer | atob (Str str) |
| Converts a CString to a Byte Buffer. | |
| Buffer | validate_str (Buffer str, size_t max_len) |
| Validates that a string received through a Socket is correct. | |
| Buffer | bufcpy (Buffer in) |
| Make a copy of a Buffer. | |
| size_t | safe_strlen (Str str, size_t max_len) |
| Does an strlen using a max size. | |
| void | Buffer_deinit (Buffer kod) |
| Deinitializes a Buffer. | |
| void | Buffer_write (Buffer buf, FILE *f) |
| Writes data from a Buffer to a file. | |
| Buffer | Buffer_read (FILE *f) |
| Reads data from a file. | |
| Buffer | SafeStr_serialize (SafeStr str) |
| Serializes a string. | |
| SafeStr | SafeStr_deserialize (Buffer buf) |
| Deserializes a string from a Buffer. | |
| Buffer | size_t_serialize (size_t a) |
| Serializes a size_t length. | |
| size_t | size_t_deserialize (Buffer buf) |
| Deserializes a size_t length. | |
| Buffer | concat (Buffer fst, Buffer snd) |
| Concatenate two Buffer's. | |
| bool | Str_eq (Str lhs, Str rhs) |
| Compares two strings. | |
| SafeStr | atoss (Str str) |
| Convert a string literal to a safe string WARNING: DO NOT USE THIS TO CONVERT THINGS THAT ARE NOT STRING LITERALS! | |
| size_t | min (size_t l, size_t r) |
| Simply returns the minimum of the two. | |
Variables | |
| LazyBuffer | |
| #define CHUNK_SIZE 0x400 |
un solo kilobyte
| #define exists | ( | a | ) |
Macro para detectar si un pointer es NULL.
Usa esta funcion si prefieres crashear el programa cuando encuetras un puntero NULL.
| #define expect | ( | cond, | |
| ... ) |
Macro para crashear el programa si una condicion no se cumple.
| #define KB4 0x1000 |
cuatro kilobytes
| #define TODO |
| #define try | ( | a | ) |
Macro para probar si un numero es negativo.
En general, esta libreria prefiere crashear el programa que dejar que el usuario arregle un error.
| typedef const char* Str |
String type.
Convert a string literal to a safe string WARNING: DO NOT USE THIS TO CONVERT THINGS THAT ARE NOT STRING LITERALS!
| str | the string literal that you want to convert |
| void Buffer_deinit | ( | Buffer | kod | ) |
| Buffer Buffer_read | ( | FILE * | f | ) |
| void Buffer_write | ( | Buffer | buf, |
| FILE * | f ) |
| DefList | ( | Buffer | ) |
This is the buffer that lazily opens the file.
< the FILE* of the file
< the position in the file
< the total size of the file
< the length of the current buffer
< the current buffer
| ListBuffer ListBuffer_deserialize | ( | Buffer | buf | ) |
| Buffer ListBuffer_serialize | ( | ListBuffer | list | ) |
| size_t min | ( | size_t | l, |
| size_t | r ) |
Simply returns the minimum of the two.
| l | the lhs |
| r | the rhs |
| bool mkdir_p | ( | SafeStr | fpath, |
| mode_t | mode ) |
Recursively create directories.
The following code:
is equivalent to the following Shell code:
| fpath | the filepath where you want to create a directory |
| mode | the mode in which you want to create the directory |
| LazyBuffer next_chunk | ( | LazyBuffer | b | ) |
Reads the next chunk of the file, if it can.
In the case where the entire file was read, the LazyBuffer::len will be 0. I'm not sure in what case you wouldn't want to do it this way, but you probably always want to use this like this:
| b | the LazyBuffer that you want to get the next chunk for |
| LazyBuffer open | ( | Str | fpath | ) |
Open a LazyBuffer.
INFO: This does not fill the LazyBuffer with anything. Consider it similar to just executing:
| fpath | the file path to the file that you want to open |
| size_t safe_strlen | ( | Str | str, |
| size_t | max_len ) |
Does an strlen using a max size.
Avoids buffer overflows.
| str | the string to look at |
| max_len | the maximum length that the string can have |
Serializes a string.
INFO: ONLY SERIALIZE STRINGS WITH THIS.
| str | a safe string to serialize |
| size_t size_t_deserialize | ( | Buffer | buf | ) |
| Buffer size_t_serialize | ( | size_t | a | ) |
Compares two strings.
| lhs | left-hand side string |
| rhs | right-hand side string |
| Buffer to_buffer | ( | const void * | thing, |
| size_t | nbytes ) |
Convert a thing to a buffer.
This can be used for types whose size is known at compile-time.
| thing | a pointer to the thing that you want to serialize |
| uint16_t uint16_t_deserialize | ( | Buffer | buf | ) |
| Buffer uint16_t_serialize | ( | uint16_t | n | ) |
| uint32_t uint32_t_deserialize | ( | Buffer | buf | ) |
| Buffer uint32_t_serialize | ( | uint32_t | n | ) |
| uint8_t uint8_t_deserialize | ( | Buffer | buf | ) |
| Buffer uint8_t_serialize | ( | uint8_t | n | ) |
| LazyBuffer |