Go to the source code of this file.
Data Structures | |
struct | log_t |
The log data structure. More... | |
Functions | |
void | log_init (log_t *l) |
Initializes the log. | |
void | log_destroy (log_t *l) |
Frees all internal memory associated with the log. | |
void | log_append (log_t *l, char *item) |
Appends an item to the end of the log. | |
char * | log_pop (log_t *l) |
Removes and returns the last item in the log. | |
char * | log_at (log_t *l, unsigned int idx) |
Returns a pointer to the idx-th element in the log, where the 0-th element is the first element appended to the log. | |
unsigned int | log_size (log_t *l) |
Returns the number of elements in the log. | |
char * | log_search (log_t *l, const char *prefix) |
Preforms a newest-to-oldest search of log entries for an entry matching a given prefix. |
void log_append | ( | log_t * | l, | |
char * | item | |||
) |
Appends an item to the end of the log.
The item MUST NOT be copied. Only a pointer is stored in the log.
You may assume that:
l | Pointer to the log data structure. | |
item | Pointer to a string to be added to the log. |
char* log_at | ( | log_t * | l, | |
unsigned int | idx | |||
) |
Returns a pointer to the idx-th element in the log, where the 0-th element is the first element appended to the log.
You may assume that:
l | Pointer to the log data structure. | |
idx | Zero-based index into the log, where the 0-th entry corresponds to the first (oldest) entry in the log and the (n-1)-th entry corresponds to the latest (newest) entry in the log. |
void log_destroy | ( | log_t * | l | ) |
Frees all internal memory associated with the log.
You may assume that:
l | Pointer to the log data structure to be destoryed. |
void log_init | ( | log_t * | l | ) |
char* log_pop | ( | log_t * | l | ) |
Removes and returns the last item in the log.
If this function was called following a call to log_append(), the return value will be the value that was just to the log. If multiple calls are made to log_pop(), is should continue to remove entries from the log in a Last-In First-Out (LIFO) or "stack" order.
You may assume that:
l | Pointer to the log data structure. |
char* log_search | ( | log_t * | l, | |
const char * | prefix | |||
) |
Preforms a newest-to-oldest search of log entries for an entry matching a given prefix.
This search starts with the newest or (n-1)-th entry in the log and compares each entry to determine if query is a prefix of the log entry. Upon reaching a match, a pointer to that element is returned. If no match is found, a NULL pointer is returned.
For example, a log may be built with five entries:
log_append(&l, "ab 1"); log_append(&l, "a 2"); log_append(&l, "abc 3"); log_append(&l, "ab 4"); log_append(&l, "a 5");
Using the log that was build above:
log_search(&l, "ab")
will return the pointer to the string "ab 4" since the search must begin from the newest entry and move backwards.log_search(&l, "a")
will return the pointer to the string "a 5".log_search(&l, "abc")
will return the pointer to the string "abc 3".log_search(&l, "d")
will return a NULL pointer.You may assume that:
l | Pointer to the log data structure. | |
prefix | The prefix to test each entry in the log for a match. |
unsigned int log_size | ( | log_t * | l | ) |
Returns the number of elements in the log.
You may assume that:
l | Pointer to the log data structure. |