/
chain.h
177 lines (160 loc) · 4.42 KB
/
chain.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#ifndef FETLANG_CHAIN_H_
#define FETLANG_CHAIN_H_
#include "core/include/fraction.h"
#include <stdio.h>
#ifdef __cplusplus
extern "C"{
#endif
/* Chains (strings) are represented as doubly linked lists */
typedef struct Link {
Fraction value;
struct Link *prev; /* Starts at NULL */
struct Link *next; /* Ends at NULL */
} Link;
typedef struct Chain {
ChainLengthInt length;
Link *start;
Link *end;
} Chain;
/**
* Construct chain
*
* Set defaults for chain: length to 0 and start to NULL
*
* @param chain The chain being constructed
*/
void init_chain(Chain * chain);
/**
* Clear chain
*
* Free all links in chain and set start to NULL
*
* @param chain The chain being cleared
*/
void clear_chain(Chain * chain);
/**
* Append a cstring to a chain
*
* Append each character of the cstring as a Link to the chain.
*
* @param chain The chain being modified
* @param text The string being added to the chain
*/
void append_cstr_to_chain(Chain * chain, const char *text);
/**
* Append fraction as single Link to chain
*
* @param chain The chain being modified
* @param fraction The fraction being added to the chain
*/
void append_flink_to_chain(Chain * chain, Fraction fraction);
/**
* Append fraction as string to chain
*
* Fraction is first converted into a string format. For example, 25/1 would be
* converted to "twenty five" and then appended to the chain
*
* @param chain The chain being modified
* @param fraction The fraction being added to the chain
*/
void append_fraction_to_chain(Chain * chain, Fraction fraction);
/**
* Append the contents in one chain to another
*
* @param chain1 The chain being modified
* @param chain2 The chain being appended to chain1
*/
void append_chain_to_chain(Chain * chain1, Chain chain2);
/**
* Append the contents of a stream to a chain
*
* Each char is read from the stream and copied to the chain as a fraction/Link.
*
* @param chain The chain being modified
* @param stream The stream whose contents are read
*/
void append_stream_to_chain(Chain * chain, FILE * stream);
/**
* Erase stream contents
*
* @param stream The stream being cleared
*/
void clear_stream(FILE * stream);
/**
* Append chain to stream
*
* Print each fraction of chain to the stream to be appended as a char
*
* @param chain The chain being read
* @param stream The stream being appended to
*/
void append_chain_to_stream(Chain chain, FILE * stream);
/**
* Assign chain to stream
*
* Clear the stream, then call append_chain_to_stream
*
* @param chain The chain being read
* @param stream The stream being copied to
*/
void chain_to_stream(Chain chain, FILE * stream);
/**
* Convert chain to cstring
*
* Print each fraction of chain to the cstring buffer to be appended as a char
* This function is not safe. If buffer is too small to hold the contents of
* chain, a segfault will occur
*
* @param chain The chain being read
* @param buffer The cstring being appended to
* @return The length of the buffer.
*/
int chain_to_cstr(Chain chain, char * buffer);
/**
* Set file contents to chain's contents
*
* Open a file and set its contents to that of the chain's. As with other
* functions, each fraction is converted to a char. An error will be thrown
* upon any problem opening the file
*
* @param chain The chain being read
* @param filename A chain holding the filename of the file we're opening
*/
void write_chain_to_file(Chain chain, Chain filename);
/**
* Set chain contents to file's contents
*
* Open a file and read its contents to the chain's, replacing any previous
* values(an assign, not append). As with other functions, each char is
* converted to a fraction. An error will be thrown upon any problem opening
* the file
*
* @param chain The chain being overwritten
* @param filename A chain holding the filename of the file we're opening
*/
void read_file_to_chain(Chain * chain, Chain filename);
/**
* Compare chains
*
* Check if chaims are equivalent. Will return 0 if they are, and another value
* if they are not.
*
* @param a The first chain
* @param b The second chain
* @return The comparison value
*/
int compare_chains(Chain a, Chain b);
/* Parse chain to construct fraction */
/**
* Convert chain to fraction
*
* Parse chain to convert to a fraction value. Return 0/1 if chain is invalid.
*
* @param chain The chain being parsed
* @return The fraction value of the chain
*/
Fraction chain_to_fraction(Chain chain);
#ifdef __cplusplus
}
#endif
#endif