-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
152 lines (133 loc) · 3.8 KB
/
main.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
/*
* ICT1002 Assignment 2, 2018-19 Trimester 1.
*
* This file implements the main program, including parsing of commands.
*
* DO NOT MODIFY THIS FILE. You may invoke its functions if you like, however.
*/
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sheet1002.h"
/*
* Main loop.
*/
int main(int argc, char *argv[])
{
int num_tokens; /* the number of tokens in the input */
char command[MAX_INPUT]; /* buffer for holding the command */
char arg1[MAX_INPUT]; /* buffer for holding the first argument */
char arg2[MAX_INPUT]; /* buffer for holding the second argument */
char output[MAX_OUTPUT]; /* buffer for holding the output of the command */
int done = 0; /* set to 1 to end the main loop */
/* initialise the output with a welcome message */
strcpy(output, "Welcome to the ICT1002 Spreadsheet!");
/* main command loop */
do
{
/* display the worksheet */
viewport_display(TERMINAL_COLS, TERMINAL_ROWS - 2);
/* print the output of the last command */
printf("%s\n", output);
/* read a line of input */
do
{
printf("? ");
num_tokens = lscanf("%s%s%s", command, arg1, arg2);
} while (num_tokens < 1);
if (num_tokens == 1)
{
/* command with no argument */
done = do_command(command, NULL, NULL, output);
}
else if (num_tokens == 2)
{
/* command with one argument */
done = do_command(command, arg1, NULL, output);
}
else
{
/* command with two arguments */
done = do_command(command, arg1, arg2, output);
}
} while (!done);
/* print a goodbye message */
printf("Goodbye!\n");
return 0;
}
/*
* Utility function for comparing commands, arguments, etc. case-insensitively.
*
* Input:
* token1 - the first token
* token2 - the second token
*
* Returns:
* as strcmp()
*/
int compare_token(const char *token1, const char *token2)
{
int i = 0;
while (token1[i] != '\0' && token2[i] != '\0')
{
if (toupper(token1[i]) < toupper(token2[i]))
return -1;
if (toupper(token1[i]) > toupper(token2[i]))
return 1;
i++;
}
if (token1[i] == '\0' && token2[i] == '\0')
return 0;
if (token1[i] == '\0')
return -1;
return 1;
}
/*
* Utility function for parsing a line of input, ensuring that everything is
* read to the end of the line.
*
* The format string for this function also accepts a special format string
* "%z", which will read the whole line into the first non-format argument.
* This argument should have space for at least MAX_INPUT characters.
*
* Input:
* as scanf()
*
* Returns:
* the number of tokens successfully parsed
*/
int lscanf(const char *format, ...)
{
char line[MAX_INPUT]; /* buffer for holding the line of input */
/* initialise variable argument list */
va_list args;
va_start(args, format);
/* read the line */
fgets(line, MAX_INPUT, stdin);
int num_tokens = 0;
if (format[0] == '%' && format[strlen(format) - 1] == 'z')
{
/* get the maximum number of characters to read, if any */
int n = atoi(format + 1);
if (n <= 0)
n = MAX_INPUT;
/* copy the whole line, up to n characters */
char *output = va_arg(args, char *);
strncpy(output, line, n);
num_tokens = 1;
/* remove the trailing '\n' */
char *p = strchr(output, '\n');
if (p != NULL)
*p = '\0';
}
else
{
/* pass to scanf */
num_tokens = vsscanf(line, format, args);
}
/* clean up variable argument list */
va_end(args);
return num_tokens;
}