2 ** Copyright (C) 2005-2009 Erik de Castro Lopo
4 ** This program is free software; you can redistribute it and/or modify
5 ** it under the terms of the GNU General Public License as published by
6 ** the Free Software Foundation; either version 2 of the License, or
7 ** (at your option) any later version.
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ** GNU General Public License for more details.
14 ** You should have received a copy of the GNU General Public License
15 ** along with this program; if not, write to the Free Software
16 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
42 /* Filename and pathname for file. */
46 /* Storage for createding SQL commands. Must be larger than logbuf below. */
47 char cmdbuf [1 << 15] ;
49 /* Storage for log buffer retrieved from SNDFILE* .*/
50 char logbuf [1 << 14] ;
55 int calc_checksum (SNDFILE * file, const SF_INFO * info) ;
57 static void get_filename_pathname (REGTEST_DB * db, const char *filepath) ;
58 static void single_quote_replace (char * buf) ;
60 static int get_ekey_from_filename (REGTEST_DB * db, const char *filepath) ;
61 static int get_filename_pathname_by_ekey (REGTEST_DB * db, int ekey) ;
62 static int check_file_by_ekey (REGTEST_DB * db, int ekey) ;
64 static int count_callback (REGTEST_DB * db, int argc, char **argv, char **colname) ;
65 static int ekey_max_callback (REGTEST_DB * db, int argc, char **argv, char **colname) ;
66 static int callback (void *unused, int argc, char **argv, char **colname) ;
69 db_open (const char * db_name)
73 if ((db = malloc (sizeof (REGTEST_DB))) == NULL)
78 if ((err = sqlite3_open (db_name, &(db->sql))) != 0)
79 { printf ("Can't open database: %s\n", sqlite3_errmsg (db->sql)) ;
80 sqlite3_close (db->sql) ;
85 return (REG_DB *) db ;
89 db_create (const char * db_name)
92 char * errmsg = NULL ;
95 db = (REGTEST_DB *) db_open (db_name) ;
97 cmd = "create table sndfile (ekey INTEGER PRIMARY KEY,"
104 "checksum VARCHAR(1),"
108 err = sqlite3_exec (db->sql, cmd, callback, 0, &errmsg) ;
109 if (err != SQLITE_OK)
110 printf ("Line %d : SQL error: %s\n", __LINE__, errmsg) ;
112 sqlite3_close (db->sql) ;
119 db_close (REG_DB * db_handle)
122 db = (REGTEST_DB *) db_handle ;
124 sqlite3_close (db->sql) ;
130 /*==============================================================================
134 db_file_exists (REG_DB * db_handle, const char * filename)
140 db = (REGTEST_DB *) db_handle ;
142 if ((cptr = strrchr (filename, '/')) != NULL)
143 filename = cptr + 1 ;
145 snprintf (db->cmdbuf, sizeof (db->cmdbuf), "select fname from sndfile where fname='%s'", filename) ;
148 err = sqlite3_exec (db->sql, db->cmdbuf, (sqlite3_callback) count_callback, db, &errmsg) ;
153 } /* db_file_exists */
156 db_add_file (REG_DB * db_handle, const char * filepath)
163 db = (REGTEST_DB *) db_handle ;
165 get_filename_pathname (db, filepath) ;
167 if (db_file_exists (db_handle, filepath))
168 { printf (" %s : already in database\n", db->filename) ;
172 memset (&info, 0, sizeof (info)) ;
173 sndfile = sf_open (db->pathname, SFM_READ, &info) ;
174 sf_command (sndfile, SFC_GET_LOG_INFO, db->logbuf, sizeof (db->logbuf)) ;
175 checksum = (sndfile == NULL) ? 0 : calc_checksum (sndfile, &info) ;
179 { printf (" %s : could not open : %s\n", db->filename, sf_strerror (NULL)) ;
184 single_quote_replace (db->logbuf) ;
186 snprintf (db->cmdbuf, sizeof (db->cmdbuf), "insert into sndfile "
187 "(fname, fpath, srate, frames, channels, format, checksum, logbuf) values"
188 "('%s','%s',%d,'%ld', %d, '0x%08x', '0x%08x', '%s');",
189 db->filename, db->pathname, info.samplerate, (long) info.frames, info.channels, info.format, checksum, db->logbuf) ;
191 if (strlen (db->cmdbuf) >= sizeof (db->cmdbuf) - 1)
192 { printf ("strlen (db->cmdbuf) too long.\n") ;
196 err = sqlite3_exec (db->sql, db->cmdbuf, callback, 0, &errmsg) ;
197 if (err != SQLITE_OK)
198 { printf ("Line %d : SQL error: %s\n", __LINE__, errmsg) ;
206 db_check_file (REG_DB * db_handle, const char * filepath)
210 if (db_file_exists (db_handle, filepath) == 0)
211 { printf ("\nFile not in database.\n\n") ;
215 db = (REGTEST_DB *) db_handle ;
217 ekey = get_ekey_from_filename (db, filepath) ;
219 return check_file_by_ekey (db, ekey) ;
220 } /* db_check_file */
222 /*==============================================================================
226 db_check_all (REG_DB * db_handle)
231 db = (REGTEST_DB *) db_handle ;
235 snprintf (db->cmdbuf, sizeof (db->cmdbuf), "select ekey from sndfile") ;
237 err = sqlite3_exec (db->sql, db->cmdbuf, (sqlite3_callback) ekey_max_callback, db, &errmsg) ;
238 if (err != SQLITE_OK)
239 { printf ("Line %d : SQL error: %s\n", __LINE__, errmsg) ;
243 for (ekey = 1 ; ekey <= db->ekey_max ; ekey++)
244 if (get_filename_pathname_by_ekey (db, ekey) != 0)
245 check_file_by_ekey (db, ekey) ;
252 db_list_all (REG_DB * db_handle)
254 printf ("%s : %p\n", __func__, db_handle) ;
259 db_del_entry (REG_DB * db_handle, const char * entry)
261 printf ("%s : %p %s\n", __func__, db_handle, entry) ;
265 /*==============================================================================
269 get_ekey_from_filename (REGTEST_DB * db, const char *filepath)
270 { char * errmsg, **result ;
271 int err, ekey = 0, rows, cols ;
273 get_filename_pathname (db, filepath) ;
275 snprintf (db->cmdbuf, sizeof (db->cmdbuf), "select ekey from sndfile where fname='%s'", db->filename) ;
277 err = sqlite3_get_table (db->sql, db->cmdbuf, &result, &rows, &cols, &errmsg) ;
278 if (err != SQLITE_OK)
279 { printf ("Line %d : SQL error: %s\n", __LINE__, errmsg) ;
283 if (cols != 1 || rows != 1)
284 { printf ("Bad juju!! rows = %d cols = %d\n", rows, cols) ;
288 ekey = strtol (result [1], NULL, 10) ;
290 sqlite3_free_table (result) ;
293 } /* get_ekey_from_filename */
296 get_filename_pathname_by_ekey (REGTEST_DB * db, int ekey)
297 { char *errmsg, **result ;
298 int err, rows, cols ;
300 snprintf (db->cmdbuf, sizeof (db->cmdbuf), "select fname,fpath from sndfile where ekey='%d'", ekey) ;
302 err = sqlite3_get_table (db->sql, db->cmdbuf, &result, &rows, &cols, &errmsg) ;
303 if (err != SQLITE_OK)
304 { printf ("Line %d : SQL error: %s\n", __LINE__, errmsg) ;
309 if (cols != 2 || rows != 1)
310 { printf ("\nError (%s %d) : rows = %d cols = %d\n", __func__, __LINE__, rows, cols) ;
314 snprintf (db->filename, sizeof (db->filename), "%s", result [2]) ;
315 snprintf (db->pathname, sizeof (db->pathname), "%s", result [3]) ;
317 sqlite3_free_table (result) ;
320 } /* get_filename_pathname_by_ekey */
323 check_file_by_ekey (REGTEST_DB * db, int ekey)
324 { SNDFILE * sndfile ;
326 char * errmsg, **result ;
327 int err, k, rows, cols, checksum ;
329 printf (" %s : ", db->filename) ;
332 memset (&info, 0, sizeof (info)) ;
333 sndfile = sf_open (db->pathname, SFM_READ, &info) ;
334 sf_command (sndfile, SFC_GET_LOG_INFO, db->logbuf, sizeof (db->logbuf)) ;
335 checksum = (sndfile == NULL) ? 0 : calc_checksum (sndfile, &info) ;
339 { printf ("\n\nError : Could not open '%s' : %s\n", db->pathname, sf_strerror (NULL)) ;
344 single_quote_replace (db->logbuf) ;
346 snprintf (db->cmdbuf, sizeof (db->cmdbuf), "select fname,srate,frames,channels,format,"
347 "checksum,logbuf from sndfile where ekey='%d'", ekey) ;
349 err = sqlite3_get_table (db->sql, db->cmdbuf, &result, &rows, &cols, &errmsg) ;
350 if (err != SQLITE_OK)
351 { printf ("Line %d : SQL error: %s\n", __LINE__, errmsg) ;
355 for (k = 0 ; k < cols ; k++)
356 { if (strcmp (result [k], "fname") == 0)
357 { if (strcmp (result [k + cols], db->filename) == 0)
359 printf ("\n\nError : fname doesn't match : %s != %s\n", result [k + cols], db->filename) ;
362 if (strcmp (result [k], "srate") == 0)
363 { if (strtol (result [k + cols], NULL, 10) == info.samplerate)
365 printf ("\n\nError : srate doesn't match : %s == %d\n", result [k + cols], info.samplerate) ;
368 if (strcmp (result [k], "frames") == 0)
369 { if (strtoll (result [k + cols], NULL, 10) == info.frames)
371 printf ("\n\nError : frames doesn't match : %s == %ld\n", result [k + cols], (long) info.frames) ;
374 if (strcmp (result [k], "channels") == 0)
375 { if (strtol (result [k + cols], NULL, 10) == info.channels)
377 printf ("\n\nError : channels doesn't match : %s == %d\n", result [k + cols], info.channels) ;
380 if (strcmp (result [k], "format") == 0)
381 { if (strtol (result [k + cols], NULL, 16) == info.format)
383 printf ("\n\nError : format doesn't match : %s == 0x%08x\n", result [k + cols], info.format) ;
386 if (strcmp (result [k], "checksum") == 0)
387 { int db_val = (int) strtoll (result [k + cols], NULL, 16) ;
389 if (db_val == checksum)
391 printf ("\n\nError : checksum doesn't match : 0x%08x == 0x%08x\n", db_val, checksum) ;
394 if (strcmp (result [k], "logbuf") == 0)
397 printf ("\nHere is the old logubuffer :\n\n%s\n\nand the new :\n\n%s\n\n", result [2 * cols - 1], db->logbuf) ;
401 sqlite3_free_table (result) ;
406 } /* check_file_by_ekey */
408 /*==============================================================================
412 get_filename_pathname (REGTEST_DB * db, const char *filepath)
413 { const char * cptr ;
416 if (filepath [0] != '/')
417 { memset (db->pathname, 0, sizeof (db->pathname)) ;
418 if (getcwd (db->pathname, sizeof (db->pathname)) == NULL)
419 { perror ("\ngetcwd failed") ;
423 slen = strlen (db->pathname) ;
424 db->pathname [slen ++] = '/' ;
425 snprintf (db->pathname + slen, sizeof (db->pathname) - slen, "%s", filepath) ;
428 snprintf (db->pathname, sizeof (db->pathname), "%s", filepath) ;
430 if ((cptr = strrchr (db->pathname, '/')) == NULL)
431 { printf ("\nError : bad pathname %s\n", filepath) ;
435 snprintf (db->filename, sizeof (db->filename), "%s", cptr + 1) ;
436 } /* get filename_pathname */
439 single_quote_replace (char * buf)
440 { while ((buf = strchr (buf, '\'')) != 0)
442 } /* single_quote_replace */
445 count_callback (REGTEST_DB * db, int argc, char **argv, char **colname)
452 } /* count_callback */
455 ekey_max_callback (REGTEST_DB * db, int argc, char **argv, char **unused)
461 ekey = strtol (argv [0], NULL, 10) ;
462 if (ekey > db->ekey_max)
463 db->ekey_max = ekey ;
466 } /* ekey_max_callback */
469 callback (void *unused, int argc, char **argv, char **colname)
474 for (k = 0 ; k < argc ; k++)
475 printf ("%s = %s\n", colname [k], argv [k] ? argv [k] : "NULL") ;
489 ** Empty dummy fnction so tha compiler doesn't winge about an