bytes.h
1 #ifndef FLASHLIGHT_BYTES_H 2 #define FLASHLIGHT_BYTES_H 3 4 /** @file bytes.h 5 * @brief Helpers for tracking partial byte indexes/offsets in a text file 6 * 7 * FBytes offsets are aggregated into FBytesNode linked lists. 8 * This allows for indexing parts of a file to later be recombined into a single FNode 9 */ 10 11 /** @struct FBytes 12 * @brief representation of the start of a file unit to index 13 * @var FBytes::atomic 14 * true if the bytes offset decmarate the beginning of a new logical unit (ie. a new line) 15 * @var FBytes::offset 16 * the offset in the target file to 17 */ 18 typedef struct FBytes 19 { 20 bool atomic; 21 size_t offset; 22 } f_bytes; 23 24 25 /** @struct FBytesNode 26 * @brief a linked list of FBytes. 27 * Typically used to represent an index of newlines in a text file 28 * @var FBytesNode::bytes 29 * The bytes for this node 30 * @var FBytesNode::next 31 * The next node in the linked list 32 */ 33 typedef struct FBytesNode 34 { 35 f_bytes* bytes; 36 struct FBytesNode* next; 37 } f_bytes_node; 38 39 /** 40 Initializer for FBytes. 41 42 @param out the initialized f_bytes struct 43 @param atomic true if the offset captures the beginning of a new line 44 @param offset the bytes offset 45 @return non zero for an error 46 */ 47 int f_bytes_new(f_bytes** out, bool atomic, size_t offset); 48 49 /** 50 Free an FBytes 51 52 @param bytes the f_bytes to free 53 */ 54 void f_bytes_free(f_bytes** bytes); 55 56 /** 57 Initializer for a new FBytesNode 58 59 @param out the FBytesNode to initialize 60 @param bytes the first f_bytes to use in the node 61 @return non zero for an error 62 */ 63 int f_bytes_node_new(f_bytes_node** out, f_bytes* bytes); 64 65 66 /** 67 Clone an FBytesNode 68 69 @param out the cloned FBytesNode 70 @param node the node to clone 71 @return non zero for an error 72 */ 73 int f_bytes_node_clone(f_bytes_node**out, f_bytes_node* node); 74 75 /** 76 Free an FBytesNode 77 78 Note: Also frees all the FBytes in this node 79 80 @param node the node to free 81 */ 82 void f_bytes_node_free(f_bytes_node** node); 83 84 /** 85 Create an FNode from an FBytesNode 86 87 This function assumes the FBytesNode is in reverse order. 88 The FBytesNode is not freed. 89 90 ``` 91 if the list structure looks like 92 {8,false} -> {6,true} -> {2,false} -> {1,true} 93 94 the function aggregates non atomic offsets with atomic ones and creates a reversed aggregate 95 {0, 3, 14} 96 ``` 97 98 @param out the FNode to create 99 @param bytes_head the FBytesNode to transform 100 */ 101 int f_node_from_reversed_bytes_node(f_node** out, f_bytes_node** bytes_head); 102 103 /** 104 Prepend onto an FBytesNode 105 106 @param head the FBytesNode to prepend 107 @param parent the target FBytesNode to prepend onto 108 */ 109 int f_bytes_node_prepend(f_bytes_node** head, f_bytes_node** parent); 110 111 #endif