unsigned char LevelManager::LoadLevels() { FileHandle f; if (f.Open("Holes/course.db") != STATUS_OK) { return 0x2; // Failed to open file } char func[MAX_FUNC_SIZE], params[MAX_PARAM_SIZE]; while(!f.eof()) { f.ReadLine('\n', func, params); const string funcStr(func); if (ExecuteCallback(__levelMgrFuncMap, funcStr, params, this) == STATUS_OK) { continue; } else if (ExecuteCallback(__levelFuncMap, funcStr, params, _levels->at(_levels->size() - 1)) == STATUS_OK) { continue; } // If this gets hit, then either the function specified in the file // was incorrect, or there is no registered callback for that // particular function return 0x4; } f.Close(); return STATUS_OK; }
bool test_sf_file_write_fhandle(const char *filename, const int kib_rw) { bool result = true; FileHandle* file = sd.open(filename, O_WRONLY | O_CREAT | O_TRUNC); if (file != NULL) { int byte_write = 0; timer.start(); for (int i = 0; i < kib_rw; i++) { if (file->write(buffer, sizeof(buffer)) != sizeof(buffer)) { result = false; file->close(); printf("Write error!\r\n"); break; } else { byte_write++; } } timer.stop(); file->close(); double test_time_sec = timer.read_us() / 1000000.0; double speed = kib_rw / test_time_sec; printf("%d KiB write in %.3f sec with speed of %.4f KiB/s\r\n", byte_write, test_time_sec, speed); notify_performance_coefficient("write_kibps", speed); } else { printf("File '%s' not opened\r\n", filename); result = false; } timer.reset(); return result; }
FileHandle::FileHandle(const FileHandle& handle) : m_hFileHandle(fdopen(dup(fileno(handle.getHandle())), handle.getMode())) , m_bIsOpen(handle.isOpen()) , m_szMode("rb") , m_uiOffset(0) { }
//===----------------------------------------------------------------------===// // Testcases //===----------------------------------------------------------------------===// TEST_F(ReadStageTest, quake) { mcld::sys::fs::Path top_level = TOPDIR; // set up output m_pLinker->config()->output().setType(mcld::Output::DynObj); m_pLinker->setOutput(top_level + "unittests/plasma.so"); // set up input m_pLinker->addObject(top_level + "test/libs/ARM/Android/android-14/crtbegin_so.o"); m_pLinker->addObject(top_level + "test/Android/Plasma/ARM/plasma.o"); m_pLinker->addNameSpec("m"); m_pLinker->addNameSpec("log"); m_pLinker->addNameSpec("jnigraphics"); m_pLinker->addNameSpec("c"); m_pLinker->addObject(top_level + "test/libs/ARM/Android/android-14/crtend_so.o"); // dump status m_pLinker->getDriver()->normalize(); FileHandle file; file.open(top_level + "unittests/read_stage.xml", FileHandle::ReadWrite | FileHandle::Create | FileHandle::Truncate, 0644); InputTree::iterator input, inEnd = m_pLinker->config()->inputs().end(); for (input = m_pLinker->config()->inputs().begin(); input != inEnd; ++input) { dumpInput(**input, file, 1); } dumpOutput(m_pLinker->config()->output(), file, 1); // dump status ASSERT_TRUE(m_pLinker->getDriver()->mergeSections()); }
//void ConsumeData(void*) //{ // while (!g_ProducerStarted) // { // Sleep(10); // } // // FileHandle hConsumer = CreateFile(LN250FileName.c_str(), // GENERIC_WRITE, // FILE_SHARE_READ | FILE_SHARE_WRITE, // NULL, // OPEN_EXISTING, 0, NULL); // // if (!hConsumer.isValid()) // { // UCSBUtility::LogError(__FUNCTION__, __FILE__, __LINE__, "Failed to open file %ls %d\\n", LN250FileName.c_str(), // GetLastError()); // return; // } // // char buffer[1024]; // DWORD totalRead = 0; // while(g_ProducerStarted) // { // //DWORD size = GetFileSize(hFile, NULL); // DWORD numRead = 0; // ReadFile(hConsumer, &buffer, sizeof(buffer), &numRead, NULL); // totalRead += numRead; // Sleep(sleepConsumer); // } // // UCSBUtility::LogError(__FUNCTION__, __FILE__, __LINE__, "Total Bytes Read %d\\n", totalRead); //}; // // int _tmain(int argc, _TCHAR* argv[]) { HANDLE hFile = CreateFile(LN250FileName.c_str(), 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, NULL); CloseHandle(hFile); FileHandle hProducer = CreateFile(LN250FileName.c_str(), GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, 0, NULL); if (!hProducer.isValid()) { UCSBUtility::LogError(__FUNCTION__, __FILE__, __LINE__, "Failed to open file %ls %d\\n", LN250FileName.c_str(), GetLastError()); return 1; } HANDLE hThread0 = (HANDLE) _beginthread(ProduceData, 0, (HANDLE)hProducer); HANDLE hThread1 = (HANDLE) _beginthread(ProduceData, 0, (HANDLE)hProducer); HANDLE handles[] = { hThread0, hThread1 }; WaitForMultipleObjects(2, handles, true, INFINITE); return 0; }
SmartPtr<VKShader> VKDevice::create_shader (const char *file_name) { FileHandle file (file_name, "rb"); XCAM_FAIL_RETURN ( ERROR, file.is_valid (), NULL, "VKDevice load shader failed when opend shader file:%s.", XCAM_STR (file_name)); size_t file_size; XCAM_FAIL_RETURN ( ERROR, xcam_ret_is_ok (file.get_file_size (file_size)) || file_size == 0, NULL, "VKDevice load shader failed when read shader file:%s.", XCAM_STR (file_name)); std::vector<uint32_t> content (XCAM_ALIGN_UP (file_size, 4) / 4, 0); XCAM_FAIL_RETURN ( ERROR, xcam_ret_is_ok (file.read_file ((void *)content.data (), file_size)), NULL, "VKDevice load shader failed when read shader file:%s.", XCAM_STR (file_name)); file.close (); SmartPtr<VKShader> shader = create_shader (content); if (shader.ptr ()) shader->set_name (file_name); return shader; }
reg_t kFileIOClose(EngineState *s, int argc, reg_t *argv) { debugC(kDebugLevelFile, "kFileIO(close): %d", argv[0].toUint16()); if (argv[0] == SIGNAL_REG) return s->r_acc; uint16 handle = argv[0].toUint16(); #ifdef ENABLE_SCI32 if (handle == VIRTUALFILE_HANDLE) { s->_virtualIndexFile->close(); return SIGNAL_REG; } #endif FileHandle *f = getFileFromHandle(s, handle); if (f) { f->close(); if (getSciVersion() <= SCI_VERSION_0_LATE) return s->r_acc; // SCI0 semantics: no value returned return SIGNAL_REG; } if (getSciVersion() <= SCI_VERSION_0_LATE) return s->r_acc; // SCI0 semantics: no value returned return NULL_REG; }
int PageDirectory::nextRecord(FileHandle &fh, RID &rid){ char buffer[PAGE_SIZE]; int pgid = rid.pageNum, slotid = -1; RecordPage rp; if (rid.pageNum != 0) { fh.readPage(rid.pageNum, buffer); rp = RecordPage(buffer); if ((slotid = rp.nextRecord(rid.slotNum)) != -1) { // - 1 is current. + 0 is next rid.slotNum = slotid; return 0; } } while (1) { if ((pgid = nextRecordPageID(fh, pgid)) == -1) return -1; fh.readPage(pgid, buffer); rp= RecordPage(buffer); if ((slotid = rp.nextRecord(0)) != -1) { rid.pageNum = pgid; rid.slotNum = slotid; return 0; } } return -1; }
void file_close(EngineState *s, int handle) { debugC(2, kDebugLevelFile, "Closing file %d\n", handle); FileHandle *f = getFileFromHandle(s, handle); if (f) f->close(); }
// Open // // The caller gets a lock to the returned node handle. status_t ClientVolume::Open(Node* node, int openMode, FileHandle** _handle) { if (!node || !_handle) return B_BAD_VALUE; // open the node FileHandle* handle = NULL; status_t error = node->Open(openMode, &handle); if (error != B_OK) return error; BReference<NodeHandle> handleReference(handle, true); // lock the handle handle->Lock(); // add the handle error = fNodeHandles->AddNodeHandle(handle); if (error != B_OK) return error; handleReference.Detach(); *_handle = handle; return B_OK; }
int PageDirectory::moveToNext(FileHandle &fh){ assert(get_next() != 0); assert(get_pgnum() == PageDirectory::MaximunEntryNum()); fh.writePage(in_the_page, data); in_the_page = get_next(); fh.readPage(in_the_page, data); return in_the_page; }
void PlayerManager::back() { m_playlistInterface->playPrevious(); FileHandle file = m_playlistInterface->currentFile(); if(!file.isNull()) play(file); else stop(); }
void PlayerManager::forward() { m_playlistInterface->playNext(); FileHandle file = m_playlistInterface->currentFile(); if(!file.isNull()) play(file); else stop(); }
MemoryArea* MemoryAreaFactory::produce(int pFD, FileHandle::OpenMode pMode) { FileHandle* handler = new FileHandle(); handler->delegate(pFD, pMode); MemoryArea* result = allocate(); new (result) MemoryArea(*handler); return result; }
bool Linker::emit(int pFileDescriptor) { FileHandle file; file.delegate(pFileDescriptor); MemoryArea* output = new MemoryArea(file); bool result = emit(*output); delete output; file.close(); return result; }
/** Returns a stream for writing to this file. Parent directories will be created if necessary. * @param append If false, this file will be overwritten if it exists, otherwise it will be appended. * @throw GdxRuntimeException if this file handle represents a directory, if it is a {@link FileType#Classpath} or * {@link FileType#Internal} file, or if it could not be written. */ void FileHandle::write(bool append, ofstream& stream) const { FileHandle parentHandle = parent(); parentHandle.mkdirs(); std::ios_base::openmode mode; mode = append? ios_base::out | ios_base::app: ios_base::out | ios_base::trunc; stream.open(m_strFullPath.c_str(), mode); if(stream.fail()) { throw GdxRuntimeException("Error writing file: " + m_strFullPath); } }
void PlayerManager::slotNeedNextUrl() { if(m_file.isNull() || !m_crossfadeTracks) return; m_playlistInterface->playNext(); FileHandle nextFile = m_playlistInterface->currentFile(); if(!nextFile.isNull()) { m_file = nextFile; crossfadeToFile(m_file); } }
void test_filehandle() { FileHandle *fh = new FileHandle("test_file"); console->log("Created fh object"); assert(fh->still_open); console->log("Assert 1"); string s = fh->get_line(); console->log("fh->get_line()"); console->log("\"" + s + "\""); assert(0 == s.compare("this is a test file")); assert(0 == fh->get_line().compare("and this is the second line of the test file")); fh->get_line(); assert(!fh->still_open); }
// timeout -1 forever, or milliseconds int poll(pollfh fhs[], unsigned nfhs, int timeout) { /** * TODO Proper wake-up mechanism. * In order to correctly detect availability of read/write a FileHandle, we needed * a select or poll mechanisms. We opted for poll as POSIX defines in * http://pubs.opengroup.org/onlinepubs/009695399/functions/poll.html Currently, * mbed::poll() just spins and scans filehandles looking for any events we are * interested in. In future, his spinning behaviour will be replaced with * condition variables. */ Timer timer; if (timeout > 0) { timer.start(); } int count = 0; for (;;) { /* Scan the file handles */ for (unsigned n = 0; n < nfhs; n++) { FileHandle *fh = fhs[n].fh; short mask = fhs[n].events | POLLERR | POLLHUP | POLLNVAL; if (fh) { fhs[n].revents = fh->poll(mask) & mask; } else { fhs[n].revents = POLLNVAL; } if (fhs[n].revents) { count++; } } if (count) { break; } /* Nothing selected - this is where timeout handling would be needed */ if (timeout == 0 || (timeout > 0 && timer.read_ms() > timeout)) { break; } #ifdef MBED_CONF_RTOS_PRESENT // TODO - proper blocking // wait for condition variable, wait queue whatever here rtos::Thread::wait(1); #endif } return count; }
/** Copies this file or directory to the specified file or directory. If this handle is a file, then 1) if the destination is a * file, it is overwritten, or 2) if the destination is a directory, this file is copied into it, or 3) if the destination * doesn't exist, {@link #mkdirs()} is called on the destination's parent and this file is copied into it with a new name. If * this handle is a directory, then 1) if the destination is a file, GdxRuntimeException is thrown, or 2) if the destination is * a directory, this directory is copied recursively into it as a subdirectory, overwriting existing files, or 3) if the * destination doesn't exist, {@link #mkdirs()} is called on the destination and this directory is copied recursively into it * as a subdirectory. * @throw GdxRuntimeException if the destination file handle is a {@link FileType#Classpath} or {@link FileType#Internal} file, * or copying failed. */ void FileHandle::copyTo(const FileHandle& destination) const { /* boolean sourceDir = isDirectory(); if (!sourceDir) { if (dest.isDirectory()) dest = dest.child(name()); copyFile(this, dest); return; } if (dest.exists()) { if (!dest.isDirectory()) throw GdxRuntimeException("Destination exists but is not a directory: " + dest); } else { dest.mkdirs(); if (!dest.isDirectory()) throw GdxRuntimeException("Destination directory cannot be created: " + dest); } if (!sourceDir) dest = dest.child(name()); copyDirectory(this, dest); */ FileHandle dest = destination; bool sourceIsDir = isDirectory(); if (!sourceIsDir) { if (dest.isDirectory()) { dest = dest.child(name()); } copyFile(*this, dest); return; } if (dest.exists()) { if (!dest.isDirectory()) throw GdxRuntimeException("Destination exists but is not a directory: " + dest.toString()); } else { dest.mkdirs(); if (!dest.isDirectory()) throw GdxRuntimeException("Destination directory cannot be created: " + dest.toString()); } if (!sourceIsDir) { dest = dest.child(name()); } copyDirectory(*this, dest); }
Bool CloseFile(FileHandle& Handle) { HANDLE file=reinterpret_cast<HANDLE>(Handle.GetPointer()); Bool result=static_cast<Bool>(CloseHandle(file) == TRUE); Handle=FileHandle::Zero(); return result; }
void Space::Release(Space* pSpace, FileHandle& pHandler) { if (NULL == pSpace) return; switch(pSpace->type()) { case ALLOCATED_ARRAY: free(pSpace->memory()); break; case MMAPED: if (!pHandler.munmap(pSpace->memory(), pSpace->size())) error(diag::err_cannot_munmap_file) << pHandler.path(); break; default: // external and unallocated memory buffers break; } // end of switch }
void PlayerManager::play(const FileHandle &file) { if(!m_setup) setup(); if(!m_media[0] || !m_media[1] || !m_playlistInterface) return; stopCrossfade(); // The "currently playing" media object. Phonon::MediaObject *mediaObject = m_media[m_curOutputPath]; if(file.isNull()) { if(paused()) mediaObject->play(); else if(playing()) { mediaObject->seek(0); emit seeked(0); } else { m_playlistInterface->playNext(); m_file = m_playlistInterface->currentFile(); if(!m_file.isNull()) { mediaObject->setCurrentSource(KUrl::fromPath(m_file.absFilePath())); mediaObject->play(); emit signalItemChanged(m_file); } } } else { mediaObject->setCurrentSource(KUrl::fromPath(file.absFilePath())); mediaObject->play(); if(m_file != file) emit signalItemChanged(file); m_file = file; } // Our state changed handler will perform the follow up actions necessary // once we actually start playing. }
void write_short(FileHandle& fh, uint16_t value) { auto bytes_written = fh.fwrite(&value, sizeof(value), 1) * sizeof(value); if (bytes_written != sizeof(value)) { fprintf(stderr, "fwrite wrote %zd, not %zd\n", bytes_written, sizeof(value)); } CHECK(bytes_written == sizeof(value)); }
FileScan::FileScan(FileHandle &handle, AttrType attrType, int attrLength, int attrOffset, int nullBitOffset, int nullBitMask, CompOp compOp, void *value) : handle_(handle), attrType_(attrType), attrLength_(attrLength), attrOffset_(attrOffset), nullBitOffset_(nullBitOffset), nullBitMask_(nullBitMask), value_(value), compOp_(compOp), pageNum_(1), slotNum_(0), info_(handle.getInfo()), c_(info_.recordSize) { }
UInt64 SeekFile(const FileHandle& Handle, const UInt64 Offset, const SeekOrigin::Type Origin) { long hi=static_cast<long>(Offset), lo=Offset>>32; lo=SetFilePointer(reinterpret_cast<HANDLE>(Handle.GetPointer()), lo, &hi, GetNativeSeekOrigin(Origin)); if (lo!=INVALID_SET_FILE_POINTER) return (static_cast<UInt64>(hi) << 32) + lo; else return 0; }
UInt64 TellFile(const FileHandle& Handle) { long hi=0, lo; lo=SetFilePointer(reinterpret_cast<HANDLE>(Handle.GetPointer()), 0, &hi, FILE_CURRENT); if (lo!=INVALID_SET_FILE_POINTER) return (static_cast<UInt64>(hi) << 32) + lo; else return 0; }
int main() { /*加载字典,创建Simhasher对象*/ Simhasher simhasher("DuplicateRemoval/dict/jieba.dict.utf8", "DuplicateRemoval/dict/hmm_model.utf8", "DuplicateRemoval/dict/idf.utf8", "DuplicateRemoval/dict/stop_words.utf8"); /*扫描文件夹,获取所有文件的路径*/ std::vector<std::string> oldVec; DirScan ds(oldVec); ds.traverse("material");//遍历语料库 /*网页去重,得到一个不重复网页的路径 * * 计算时间 * */ std::vector<std::string> newVec; std::set<uint64_t> hashcode;//这里可以使用hashset进行优化。 DuplicateRemoval dr(oldVec,newVec,hashcode); time_t begin,end,last; (void) time(&begin); dr.handle(simhasher);//把Simhasher的对象传给 去重工具 (void) time(&end); last = end-begin; #if 1 /*print info*/ printf("using %ld seconds\n",last); cout<<"oldVec size = "<<oldVec.size()<<endl; cout<<"newVec size = "<<newVec.size()<<endl; #endif std::string titlename = "【 标 题 】"; const std::string filename("./Library/pagesLibrary.lib"); const std::string offset_file("./Library/pagesOffset.txt"); FileHandle *handler = new FileHandle(newVec,titlename); handler->handle(filename,offset_file); return 0; }
Bool WriteFile(const FileHandle& Handle, const UInt8* Buffer, const UInt64 WriteBytes, UInt64& BytesWritten) { DWORD lowWriteBytes = 0; BOOL result = ::WriteFile(reinterpret_cast<HANDLE>(Handle.GetPointer()), reinterpret_cast<const void*>(Buffer), static_cast<DWORD>(WriteBytes), reinterpret_cast<DWORD*>(&lowWriteBytes), reinterpret_cast<LPOVERLAPPED>(0)) == TRUE; BytesWritten = lowWriteBytes; return result == TRUE; }
bool Linker::emit(const std::string& pPath) { FileHandle file; FileHandle::Permission perm = FileHandle::Permission(0x755); if (!file.open(pPath, FileHandle::ReadWrite | FileHandle::Truncate | FileHandle::Create, perm)) { error(diag::err_cannot_open_output_file) << "Linker::emit()" << pPath; return false; } MemoryArea* output = new MemoryArea(file); bool result = emit(*output); delete output; file.close(); return result; }