unsigned long File::FileImpl::Write(const void *buf, unsigned long bytes) { DWORD WriteBytes = static_cast<DWORD>(bytes); if (!WriteFile(FileHandle, buf, static_cast<DWORD>(bytes), &WriteBytes, NULL)) throw FileException("Can't write"); return static_cast<unsigned long>(WriteBytes); }
std::string File::readString(size_t len) { if (!_file) throw FileException("File is not open"); if ((_mode & FILEMODE_READ) == 0) throw FileException("Tried to read from file opened in write mode (" + _name.getFullPath() + ")"); std::string s('\0', len); std::string::iterator is = s.begin(); char c; while ((c = readByte())) { *is = c; } return s; }
unsigned long File::FileImpl::GetSize() const { DWORD Size = 0; if ((Size = GetFileSize(FileHandle, 0)) == INVALID_FILE_SIZE) throw FileException("Can't get file size"); return static_cast<unsigned long>(Size); }
unsigned long File::FileImpl::Read(void *buf, unsigned long bufSize) { DWORD ReadBytes = static_cast<DWORD>(bufSize); if (!ReadFile(FileHandle, buf, static_cast<DWORD>(bufSize), &ReadBytes, NULL)) throw FileException("Can't Read"); return static_cast<unsigned long>(ReadBytes); }
void File::copyFile(const tstring& source, const tstring& target) { if (!::CopyFile(formatPath(source).c_str(), formatPath(target).c_str(), FALSE)) { throw FileException(Util::translateError(GetLastError())); // 2012-05-03_22-05-14_LZE57W5HZ7NI3VC773UG4DNJ4QIKP7Q7AEBLWOA_AA236F48_crash-stack-r502-beta24-x64-build-9900.dmp } }
void FileImpl::renameToImpl(const std::string& path) { poco_assert (!_path.empty()); POCO_DESCRIPTOR_STRING(oldNameDsc, _path); POCO_DESCRIPTOR_STRING(newNameDsc, path); int res; if ((res = lib$rename_file(&oldNameDsc, &newNameDsc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)) != 1) { switch (res & 0x0FFFFFFF) { case RMS$_FNF: throw FileNotFoundException(_path); case RMS$_DEV: case RMS$_DNF: throw PathNotFoundException(_path); case RMS$_SYN: throw PathSyntaxException(path); case RMS$_RMV: throw FileAccessDeniedException(_path); case RMS$_PRV: throw FileAccessDeniedException("insufficient privileges", _path); default: throw FileException(path); } } }
/** @brief FileReaderDataSource * * @todo: document this function */ FileReaderDataSource::FileReaderDataSource(std::string FileName) { FileHandle = fopen(FileName.c_str(), "rb"); if (FileHandle == NULL) throw FileException(std::string("Could not open ") + FileName); reset(); }
utils::File::File(const zchar* const filename, FileAccess access, FileShare share, FileMode mode, FileType type) { hFile = CreateFileW(filename, static_cast<DWORD>(access), static_cast<DWORD>(share), nullptr, static_cast<DWORD>(mode), static_cast<DWORD>(type), nullptr); if (hFile == INVALID_HANDLE_VALUE) { OutputDebugStringW(L"Something is wrong:\n"); auto error = GetLastError(); switch (error) { case ERROR_FILE_EXISTS: OutputDebugStringW(L"File already exists.\n"); throw FileAlreadyExistsException(); case ERROR_FILE_NOT_FOUND: OutputDebugStringW(L"File not found.\n"); throw FileNotFoundException(); case ERROR_SHARING_VIOLATION: OutputDebugStringW(L"File cannot be shared.\n"); throw FileSharingViolationException(); default: OutputDebugStringW(L"Reason is not defined.\n"); throw FileException(); } } }
void DirectoryListing::loadFile(const string& name) throw(FileException, SimpleXMLException) { string txt; // For now, we detect type by ending... string ext = Util::getFileExt(name); if(Util::stricmp(ext, ".bz2") == 0) { ::File ff(name, ::File::READ, ::File::OPEN); FilteredInputStream<UnBZFilter, false> f(&ff); const size_t BUF_SIZE = 64*1024; AutoArray<char> buf(BUF_SIZE); size_t len; size_t bytesRead = 0; for(;;) { size_t n = BUF_SIZE; len = f.read(buf, n); txt.append(buf, len); bytesRead += len; if(SETTING(MAX_FILELIST_SIZE) && bytesRead > (size_t)SETTING(MAX_FILELIST_SIZE)*1024*1024) break; if(len < BUF_SIZE) break; } } else if(Util::stricmp(ext, ".xml") == 0) { int64_t sz = ::File::getSize(name); if(sz == -1 || sz >= static_cast<int64_t>(txt.max_size())) throw(FileException(CSTRING(FILE_NOT_AVAILABLE))); txt.resize((size_t) sz); size_t n = txt.length(); ::File(name, ::File::READ, ::File::OPEN).read(&txt[0], n); } loadXML(txt, false); }
void SerialChannelImpl::handleError(const std::string& name) { std::string errorText; DWORD error = GetLastError(); switch (error) { case ERROR_FILE_NOT_FOUND: throw FileNotFoundException(name, getErrorText(errorText)); case ERROR_ACCESS_DENIED: throw FileAccessDeniedException(name, getErrorText(errorText)); case ERROR_ALREADY_EXISTS: case ERROR_FILE_EXISTS: throw FileExistsException(name, getErrorText(errorText)); case ERROR_FILE_READ_ONLY: throw FileReadOnlyException(name, getErrorText(errorText)); case ERROR_CANNOT_MAKE: case ERROR_INVALID_NAME: case ERROR_FILENAME_EXCED_RANGE: throw CreateFileException(name, getErrorText(errorText)); case ERROR_BROKEN_PIPE: case ERROR_INVALID_USER_BUFFER: case ERROR_INSUFFICIENT_BUFFER: throw IOException(name, getErrorText(errorText)); case ERROR_NOT_ENOUGH_MEMORY: throw OutOfMemoryException(name, getErrorText(errorText)); case ERROR_HANDLE_EOF: break; default: throw FileException(name, getErrorText(errorText)); } }
GzFileWriter(const std::string &filename) { file = gzopen(filename.c_str(), "wb9"); if (!file) { fprintf(stderr, "Can't open file '%s' for writing\n", filename.c_str()); throw FileException(); } }
void FileImpl::handleLastErrorImpl(const std::string& path) { DWORD err = GetLastError(); switch (err) { case ERROR_FILE_NOT_FOUND: throw FileNotFoundException(path, err); case ERROR_PATH_NOT_FOUND: case ERROR_BAD_NETPATH: case ERROR_CANT_RESOLVE_FILENAME: case ERROR_INVALID_DRIVE: throw PathNotFoundException(path, err); case ERROR_ACCESS_DENIED: throw FileAccessDeniedException(path, err); case ERROR_ALREADY_EXISTS: case ERROR_FILE_EXISTS: throw FileExistsException(path, err); case ERROR_INVALID_NAME: case ERROR_DIRECTORY: case ERROR_FILENAME_EXCED_RANGE: case ERROR_BAD_PATHNAME: throw PathSyntaxException(path, err); case ERROR_FILE_READ_ONLY: throw FileReadOnlyException(path, err); case ERROR_CANNOT_MAKE: throw CreateFileException(path, err); case ERROR_DIR_NOT_EMPTY: throw FileException("directory not empty", path, err); case ERROR_WRITE_FAULT: throw WriteFileException(path, err); case ERROR_READ_FAULT: throw ReadFileException(path, err); case ERROR_SHARING_VIOLATION: throw FileException("sharing violation", path, err); case ERROR_LOCK_VIOLATION: throw FileException("lock violation", path, err); case ERROR_HANDLE_EOF: throw ReadFileException("EOF reached", path, err); case ERROR_HANDLE_DISK_FULL: case ERROR_DISK_FULL: throw WriteFileException("disk is full", path, err); case ERROR_NEGATIVE_SEEK: throw FileException("negative seek", path, err); default: throw FileException(path, err); } }
void File::setEOF() { dcassert(isOpen()); if (!SetEndOfFile(h)) { throw FileException(Util::translateError(GetLastError())); } }
std::size_t FileInputStream::available() const { long temp = std::ftell(_fp); if (temp == -1L) throw FileException(); return _fileSize - temp; }
unsigned int ReadableFromZCSV::read(const std::string& string, unsigned int pos, ReadableFromZCSV* data) { pos = data->readFromString(string, pos); if (static_cast<int>(pos) < 0) throw FileException(m_fileName, "Failed to read an element."); return (pos); }
void File::movePos(int64_t pos) throw(FileException) { // [!] IRainman use SetFilePointerEx function! LARGE_INTEGER x = {0}; x.QuadPart = pos; if (!::SetFilePointerEx(h, x, &x, FILE_CURRENT)) throw(FileException(Util::translateError(GetLastError()))); //[+]PPA }
void utils::File::Write(const void * data, zuint32 count) const { zuint32l bytesWritten = 0; if (!WriteFile(hFile, data, count, &bytesWritten, nullptr)) { throw FileException(); } }
void utils::File::Read(void* data, zuint32 count) const { zuint32l bytesRead; if (!ReadFile(hFile, data, count, &bytesRead, nullptr)) { throw FileException(); } }
WP5DefinitionGroup_DefineTablesSubGroup::WP5DefinitionGroup_DefineTablesSubGroup(librevenge::RVNGInputStream *input, WPXEncryption *encryption, unsigned short subGroupSize) : WP5VariableLengthGroup_SubGroup(), m_position(0), m_numColumns(0), m_leftOffset(0), m_leftGutter(0), m_rightGutter(0) { long startPosition = input->tell(); // Skip useless old values to read the old column number input->seek(2, librevenge::RVNG_SEEK_CUR); m_numColumns = readU16(input, encryption); // Skip to new values input->seek(20+(5*m_numColumns), librevenge::RVNG_SEEK_CUR); // Read the new values unsigned char tmpFlags = readU8(input, encryption); m_position = tmpFlags & 0x07; input->seek(1, librevenge::RVNG_SEEK_CUR); m_numColumns = readU16(input, encryption); input->seek(4, librevenge::RVNG_SEEK_CUR); m_leftGutter = readU16(input, encryption); m_rightGutter = readU16(input, encryption); input->seek(10, librevenge::RVNG_SEEK_CUR); m_leftOffset = readU16(input, encryption); int i; if ((m_numColumns > 32) || ((input->tell() - startPosition + m_numColumns*5) > (subGroupSize - 4))) throw FileException(); for (i=0; i < m_numColumns; i++) { if (input->isEnd()) throw FileException(); m_columnWidth[i] = readU16(input, encryption); } for (i=0; i < m_numColumns; i++) { if (input->isEnd()) throw FileException(); m_attributeBits[i] = readU16(input, encryption); } for (i=0; i < m_numColumns; i++) { if (input->isEnd()) throw FileException(); m_columnAlignment[i] = readU8(input, encryption); } }
/// /// \brief DeviceManager::OpenDeviceFile Открывает файл устройтсва по заданному имени /// \param fileName Имя файла /// void DeviceManager::OpenDeviceFile(const std::string& fileName) { deviceFileDescriptor = open(fileName.c_str(),O_RDWR); if (deviceFileDescriptor < 0) { throw FileException("Error open file: no such file! Kernel module was not loaded!"); } }
std::string File::readString() { if (!_file) throw FileException("File is not open"); if ((_mode & FILEMODE_READ) == 0) throw FileException("Tried to read from file opened in write mode (" + _name.getFullPath() + ")"); std::string s; try { char c; while ((c = readByte())) { s += c; } } catch (FileException &) { // pass, we reached EOF } return s; }
//[+] Greylink int64_t File::getTimeStamp(const string& aFileName) throw(FileException) { WIN32_FIND_DATA fd; HANDLE hFind = FindFirstFile(Text::toT(formatPath(aFileName)).c_str(), &fd); if (hFind == INVALID_HANDLE_VALUE) throw FileException(Util::translateError(GetLastError()) + ": " + aFileName); FindClose(hFind); return *(int64_t*)&fd.ftLastWriteTime; }
long FileInputStream::getReadPosition() const { long rVal = ftell(_fp); if (rVal == -1L) throw FileException(); return rVal; }
Item::Item(std::string filename) { std::ifstream fs_item (filename); if (!fs_item.good()) { throw FileException("File '" + filename + "' not found or is empty"); } std::getline(fs_item, name); std::getline(fs_item, description); debug_println(BIT6,"Item created from file '" << filename << "' has name set to " << name << " and description set to " << description); }
void checkTrees() throw(FileException) { while(cur.getLeaves().size() > verified) { if(cur.getLeaves().size() > real.getLeaves().size() || !(cur.getLeaves()[verified] == real.getLeaves()[verified])) { throw FileException(STRING(TTH_INCONSISTENCY)); } verified++; } }
size_t File::read(void* buf, size_t& len) { DWORD x = 0; if (!::ReadFile(h, buf, (DWORD)len, &x, NULL)) { throw(FileException(Util::translateError(GetLastError()))); } len = x; return x; }
void utils::File::Seek(zint64 position) const { zint32 low = position & 0xFFFFFFFF; zint32l high = position >> 32; auto result = SetFilePointer(hFile, low, &high, FILE_BEGIN); if (result == INVALID_SET_FILE_POINTER) { throw FileException(); } }
void File::FileImpl::SeekTo(unsigned long pos) { if (!pos) { SeekToBegin(); return; } if (SetFilePointer(FileHandle, static_cast<LONG>(pos), 0, FILE_CURRENT) == INVALID_SET_FILE_POINTER) throw FileException("Can't seek to mew position"); }
size_t File::Write(BYTE* in, size_t len) { size_t _len = 0; _len = fwrite(in, sizeof(BYTE), len, m_handle); if(_len != len) throw FileException(m_path, errno); return _len; }
size_t File::Read(BYTE* out, size_t len) { size_t _len = 0; _len = fread(out, sizeof(BYTE), len, m_handle); if (_len == 0) throw FileException(m_path, errno); return _len; }