Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define IN_LINE_BUFSIZE 1024
- #define TOK_BUFSIZE 128
- #define TOK_DELIMITER " ,\t"
- char *get_line(void) {
- int bufsize = IN_LINE_BUFSIZE, pos = 0, c;
- char *buffer = malloc(sizeof(char) * bufsize);
- if (!buffer) {
- fprintf(stderr, "FATAL: failed to allocate memory");
- exit(EXIT_FAILURE);
- }
- while (1) {
- c = getchar();
- if (c == EOF || c == '\n') {
- buffer[pos] = '\0';
- return buffer;
- }
- else {
- buffer[pos] = c;
- }
- pos++;
- if (pos >= bufsize) {
- bufsize += IN_LINE_BUFSIZE;
- buffer = realloc(buffer, bufsize);
- if (!buffer) {
- fprintf(stderr, "FATAL: failed to allocate memory");
- exit(EXIT_FAILURE);
- }
- }
- }
- }
- char **parse_line(char *line) {
- int bufsize = TOK_BUFSIZE, pos = 0;
- char **tokens = malloc(bufsize * sizeof(char*));
- char *token;
- if (!tokens) {
- fprintf(stderr, "FATAL: failed to allocate memory");
- exit(EXIT_FAILURE);
- }
- token = strtok(line, TOK_DELIMITER);
- printf("Parse: token: %s\n", token);
- while (token != NULL) {
- tokens[pos] = token;
- pos++;
- if (pos >= bufsize) {
- bufsize += TOK_BUFSIZE;
- tokens = realloc(tokens, bufsize * sizeof(char*));
- if (!tokens) {
- fprintf(stderr, "FATAL: failed to allocate memory");
- exit(EXIT_FAILURE);
- }
- }
- token = strtok(NULL, TOK_DELIMITER);
- }
- tokens[pos] = NULL;
- return tokens;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement