📄 mp_file.c
字号:
if(flagWrite) { // Writing fprintf(stream,"<molecule> %hu %hu\n",molecule->dim,molecule->nChannels); } else { // Reading fgets(buffer,bufferSize,stream); cnt = sscanf(buffer,"<molecule> %hu %hu\n",&dim,&nChannels); if(cnt != 2) Errorf("ReadWriteStreamMolecule : error while reading [%s]",buffer); } } // Read or write the atoms if(flagWrite) { // Writing for(channel = 0; channel < molecule->nChannels; channel++) { for(k=0; k < molecule->dim; k++) { atom = GetMoleculeAtom(molecule,channel,k); ReadWriteStreamAtom_2_0(atom,stream,flagWrite,flagBinary); } } } else { // Reading SizeMolecule(molecule,nChannels*dim); // Read the first channel for(k=0; k < dim; k++) { atom = NewAtom(); CopyFieldsTFContent(tfContent,atom); ReadWriteStreamAtom_2_0(atom,stream,flagWrite,flagBinary); AddAtom2Molecule(molecule,atom); } // Read the other channels if necessary for(channel = 1; channel < molecule->nChannels; channel++) { ClearMolecule(tmpMolecule); for(k=0; k < molecule->dim; k++) { atom = NewAtom(); CopyFieldsTFContent(tfContent,atom); ReadWriteStreamAtom_2_0(atom,stream,flagWrite,flagBinary); AddAtom2Molecule(tmpMolecule,atom); } AddChannel2Molecule(molecule,tmpMolecule); } } // Case when we need to read or write the terminating flag if(!flagBinary) { if(flagWrite) { // Writing fprintf(stream,"</molecule>\n"); } else { // Reading fgets(buffer,bufferSize,stream); sscanf(buffer,"</molecule>\n"); } }}/* * Write/Read a Book from a stream, either in binary or XML-like format. * When called with NULL book in write mode, it writes an XML-like description of the format. * When called with NULL book in read mode, it checks the presence of a XML-like description * of the format compatible with this version. If not, an error is generated. */static void ReadWriteStreamBook_2_0(BOOK book,FILE *stream,char flagWrite,char flagBinary){ int cnt; int bufferSize = 63; char buffer[63]; unsigned long size; unsigned long n; MOLECULE molecule; if(book == NULL) { // Checking or Writing Info if(flagWrite) { fprintf(stream,"<item> <id>book</id> <version>2.0</version> <content>\n"); fprintf(stream,"<item> <id>size</id> <type>unsigned long</type> </item> \n"); ReadWriteStreamTFContent_2_0(NULL,stream,YES,NO); ReadWriteStreamMolecule_2_0(NULL,stream,YES,NO,NULL); fprintf(stream,"</content> </item>\n"); } else { fgets(buffer,bufferSize,stream); if(strcmp(buffer,"<item> <id>book</id> <version>2.0</version> <content>\n")) Errorf("ReadWriteStreamBook : bad format beginning [%s]",buffer); fgets(buffer,bufferSize,stream); if(strcmp(buffer,"<item> <id>size</id> <type>unsigned long</type> </item> \n")) Errorf("ReadWriteStreamBook : bad format [%s]",buffer); ReadWriteStreamTFContent_2_0(NULL,stream,NO,NO); ReadWriteStreamMolecule_2_0(NULL,stream,NO,NO,NULL); fgets(buffer,bufferSize,stream); if(strcmp(buffer,"</content> </item>\n")) Errorf("ReadWriteStreamBook : bad format beginning [%s]",buffer); } return; } // Read or write the tfContent if(!flagWrite) ClearBook(book); ReadWriteStreamTFContent_2_0((ATFCONTENT)book,stream,flagWrite,flagBinary); // Read or write the size if(flagBinary) {// Binary mode if(flagWrite) { // Writing size = book->size; fwrite((char *)(&size),sizeof(unsigned long),1,stream); } else { // Reading fread((char *)(&size),sizeof(unsigned long),1,stream); } } else { // XML-like mode if(flagWrite) { // Writing size = book->size; fprintf(stream,"<book> %lu\n",size); } else { // Reading fgets(buffer,bufferSize,stream); cnt = sscanf(buffer,"<book> %lu\n",&size); if(cnt != 1) Errorf("ReadWriteStreamBook : error while reading [%s]",buffer); } } // Read or write the molecules if(flagWrite) {// Writing for(n=0; n < book->size; n++) { molecule = GetBookMolecule(book,n); ReadWriteStreamMolecule_2_0(molecule,stream,flagWrite,flagBinary,NULL); } } else { // Reading SizeBook(book,size); for(n=0; n < size; n++) { molecule = NewMolecule(); ReadWriteStreamMolecule_2_0(molecule,stream,flagWrite,flagBinary,(ATFCONTENT)book); AddMolecule2Book(book,molecule); } } // Case when we need to read or write the terminating flag if(!flagBinary) { if(flagWrite) {// Writing fprintf(stream,"</book>\n"); } else { // Reading fgets(buffer,bufferSize,stream); sscanf(buffer,"</book>\n"); } }}/* * Write/Read a Book from a stream, either in binary or XML-like format. */void ReadWriteBook(BOOK book,FILE *stream,char flagWrite,char flagBinary){ int cnt; int bufferSize = 63; char buffer[63]; char binaryStr[63]; // Read or write a header if(flagWrite) { fprintf(stream,"LastWave Header\n"); // The binary mode first if(flagBinary) { if(IsCPULittleEndian) fprintf(stream,"<binary> little </binary>\n"); else fprintf(stream,"<binary> big </binary>\n"); } else fprintf(stream,"<binary> no </binary>\n"); fprintf(stream,"End of Header\n"); } else { fgets(buffer,bufferSize,stream); if(strcmp(buffer,"LastWave Header\n")) Errorf("ReadWriteBook : bad header beginning [%s]",buffer); // The binary mode first fgets(buffer,bufferSize,stream); cnt = sscanf(buffer,"<binary> %s </binary>\n",binaryStr); if(cnt!=1) Errorf("ReadWriteBook : bad binary format [%s]",binaryStr); if(!strcmp(binaryStr,"no")) { // Case where we have to read in ASCII flagBinary = NO; } else if((!strcmp(binaryStr,"little") && IsCPULittleEndian) || (!strcmp(binaryStr,"big") && !IsCPULittleEndian)) { flagBinary = YES; } else { // Case where we have to convert between LittleEndian and BigEndian Errorf("ReadWriteBook : conversion between LittleEndian and BigEndian not implemented yet"); } fgets(buffer,bufferSize,stream); if(strcmp(buffer,"End of Header\n")) Errorf("ReadWriteBook : bad format beginning [%s]",buffer); } ReadWriteStreamBook_2_0(NULL,stream,flagWrite,flagBinary); ReadWriteStreamBook_2_0(book,stream,flagWrite,flagBinary);}/*********************//* * Utilities for some old file formats *//*********************/static unsigned long ComputeStftMaxFreqId(unsigned long signalSize){ unsigned short logMaxFreqId; unsigned long maxFreqId; /* Checking argument */ if(signalSize <= 2) Errorf("ComputeStftMaxFreqId : signalSize %d is too small",signalSize); /* * The 'brute' logMaxFreqId is the largest 'n' * such that 2^n \le signalSize */ logMaxFreqId = (int) (log((LWFLOAT) signalSize)/log(2.0)+.5); maxFreqId = 1<<logMaxFreqId; if(maxFreqId > signalSize) { maxFreqId /= 2; logMaxFreqId -= 1; } /* * We do not want to get bigger than * the maximum allowed maxFreqId */ if(maxFreqId > STFT_MAX_WINDOWSIZE) maxFreqId = STFT_MAX_WINDOWSIZE; return(maxFreqId);}/******************************//* READ OLD BOOK FILES/******************************/extern ATOM ReadAtomOld(BOOK book,char flagBinary,char windowShape,unsigned long forceMaxFreqId,char flagChirp,FILE * stream);extern MOLECULE ReadMoleculeOld(BOOK book,char flagBinary,char windowShape,unsigned long forceMaxFreqId,char flagChirp,char flagHarmo,FILE * stream,LWFLOAT *pResEnergy);void ReadBookOld(BOOK book,FILE *stream,unsigned long forceMaxFreqId,SIGNAL residualEnergy){ struct aTFContent tfContent; int n; char str[255]; char name[255]; int flagBinary = NO; LWFLOAT dx; int signalSize; LWFLOAT signalEnergy; SIGNAL signal; int borderType; int windowShape; int oMin,oMax; int tNSON,fNSON; int flagNoFreq; char flagChirp = NO; char flagHarmo = NO; int flagMZPhase,flagHighRes,depthHighRes; int flagHarmonic; LWFLOAT freq0IdMin,freq0IdMax; int dimHarmo,iFMO; LWFLOAT energy; int size; LWFLOAT threshold; LWFLOAT resEnergy; int i; MOLECULE molecule; int cnt = 0; // Clear the book ClearBook(book); /* Let's try to read the header */ cnt += fscanf(stream,"%[^\n] ",str); if (strcmp(str,"LastWave Header")) Errorf("Error in the header of the file"); n = fscanf(stream,"Name : %[^\n] ",name); if(n != 1) Errorf("Name is missing in book header"); cnt += n; /* The signal information*/#ifdef NUMDOUBLE cnt += fscanf(stream,"Signal dx : %lf ",&dx);#else cnt += fscanf(stream,"Signal dx : %f ",&dx);#endif cnt += fscanf(stream,"Signal size : %d ",&signalSize);#ifdef NUMDOUBLE cnt += fscanf(stream,"Signal energy : %lf ",&signalEnergy);#else cnt += fscanf(stream,"Signal energy : %f ",&signalEnergy);#endif /* The dict information */ cnt += fscanf(stream,"Decomp border type %d ",&borderType); cnt += fscanf(stream,"Decomp atomWindow %d ",&windowShape); cnt += fscanf(stream,"Decomp oMin %d oMax %d ",&oMin,&oMax); cnt += fscanf(stream,"Decomp tNSON %d fNSON %d ",&tNSON,&fNSON); cnt += fscanf(stream,"Decomp flagNoFreq %d ",&flagNoFreq); cnt += fscanf(stream,"Decomp flagMZPhase %d ",&flagMZPhase); cnt += fscanf(stream,"Decomp flagHighRes %d ",&flagHighRes); cnt += fscanf(stream,"Decomp depthHighRes %d ",&depthHighRes); cnt += fscanf(stream,"Decomp flagMolecular %d ",&flagHarmonic);#ifdef NUMDOUBLE cnt += fscanf(stream,"Decomp freq0IdMin %lf freq0IdMax %lf ",&freq0IdMin,&freq0IdMax);#else cnt += fscanf(stream,"Decomp freq0IdMin %f freq0IdMax %f ",&freq0IdMin,&freq0IdMax);#endif cnt += fscanf(stream,"Decomp K %d ",&dimHarmo); cnt += fscanf(stream,"Decomp iFMO %d ",&iFMO);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -