-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakeArray.c
executable file
·113 lines (104 loc) · 1.97 KB
/
MakeArray.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
113
/*
* MakeArray.c - Creates a array of Char * out of a given Textline
*
* Original by Spy/tRSi - Rewritten and bugfixed by SieGeL/tRSi
*/
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <exec/memory.h>
#include <proto/exec.h>
#include "struct_ex.h"
#include "proto.h"
struct StringList
{
char *String ;
struct StringList *next ;
}*sl1,*sl2 ;
void Free_Structs(void);
BOOL INSERT (char *s)
{
if (!sl1)
{
if(!(sl1 = (struct StringList *) AllocPooled (mv_pool,sizeof (struct StringList)))) return(FALSE);
sl2 = sl1;
}
else
{
if(!(sl2->next = (struct StringList *) AllocPooled (mv_pool,sizeof (struct StringList)))) return(FALSE);
sl2 = sl2->next;
}
sl2->next = NULL;
if(!(sl2->String = (char *) AllocVec ((strlen(s)+2),MEMF_PUBLIC|MEMF_CLEAR))) return(FALSE);
strcpy (sl2->String,s);
return(TRUE);
}
void MoveStrings (char **res,struct StringList *sl)
{
struct StringList *slb;
char **strs;
slb = sl;
strs = res;
while(slb)
{
*strs = slb->String;
strs++;
slb = slb->next;
}
strs = NULL;
}
char **MakeArray (char *s)
{
char *str1,*str2,**result;
int i;
sl1 = NULL;
sl2 = NULL;
str1 = s;
i = 0;
while((str2 = FAMEStrChr(str1,' ')))
{
*str2 = 0;
if(INSERT (str1)==FALSE) return(NULL);
i++;
*str2 = ' ';
while ((*str2 != 0) && (*str2 == ' '))
str2++;
str1 = str2;
}
if (*str1 != 0)
{
if(INSERT (str1)==FALSE) return(NULL);
}
i++;
if(!(result = (char **) AllocVec ((i+1)*4,MEMF_PUBLIC|MEMF_CLEAR))) return(NULL);
MoveStrings (result,sl1);
return (result);
}
void FreeArray (char **strings)
{
char **strs;
int i;
i = 0 ;
strs = strings;
while (*strs)
{
i++ ;
FreeVec (*strs);
strs++ ;
}
i++ ;
FreeVec(strings);
Free_Structs();
strings=NULL;
}
void Free_Structs(void)
{
struct StringList *h=sl1;
while(sl1)
{
h=sl1;
sl1=sl1->next;
FreePooled(mv_pool,h,sizeof(struct StringList));
}
sl1=NULL;sl2=NULL;
}