-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequirement.c
112 lines (102 loc) · 2.15 KB
/
requirement.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
/*
** requirement.c for requirement in /home/walrav_j/rendu/SBMLparser
**
** Made by Jean Walrave
** Login <walrav_j@epitech.net>
**
** Started on Mon Jun 13 10:09:07 2016 Jean Walrave
** Last update Tue Jun 14 14:56:12 2016 Jean Walrave
*/
#include <stdlib.h>
#include "include/requirement.h"
static int my_is_alphanumeric(char c)
{
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9'))
return (0);
return (1);
}
static char *my_strncpy(char *dest, const char *str, int size, int is_allocated)
{
int i;
i = 0;
if (str == NULL || (is_allocated && (dest = malloc(size + 1)) == NULL))
return (NULL);
while (str[i] && i < size)
{
dest[i] = str[i];
i += 1;
}
dest[i] = '\0';
return (dest);
}
static int add_to_stack(t_parser_stack **stack, const char *str)
{
t_parser_stack *new_elem;
t_parser_stack *tmp;
if ((new_elem = malloc(sizeof(*new_elem))) == NULL)
return (1);
new_elem->str = str;
new_elem->next = NULL;
if (*stack)
{
tmp = *stack;
while (tmp->next)
tmp = tmp->next;
tmp->next = new_elem;
}
else
*stack = new_elem;
return (0);
}
static char **stack_to_tab(t_parser_stack **stack)
{
int i;
char **tab;
t_parser_stack *tmp;
if (*stack == NULL)
return (NULL);
tmp = *stack;
i = 0;
while (tmp)
{
tmp = tmp->next;
i += 1;
}
if ((tab = malloc(sizeof(tab) * (i + 1))) == NULL)
return (NULL);
i = -1;
while ((*stack))
{
tmp = *stack;
tab[++i] = (char *)(*stack)->str;
*stack = (*stack)->next;
free(tmp);
}
tab[i + 1] = 0;
return (tab);
}
char **my_strtowordtab_synthesis(char *str)
{
int i;
int size;
int is_in_word;
static t_parser_stack *stack = NULL;
i = -1;
is_in_word = 0;
while (str && str[++i])
{
if (my_is_alphanumeric(str[i]))
is_in_word = 0;
else if (!is_in_word)
{
size = 0;
while (str + i && str[i + size] && !my_is_alphanumeric(str[i + size]))
++size;
if (add_to_stack(&stack, my_strncpy(NULL, str + i, size, 1)) == 1)
return (NULL);
i += size - 1;
}
}
return (stack_to_tab(&stack));
}