More VQ utility work. Utils now include:
[platform/upstream/libvorbis.git] / vq / run.c
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
14  function: utility main for loading and operating on codebooks
15  last mod: $Id: run.c,v 1.4 2000/01/05 10:14:55 xiphmont Exp $
16
17  ********************************************************************/
18
19 #include <unistd.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <math.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <fcntl.h>
28
29 #include "bookutil.h"
30
31 /* command line:
32    utilname input_book.vqh input_data.vqd [input_data.vqd]
33
34    produces output data on stdout
35    (may also take input data from stdin)
36
37  */
38
39 extern void process_vector(codebook *b,double *a,char *basename);
40 extern void process_usage(void);
41
42 int main(int argc,char *argv[]){
43   char *name;
44   char *basename;
45   double *a=NULL;
46   codebook *b=NULL;
47   argv++;
48
49   if(*argv==NULL){
50     process_usage();
51     exit(1);
52   }
53
54   name=strdup(*argv);
55   b=codebook_load(name);
56   a=alloca(sizeof(double)*b->dim);
57   argv=argv++;
58
59   {
60     char *dot;
61     basename=strrchr(name,'/');
62     if(basename)
63       basename=strdup(basename);
64     else
65       basename=strdup(name);
66     dot=strchr(basename,'.');
67     if(dot)*dot='\0';
68   }
69   
70   while(*argv){
71     /* only input files */
72     char *file=strdup(*argv++);
73     FILE *in=fopen(file,"r");
74     reset_next_value();
75
76     while(get_vector(b,in,a)!=-1)
77       process_vector(b,a,basename);
78
79     fclose(in);
80   }
81
82   /* take any data from stdin */
83   {
84     struct stat st;
85     if(fstat(STDIN_FILENO,&st)==-1){
86       fprintf(stderr,"Could not stat STDIN\n");
87       exit(1);
88     }
89     if((S_IFIFO|S_IFREG|S_IFSOCK)&st.st_mode){
90       reset_next_value();
91       while(get_vector(b,stdin,a)!=-1)
92         process_vector(b,a,basename);
93     }
94   }
95   return 0;
96 }