⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 file_07.cc

📁 这是一个从音频信号里提取特征参量的程序
💻 CC
📖 第 1 页 / 共 2 页
字号:
// file: $isip/class/system/File/file_07.cc// version: $Id: file_07.cc,v 1.9 2003/04/09 19:46:31 duncan Exp $//// system include files//#include "unistd.h"// isip include files//#include "File.h"#include <SysString.h>#include <Console.h> // method: truncate//// arguments://  long size: (input) size of file in bytes//// return: a boolean value indicating status//// truncate the file to the given size//boolean File::truncate(long size_a) {    // check the file pointer  //  if (fp_d == (FILE*)NULL) {    return Error::handle(name(), L"truncate", ERR_NOTOPN, __FILE__, __LINE__);  }  // check the mode  //  if (mode_d == READ_ONLY) {    return Error::handle(name(), L"truncate", Error::MOD_READONLY,			 __FILE__, __LINE__);  }    // call the system function  //  if (::ftruncate(fileno(fp_d), size_a) != 0) {    return Error::handle(name(), L"truncate", ERR_TRUNCT, __FILE__, __LINE__);  }    // exit gracefully  //  return true;}// method: flush//// arguments: none//// return: a boolean value indicating status//// flush the file stream//boolean File::flush() {  // check the file pointer  //  if (fp_d == (FILE*)NULL) {    return Error::handle(name(), L"flush", ERR_NOTOPN, __FILE__,			 __LINE__, Error::WARNING);  }    // check the mode  //  if (mode_d == READ_ONLY) {    return Error::handle(name(), L"flush", Error::MOD_READONLY,			 __FILE__, __LINE__, Error::WARNING);  }    // call the system function  //  if (::fflush(fp_d) == EOF) {    return Error::handle(name(), L"flush", ERR_FLUSH, __FILE__,			 __LINE__, Error::WARNING);  }    // exit gracefully  //  return true;}// method: lock//// arguments://  long retry: (input) number of times to try for the lock//  long delay: (input) pause in seconds between attempts//// return: a boolean value indicating status//// this method locks the file, if it fails, it keeps trying retry_a times//boolean File::lock(long retry_a, long delay_a) {  // make sure we have an acceptable mode  //  if (mode_d == READ_ONLY) {    return Error::handle(name(), L"lock",			 Error::FILE_LOCK_READONLY, __FILE__, __LINE__);  }    // make sure the file is not already locked  //  if (lock_d) {    return true;  }    // if no lock_retry, just try once  //  if (retry_a < 0) {    if (::lockf(fileno(fp_d), F_LOCK, 0) == 0) {      lock_d = true;    }  }    // try as many as the given times to lock the file  //  for (long n = 0; n <= retry_a; n++) {        // lockf works on nfs files across systems    //    if (::lockf(fileno(fp_d), F_TLOCK, 0) == 0) {      lock_d = true;    }    else {      if ((n + 1) <= retry_a) {	// delay some seconds before next retry	//	Console::put(L"<File::lock> waiting for file lock\n");	Integral::sleep(delay_a);      }    }  }    // error if lock unsuccessfully  //  if (!lock_d) {    Error::handle(name(), L"lock", Error::FILE_LOCK, __FILE__, __LINE__,		  Error::WARNING);    return false;  }    // exit gracefully  //  return true;  }// method: unlock//// arguments: none//// return: a boolean value indicating status//// this method unlocks the file//boolean File::unlock() {  // reset the lock  //  if (lock_d) {        // lockf works on nfs files across systems    //    if (::lockf(fileno(fp_d), F_ULOCK, 0) != 0) {      return Error::handle(name(), L"unlock", Error::FILE_UNLOCK,			   __FILE__, __LINE__);    }    lock_d = false;  }    // exit gracefully  //  return true;}// method: open//// arguments://  const unichar* filename: (input) file to open//  MODE mode: (input) open mode//  TYPE type: (input) file type//// return: a boolean value indicating status//// open the file, if it fails, it keeps trying open_retry_d times//boolean File::open(const unichar* filename_a, MODE mode_a, TYPE type_a) {  SysString temp(filename_a);  // call the master function  //  return open(temp, mode_a, type_a);}// method: open//// arguments://  const SysString& filename: (input) file to open//  MODE mode: (input) open mode//  TYPE type: (input) file type//// return: a boolean value indicating status//// open the file, if it fails, it keeps trying open_retry_d times//boolean File::open(const SysString& filename_a, MODE mode_a, TYPE type_a) {  // check the file pointer  //  if (fp_d != (FILE*)NULL) {    Error::handle(name(), L"open", ERR_REOPEN, __FILE__, __LINE__,		  Error::WARNING);        if (!close()) {      return Error::handle(name(), L"open", ERR_CLOSE, __FILE__, __LINE__,			   Error::WARNING);    }  }  // set the file type  //  if (type_d != type_a) {    type_d = type_a;  }    // if the filename is a stream, set it to either read only or write only  //  if (filename_a.eq(STREAM_FILE)) {    if (mode_a == READ_ONLY) {      // set the file mode      //      if (mode_d != mode_a) {	mode_d = mode_a;      }      // set the stream_d to IN so that the destructor will remove the      // temporary file      //      setStreamType(IN);      fp_d = stdin;            // create Systring object for temporary file      //      SysString temp_file;            // declare an output file pointer      //      FILE* o_fp;            // create temporary file.      //      Integral::makeTemp(temp_file);            // register this temporary file so that it can be deleted later      // by the destructor      //      registerTemp(temp_file);            // create an 8 bit buffer      //      byte8 buffer;            // get a file pointer to the temp file      //      o_fp = fopen((char*)(byte*)temp_file, SYS_BINARY_WRITE_PLUS);            // read one unit (8 bits) from stdin and write it to the temp      // file, until fread does not return 1      //      while(::fread(&buffer, sizeof(byte8), 1, fp_d) == 1) {		// null file pointer, error	//	if (o_fp == (FILE*)NULL) { 	  return Error::handle(name(), L"open", ERR_NOTOPN,			       __FILE__, __LINE__);	}		// write the 8-bit buffer	//	if (::fwrite(&buffer, sizeof(byte8),1,o_fp) != 1) {	  return Error::handle(name(), L"open", Error::WRITE,			       __FILE__, __LINE__);	}      }            // set file pointer back to start of file      //      isip_fseek(o_fp,0,POS);            // point the file pointer to the temp file      //      fp_d = o_fp;            return true;    }    else if (mode_a == WRITE_ONLY) {      // set the file mode      //      if (mode_d != mode_a) {	mode_d = mode_a;      }            fp_d = stdout;      return true;    }    else {      return Error::handle(name(), L"open", Error::ARG, __FILE__, __LINE__);    }  }    // see if we can expand the filename  //  SysString fn;  if (!Integral::expandName(fn, filename_a)) {    SysString output(L"File not found: ");    output.concat(filename_a);    output.concat(L"\n");    return Error::handle(name(), L"open", Error::FILENAME_EXPAND,			 __FILE__, __LINE__, Error::WARNING, output);  }  // loop the file is open or until the number of retries is exhausted  //  for (long count = 0; (count <= open_retry_d) && (fp_d == (FILE*)NULL);       count++) {    // branch on type    //    if (type_a == TEXT) {            // branch on mode, open the file      //      if (mode_a == READ_ONLY) {	fp_d = fopen((char*)(byte*)fn, SYS_TEXT_READ_ONLY);      }            else if (mode_a == READ_PLUS) {	fp_d = fopen((char*)(byte*)fn, SYS_TEXT_READ_PLUS);      }            else if (mode_a == WRITE_ONLY) {	fp_d = fopen((char*)(byte*)fn, SYS_TEXT_WRITE_ONLY);      }            else if (mode_a == WRITE_PLUS) {	fp_d = fopen((char*)(byte*)fn, SYS_TEXT_WRITE_PLUS);      }            else if (mode_a == APPEND_ONLY) {	fp_d = fopen((char*)(byte*)fn, SYS_TEXT_APPEND_ONLY);      }      else if (mode_a == APPEND_PLUS) {	fp_d = fopen((char*)(byte*)fn, SYS_TEXT_APPEND_PLUS);      }    }        else if (type_a == BINARY) {            // branch on mode, open the file      //      if (mode_a == READ_ONLY) {	fp_d = fopen((char*)(byte*)fn, SYS_BINARY_READ_ONLY);      }      else if (mode_a == READ_PLUS) {	fp_d = fopen((char*)(byte*)fn, SYS_BINARY_READ_PLUS);      }            else if (mode_a == WRITE_ONLY) {	fp_d = fopen((char*)(byte*)fn, SYS_BINARY_WRITE_ONLY);      }      else if (mode_a == WRITE_PLUS) {	fp_d = fopen((char*)(byte*)fn, SYS_BINARY_WRITE_PLUS);      }      else if (mode_a == APPEND_ONLY) {	fp_d = fopen((char*)(byte*)fn, SYS_BINARY_APPEND_ONLY);      }      else if (mode_a == APPEND_PLUS) {	fp_d = fopen((char*)(byte*)fn, SYS_BINARY_APPEND_PLUS);      }    }        // not a valid open mode    //    else {      return Error::handle(name(), L"open", Error::ARG, __FILE__, __LINE__);    }        // if the file was not opened then delay if required    //    if (fp_d == (FILE*)NULL) {            // sleep if we need to retry the open      //      if (count < open_retry_d) {	SysString msg(L"<File::open> waiting for file open: ");	msg.concat(fn);	Console::put(msg);	Integral::sleep(open_delay_d );      }      // return with a warning      //      else {	SysString output(L"File: ");	output.concat(filename_a);	output.concat(L"\n");	return Error::handle(name(), L"openRead", Error::FILE_NOTFND,			     __FILE__, __LINE__, Error::WARNING, output);      }    }  }  // set other class data  //  mode_d = mode_a;  type_d = type_a;  lock_d = false;      // exit gracefully  //  return true;}// method: close//// arguments: none//// return: a boolean value indicating status//// close the file//

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -