Use sonames.
[jelmer/ptabtools.git] / ptb-tuning.c
1 /*
2    Parser library for the PowerTab (PTB) file format
3    Parser for tuning dictionary files
4    (c) 2005-2006: Jelmer Vernooij <jelmer@samba.org>
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., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 #include <stdio.h>
22 #include <fcntl.h>
23 #include <sys/stat.h>
24 #include <string.h>
25 #include <stdarg.h>
26 #include "dlinklist.h"
27
28 #ifdef HAVE_CONFIG_H
29 #  include "config.h"
30 #endif
31
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>
34 #else
35 #include <io.h>
36 typedef int ssize_t;
37 #endif
38
39 #define PTB_CORE
40 #include "ptb.h"
41
42 #define malloc_p(t,n) (t *) calloc(sizeof(t), n)
43
44 struct ptb_tuning_dict *ptb_read_tuning_dict(const char *f)
45 {
46         struct ptb_tuning_dict *ptbf = malloc_p(struct ptb_tuning_dict, 1);
47         char unknown[8];
48         int i;
49         int fd;
50
51         fd = open(f, O_RDONLY
52 #ifdef O_BINARY
53                                                 | O_BINARY
54 #endif
55                                    );
56         
57         read(fd, &ptbf->nr_tunings, 1);
58         read(fd, unknown, 7);
59         read(fd, unknown, 7); /* Class name (CTuning) */
60
61         ptbf->tunings = malloc_p(struct ptb_tuning, ptbf->nr_tunings);
62         for (i = 0; i < ptbf->nr_tunings; i++) {
63                 uint8_t name_len;
64                 read(fd, &name_len, 1);
65
66                 ptbf->tunings[i].name = malloc_p(char, name_len+1);
67                 read(fd, ptbf->tunings[i].name, name_len);
68                 ptbf->tunings[i].name[name_len] = '\0';
69
70                 read(fd, &ptbf->tunings[i].capo, 1);
71                 read(fd, &ptbf->tunings[i].nr_strings, 1);
72                 ptbf->tunings[i].strings = malloc_p(uint8_t, ptbf->tunings[i].nr_strings);
73                 read(fd, ptbf->tunings[i].strings, ptbf->tunings[i].nr_strings);
74
75                 read(fd, unknown, 2);
76         }
77
78         close(fd);
79
80         return ptbf;
81 }
82
83 int ptb_write_tuning_dict(const char *f, struct ptb_tuning_dict *t)
84 {
85         struct ptb_tuning_dict *ptbf = malloc_p(struct ptb_tuning_dict, 1);
86         char unknown[8];
87         int i;
88         int fd;
89
90         fd = open(f, O_WRONLY
91 #ifdef O_BINARY
92                                                 | O_BINARY
93 #endif
94                                    );
95
96         if (!fd) {
97                 return -1;
98         }
99         
100         write(fd, &ptbf->nr_tunings, 1);
101         write(fd, unknown, 7);
102         write(fd, "CTuning", 7); /* Class name */
103
104         for (i = 0; i < ptbf->nr_tunings; i++) {
105                 uint8_t name_len = strlen(ptbf->tunings[i].name);
106                 uint32_t next = 0x8001;
107                 write(fd, &name_len, 1);
108
109                 write(fd, ptbf->tunings[i].name, name_len);
110
111                 write(fd, &ptbf->tunings[i].capo, 1);
112                 write(fd, &ptbf->tunings[i].nr_strings, 1);
113                 write(fd, ptbf->tunings[i].strings, ptbf->tunings[i].nr_strings);
114
115                 write(fd, &next, 2);
116         }
117
118         close(fd);
119
120         return 0;
121 }
122
123 const char *ptb_tuning_get_note(char n)
124 {
125         const char *notes[] = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
126         return notes[n%12];
127 }
128
129 void ptb_free_tuning_dict(struct ptb_tuning_dict *t)
130 {
131         int i;
132         for (i = 0; i < t->nr_tunings; i++) {
133                 free(t->tunings[i].name);
134                 free(t->tunings[i].strings);
135         }
136         free(t->tunings);
137         free(t);
138 }