comparison src/xs_slsup.c @ 891:5048b4799310

Cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 09 Nov 2012 06:39:43 +0200
parents 61a527ac3baa
children be2a8436461a
comparison
equal deleted inserted replaced
890:62d164e3f0ce 891:5048b4799310
33 33
34 /* STIL-database handling 34 /* STIL-database handling
35 */ 35 */
36 gint xs_stil_init(void) 36 gint xs_stil_init(void)
37 { 37 {
38 gint res = 0;
38 XS_MUTEX_LOCK(xs_cfg); 39 XS_MUTEX_LOCK(xs_cfg);
39 40
40 if (!xs_cfg.stilDBPath) { 41 if (xs_cfg.stilDBPath == NULL)
42 {
41 XS_MUTEX_UNLOCK(xs_cfg); 43 XS_MUTEX_UNLOCK(xs_cfg);
42 return -1; 44 return -1;
43 } 45 }
44 46
45 XS_MUTEX_LOCK(xs_stildb_db); 47 XS_MUTEX_LOCK(xs_stildb_db);
48 if (xs_stildb_db) 50 if (xs_stildb_db)
49 xs_stildb_free(xs_stildb_db); 51 xs_stildb_free(xs_stildb_db);
50 52
51 /* Allocate database */ 53 /* Allocate database */
52 xs_stildb_db = (XSSTILDB *) g_malloc0(sizeof(XSSTILDB)); 54 xs_stildb_db = (XSSTILDB *) g_malloc0(sizeof(XSSTILDB));
53 if (!xs_stildb_db) { 55 if (xs_stildb_db == NULL)
54 XS_MUTEX_UNLOCK(xs_cfg); 56 {
55 XS_MUTEX_UNLOCK(xs_stildb_db); 57 res = -2;
56 return -2; 58 goto error;
57 } 59 }
58 60
59 /* Read the database */ 61 /* Read the database */
60 if (xs_stildb_read(xs_stildb_db, xs_cfg.stilDBPath) != 0) { 62 if (xs_stildb_read(xs_stildb_db, xs_cfg.stilDBPath) != 0)
61 xs_stildb_free(xs_stildb_db); 63 {
62 xs_stildb_db = NULL; 64 res = -3;
63 XS_MUTEX_UNLOCK(xs_cfg); 65 goto error;
64 XS_MUTEX_UNLOCK(xs_stildb_db);
65 return -3;
66 } 66 }
67 67
68 /* Create index */ 68 /* Create index */
69 if (xs_stildb_index(xs_stildb_db) != 0) { 69 if (xs_stildb_index(xs_stildb_db) != 0)
70 xs_stildb_free(xs_stildb_db); 70 {
71 xs_stildb_db = NULL; 71 res = -4;
72 XS_MUTEX_UNLOCK(xs_cfg); 72 goto error;
73 XS_MUTEX_UNLOCK(xs_stildb_db);
74 return -4;
75 } 73 }
76 74
77 XS_MUTEX_UNLOCK(xs_cfg); 75 XS_MUTEX_UNLOCK(xs_cfg);
78 XS_MUTEX_UNLOCK(xs_stildb_db); 76 XS_MUTEX_UNLOCK(xs_stildb_db);
79 return 0; 77 return 0;
78
79 error:
80 xs_stildb_free(xs_stildb_db);
81 xs_stildb_db = NULL;
82 XS_MUTEX_UNLOCK(xs_cfg);
83 XS_MUTEX_UNLOCK(xs_stildb_db);
84 return res;
80 } 85 }
81 86
82 87
83 void xs_stil_close(void) 88 void xs_stil_close(void)
84 { 89 {
90 95
91 96
92 XSSTILNode *xs_stil_get(gchar *filename) 97 XSSTILNode *xs_stil_get(gchar *filename)
93 { 98 {
94 XSSTILNode *result; 99 XSSTILNode *result;
95 gchar *tmpFilename;
96 100
97 XS_MUTEX_LOCK(xs_stildb_db); 101 XS_MUTEX_LOCK(xs_stildb_db);
98 XS_MUTEX_LOCK(xs_cfg); 102 XS_MUTEX_LOCK(xs_cfg);
99 103
100 if (xs_cfg.stilDBEnable && xs_stildb_db) { 104 if (xs_cfg.stilDBEnable && xs_stildb_db != NULL)
101 if (xs_cfg.hvscPath) { 105 {
106 gchar *tmpFilename;
107
108 if (xs_cfg.hvscPath != NULL)
109 {
102 /* Remove postfixed directory separator from HVSC-path */ 110 /* Remove postfixed directory separator from HVSC-path */
103 tmpFilename = strrchr(xs_cfg.hvscPath, '/'); 111 tmpFilename = strrchr(xs_cfg.hvscPath, '/');
104 if (tmpFilename && tmpFilename[1] == 0) 112 if (tmpFilename && tmpFilename[1] == 0)
105 tmpFilename[0] = 0; 113 tmpFilename[0] = 0;
106 114
107 /* Remove HVSC location-prefix from filename */ 115 /* Remove HVSC location-prefix from filename */
108 tmpFilename = strstr(filename, xs_cfg.hvscPath); 116 tmpFilename = strstr(filename, xs_cfg.hvscPath);
109 if (tmpFilename) 117 if (tmpFilename != NULL)
110 tmpFilename += strlen(xs_cfg.hvscPath); 118 tmpFilename += strlen(xs_cfg.hvscPath);
111 else 119 else
112 tmpFilename = filename; 120 tmpFilename = filename;
113 } else 121 }
122 else
114 tmpFilename = filename; 123 tmpFilename = filename;
115 124
116 result = xs_stildb_get_node(xs_stildb_db, tmpFilename); 125 result = xs_stildb_get_node(xs_stildb_db, tmpFilename);
117 } else 126 }
127 else
118 result = NULL; 128 result = NULL;
119 129
120 XS_MUTEX_UNLOCK(xs_stildb_db); 130 XS_MUTEX_UNLOCK(xs_stildb_db);
121 XS_MUTEX_UNLOCK(xs_cfg); 131 XS_MUTEX_UNLOCK(xs_cfg);
122 132
126 136
127 /* Song length database handling glue 137 /* Song length database handling glue
128 */ 138 */
129 gint xs_songlen_init(void) 139 gint xs_songlen_init(void)
130 { 140 {
141 gint res = 0;
142
131 XS_MUTEX_LOCK(xs_cfg); 143 XS_MUTEX_LOCK(xs_cfg);
132 144 if (xs_cfg.songlenDBPath == NULL)
133 if (!xs_cfg.songlenDBPath) { 145 {
134 XS_MUTEX_UNLOCK(xs_cfg); 146 XS_MUTEX_UNLOCK(xs_cfg);
135 return -1; 147 return -1;
136 } 148 }
137 149
138 XS_MUTEX_LOCK(xs_sldb_db); 150 XS_MUTEX_LOCK(xs_sldb_db);
139 151
140 /* Check if already initialized */ 152 /* Check if already initialized */
141 if (xs_sldb_db) 153 if (xs_sldb_db != NULL)
142 xs_sldb_free(xs_sldb_db); 154 xs_sldb_free(xs_sldb_db);
143 155
144 /* Allocate database */ 156 /* Allocate database */
145 xs_sldb_db = (XSSLDB *) g_malloc0(sizeof(XSSLDB)); 157 xs_sldb_db = (XSSLDB *) g_malloc0(sizeof(XSSLDB));
146 if (!xs_sldb_db) { 158 if (xs_sldb_db == NULL)
147 XS_MUTEX_UNLOCK(xs_cfg); 159 {
148 XS_MUTEX_UNLOCK(xs_sldb_db); 160 res = -1;
149 return -2; 161 goto error;
150 } 162 }
151 163
152 /* Read the database */ 164 /* Read the database */
153 if (xs_sldb_read(xs_sldb_db, xs_cfg.songlenDBPath) != 0) { 165 if (xs_sldb_read(xs_sldb_db, xs_cfg.songlenDBPath) != 0)
154 xs_sldb_free(xs_sldb_db); 166 {
155 xs_sldb_db = NULL; 167 res = -3;
156 XS_MUTEX_UNLOCK(xs_cfg); 168 goto error;
157 XS_MUTEX_UNLOCK(xs_sldb_db);
158 return -3;
159 } 169 }
160 170
161 /* Create index */ 171 /* Create index */
162 if (xs_sldb_index(xs_sldb_db) != 0) { 172 if (xs_sldb_index(xs_sldb_db) != 0)
163 xs_sldb_free(xs_sldb_db); 173 {
164 xs_sldb_db = NULL; 174 res = -4;
165 XS_MUTEX_UNLOCK(xs_cfg); 175 goto error;
166 XS_MUTEX_UNLOCK(xs_sldb_db);
167 return -4;
168 } 176 }
169 177
170 XS_MUTEX_UNLOCK(xs_cfg); 178 XS_MUTEX_UNLOCK(xs_cfg);
171 XS_MUTEX_UNLOCK(xs_sldb_db); 179 XS_MUTEX_UNLOCK(xs_sldb_db);
172 return 0; 180 return 0;
181
182 error:
183 xs_sldb_free(xs_sldb_db);
184 xs_sldb_db = NULL;
185 XS_MUTEX_UNLOCK(xs_cfg);
186 XS_MUTEX_UNLOCK(xs_sldb_db);
187 return res;
173 } 188 }
174 189
175 190
176 void xs_songlen_close(void) 191 void xs_songlen_close(void)
177 { 192 {
211 XSSLDBNode *tmpLength; 226 XSSLDBNode *tmpLength;
212 gint i; 227 gint i;
213 228
214 /* Allocate structure */ 229 /* Allocate structure */
215 result = (XSTuneInfo *) g_malloc0(sizeof(XSTuneInfo)); 230 result = (XSTuneInfo *) g_malloc0(sizeof(XSTuneInfo));
216 if (!result) { 231 if (result == NULL)
232 {
217 xs_error("Could not allocate memory for tuneinfo ('%s')\n", 233 xs_error("Could not allocate memory for tuneinfo ('%s')\n",
218 filename); 234 filename);
219 return NULL; 235 return NULL;
220 } 236 }
221 237
222 result->sidFilename = XS_CS_FILENAME(filename); 238 if ((result->sidFilename = XS_CS_FILENAME(filename)) == NULL)
223 if (!result->sidFilename) { 239 {
224 xs_error("Could not allocate sidFilename ('%s')\n", 240 xs_error("Could not allocate sidFilename ('%s')\n",
225 filename); 241 filename);
226 g_free(result); 242 g_free(result);
227 return NULL; 243 return NULL;
228 } 244 }
229 245
230 /* Allocate space for subtune information */ 246 /* Allocate space for subtune information */
231 result->subTunes = g_malloc0(sizeof(XSSubTuneInfo) * (nsubTunes + 1)); 247 result->subTunes = g_malloc0(sizeof(XSSubTuneInfo) * (nsubTunes + 1));
232 if (!result->subTunes) { 248 if (!result->subTunes)
249 {
233 xs_error("Could not allocate memory for subtuneinfo ('%s', %i)\n", 250 xs_error("Could not allocate memory for subtuneinfo ('%s', %i)\n",
234 filename, nsubTunes); 251 filename, nsubTunes);
235 252
236 g_free(result->sidFilename); 253 g_free(result->sidFilename);
237 g_free(result); 254 g_free(result);
256 273
257 /* Get length information (NOTE: Do not free this!) */ 274 /* Get length information (NOTE: Do not free this!) */
258 tmpLength = xs_songlen_get(filename); 275 tmpLength = xs_songlen_get(filename);
259 276
260 /* Fill in sub-tune information */ 277 /* Fill in sub-tune information */
261 for (i = 0; i < result->nsubTunes; i++) { 278 for (i = 0; i < result->nsubTunes; i++)
262 if (tmpLength && (i < tmpLength->nlengths)) 279 {
263 result->subTunes[i].tuneLength = tmpLength->lengths[i]; 280 result->subTunes[i].tuneLength =
264 else 281 (tmpLength && (i < tmpLength->nlengths)) ? tmpLength->lengths[i] : -1;
265 result->subTunes[i].tuneLength = -1;
266
267 result->subTunes[i].tuneSpeed = -1; 282 result->subTunes[i].tuneSpeed = -1;
268 } 283 }
269 284
270 return result; 285 return result;
271 } 286 }
273 288
274 /* Free given tune information structure 289 /* Free given tune information structure
275 */ 290 */
276 void xs_tuneinfo_free(XSTuneInfo * tune) 291 void xs_tuneinfo_free(XSTuneInfo * tune)
277 { 292 {
278 if (!tune) return; 293 if (tune == NULL)
294 return;
279 295
280 g_free(tune->subTunes); 296 g_free(tune->subTunes);
281 g_free(tune->sidFilename); 297 g_free(tune->sidFilename);
282 g_free(tune->sidName); 298 g_free(tune->sidName);
283 g_free(tune->sidComposer); 299 g_free(tune->sidComposer);