-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.h
135 lines (119 loc) · 2.78 KB
/
util.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
// -*- mode: c -*-
/** @file
* provides utility functions and interface to container, error and test.
*
* interfaces
* \li error - interface to manipulate errors.
* \li test - interface for automated testing.
*
* Containers
* \li ArgsIter - an iterator for arguments
* \li Vector - an ordered collection.
* \li Map - an object that maps keys to values.
* \li StringBuffer - mutable sequence of characters.
*
* Functions
* \li intdup() - duplicate an integer
*/
#pragma once
#include <stdbool.h> // bool
#include <stdnoreturn.h> // noreturn
/* util.c */
typedef enum {
E_Okay,
E_Failure,
HM_EmptyRequest,
HM_BadRequest,
O_IllegalArgument,
} ExceptionType;
/** @struct Exception
*/
typedef struct {
ExceptionType ty;
char *msg;
} Exception;
/** @struct ArgsIter
* @brief An iterator for arguments.
*
* \li new_ArgsIter()
* \li delete_ArgsIter()
* \li ArgsIter_getProgName() returns name used to invoke the program itself.
* \li ArgsIter_hasNext()
* \li ArgsIter_next() get the next argument.
*
* @example args_iter.c
*/
typedef struct {
char *prog_name;
int argc;
char **argv;
} ArgsIter;
ArgsIter *new_ArgsIter(int, char **);
void delete_ArgsIter(ArgsIter *);
char *ArgsIter_getProgName(ArgsIter *);
bool ArgsIter_hasNext(ArgsIter *);
char *ArgsIter_next(ArgsIter *);
/** @struct Vector
*
* \li new_Vector();
* \li delete_Vector()
* \li Vector_push()
* \li Vector_pop()
* \li Vector_last()
*/
typedef struct {
void **data;
int capacity;
int len;
} Vector;
Vector *new_Vector();
void delete_Vector(Vector *);
void Vector_push(Vector *, void *);
void *Vector_pop(Vector *);
void *Vector_last(Vector *);
/** @struct Map
*
* \li new_Map()
* \li delete_Map()
* \li Map_put()
* \li Map_get()
*/
typedef struct {
Vector *keys;
Vector *vals;
} Map;
Map *new_Map();
void delete_Map(Map *);
void Map_put(Map *, char *, void *);
void *Map_get(Map *, const char *);
/** @struct StringBuffer
*
* new_StringBuffer()
* delete_StringBuffer()
* StringBuffer_append()
* StringBuffer_appendChar()
* StringBuffer_toString()
*/
typedef struct {
int len;
Vector *_body;
char *_buf;
int _buf_len;
int _buf_siz;
} StringBuffer;
StringBuffer *new_StringBuffer();
void delete_StringBuffer(StringBuffer *);
void StringBuffer_append(StringBuffer *sb, const char *string);
void StringBuffer_appendChar(StringBuffer *sb, char c);
char *StringBuffer_toString(StringBuffer *);
int *intdup(int);
noreturn void error(char *, ...);
//
// test
//
void expect(int line, int expected, int actual);
void expect_str(int line, const char *expected, const char *actual);
void expect_ptr(int line, const void *expected, const void *actual);
void expect_bool(int line, bool expected, bool actual);
/* util_test.c */
void run_all_test_util();