void MPQCInit::init_work_dir() { char *mpqc_work_dir; mpqc_work_dir = std::getenv("MPQC_WORK_DIR"); // if not user defined, use current path if (mpqc_work_dir == nullptr) { path curr_path = current_path(); // set the work dir in FormIO FormIO::set_default_work_dir(curr_path.string()); } else { // check the correctness of path path work_path(mpqc_work_dir); bool path_exists = exists(work_path); if (!path_exists) { throw FileOperationFailed( "Path ${MPQC_WORK_DIR} does not exists! Please set environment " "variable MPQC_WORK_DIR to a valid path.\n", __FILE__, __LINE__, mpqc_work_dir, FileOperationFailed::Exists); } bool is_dir = is_directory(work_path); if (!is_dir) { throw FileOperationFailed( "Path ${MPQC_WORK_DIR} is not a directory! Please set environment " "variable MPQC_WORK_DIR to an existing directory.\n", __FILE__, __LINE__, mpqc_work_dir, FileOperationFailed::Chdir); } // set the work dir in FormIO FormIO::set_default_work_dir(mpqc_work_dir); } }
void MatrixFile::initialise() { Profiler profiler("MatrixFile::initialise", true); assert(m_mode == WriteOnly); m_setColumns = new ResizeableBitset(m_width); off_t off = m_headerSize + (m_width * m_height * m_cellSize) + m_width; #ifdef DEBUG_MATRIX_FILE std::cerr << "MatrixFile[" << m_fd << "]::initialise(" << m_width << ", " << m_height << "): cell size " << m_cellSize << ", header size " << m_headerSize << ", resizing file" << std::endl; #endif if (::lseek(m_fd, off - 1, SEEK_SET) < 0) { ::perror("ERROR: MatrixFile::initialise: seek to end failed"); throw FileOperationFailed(m_fileName, "lseek"); } unsigned char byte = 0; if (::write(m_fd, &byte, 1) != 1) { ::perror("ERROR: MatrixFile::initialise: write at end failed"); throw FileOperationFailed(m_fileName, "write"); } if (::lseek(m_fd, 0, SEEK_SET) < 0) { ::perror("ERROR: MatrixFile::initialise: Seek to write header failed"); throw FileOperationFailed(m_fileName, "lseek"); } size_t header[2]; header[0] = m_width; header[1] = m_height; if (::write(m_fd, header, 2 * sizeof(size_t)) != 2 * sizeof(size_t)) { ::perror("ERROR: MatrixFile::initialise: Failed to write header"); throw FileOperationFailed(m_fileName, "write"); } if (m_mode == WriteOnly) { totalStorage += (m_headerSize + (m_width * m_height * m_cellSize) + m_width); } #ifdef DEBUG_MATRIX_FILE std::cerr << "MatrixFile[" << m_fd << "]::initialise(" << m_width << ", " << m_height << "): storage " << (m_headerSize + m_width * m_height * m_cellSize + m_width) << std::endl; std::cerr << "MatrixFile: Total storage " << totalStorage/1024 << "K" << std::endl; #endif seekTo(0); }
bool MatrixFile::haveSetColumnAt(size_t x) const { if (m_mode == WriteOnly) { return m_setColumns->get(x); } if (m_readyToReadColumn >= 0 && size_t(m_readyToReadColumn) == x) return true; Profiler profiler("MatrixFile::haveSetColumnAt"); #ifdef DEBUG_MATRIX_FILE_READ_SET std::cerr << "MatrixFile[" << m_fd << "]::haveSetColumnAt(" << x << ")" << std::endl; // std::cerr << "."; #endif unsigned char set = 0; if (!seekTo(x)) { std::cerr << "ERROR: MatrixFile::haveSetColumnAt(" << x << "): Seek failed" << std::endl; throw FileOperationFailed(m_fileName, "seek"); } ssize_t r = -1; r = ::read(m_fd, &set, 1); if (r < 0) { ::perror("MatrixFile::haveSetColumnAt: read failed"); throw FileReadFailed(m_fileName); } if (set) m_readyToReadColumn = int(x); return set; }
void MatrixFile::getColumnAt(size_t x, void *data) { assert(m_mode == ReadOnly); #ifdef DEBUG_MATRIX_FILE_READ_SET std::cerr << "MatrixFile[" << m_fd << "]::getColumnAt(" << x << ")" << std::endl; #endif Profiler profiler("MatrixFile::getColumnAt"); ssize_t r = -1; if (m_readyToReadColumn < 0 || size_t(m_readyToReadColumn) != x) { unsigned char set = 0; if (!seekTo(x)) { std::cerr << "ERROR: MatrixFile::getColumnAt(" << x << "): Seek failed" << std::endl; throw FileOperationFailed(m_fileName, "seek"); } r = ::read(m_fd, &set, 1); if (r < 0) { ::perror("MatrixFile::getColumnAt: read failed"); throw FileReadFailed(m_fileName); } if (!set) { std::cerr << "MatrixFile[" << m_fd << "]::getColumnAt(" << x << "): Column has not been set" << std::endl; return; } } r = ::read(m_fd, data, m_height * m_cellSize); if (r < 0) { ::perror("MatrixFile::getColumnAt: read failed"); throw FileReadFailed(m_fileName); } }
MatrixFile::MatrixFile(QString fileBase, Mode mode, size_t cellSize, size_t width, size_t height) : m_fd(-1), m_mode(mode), m_flags(0), m_fmode(0), m_cellSize(cellSize), m_width(width), m_height(height), m_headerSize(2 * sizeof(size_t)), m_setColumns(0), m_autoClose(false), m_readyToReadColumn(-1) { Profiler profiler("MatrixFile::MatrixFile", true); #ifdef DEBUG_MATRIX_FILE std::cerr << "MatrixFile::MatrixFile(" << fileBase.toStdString() << ", " << int(mode) << ", " << cellSize << ", " << width << ", " << height << ")" << std::endl; #endif m_createMutex.lock(); QDir tempDir(TempDirectory::getInstance()->getPath()); QString fileName(tempDir.filePath(QString("%1.mfc").arg(fileBase))); bool newFile = !QFileInfo(fileName).exists(); if (newFile && m_mode == ReadOnly) { std::cerr << "ERROR: MatrixFile::MatrixFile: Read-only mode " << "specified, but cache file does not exist" << std::endl; throw FileNotFound(fileName); } if (!newFile && m_mode == WriteOnly) { std::cerr << "ERROR: MatrixFile::MatrixFile: Write-only mode " << "specified, but file already exists" << std::endl; throw FileOperationFailed(fileName, "create"); } m_flags = 0; m_fmode = S_IRUSR | S_IWUSR; if (m_mode == WriteOnly) { m_flags = O_WRONLY | O_CREAT; } else { m_flags = O_RDONLY; } #ifdef _WIN32 m_flags |= O_BINARY; #endif #ifdef DEBUG_MATRIX_FILE std::cerr << "MatrixFile(" << this << ")::MatrixFile: opening " << fileName.toStdString() << "..." << std::endl; #endif if ((m_fd = ::open(fileName.toLocal8Bit(), m_flags, m_fmode)) < 0) { ::perror("Open failed"); std::cerr << "ERROR: MatrixFile::MatrixFile: " << "Failed to open cache file \"" << fileName.toStdString() << "\""; if (m_mode == WriteOnly) std::cerr << " for writing"; std::cerr << std::endl; throw FailedToOpenFile(fileName); } m_createMutex.unlock(); #ifdef DEBUG_MATRIX_FILE std::cerr << "MatrixFile(" << this << ")::MatrixFile: fd is " << m_fd << std::endl; #endif if (newFile) { initialise(); // write header and "unwritten" column tags } else { size_t header[2]; if (::read(m_fd, header, 2 * sizeof(size_t)) < 0) { ::perror("MatrixFile::MatrixFile: read failed"); std::cerr << "ERROR: MatrixFile::MatrixFile: " << "Failed to read header (fd " << m_fd << ", file \"" << fileName.toStdString() << "\")" << std::endl; throw FileReadFailed(fileName); } if (header[0] != m_width || header[1] != m_height) { std::cerr << "ERROR: MatrixFile::MatrixFile: " << "Dimensions in file header (" << header[0] << "x" << header[1] << ") differ from expected dimensions " << m_width << "x" << m_height << std::endl; throw FailedToOpenFile(fileName); } } m_fileName = fileName; ++m_refcount[fileName]; #ifdef DEBUG_MATRIX_FILE std::cerr << "MatrixFile[" << m_fd << "]::MatrixFile: File " << fileName.toStdString() << ", ref " << m_refcount[fileName] << std::endl; std::cerr << "MatrixFile[" << m_fd << "]::MatrixFile: Done, size is " << "(" << m_width << ", " << m_height << ")" << std::endl; #endif ++totalCount; ++openCount; }
void MatrixFile::setColumnAt(size_t x, const void *data) { assert(m_mode == WriteOnly); if (m_fd < 0) return; // closed #ifdef DEBUG_MATRIX_FILE_READ_SET std::cerr << "MatrixFile[" << m_fd << "]::setColumnAt(" << x << ")" << std::endl; // std::cerr << "."; #endif ssize_t w = 0; if (!seekTo(x)) { std::cerr << "ERROR: MatrixFile::setColumnAt(" << x << "): Seek failed" << std::endl; throw FileOperationFailed(m_fileName, "seek"); } unsigned char set = 0; w = ::write(m_fd, &set, 1); if (w != 1) { ::perror("WARNING: MatrixFile::setColumnAt: write failed (1)"); throw FileOperationFailed(m_fileName, "write"); } w = ::write(m_fd, data, m_height * m_cellSize); if (w != ssize_t(m_height * m_cellSize)) { ::perror("WARNING: MatrixFile::setColumnAt: write failed (2)"); throw FileOperationFailed(m_fileName, "write"); } /* if (x == 0) { std::cerr << "Wrote " << m_height * m_cellSize << " bytes, as follows:" << std::endl; for (int i = 0; i < m_height * m_cellSize; ++i) { std::cerr << (int)(((char *)data)[i]) << " "; } std::cerr << std::endl; } */ if (!seekTo(x)) { std::cerr << "MatrixFile[" << m_fd << "]::setColumnAt(" << x << "): Seek failed" << std::endl; throw FileOperationFailed(m_fileName, "seek"); } set = 1; w = ::write(m_fd, &set, 1); if (w != 1) { ::perror("WARNING: MatrixFile::setColumnAt: write failed (3)"); throw FileOperationFailed(m_fileName, "write"); } m_setColumns->set(x); if (m_autoClose) { if (m_setColumns->isAllOn()) { #ifdef DEBUG_MATRIX_FILE std::cerr << "MatrixFile[" << m_fd << "]::setColumnAt(" << x << "): All columns set: auto-closing" << std::endl; #endif close(); /* } else { int set = 0; for (int i = 0; i < m_width; ++i) { if (m_setColumns->get(i)) ++set; } std::cerr << "MatrixFile[" << m_fd << "]::setColumnAt(" << x << "): Auto-close on, but not all columns set yet (" << set << " of " << m_width << ")" << std::endl; */ } } }
std::tuple<std::string, std::string> process_options( const std::shared_ptr<GetLongOpt> &options) { // set the working dir if (*options->retrieve("W") != ".") { std::string dir = *options->retrieve("W"); auto err = chdir(dir.c_str()); if (err) throw FileOperationFailed("could not change directory", __FILE__, __LINE__, dir.c_str(), FileOperationFailed::Chdir); } // redirect the output, if needed auto output_opt = options->retrieve("o"); std::string output_filename = output_opt ? *output_opt : std::string(); auto print_std_header = []() { ExEnv::out0() << indent << "MPQC version " << MPQC_VERSION << std::endl << indent << "compiled for " << TARGET_ARCH << std::endl << FormIO::copyright << std::endl; }; if (options->retrieve("h")) { print_std_header(); options->usage(ExEnv::out0()); std::exit(0); } if (options->retrieve("v")) { print_std_header(); std::exit(0); } if (options->retrieve("w")) { print_std_header(); ExEnv::out0() << FormIO::warranty << std::endl; std::exit(0); } if (options->retrieve("L")) { print_std_header(); ExEnv::out0() << FormIO::license << std::endl; std::exit(0); } if (options->retrieve("k")) { print_std_header(); ExEnv::out0() << std::endl << indent << "DescribedClass KeyVal-ctor registry:" << incindent << std::endl; for (const auto &entry : DescribedClass::keyval_ctor_registry()) { ExEnv::out0() << indent << entry.first << std::endl; } std::exit(0); } if (options->retrieve("t")) { KeyVal::set_throw_if_deprecated_path(true); } // get input file name ... can be given as the last option auto input_opt = options->retrieve("i"); std::string input_filename; if (input_opt) { input_filename = *input_opt; } else if (MPQCInit::instance().argc() - options->first_unprocessed_arg() == 1) { input_filename = MPQCInit::instance().argv()[options->first_unprocessed_arg()]; } else { options->usage(); throw std::invalid_argument("input filename not given"); } return std::make_tuple(input_filename, output_filename); }
CoreAudioReadStream::CoreAudioReadStream(string path) : m_path(path), m_d(new D) { m_channelCount = 0; m_sampleRate = 0; if (!QFile(m_path).exists()) { throw FileNotFound(m_path); } QByteArray ba = m_path.toLocal8Bit(); CFURLRef url = CFURLCreateFromFileSystemRepresentation (kCFAllocatorDefault, (const UInt8 *)ba.data(), (CFIndex)ba.length(), false); //!!! how do we find out if the file open fails because of DRM protection? #if (MACOSX_DEPLOYMENT_TARGET <= 1040 && MAC_OS_X_VERSION_MIN_REQUIRED <= 1040) FSRef fsref; if (!CFURLGetFSRef(url, &fsref)) { // returns Boolean, not error code m_error = "CoreAudioReadStream: Error looking up FS ref (file not found?)"; throw FileReadFailed(m_path); } m_d->err = ExtAudioFileOpen(&fsref, &m_d->file); #else m_d->err = ExtAudioFileOpenURL(url, &m_d->file); #endif CFRelease(url); if (m_d->err) { m_error = "CoreAudioReadStream: Error opening file: code " + codestr(m_d->err); throw InvalidFileFormat(path, "failed to open audio file"); } if (!m_d->file) { m_error = "CoreAudioReadStream: Failed to open file, but no error reported!"; throw InvalidFileFormat(path, "failed to open audio file"); } UInt32 propsize = sizeof(AudioStreamBasicDescription); m_d->err = ExtAudioFileGetProperty (m_d->file, kExtAudioFileProperty_FileDataFormat, &propsize, &m_d->asbd); if (m_d->err) { m_error = "CoreAudioReadStream: Error in getting basic description: code " + codestr(m_d->err); ExtAudioFileDispose(m_d->file); throw FileOperationFailed(m_path, "get basic description", codestr(m_d->err)); } m_channelCount = m_d->asbd.mChannelsPerFrame; m_sampleRate = m_d->asbd.mSampleRate; cerr << "CoreAudioReadStream: " << m_channelCount << " channels, " << m_sampleRate << " Hz" << std::endl; m_d->asbd.mSampleRate = getSampleRate(); m_d->asbd.mFormatID = kAudioFormatLinearPCM; m_d->asbd.mFormatFlags = kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked | kAudioFormatFlagsNativeEndian; m_d->asbd.mBitsPerChannel = sizeof(float) * 8; m_d->asbd.mBytesPerFrame = sizeof(float) * m_channelCount; m_d->asbd.mBytesPerPacket = sizeof(float) * m_channelCount; m_d->asbd.mFramesPerPacket = 1; m_d->asbd.mReserved = 0; m_d->err = ExtAudioFileSetProperty (m_d->file, kExtAudioFileProperty_ClientDataFormat, propsize, &m_d->asbd); if (m_d->err) { m_error = "CoreAudioReadStream: Error in setting client format: code " + codestr(m_d->err); throw FileOperationFailed(m_path, "set client format", codestr(m_d->err)); } m_d->buffer.mNumberBuffers = 1; m_d->buffer.mBuffers[0].mNumberChannels = m_channelCount; m_d->buffer.mBuffers[0].mDataByteSize = 0; m_d->buffer.mBuffers[0].mData = 0; }