feat(line): add line reading and spliting
This commit is contained in:
parent
c2b3c98e31
commit
14dc5a0975
4 changed files with 97 additions and 4 deletions
6
Makefile
6
Makefile
|
@ -1,8 +1,10 @@
|
|||
OBJ=main.o
|
||||
OBJ=main.o line.o
|
||||
CC=gcc
|
||||
CFLAGS= -O3 -Wall
|
||||
|
||||
all:dish
|
||||
all:debug
|
||||
|
||||
release:dish
|
||||
|
||||
dish: $(OBJ)
|
||||
$(CC) $(CFLAGS) -o dish $(OBJ)
|
||||
|
|
87
line.c
Normal file
87
line.c
Normal file
|
@ -0,0 +1,87 @@
|
|||
#include "line.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char *readLine(void) {
|
||||
int bufsize = LINE_BUF_SIZE;
|
||||
int position = 0;
|
||||
char *buffer = malloc(sizeof(char) * bufsize);
|
||||
char c;
|
||||
|
||||
if (!buffer) {
|
||||
fprintf(stderr, "dish: allocation error\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
while (1) {
|
||||
// read a character
|
||||
c = getchar();
|
||||
printf("%s", &c);
|
||||
// repleace EOF with a null character and return
|
||||
if (c == EOF || c == '\n') {
|
||||
buffer[position] = '\0';
|
||||
return buffer;
|
||||
} else {
|
||||
buffer[position] = c;
|
||||
}
|
||||
position++;
|
||||
|
||||
/* if the buffer is full, reallocate */
|
||||
if (position >= bufsize) {
|
||||
bufsize += LINE_BUF_SIZE;
|
||||
buffer = realloc(buffer, bufsize);
|
||||
if (!buffer) {
|
||||
fprintf(stderr, "dish: allocation error\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
char **splitLine(char *line) {
|
||||
int bufsize = LINE_BUF_SIZE, position = 0;
|
||||
char **tokens = malloc(bufsize * sizeof(char *));
|
||||
char *token;
|
||||
|
||||
if (!tokens) {
|
||||
fprintf(stderr, "dish: allocation error");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* get sub string
|
||||
*" \t\r\n\a" is a set of delimiters,
|
||||
* which stands for space, tab, carriage, newline, and alert.
|
||||
*/
|
||||
token = strtok(line, " \t\r\n\a");
|
||||
|
||||
while (token != NULL) {
|
||||
tokens[position] = token;
|
||||
position++;
|
||||
|
||||
/* if the buffer is full, reallocate */
|
||||
if (position >= bufsize) {
|
||||
bufsize += LINE_BUF_SIZE;
|
||||
tokens = realloc(tokens, bufsize);
|
||||
if (!tokens) {
|
||||
fprintf(stderr, "dish: allocation error\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
token = strtok(NULL, " \t\r\n\a");
|
||||
}
|
||||
|
||||
/* set the last element to NULL */
|
||||
tokens[position] = NULL;
|
||||
return tokens;
|
||||
}
|
||||
|
||||
void line() {
|
||||
char *line;
|
||||
int status = 1;
|
||||
char str;
|
||||
printf("> ");
|
||||
line = readLine();
|
||||
free(line);
|
||||
}
|
5
line.h
Normal file
5
line.h
Normal file
|
@ -0,0 +1,5 @@
|
|||
#ifndef _LINE_H_
|
||||
#define _LINE_H_
|
||||
#define LINE_BUF_SIZE 1024
|
||||
void line();
|
||||
#endif
|
3
main.c
3
main.c
|
@ -19,8 +19,7 @@ int main(int argc, char *argv[]) {
|
|||
printf("DEBUG is defined\n");
|
||||
#endif
|
||||
while (1) {
|
||||
printf("runing!\n");
|
||||
sleep(1);
|
||||
line();
|
||||
}
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue