forked from nayefc/mips-assembler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenizer.h
60 lines (45 loc) · 1.53 KB
/
tokenizer.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
#ifndef __PARSE_UTILS_H_
#define __PARSE_UTILS_H_
/*
author: dr. srinidhi varadarajan
copyright (C) 2005
this file contains a thread-safe string tokenization function that can be
used for general purpose parsing.
*/
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
/* parses the token in_str delimited by the characters in delim. it returns
an output string out_str, which contains the remaining elements of
the string after the first token. this can be used to parse all tokens in
a line by recursively calling the parse function.
it also returns the actual delimiting character in delim_char, which can
be used to define numerical ranges. if this is not desired, delim_char may be
set to NULL.
it returns the first token delimited by characters in delim or NULL,
if no such token is found.
this is a thread safe implementation
*/
static inline char *parse_token(char *in_str, char *delim, char **out_str, char *delim_char)
{
int len;
char *ptr, *tptr, *token;
/* Bypass leading whitespace delimiters */
len = strspn(in_str, delim);
ptr = (in_str + len);
/* Get end of token */
tptr = strpbrk(ptr, delim);
if (tptr == NULL) return(NULL);
len = tptr - ptr;
if (delim_char != NULL) *delim_char = *tptr;
/* Create output string */
*out_str = tptr + 1; /* go past the delimiter */
/* Create token */
token = (char *) malloc(len + 1);
if (token == NULL) return(NULL);
memcpy(token, ptr, len);
token[len] = (char) 0;
return(token);
}
#endif