/************************************************************************* *************************************************************************/ char prog_name[] ="VUTIL"; char prog_version[]="1.00"; /* Standard includes */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //#include /************************************************************************ Global Variables *************************************************************************/ char junk[1000]; char buf[64000]; struct ix_header { long max_entries; long sampling_rate; //6 or 8 long prompts_used; long reserved; long bytes_used_in_file; long reserved1; } hdr; struct ix_entry { long offset; long length; long comment_offset; } vf; /************************************************************************** Common Function Prototypes ***************************************************************************/ int main(int argc, char **argv); void upch(char *s); void strip(char *s); int create_file(char *num_entries,char *fname); int import_file(char *ix_file,char *import_file,char *prompt,char *comment); int export_file(char *ix_file,char *import_file,char *prompt); int add_comment(char *ix_file, char *prompt, char *comment); int produce_list(char *ix_file); int compress_file(char *ix_file); void print_usage(void); int update_dir(char *ix_file,char *prefix,char *suffix); int export_dir(char *ix_file,char *prefix,char *suffix); int custom(char *ix_file); /******************************************************************************\ * * FUNCTION: Main * * INPUTS: COMMAND LINE VARIABLES * * RETURNS: 0 IF SUCCESSFUL, 1 IF NOT * \******************************************************************************/ int main(int argc, char **argv) { int ret; //printf("WaveHdr size: %d - %d",sizeof(w),sizeof(w.w)); //getch(); if(argc < 3) { print_usage(); return(-1); } upch(argv[1]); if(strcmp(argv[1],"-CREATE") == 0) { if(argc != 4) { print_usage(); return(-1); } printf("\nCreating file %s",argv[2]); ret=create_file(argv[3],argv[2]); } else if(strcmp(argv[1],"-COMPRESS") == 0) { if(argc != 3) { print_usage(); return(-1); } printf("\nCompressing %s",argv[2]); ret=1; ret=compress_file(argv[2]); } else if(strcmp(argv[1],"-IMPORT") == 0) { if(argc != 6) { print_usage(); return(-1); } printf("\nImporting %s into %s",argv[3],argv[2]); ret=import_file(argv[2],argv[3],argv[4],argv[5]); } else if(strcmp(argv[1],"-EXPORT") == 0) { if(argc != 5) { print_usage(); return(-1); } printf("\nExporting %s from %s",argv[3],argv[2]); ret=export_file(argv[2],argv[3],argv[4]); } else if(strcmp(argv[1],"-COMMENT") == 0) { if(argc != 5) { print_usage(); return(-1); } printf("\nAdding comment to prompt %s, file %s",argv[3],argv[2]); ret=add_comment(argv[2],argv[3],argv[4]); } else if(strcmp(argv[1],"-LIST") == 0) { if(argc != 3) { print_usage(); return(-1); } ret=produce_list(argv[2]); } else if(strcmp(argv[1],"-UPDATEDIR") == 0) { if(argc != 5) { print_usage(); return(-1); } ret=update_dir(argv[2],argv[3],argv[4]); } else if(strcmp(argv[1],"-EXPORTDIR") == 0) { if(argc != 5) { print_usage(); return(-1); } ret=export_dir(argv[2],argv[3],argv[4]); } else if(strcmp(argv[1],"-CUSTOM") == 0) { custom(argv[2]); } else { print_usage(); return(-1); } //cout << "Enter how many total prompts this file can contain: " << flush; //cin.getline( num_entries, 10); //cout << "\nEnter file name to create: " << flush; //cin.getline( fname, 80); //ret=create_file(num_entries,fname); if(ret == 0) { printf("\nOperation on %s performed successfully\n",argv[2]); } else { printf("\nError on performing operation on %s\n",argv[2]); } return(0); } void print_usage(void) { printf("\nUSAGE:"); printf("\n VUTIL -CREATE "); printf("\n VUTIL -IMPORT "); printf("\n VUTIL -EXPORT "); printf("\n VUTIL -COMMENT "); printf("\n VUTIL -COMPRESS "); printf("\n VUTIL -UPDATEDIR "); printf("\n VUTIL -EXPORTDIR "); printf("\n VUTIL -LIST "); printf("\n\n"); } void strip(char *s) { int i; for(i=(int)strlen(s)-1;i>=0;i--) { if(*(s+i) <= 32) *(s+i)=0; else break; } } void upch(char *s) { int i; int c; for(i=0;i<(int)strlen(s);i++) { c=*(s+i); *(s+i)=(char)toupper(c); } } int create_file(char *num_entries,char *fname) { struct stat info; int ret; char answer[5]; int fh; int i; ret=stat(fname,&info); if(ret == 0) { printf("\nFile %s exists, overwrite it? ",fname); answer[0]=getch(); //cin.getline( answer, 2); if(answer[0] == 'y' || answer[0] == 'Y') { remove(fname); } else { return(-1); } } fh=open(fname,O_RDWR | O_BINARY | O_CREAT,S_IWRITE); if(fh == -1) return(-1); memset(&hdr,0,sizeof(hdr)); hdr.max_entries=atol(num_entries); hdr.sampling_rate=8; hdr.bytes_used_in_file=(hdr.max_entries * 12) + 24; write(fh,&hdr,sizeof(hdr)); memset(&vf,0,sizeof(hdr)); for(i=0;i= hdr.max_entries) { printf("\nPrompt #%ld is out of range",p); close(fh); close(fh1); return(-1); } if(strlen(comment) > 0) { vf.comment_offset=lseek(fh,0L,SEEK_END); write(fh,comment,strlen(comment)); hdr.bytes_used_in_file+=strlen(comment); write(fh,a_null,1); hdr.bytes_used_in_file+=1; } vf.length=import_size; vf.offset=lseek(fh,0L,SEEK_END); for(;;) { bytes_read=read(fh1,buf,64000); write(fh,buf,bytes_read); hdr.bytes_used_in_file+=bytes_read; if(bytes_read != 64000) break; } lseek(fh,(p*12) + 24 + waveoffset,SEEK_SET); read(fh,&vf1,sizeof(vf)); lseek(fh,(p*12) + 24 + waveoffset,SEEK_SET); write(fh,&vf,sizeof(vf)); if(vf1.offset == 0) hdr.prompts_used++; lseek(fh,0L + waveoffset,SEEK_SET); write(fh,&hdr,sizeof(hdr)); close(fh); close(fh1); printf("\nSuccessfully imported %s at %ld for length %ld",comment,vf.offset,vf.length); return(0); } int export_file(char *ix_file,char *export_file,char *prompt) { struct stat info; int ret; int fh; long p; int fh1; unsigned int bytes_to_read; unsigned int bytes_read; long save_len; int waveoffset=0; ret=stat(ix_file,&info); if(ret != 0) { printf("\n%s could not be found.",ix_file); return(-1); } fh=open(ix_file,O_RDWR | O_BINARY); if(fh == -1) { printf("\nError opening %s",ix_file); return(-1); } lseek(fh,0L,SEEK_SET); read(fh,&hdr,sizeof(hdr)); p=atol(prompt); if(p < 0 || p >= hdr.max_entries) { printf("\nPrompt #%ld is out of range",p); close(fh); return(-1); } lseek(fh,(p*12) + 24 + waveoffset,SEEK_SET); read(fh,&vf,sizeof(vf)); if(vf.offset == 0) { printf("\nThere is no recording for prompt %ld",p); close(fh); return(-1); } lseek(fh,vf.offset,SEEK_SET); save_len=vf.length; remove(export_file); fh1=open(export_file,O_RDWR | O_BINARY | O_CREAT,S_IWRITE); if(fh1 == -1) { printf("\nError opening %s",export_file); close(fh); return(-1); } for(;;) { if(vf.length >= 64000) { bytes_to_read=64000; vf.length-=64000; } else bytes_to_read=vf.length; bytes_read=read(fh,buf,bytes_to_read); write(fh1,buf,bytes_read); if(bytes_to_read != 64000) break; } close(fh); close(fh1); printf("\nSuccessfully exported prompt %ld at offset %ld for length %ld",p,vf.offset,save_len); return(0); } int add_comment(char *ix_file, char *prompt, char *comment) { struct stat info; int ret; int fh; long p; char a_null[2]; int waveoffset=0; a_null[0]=0; ret=stat(ix_file,&info); if(ret != 0) { printf("\n%s could not be found.",ix_file); return(-1); } fh=open(ix_file,O_RDWR | O_BINARY); if(fh == -1) { printf("\nError opening %s",ix_file); return(-1); } lseek(fh,0L,SEEK_SET); read(fh,&hdr,sizeof(hdr)); p=atol(prompt); if(p < 0 || p >= hdr.max_entries) { printf("\nPrompt #%ld is out of range",p); close(fh); return(-1); } if(strlen(comment) <= 0) { printf("\nInvalid comment"); close(fh); return(-1); } lseek(fh,(p*12) + 24 + waveoffset,SEEK_SET); read(fh,&vf,sizeof(vf)); vf.comment_offset=lseek(fh,0L,SEEK_END); write(fh,comment,strlen(comment)); hdr.bytes_used_in_file+=strlen(comment); write(fh,a_null,1); hdr.bytes_used_in_file+=1; lseek(fh,(p*12) + 24 + waveoffset,SEEK_SET); write(fh,&vf,sizeof(vf)); lseek(fh,0L + waveoffset,SEEK_SET); write(fh,&hdr,sizeof(hdr)); close(fh); printf("\nSuccessfully added comment to prompt %ld, file %s.",p,ix_file); return(0); } int produce_list(char *ix_file) { struct stat info; int ret; int fh; int i; unsigned char c; int waveoffset=0; ret=stat(ix_file,&info); if(ret != 0) { printf("\n%s could not be found.",ix_file); return(-1); } fh=open(ix_file,O_RDWR | O_BINARY); if(fh == -1) { printf("\nError opening %s",ix_file); return(-1); } lseek(fh,0L,SEEK_SET); read(fh,&hdr,sizeof(hdr)); printf("\nListing for file %s",ix_file); printf("\n Max entries: %ld, Sampling rate: %ld, Prompts used: %ld, Bytes: %ld",hdr.max_entries,hdr.sampling_rate,hdr.prompts_used,hdr.bytes_used_in_file); printf("\n"); for(i=0;i 0) { vf1.comment_offset=lseek(fh1,0L,SEEK_END); lseek(fh,vf.comment_offset,SEEK_SET); for(;;) { read(fh,&c,1); write(fh1,&c,1); hdr1.bytes_used_in_file++; if(c == 0) break; } } //Copy the voice prompt vf1.offset=lseek(fh1,0L,SEEK_END); vf1.length=vf.length; lseek(fh,vf.offset,SEEK_SET); for(;;) { if(vf.length >= 64000) { bytes_to_read=64000; vf.length-=64000; } else bytes_to_read=vf.length; bytes_read=read(fh,buf,bytes_to_read); write(fh1,buf,bytes_read); printf("\nWrote %u bytes",bytes_read); hdr1.bytes_used_in_file+=bytes_read; if(bytes_to_read != 64000) break; } lseek(fh1,(i*12) + 24 + waveoffset,SEEK_SET); write(fh1,&vf1,sizeof(vf1)); hdr1.prompts_used++; } lseek(fh1,0L + waveoffset,SEEK_SET); write(fh1,&hdr1,sizeof(hdr1)); close(fh); close(fh1); remove(ix_file); rename(fname1,ix_file); printf("\nSuccessfully compressed %ld prompts in file %s - %ld bytes",hdr1.prompts_used,ix_file,hdr1.bytes_used_in_file); return(0); } //void SetWaveHeader(int vsize) //{ // memcpy(w.riff,"RIFF",4); // w.tbytes=vsize - 8; // memcpy(w.wave,"WAVE",4); // memcpy(w.fmt,"fmt ",4); // w.fsize=sizeof(w.w); // w.w.wFormatTag=1; // w.w.nChannels=1; // w.w.nSamplesPerSec=11025; //11Khz Wav // w.w.nAvgBytesPerSec=11025; // w.w.nBlockAlign=1; // w.w.wBitsPerSample=8; // w.w.cbSize=0; // memcpy(w.data,"data",4); // w.dsize=vsize - sizeof(w); //} int update_dir(char *ix_file,char *prefix,char *suffix) { struct stat info; int ret; int fh; long p; int fh1; unsigned int bytes_read; char a_null[2]; struct ix_entry vf1; int waveoffset=0; int i; char name[250]; char comment[100]; int cnt=0; a_null[0]=0; ret=stat(ix_file,&info); if(ret != 0) { printf("\n%s could not be found.",ix_file); return(-1); } fh=open(ix_file,O_RDWR | O_BINARY); if(fh == -1) { printf("\nError opening %s",ix_file); return(-1); } read(fh,&hdr,sizeof(hdr)); for(i=0;i 0) { vf.comment_offset=lseek(fh,0L,SEEK_END); write(fh,comment,strlen(comment)); hdr.bytes_used_in_file+=strlen(comment); write(fh,a_null,1); hdr.bytes_used_in_file+=1; } vf.length=info.st_size; vf.offset=lseek(fh,0L,SEEK_END); for(;;) { bytes_read=read(fh1,buf,64000); write(fh,buf,bytes_read); hdr.bytes_used_in_file+=bytes_read; if(bytes_read != 64000) break; } lseek(fh,(p*12) + 24 + waveoffset,SEEK_SET); write(fh,&vf,sizeof(vf)); if(vf1.offset == 0) hdr.prompts_used++; lseek(fh,0L + waveoffset,SEEK_SET); write(fh,&hdr,sizeof(hdr)); close(fh1); if(vf1.offset == 0) printf("\nSuccessfully ADDED %s at %ld for length %ld",comment,vf.offset,vf.length); else printf("\nSuccessfully UPDATED %s at %ld for length %ld",comment,vf.offset,vf.length); cnt++; } close(fh); printf("\nAdded/Update %d Files",cnt); return(0); } int export_dir(char *ix_file,char *prefix,char *suffix) { struct stat info; int ret; int fh; long p; int fh1; unsigned int bytes_to_read; unsigned int bytes_read; char a_null[2]; struct ix_entry vf1; int waveoffset=0; int i; char name[250]; int cnt=0; a_null[0]=0; ret=stat(ix_file,&info); if(ret != 0) { printf("\n%s could not be found.",ix_file); return(-1); } fh=open(ix_file,O_RDONLY | O_BINARY); if(fh == -1) { printf("\nError opening %s",ix_file); return(-1); } read(fh,&hdr,sizeof(hdr)); for(i=0;i= 64000) { bytes_to_read=64000; vf1.length-=64000; } else bytes_to_read=vf1.length; bytes_read=read(fh,buf,bytes_to_read); write(fh1,buf,bytes_read); if(bytes_to_read != 64000) break; } close(fh1); printf("\nSuccessfully Wrote %s at %ld for length %ld",name,vf1.offset,vf1.length); cnt++; } close(fh); printf("\nWrote %d Files",cnt); return(0); } int custom(char *ix_file) { struct stat info; int ret; int fh; int fh1; unsigned int bytes_read; unsigned int bytes_to_read; char fname1[81]; struct ix_entry vf1; struct ix_header hdr1; long l; int i; unsigned char c; int waveoffset=0; ret=stat(ix_file,&info); if(ret != 0) { printf("\n%s could not be found.",ix_file); return(-1); } strcpy(fname1,ix_file); l=strlen(fname1); for(;;) { fname1[l - 1] = fname1[l-1] + 1; ret=stat(fname1,&info); if(ret != 0) break; } fh=open(ix_file,O_RDWR | O_BINARY); if(fh == -1) { printf("\nError opening %s",ix_file); return(-1); } read(fh,&hdr,sizeof(hdr)); fh1=open(fname1,O_RDWR | O_BINARY | O_CREAT,S_IWRITE); if(fh1 == -1) { printf("\nError opening %s",fname1); close(fh); return(-1); } memset(&hdr1,0, sizeof(hdr1)); hdr1.max_entries = hdr.max_entries; hdr1.sampling_rate = hdr.sampling_rate; hdr1.bytes_used_in_file=(hdr.max_entries * 12) + 24 + waveoffset; memset(&vf1,0,sizeof(vf1)); write(fh1,&hdr1,sizeof(hdr1)); for(i=0;i 1000) continue; //We have a valid entry //Copy the comment if there is one if(vf.comment_offset > 0) { vf1.comment_offset=lseek(fh1,0L,SEEK_END); lseek(fh,vf.comment_offset,SEEK_SET); for(;;) { read(fh,&c,1); write(fh1,&c,1); hdr1.bytes_used_in_file++; if(c == 0) break; } } //Copy the voice prompt vf1.offset=lseek(fh1,0L,SEEK_END); vf1.length=vf.length; lseek(fh,vf.offset,SEEK_SET); for(;;) { if(vf.length >= 64000) { bytes_to_read=64000; vf.length-=64000; } else bytes_to_read=vf.length; bytes_read=read(fh,buf,bytes_to_read); write(fh1,buf,bytes_read); printf("\nWrote %u bytes",bytes_read); hdr1.bytes_used_in_file+=bytes_read; if(bytes_to_read != 64000) break; } lseek(fh1,(i*12) + 24 + waveoffset,SEEK_SET); write(fh1,&vf1,sizeof(vf1)); hdr1.prompts_used++; } lseek(fh1,0L + waveoffset,SEEK_SET); write(fh1,&hdr1,sizeof(hdr1)); close(fh); close(fh1); //remove(ix_file); //rename(fname1,ix_file); printf("\nSuccessfully compressed %ld prompts in file %s - %ld bytes",hdr1.prompts_used,ix_file,hdr1.bytes_used_in_file); return(0); }