run.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
  5. * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE. *
  6. * PLEASE READ THESE TERMS DISTRIBUTING. *
  7. * *
  8. * THE OggSQUISH SOURCE CODE IS (C) COPYRIGHT 1994-2000 *
  9. * by Monty <monty@xiph.org> and The XIPHOPHORUS Company *
  10. * http://www.xiph.org/ *
  11. * *
  12. ********************************************************************
  13. function: utility main for loading and operating on codebooks
  14. last mod: $Id: run.c,v 1.10.8.1 2000/08/31 09:00:03 xiphmont Exp $
  15. ********************************************************************/
  16. #include <unistd.h>
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <math.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <sys/stat.h>
  23. #include <sys/types.h>
  24. #include <fcntl.h>
  25. #include "bookutil.h"
  26. /* command line:
  27. utilname [-i] +|* input_book.vqh [+|* input_book.vqh]
  28. input_data.vqd [input_data.vqd]
  29. produces output data on stdout
  30. (may also take input data from stdin)
  31. */
  32. extern void process_preprocess(codebook **b,char *basename);
  33. extern void process_postprocess(codebook **b,char *basename);
  34. extern void process_vector(codebook **b,int *addmul, int inter,float *a,int n);
  35. extern void process_usage(void);
  36. int main(int argc,char *argv[]){
  37. char *basename;
  38. codebook **b=calloc(1,sizeof(codebook *));
  39. int *addmul=calloc(1,sizeof(int));
  40. int books=0;
  41. int input=0;
  42. int interleave=0;
  43. int j;
  44. int start=0;
  45. int num=-1;
  46. argv++;
  47. if(*argv==NULL){
  48. process_usage();
  49. exit(1);
  50. }
  51. /* yes, this is evil. However, it's very convenient to parse file
  52. extentions */
  53. while(*argv){
  54. if(*argv[0]=='-'){
  55. /* option */
  56. if(argv[0][1]=='s'){
  57. /* subvector */
  58. if(sscanf(argv[1],"%d,%d",&start,&num)!=2){
  59. num= -1;
  60. if(sscanf(argv[1],"%d",&start)!=1){
  61. fprintf(stderr,"Syntax error using -s\n");
  62. exit(1);
  63. }
  64. }
  65. argv+=2;
  66. }
  67. if(argv[0][1]=='i'){
  68. /* interleave */
  69. interleave=1;
  70. argv+=1;
  71. }
  72. }else{
  73. /* input file. What kind? */
  74. char *dot;
  75. char *ext=NULL;
  76. char *name=strdup(*argv++);
  77. dot=strrchr(name,'.');
  78. if(dot)
  79. ext=dot+1;
  80. else
  81. ext="";
  82. /* codebook */
  83. if(!strcmp(ext,"vqh")){
  84. int multp=0;
  85. if(input){
  86. fprintf(stderr,"specify all input data (.vqd) files following\n"
  87. "codebook header (.vqh) files\n");
  88. exit(1);
  89. }
  90. /* is it additive or multiplicative? */
  91. if(name[0]=='*'){
  92. multp=1;
  93. name++;
  94. }
  95. if(name[0]=='+')name++;
  96. basename=strrchr(name,'/');
  97. if(basename)
  98. basename=strdup(basename)+1;
  99. else
  100. basename=strdup(name);
  101. dot=strrchr(basename,'.');
  102. if(dot)*dot='\0';
  103. b=realloc(b,sizeof(codebook *)*(books+2));
  104. b[books]=codebook_load(name);
  105. addmul=realloc(addmul,sizeof(int)*(books+1));
  106. addmul[books++]=multp;
  107. b[books]=NULL;
  108. }
  109. /* data file */
  110. if(!strcmp(ext,"vqd")){
  111. int cols;
  112. long lines=0;
  113. char *line;
  114. float *vec;
  115. FILE *in=fopen(name,"r");
  116. if(!in){
  117. fprintf(stderr,"Could not open input file %s\n",name);
  118. exit(1);
  119. }
  120. if(!input){
  121. process_preprocess(b,basename);
  122. input++;
  123. }
  124. reset_next_value();
  125. line=setup_line(in);
  126. /* count cols before we start reading */
  127. {
  128. char *temp=line;
  129. while(*temp==' ')temp++;
  130. for(cols=0;*temp;cols++){
  131. while(*temp>32)temp++;
  132. while(*temp==' ')temp++;
  133. }
  134. }
  135. vec=alloca(cols*sizeof(float));
  136. while(line){
  137. lines++;
  138. for(j=0;j<cols;j++)
  139. if(get_line_value(in,vec+j)){
  140. fprintf(stderr,"Too few columns on line %ld in data file\n",lines);
  141. exit(1);
  142. }
  143. /* ignores -s for now */
  144. process_vector(b,addmul,interleave,vec,cols);
  145. line=setup_line(in);
  146. }
  147. fclose(in);
  148. }
  149. }
  150. }
  151. /* take any data from stdin */
  152. {
  153. struct stat st;
  154. if(fstat(STDIN_FILENO,&st)==-1){
  155. fprintf(stderr,"Could not stat STDIN\n");
  156. exit(1);
  157. }
  158. if((S_IFIFO|S_IFREG|S_IFSOCK)&st.st_mode){
  159. int cols;
  160. char *line;
  161. long lines=0;
  162. float *vec;
  163. if(!input){
  164. process_preprocess(b,basename);
  165. input++;
  166. }
  167. line=setup_line(stdin);
  168. /* count cols before we start reading */
  169. {
  170. char *temp=line;
  171. while(*temp==' ')temp++;
  172. for(cols=0;*temp;cols++){
  173. while(*temp>32)temp++;
  174. while(*temp==' ')temp++;
  175. }
  176. }
  177. vec=alloca(cols*sizeof(float));
  178. while(line){
  179. lines++;
  180. for(j=0;j<cols;j++)
  181. if(get_line_value(stdin,vec+j)){
  182. fprintf(stderr,"Too few columns on line %ld in data file\n",lines);
  183. exit(1);
  184. }
  185. /* ignores -s for now */
  186. process_vector(b,addmul,interleave,vec,cols);
  187. line=setup_line(stdin);
  188. }
  189. }
  190. }
  191. process_postprocess(b,basename);
  192. return 0;
  193. }