reading.h (1878B)
1 #ifndef READING_H 2 #define READING_H 3 4 #include <stdint.h> 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <string.h> 8 9 #define MAXIMUM_FILESIZE_BYTES 256000 10 #define MAXIMUM_LINE_BYTES 256000 11 #define LINES_ALLOC_MIN 2048 12 13 /** 14 * Read all chars from a file. 15 * 16 * Reads an entire file (assuming its size is < MAXIMUM_FILESIZE_BYTES). 17 * This allocates memory that needs to be free'd. 18 * 19 * @param output pointer to the address where the allocated string 20 * should be written 21 * @param filename the name of the file 22 * @return the number of bytes read (excluding final null byte) 23 */ 24 size_t readall(char **output, char *filename); 25 26 /** 27 * Read all lines from a file. 28 * 29 * Reads a file, line-by-line (assuming no line is longer than 30 * MAXIMUM_LINE_BYTES). 31 * Allocates memory for each line that needs to be free'd. 32 * 33 * @param lines pointer to the address where the list of pointers 34 * should be written 35 * @param filename the name of the file 36 * @return the number of lines read 37 */ 38 size_t readlines(char ***lines, char *filename); 39 40 /** 41 * Read the next uint64_t from FILE ptr. 42 * 43 * This has no checks for overflows, if it doesnt fit it will have 44 * undefined behaviour. Returns EOF if EOF is found before any 45 * uints. 46 * 47 * @param n pointer to where to write result 48 * @param fp file pointer to read from 49 * @returns the number of characters read as part of u64 50 */ 51 int read_next_u64(uint64_t *n, FILE *fp); 52 53 /** 54 * Read the next uin64_t from string. 55 * 56 * This assumes that the string is a well-formatted null-terminated 57 * string. Returns a pointer to the * first char after the uint64 in 58 * the string, or NULL if this is not a part of the string or if it 59 * failed to read an uint64_t. 60 * 61 * @param n pointer to where to write the result 62 * @param s the string 63 * @returns pointer to next char in string, or NULL 64 */ 65 char *sread_next_u64(uint64_t *n, char *s); 66 67 #endif