This repository has been archived by the owner on Sep 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.c
181 lines (149 loc) · 3.56 KB
/
task.c
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
178
179
180
181
/*
*
* task.c
*
* Baltasar Dinis 89416
* IAED project
*
* defines the task datatype
*/
#include "task.h"
/* buffer for the description of a task (8000 chars as per specification) */
#define DESCRIPT_BUFFER 8000
/* max size of an ulong */
#if ULONG_WIDTH == 32
#define ULONG_BUFFER 10
#else
#define ULONG_BUFFER 20
#endif
/*-------------------------------*/
/* prototypes */
/*-------------------------------*/
/* verifies correctess of the input string */
static bool verify_quoted_str(char *str, size_t buffer_size);
/*-------------------------------*/
/*-------------------------------*/
/*-------------------------------*/
/*
* function: task_
*
* constructor for the task datatype
* id: task identifier, must be 32-bit positive integer
* descript: task description, enclosed in quotes
* dur: task duration, must be 32-bit positive integer
*
* return: ptr to task
*
* if an error is detected in the input, return NULL
* (without the quotes or exceeds DESCRIPT_BUFFER)
*/
task *task_(unsigned long id, char *descript, unsigned long dur)
{
size_t src_len;
task *a = NULL;
/* verify input correctness
* exits with error if the input is incorrect */
if (!verify_quoted_str(descript, DESCRIPT_BUFFER)) {
return NULL;
}
a = (task *) malloc(sizeof(task));
id(*a) = id;
dur(*a) = dur;
src_len = strlen(descript);
descript(*a) = (char *) malloc((src_len + 1) * sizeof(char));
strncpy(descript(*a), descript, src_len + 1);
return a;
}
/*
* function: verify_quoted_str
*
* verifies correctess of the input string
* str: string to be verified
* buffer_size: maximum size of the string
*
* return: true if str is correct; false otherwise
*
* str is said to be incorrect if it isn't delimited by quotes
* or exceeds the buffer
*/
static bool verify_quoted_str(char *str, size_t buffer_size)
{
size_t len;
if (str == NULL) return false;
len = strlen(str);
if (str[0] != '\"' || str[len - 1] != '\"' || len > buffer_size)
return false;
return true;
}
/*
* function: free_task
*
* destructor for the task datatype
* a: ptr to task
*
* frees task a
*/
void free_task(task *a)
{
if (a != NULL) {
free(descript(*a));
descript(*a) = NULL;
free(a);
a = NULL;
}
}
/*
* function: change_task_description
*
* modifier for the task datatype
* t: ptr to a task
* new_desc: new description string
*
* returns: true if the change was successful;
* if it wasn't the description remains the same
*/
bool change_task_description(task *t, char *new_desc)
{
size_t src_len;
if (!verify_quoted_str(new_desc, DESCRIPT_BUFFER))
return false;
src_len = strlen(new_desc);
free(descript(*t));
descript(*t) = (char *) malloc((src_len + 1) * sizeof(char));
strncpy(descript(*t), new_desc, src_len + 1);
return true;
}
/*
* function: change_task_duration
*
* modifier for the task datatype
* t: ptr to a task
* new_dur: new task duration
*
* returns: true if the change was successful;
*/
bool change_task_duration(task *t, unsigned long new_dur)
{
dur(*t) = new_dur;
return true;
}
/*
* function: print_task
*
* external representation function for task datatype
* a: task
*
* return: str with external representation of a task
*
* representation: <id> <description> <duration>
*/
char *print_task(task *a)
{
size_t len;
char *str;
/* note: 2 spaces between values */
len = 2 * ULONG_BUFFER + strlen(descript(*a)) + 2;
str = malloc((len + 1) * sizeof(char));
sprintf(str, "%lu %s %lu", id(*a), descript(*a), dur(*a));
return str;
}