sfdir.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (c) 1983-2013 Martin Atkins, Richard Dobson and Composers Desktop Project Ltd
  3. * http://people.bath.ac.uk/masrwd
  4. * http://www.composersdesktop.com
  5. *
  6. This file is part of the CDP System.
  7. The CDP System is free software; you can redistribute it
  8. and/or modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2.1 of the License, or (at your option) any later version.
  11. The CDP System 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 Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with the CDP System; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  18. 02111-1307 USA
  19. *
  20. */
  21. /*
  22. * portable sfsys - Unix version of sfdir, etc
  23. */
  24. /*
  25. * $Log: sfdir.c%v $
  26. * Revision 1.1 1994/10/31 15:41:38 martin
  27. * Initial revision
  28. *
  29. */
  30. /*RWD OCT97: rebuild under CDP97 to recognise *.ana as analysis file*/
  31. /* RWD Dec 2009 restored fix to fmt info code! NB some sf_direct fields now int or unsigned int*/
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <ctype.h>
  35. #include <osbind.h>
  36. #include <sfsys.h> /*RWD: don't want local copies of this!*/
  37. #include "sffuncs.h"
  38. #if defined(_SGI_SOURCE) || defined unix
  39. #include <dirent.h>
  40. #else
  41. #include "scandir.h"
  42. #endif
  43. #if defined WIN32 || defined linux
  44. static int
  45. selfn(const struct dirent *d)
  46. #else
  47. static int
  48. selfn(struct dirent *d)
  49. #endif
  50. {
  51. char *dotp = strrchr(d->d_name, '.');
  52. if(dotp == 0)
  53. return 0;
  54. if(_stricmp(dotp, ".aif") == 0
  55. ||_stricmp(dotp, ".aiff") == 0
  56. ||_stricmp(dotp, ".wav") == 0
  57. #ifdef CDP97
  58. /*RWD.1.99 add AIFC - just how many xtensions can there be?*/
  59. || _stricmp(dotp,".afc") == 0 /*official Apple*/
  60. || _stricmp(dotp,".aifc") ==0 /* unix?*/
  61. || _stricmp(dotp,".aic") == 0 /* lots of other people...*/
  62. #ifdef FILE_AMB_SUPPORT
  63. ||_stricmp(dotp, ".amb") == 0
  64. ||_stricmp(dotp, ".wxyz") == 0
  65. #endif
  66. /*RWD OCT97;JULY98: recognise TW filetypes*/
  67. || _stricmp(dotp,".ana") == 0
  68. || _stricmp(dotp,".frq") == 0
  69. || _stricmp(dotp,".fmt") == 0 /* lose this in time */
  70. || _stricmp(dotp,".for") == 0
  71. || _stricmp(dotp,".env") == 0 /* lose this in time */
  72. || _stricmp(dotp,".evl") == 0
  73. || _stricmp(dotp,".trn") == 0
  74. || _stricmp(dotp,".lnk") == 0 /*RWD98 VERY experimental ; may be wrong place to do it, or wrong way...*/
  75. #endif
  76. )
  77. return 1;
  78. return 0;
  79. }
  80. int
  81. sfdir(int SFCALLS (*func)(struct sf_direct *filedetails), int flags)
  82. {
  83. struct dirent **filelist = 0;
  84. int numfiles, file;
  85. int fdes;
  86. int rc = SFDIR_NOTFOUND;
  87. struct sf_direct dir;
  88. if((numfiles = scandir(".", &filelist, selfn, alphasort)) < 0)
  89. return SFDIR_ERROR;
  90. if(numfiles == 0)
  91. return SFDIR_NOTFOUND;
  92. if(filelist == 0)
  93. abort(); /*RWD: ouch!*/
  94. for(file = 0; file < numfiles; file++) {
  95. struct dirent *f = filelist[file];
  96. if((fdes = sfopenEx(f->d_name,CDP_OPEN_RDONLY)) >= 0){
  97. dir.flags = 0;
  98. dir.length = (unsigned int) sfsize(fdes); /*RWD 2007 */
  99. dir.index = 0;
  100. dir.seclen = dir.length>>LOGSECSIZE;
  101. #if defined CDP97 && defined _WIN32
  102. dir.is_shortcut = sf_is_shortcut(fdes,dir.targetname);
  103. if(!dir.is_shortcut)
  104. dir.targetname[0] = '\0';
  105. #endif
  106. sfformat(fdes,&(dir.fmt));
  107. strncpy(dir.name, f->d_name, MAXSFNAME-1);
  108. dir.name[MAXSFNAME-1] = '\0';
  109. if(sfgetprop(fdes, "sample type", (char *)&dir.samptype, sizeof(int)) != sizeof(int))
  110. dir.samptype = -1;
  111. if(sfgetprop(fdes, "sample rate", (char *)&dir.samprate, sizeof(int)) != sizeof(int))
  112. dir.samprate = -1;
  113. if(sfgetprop(fdes, "channels", (char *)&dir.nochans, sizeof(int)) != sizeof(int))
  114. dir.nochans = -1;
  115. #ifdef CDP97
  116. dir.origwordsize = sfgetwordsize(fdes);
  117. if(dir.origwordsize==8)
  118. dir.length /= 2;
  119. #endif
  120. sfclose(fdes);
  121. if(func(&dir)) {
  122. rc = SFDIR_FOUND;
  123. break;
  124. }
  125. } else {
  126. if(sferrno() != ESFNOTFOUND) {
  127. rc = SFDIR_ERROR;
  128. /*RWD.9.98 for dirsf*/
  129. dir.fmt = FMT_UNKNOWN;
  130. /*break;*/
  131. }
  132. dir.flags = SFFLG_IOERROR;
  133. dir.length = 0xffffffff; /* RWD Aug 2009: unsigned equiv of -1 . need to improve on this system ere long */
  134. dir.index = 0;
  135. dir.seclen = -1;
  136. strncpy(dir.name, f->d_name, MAXSFNAME-1);
  137. dir.name[MAXSFNAME-1] = '\0';
  138. dir.samptype = -1;
  139. dir.samprate = -1;
  140. dir.nochans = -1;
  141. if(func(&dir)) {
  142. rc = SFDIR_FOUND;
  143. break;
  144. }
  145. }
  146. }
  147. for(file = 0; file < numfiles; file++)
  148. free(filelist[file]);
  149. free(filelist);
  150. return rc;
  151. }
  152. int
  153. rdiskcfg(struct rdskcfg *cfg)
  154. {
  155. cfg->disksize = 1024*1024; /* we lie! */
  156. cfg->npart = 1;
  157. cfg->pinfo[0].unit_no = 0;
  158. cfg->pinfo[0].phys_start = 0;
  159. cfg->pinfo[0].sf_start = 0;
  160. cfg->pinfo[0].sf_end = 1024*1024-1;
  161. return 0;
  162. }