1 /*
2 TiMidity++ -- MIDI to WAVE converter and player
3 Copyright (C) 1999-2002 Masanao Izumo <mo@goice.co.jp>
4 Copyright (C) 1995 Tuukka Toivonen <tt@cgs.fi>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20 */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif /* HAVE_CONFIG_H */
25 #include <stdio.h>
26 #include <stdlib.h>
27 #ifndef NO_STRING_H
28 #include <string.h>
29 #else
30 #include <strings.h>
31 #endif
32 #include "timidity.h"
33 #include "common.h"
34 #include "mblock.h"
35 #include "strtab.h"
36
init_string_table(StringTable * stab)37 void init_string_table(StringTable *stab)
38 {
39 memset(stab, 0, sizeof(StringTable));
40 }
41
put_string_table(StringTable * stab,char * str,int len)42 StringTableNode *put_string_table(StringTable *stab, char *str, int len)
43 {
44 StringTableNode *p;
45
46 p = new_segment(&stab->pool, sizeof(StringTableNode) + len + 1);
47 if(p == NULL)
48 return NULL;
49 p->next = NULL;
50 if(str != NULL)
51 {
52 memcpy(p->string, str, len);
53 p->string[len] = '\0';
54 }
55
56 if(stab->head == NULL)
57 {
58 stab->head = stab->tail = p;
59 stab->nstring = 1;
60 }
61 else
62 {
63 stab->nstring++;
64 stab->tail = stab->tail->next = p;
65 }
66 return p;
67 }
68
make_string_array(StringTable * stab)69 char **make_string_array(StringTable *stab)
70 {
71 char **table, *u;
72 int i, n, s;
73 StringTableNode *p;
74
75 n = stab->nstring;
76 if(n == 0)
77 return NULL;
78 if((table = (char **)safe_malloc((n + 1) * sizeof(char *))) == NULL)
79 return NULL;
80
81 s = 0;
82 for(p = stab->head; p; p = p->next)
83 s += strlen(p->string) + 1;
84
85 if((u = (char *)safe_malloc(s)) == NULL)
86 {
87 free(table);
88 return NULL;
89 }
90
91 for(i = 0, p = stab->head; p; i++, p = p->next)
92 {
93 int len;
94
95 len = strlen(p->string) + 1;
96 table[i] = u;
97 memcpy(u, p->string, len);
98 u += len;
99 }
100 table[i] = NULL;
101 delete_string_table(stab);
102 return table;
103 }
104
delete_string_table(StringTable * stab)105 void delete_string_table(StringTable *stab)
106 {
107 reuse_mblock(&stab->pool);
108 init_string_table(stab);
109 }
110