-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.c
77 lines (74 loc) · 1.28 KB
/
helpers.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
#include "monty.h"
/**
* check_malloc - checks if a memory allocation fails
* @mem: the pointer to the memory allocated
* Return: exit with error message if failed
*/
void check_malloc(void *mem)
{
if (mem == NULL)
{
fprintf(stderr, "Error: malloc failed\n");
fail_exit(EXIT_FAILURE);
}
}
/**
* tokenize_line - parse the line of a monty file into tokens
* Return: a buffer containing the tokenised line
*/
char **tokenize_line(char *ln)
{
char *line = ln;
extern char **tokens;
char *token;
unsigned int token_size = TOK_SIZE;
unsigned int index = 0;
if (line == NULL)
return (NULL);
token = strtok(line, TOK_DELIM);
while (index < token_size)
{
tokens[index] = token;
token = strtok(NULL, TOK_DELIM);
index++;
}
return (tokens);
}
/**
* is_int - check if the string passed is an integer
* @str: the string to check
* Return: 1 if true 0 if false
*/
int is_int(char *str)
{
int dig;
if (str == NULL)
return (0);
if (*str == '-')
str++;
dig = *str;
while (dig != '\0')
{
if (!isdigit(dig))
return (0);
dig = *(str++);
}
return (1);
}
/**
* free_stack - free a stack_t stack
* Return: void
*/
void free_stack()
{
extern stack_t *top;
stack_t *prev;
if (top == NULL)
return;
while (top != NULL)
{
prev = top;
top = top->prev;
free(prev);
}
}