PageRenderTime 49ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/libsndfile/libsndfile-1.0.21/regtest/database.c

https://bitbucket.org/thelearninglabs/uclinux-distro-tll-public
C | 495 lines | 360 code | 106 blank | 29 comment | 76 complexity | 73b1ef2140914665e53aa7976f5af0bd MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-3.0, Unlicense, GPL-2.0, GPL-3.0, CC-BY-SA-3.0, AGPL-1.0, ISC, MIT, 0BSD, LGPL-2.0
  1. /*
  2. ** Copyright (C) 2005-2009 Erik de Castro Lopo
  3. **
  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.
  8. **
  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.
  13. **
  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.
  17. */
  18. #include "config.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <string.h>
  23. #include <fcntl.h>
  24. #include <sys/stat.h>
  25. #include <sndfile.h>
  26. #include "regtest.h"
  27. #if HAVE_SQLITE3
  28. #include <sqlite3.h>
  29. typedef struct
  30. { sqlite3 *sql ;
  31. int count ;
  32. int ekey_max ;
  33. /* Filename and pathname for file. */
  34. char filename [256] ;
  35. char pathname [512] ;
  36. /* Storage for createding SQL commands. Must be larger than logbuf below. */
  37. char cmdbuf [1 << 15] ;
  38. /* Storage for log buffer retrieved from SNDFILE* .*/
  39. char logbuf [1 << 14] ;
  40. } REGTEST_DB ;
  41. /* In checksum.c */
  42. int calc_checksum (SNDFILE * file, const SF_INFO * info) ;
  43. static void get_filename_pathname (REGTEST_DB * db, const char *filepath) ;
  44. static void single_quote_replace (char * buf) ;
  45. static int get_ekey_from_filename (REGTEST_DB * db, const char *filepath) ;
  46. static int get_filename_pathname_by_ekey (REGTEST_DB * db, int ekey) ;
  47. static int check_file_by_ekey (REGTEST_DB * db, int ekey) ;
  48. static int count_callback (REGTEST_DB * db, int argc, char **argv, char **colname) ;
  49. static int ekey_max_callback (REGTEST_DB * db, int argc, char **argv, char **colname) ;
  50. static int callback (void *unused, int argc, char **argv, char **colname) ;
  51. REG_DB *
  52. db_open (const char * db_name)
  53. { REGTEST_DB * db ;
  54. int err ;
  55. if ((db = malloc (sizeof (REGTEST_DB))) == NULL)
  56. { perror ("malloc") ;
  57. exit (1) ;
  58. } ;
  59. if ((err = sqlite3_open (db_name, &(db->sql))) != 0)
  60. { printf ("Can't open database: %s\n", sqlite3_errmsg (db->sql)) ;
  61. sqlite3_close (db->sql) ;
  62. free (db) ;
  63. exit (1) ;
  64. } ;
  65. return (REG_DB *) db ;
  66. } /* db_open */
  67. int
  68. db_create (const char * db_name)
  69. { REGTEST_DB * db ;
  70. const char *cmd ;
  71. char * errmsg = NULL ;
  72. int err ;
  73. db = (REGTEST_DB *) db_open (db_name) ;
  74. cmd = "create table sndfile (ekey INTEGER PRIMARY KEY,"
  75. "fname VARCHAR(1),"
  76. "fpath VARCHAR(1),"
  77. "srate INTEGER,"
  78. "frames VARCHAR(1),"
  79. "channels INTEGER,"
  80. "format VARCHAR(1),"
  81. "checksum VARCHAR(1),"
  82. "logbuf VARCHAR(1)"
  83. ");" ;
  84. err = sqlite3_exec (db->sql, cmd, callback, 0, &errmsg) ;
  85. if (err != SQLITE_OK)
  86. printf ("Line %d : SQL error: %s\n", __LINE__, errmsg) ;
  87. sqlite3_close (db->sql) ;
  88. free (db) ;
  89. return 0 ;
  90. } /* db_create */
  91. int
  92. db_close (REG_DB * db_handle)
  93. { REGTEST_DB * db ;
  94. db = (REGTEST_DB *) db_handle ;
  95. sqlite3_close (db->sql) ;
  96. free (db) ;
  97. return 0 ;
  98. } /* db_close */
  99. /*==============================================================================
  100. */
  101. int
  102. db_file_exists (REG_DB * db_handle, const char * filename)
  103. { REGTEST_DB * db ;
  104. const char * cptr ;
  105. char * errmsg ;
  106. int err ;
  107. db = (REGTEST_DB *) db_handle ;
  108. if ((cptr = strrchr (filename, '/')) != NULL)
  109. filename = cptr + 1 ;
  110. snprintf (db->cmdbuf, sizeof (db->cmdbuf), "select fname from sndfile where fname='%s'", filename) ;
  111. db->count = 0 ;
  112. err = sqlite3_exec (db->sql, db->cmdbuf, (sqlite3_callback) count_callback, db, &errmsg) ;
  113. if (db->count == 1)
  114. return 1 ;
  115. return 0 ;
  116. } /* db_file_exists */
  117. int
  118. db_add_file (REG_DB * db_handle, const char * filepath)
  119. { REGTEST_DB * db ;
  120. SNDFILE * sndfile ;
  121. SF_INFO info ;
  122. char * errmsg ;
  123. int err, checksum ;
  124. db = (REGTEST_DB *) db_handle ;
  125. get_filename_pathname (db, filepath) ;
  126. if (db_file_exists (db_handle, filepath))
  127. { printf (" %s : already in database\n", db->filename) ;
  128. return 0 ;
  129. } ;
  130. memset (&info, 0, sizeof (info)) ;
  131. sndfile = sf_open (db->pathname, SFM_READ, &info) ;
  132. sf_command (sndfile, SFC_GET_LOG_INFO, db->logbuf, sizeof (db->logbuf)) ;
  133. checksum = (sndfile == NULL) ? 0 : calc_checksum (sndfile, &info) ;
  134. sf_close (sndfile) ;
  135. if (sndfile == NULL)
  136. { printf (" %s : could not open : %s\n", db->filename, sf_strerror (NULL)) ;
  137. puts (db->logbuf) ;
  138. return 1 ;
  139. } ;
  140. single_quote_replace (db->logbuf) ;
  141. snprintf (db->cmdbuf, sizeof (db->cmdbuf), "insert into sndfile "
  142. "(fname, fpath, srate, frames, channels, format, checksum, logbuf) values"
  143. "('%s','%s',%d,'%ld', %d, '0x%08x', '0x%08x', '%s');",
  144. db->filename, db->pathname, info.samplerate, (long) info.frames, info.channels, info.format, checksum, db->logbuf) ;
  145. if (strlen (db->cmdbuf) >= sizeof (db->cmdbuf) - 1)
  146. { printf ("strlen (db->cmdbuf) too long.\n") ;
  147. exit (1) ;
  148. } ;
  149. err = sqlite3_exec (db->sql, db->cmdbuf, callback, 0, &errmsg) ;
  150. if (err != SQLITE_OK)
  151. { printf ("Line %d : SQL error: %s\n", __LINE__, errmsg) ;
  152. puts (db->cmdbuf) ;
  153. } ;
  154. return 0 ;
  155. } /* db_add_file */
  156. int
  157. db_check_file (REG_DB * db_handle, const char * filepath)
  158. { REGTEST_DB * db ;
  159. int ekey ;
  160. if (db_file_exists (db_handle, filepath) == 0)
  161. { printf ("\nFile not in database.\n\n") ;
  162. exit (0) ;
  163. } ;
  164. db = (REGTEST_DB *) db_handle ;
  165. ekey = get_ekey_from_filename (db, filepath) ;
  166. return check_file_by_ekey (db, ekey) ;
  167. } /* db_check_file */
  168. /*==============================================================================
  169. */
  170. int
  171. db_check_all (REG_DB * db_handle)
  172. { REGTEST_DB * db ;
  173. char * errmsg ;
  174. int err, ekey ;
  175. db = (REGTEST_DB *) db_handle ;
  176. db->ekey_max = 0 ;
  177. snprintf (db->cmdbuf, sizeof (db->cmdbuf), "select ekey from sndfile") ;
  178. err = sqlite3_exec (db->sql, db->cmdbuf, (sqlite3_callback) ekey_max_callback, db, &errmsg) ;
  179. if (err != SQLITE_OK)
  180. { printf ("Line %d : SQL error: %s\n", __LINE__, errmsg) ;
  181. puts (db->cmdbuf) ;
  182. } ;
  183. for (ekey = 1 ; ekey <= db->ekey_max ; ekey++)
  184. if (get_filename_pathname_by_ekey (db, ekey) != 0)
  185. check_file_by_ekey (db, ekey) ;
  186. return 0 ;
  187. } /* db_check_all */
  188. int
  189. db_list_all (REG_DB * db_handle)
  190. {
  191. printf ("%s : %p\n", __func__, db_handle) ;
  192. return 0 ;
  193. } /* db_list_all */
  194. int
  195. db_del_entry (REG_DB * db_handle, const char * entry)
  196. {
  197. printf ("%s : %p %s\n", __func__, db_handle, entry) ;
  198. return 0 ;
  199. } /* db_del_entry */
  200. /*==============================================================================
  201. */
  202. static int
  203. get_ekey_from_filename (REGTEST_DB * db, const char *filepath)
  204. { char * errmsg, **result ;
  205. int err, ekey = 0, rows, cols ;
  206. get_filename_pathname (db, filepath) ;
  207. snprintf (db->cmdbuf, sizeof (db->cmdbuf), "select ekey from sndfile where fname='%s'", db->filename) ;
  208. err = sqlite3_get_table (db->sql, db->cmdbuf, &result, &rows, &cols, &errmsg) ;
  209. if (err != SQLITE_OK)
  210. { printf ("Line %d : SQL error: %s\n", __LINE__, errmsg) ;
  211. puts (db->cmdbuf) ;
  212. } ;
  213. if (cols != 1 || rows != 1)
  214. { printf ("Bad juju!! rows = %d cols = %d\n", rows, cols) ;
  215. exit (1) ;
  216. } ;
  217. ekey = strtol (result [1], NULL, 10) ;
  218. sqlite3_free_table (result) ;
  219. return ekey ;
  220. } /* get_ekey_from_filename */
  221. static int
  222. get_filename_pathname_by_ekey (REGTEST_DB * db, int ekey)
  223. { char *errmsg, **result ;
  224. int err, rows, cols ;
  225. snprintf (db->cmdbuf, sizeof (db->cmdbuf), "select fname,fpath from sndfile where ekey='%d'", ekey) ;
  226. err = sqlite3_get_table (db->sql, db->cmdbuf, &result, &rows, &cols, &errmsg) ;
  227. if (err != SQLITE_OK)
  228. { printf ("Line %d : SQL error: %s\n", __LINE__, errmsg) ;
  229. puts (db->cmdbuf) ;
  230. return 0 ;
  231. } ;
  232. if (cols != 2 || rows != 1)
  233. { printf ("\nError (%s %d) : rows = %d cols = %d\n", __func__, __LINE__, rows, cols) ;
  234. exit (1) ;
  235. } ;
  236. snprintf (db->filename, sizeof (db->filename), "%s", result [2]) ;
  237. snprintf (db->pathname, sizeof (db->pathname), "%s", result [3]) ;
  238. sqlite3_free_table (result) ;
  239. return 1 ;
  240. } /* get_filename_pathname_by_ekey */
  241. static int
  242. check_file_by_ekey (REGTEST_DB * db, int ekey)
  243. { SNDFILE * sndfile ;
  244. SF_INFO info ;
  245. char * errmsg, **result ;
  246. int err, k, rows, cols, checksum ;
  247. printf (" %s : ", db->filename) ;
  248. fflush (stdout) ;
  249. memset (&info, 0, sizeof (info)) ;
  250. sndfile = sf_open (db->pathname, SFM_READ, &info) ;
  251. sf_command (sndfile, SFC_GET_LOG_INFO, db->logbuf, sizeof (db->logbuf)) ;
  252. checksum = (sndfile == NULL) ? 0 : calc_checksum (sndfile, &info) ;
  253. sf_close (sndfile) ;
  254. if (sndfile == NULL)
  255. { printf ("\n\nError : Could not open '%s' : %s\n", db->pathname, sf_strerror (NULL)) ;
  256. puts (db->logbuf) ;
  257. exit (1) ;
  258. } ;
  259. single_quote_replace (db->logbuf) ;
  260. snprintf (db->cmdbuf, sizeof (db->cmdbuf), "select fname,srate,frames,channels,format,"
  261. "checksum,logbuf from sndfile where ekey='%d'", ekey) ;
  262. err = sqlite3_get_table (db->sql, db->cmdbuf, &result, &rows, &cols, &errmsg) ;
  263. if (err != SQLITE_OK)
  264. { printf ("Line %d : SQL error: %s\n", __LINE__, errmsg) ;
  265. puts (db->cmdbuf) ;
  266. } ;
  267. for (k = 0 ; k < cols ; k++)
  268. { if (strcmp (result [k], "fname") == 0)
  269. { if (strcmp (result [k + cols], db->filename) == 0)
  270. continue ;
  271. printf ("\n\nError : fname doesn't match : %s != %s\n", result [k + cols], db->filename) ;
  272. } ;
  273. if (strcmp (result [k], "srate") == 0)
  274. { if (strtol (result [k + cols], NULL, 10) == info.samplerate)
  275. continue ;
  276. printf ("\n\nError : srate doesn't match : %s == %d\n", result [k + cols], info.samplerate) ;
  277. } ;
  278. if (strcmp (result [k], "frames") == 0)
  279. { if (strtoll (result [k + cols], NULL, 10) == info.frames)
  280. continue ;
  281. printf ("\n\nError : frames doesn't match : %s == %ld\n", result [k + cols], (long) info.frames) ;
  282. } ;
  283. if (strcmp (result [k], "channels") == 0)
  284. { if (strtol (result [k + cols], NULL, 10) == info.channels)
  285. continue ;
  286. printf ("\n\nError : channels doesn't match : %s == %d\n", result [k + cols], info.channels) ;
  287. } ;
  288. if (strcmp (result [k], "format") == 0)
  289. { if (strtol (result [k + cols], NULL, 16) == info.format)
  290. continue ;
  291. printf ("\n\nError : format doesn't match : %s == 0x%08x\n", result [k + cols], info.format) ;
  292. } ;
  293. if (strcmp (result [k], "checksum") == 0)
  294. { int db_val = (int) strtoll (result [k + cols], NULL, 16) ;
  295. if (db_val == checksum)
  296. continue ;
  297. printf ("\n\nError : checksum doesn't match : 0x%08x == 0x%08x\n", db_val, checksum) ;
  298. } ;
  299. if (strcmp (result [k], "logbuf") == 0)
  300. continue ;
  301. printf ("\nHere is the old logubuffer :\n\n%s\n\nand the new :\n\n%s\n\n", result [2 * cols - 1], db->logbuf) ;
  302. exit (1) ;
  303. } ;
  304. sqlite3_free_table (result) ;
  305. puts ("ok") ;
  306. return 0 ;
  307. } /* check_file_by_ekey */
  308. /*==============================================================================
  309. */
  310. static void
  311. get_filename_pathname (REGTEST_DB * db, const char *filepath)
  312. { const char * cptr ;
  313. int slen ;
  314. if (filepath [0] != '/')
  315. { memset (db->pathname, 0, sizeof (db->pathname)) ;
  316. if (getcwd (db->pathname, sizeof (db->pathname)) == NULL)
  317. { perror ("\ngetcwd failed") ;
  318. exit (1) ;
  319. } ;
  320. slen = strlen (db->pathname) ;
  321. db->pathname [slen ++] = '/' ;
  322. snprintf (db->pathname + slen, sizeof (db->pathname) - slen, "%s", filepath) ;
  323. }
  324. else
  325. snprintf (db->pathname, sizeof (db->pathname), "%s", filepath) ;
  326. if ((cptr = strrchr (db->pathname, '/')) == NULL)
  327. { printf ("\nError : bad pathname %s\n", filepath) ;
  328. exit (1) ;
  329. } ;
  330. snprintf (db->filename, sizeof (db->filename), "%s", cptr + 1) ;
  331. } /* get filename_pathname */
  332. static void
  333. single_quote_replace (char * buf)
  334. { while ((buf = strchr (buf, '\'')) != 0)
  335. buf [0] = '"' ;
  336. } /* single_quote_replace */
  337. static int
  338. count_callback (REGTEST_DB * db, int argc, char **argv, char **colname)
  339. { db->count ++ ;
  340. (void) argc ;
  341. (void) argv ;
  342. (void) colname ;
  343. return 0 ;
  344. } /* count_callback */
  345. static int
  346. ekey_max_callback (REGTEST_DB * db, int argc, char **argv, char **unused)
  347. { int ekey ;
  348. (void) argc ;
  349. (void) unused ;
  350. ekey = strtol (argv [0], NULL, 10) ;
  351. if (ekey > db->ekey_max)
  352. db->ekey_max = ekey ;
  353. return 0 ;
  354. } /* ekey_max_callback */
  355. static int
  356. callback (void *unused, int argc, char **argv, char **colname)
  357. { int k ;
  358. (void) unused ;
  359. for (k = 0 ; k < argc ; k++)
  360. printf ("%s = %s\n", colname [k], argv [k] ? argv [k] : "NULL") ;
  361. printf ("\n") ;
  362. return 0 ;
  363. } /* callback */
  364. #else
  365. int dummy (void) ;
  366. int
  367. dummy (void)
  368. { /*
  369. ** Empty dummy fnction so tha compiler doesn't winge about an
  370. ** empty file.
  371. */
  372. return 0 ;
  373. } /* dummy */
  374. #endif