-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
445f27c
commit a18a9f3
Showing
3 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include <stdbool.h> | ||
|
||
// A collector of arbitrary objects. | ||
struct collector; | ||
|
||
// collector_init(obj_size, obj_print) initializes an empty collector that | ||
// stores an arbitrary objects number of objects, each of size obj_size. | ||
/// The function pointer *obj_print refers to a function that can print the | ||
// stored objects. | ||
// example: collector_init(sizeof(struct posn), print_posn) creates a collector | ||
// for storing struct posn. | ||
// effects: allocates memory [client must call collector_collapse] | ||
// time: O(1) | ||
struct collector *collector_init(int obj_size, void (*obj_print)(const void *)); | ||
|
||
// collector_insert(col, obj) inserts the object *obj to the back of the | ||
// collector *col. | ||
// effects: mutates *col | ||
// time: O(o), where o is the object size | ||
void collector_insert(struct collector *col, const void *obj); | ||
|
||
// collector_collapse(col) releases all resources used by the collector *col, | ||
// and returns a pointer to an array containing all objects from *col. | ||
// effects: invalidates *col | ||
// allocates memory [client must free the array and its content] | ||
// time: O(n * o), where n is the number of objects and o is their size | ||
void *collector_collapse(struct collector *col, int *len); | ||
|
||
// collector_is_empty(col) returns true if collector *col is empty, and false | ||
// otherwise. | ||
// time: O(1) | ||
bool collector_is_empty(struct collector *col); | ||
|
||
// collector_print(col) prints all the objects in collector *col. | ||
// Note: This function is intended to be used primarily for debugging. | ||
// time: O(n * o), where n is the number of objects and o is their size | ||
void collector_print(const struct collector *col); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// tokenize_intput(len) reads all input, breaks it into tokens of markup or | ||
// content, and returns an array of tokens. The length of the returned array | ||
// is stored in *len. | ||
// effects: allocates heap memory [client must free the array and each token] | ||
// mutates *len | ||
char **tokenize_input(int *len); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#ifndef __XMLTREE | ||
#define __XMLTREE | ||
|
||
// An XML-tree. | ||
struct xtree { | ||
struct xnode *root; // root of the XML-tree | ||
}; | ||
|
||
// A node in an XML-tree. | ||
struct xnode { | ||
const char *label; // token stored in this node | ||
struct xnode **children; // array of children | ||
int children_len; // length of children array, TEXT_NODE if leaf | ||
}; | ||
|
||
#endif // __XMLTREE | ||
|
||
// Indicates that an XML-node is a text node, i.e., a leaf. | ||
extern const int TEXT_NODE; | ||
|
||
// The label for the root node of the XML tree. | ||
extern const char *ROOT_TAG; | ||
|
||
// tree_create(tokens, len) creates an XML-tree that contains all strings from | ||
// the array tokens of length len. | ||
// effects: allocates memory [client must call tree_destroy] | ||
struct xtree *tree_create(char **tokens, int len); | ||
|
||
// tree_destroy(tr) releases all resources used by XML tree *tr. | ||
// effects: invalidates *tr | ||
void tree_destroy(struct xtree *tr); | ||
|
||
// tree_print(tr) prints out the content of XML tree *tr. | ||
// effects: writes to output | ||
void tree_print(struct xtree *tr); |