sfdir.c 5.5 KB

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