static void SaveToFile(Widget w, FILE *fp) { DviWidget dw = (DviWidget)w; long pos; int c; if (dw->dvi.tmpFile) { pos = ftell(dw->dvi.tmpFile); if (dw->dvi.ungot) { pos--; dw->dvi.ungot = 0; /* The ungot character is in the tmpFile, so we don't want to read it from file. */ (void)getc(dw->dvi.file); } } else pos = ftell(dw->dvi.file); FileSeek(dw, 0L); while (DviGetC(dw, &c) != EOF) if (putc(c, fp) == EOF) { /* XXX print error message */ break; } FileSeek(dw, pos); }
void __fastcall TFrmMain::btRowSaveClick(TObject* Sender) { if (OpenOk == false) return; int iFileHandle; //文件句柄 char Txtbuf[255]; int iVal; char buf[4]; float fVal; FILE* stream; long curpos, length; DWORD dwRows, dwCols, dwRowLen, dwTextLen; DWORD dwTextStartPos; char* pTextPtr ; //if ((stream = fopen(CurrentOpenFile.c_str(), "r+")) // == NULL) //{ // ShowMessage("打开文件出错"); // return; //} //curpos = ftell(stream); //fseek(stream, 0L, SEEK_END); //length = ftell(stream); iFileHandle = FileOpen(CurrentOpenFile, fmOpenRead | fmOpenWrite); //打开文件 for (int i = 0; i < sgEdit->ColCount - 1; i++) { switch (thOpen->ColType[i]) { case 0: //整型值 sgEdit->Row //fseek(stream, 0x14+((sgEdit->Row-1)*(sgEdit->ColCount-1)+i)*4, 0); //iVal=StrToInt(sgEdit->Cells[i+1][sgEdit->Row]); //fwrite(&iVal, 4, 1, stream); iVal = StrToInt(sgEdit->Cells[i + 1][sgEdit->Row]); memcpy(buf, &iVal, 4); FileSeek(iFileHandle, 0x14 + ((sgEdit->Row - 1) * (sgEdit->ColCount - 1) + i) * 4, 0); FileWrite(iFileHandle, buf, 4); break; case 1: //浮点值 //fseek(stream, 0x14+((sgEdit->Row-1)*(sgEdit->ColCount-1)+i)*4, 0); //fVal=StrToFloat(sgEdit->Cells[i+1][sgEdit->Row]); //fwrite(&fVal, 4, 1, stream); fVal = StrToFloat(sgEdit->Cells[i + 1][sgEdit->Row]); memcpy(buf, &fVal, 4); FileSeek(iFileHandle, 0x14 + ((sgEdit->Row - 1) * (sgEdit->ColCount - 1) + i) * 4, 0); FileWrite(iFileHandle, buf, 4); break; case 2: //文本 不存 break; } } //fclose(stream); FileClose(iFileHandle); ShowMessage("The " + IntToStr(sgEdit->Row) + " Row Write Ok!"); }
/* * mdextend() -- Add a block to the specified relation. * * The semantics are basically the same as mdwrite(): write at the * specified position. However, we are expecting to extend the * relation (ie, blocknum is the current EOF), and so in case of * failure we clean up by truncating. * * This routine returns true or false, with errno set as appropriate. * * Note: this routine used to call mdnblocks() to get the block position * to write at, but that's pretty silly since the caller needs to know where * the block will be written, and accordingly must have done mdnblocks() * already. Might as well pass in the position and save a seek. */ bool mdextend(SMgrRelation reln, BlockNumber blocknum, char *buffer, bool isTemp) { long seekpos; int nbytes; MdfdVec *v; v = _mdfd_getseg(reln, blocknum, false); #ifndef LET_OS_MANAGE_FILESIZE seekpos = (long) (BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE))); Assert(seekpos < BLCKSZ * RELSEG_SIZE); #else seekpos = (long) (BLCKSZ * (blocknum)); #endif /* * Note: because caller obtained blocknum by calling _mdnblocks, which did * a seek(SEEK_END), this seek is often redundant and will be optimized * away by fd.c. It's not redundant, however, if there is a partial page * at the end of the file. In that case we want to try to overwrite the * partial page with a full page. It's also not redundant if bufmgr.c had * to dump another buffer of the same file to make room for the new page's * buffer. */ if (FileSeek(v->mdfd_vfd, seekpos, SEEK_SET) != seekpos) return false; if ((nbytes = FileWrite(v->mdfd_vfd, buffer, BLCKSZ)) != BLCKSZ) { if (nbytes > 0) { int save_errno = errno; /* Remove the partially-written page */ FileTruncate(v->mdfd_vfd, seekpos); FileSeek(v->mdfd_vfd, seekpos, SEEK_SET); errno = save_errno; } return false; } if (!isTemp) { if (!register_dirty_segment(reln, v)) return false; } #ifndef LET_OS_MANAGE_FILESIZE Assert(_mdnblocks(v->mdfd_vfd, BLCKSZ) <= ((BlockNumber) RELSEG_SIZE)); #endif return true; }
void TForm1::ProcessFile(String fName){ int iFileHandle; if(theBuffer) delete [] theBuffer; theBuffer=NULL; for(int i=0; i<lstResults->Items->Count; i++){ delete (MyResult*)lstResults->Items->Item[i]->Data; } lstResults->Items->BeginUpdate(); lstResults->Items->Clear(); lstResults->Items->EndUpdate(); char* rawBuffer=NULL; Screen->Cursor = crHourGlass; try{ if(!FileExists(fName)) return; iFileHandle = FileOpen(fName, fmOpenRead); if(iFileHandle == -1){ MessageBox(this->Handle, L"Could not open the specified file.", L"Error", MB_ICONEXCLAMATION); return; } int fileLength = FileSeek(iFileHandle,0,2); FileSeek(iFileHandle,0,0); rawBuffer = new char[fileLength+1]; FileRead(iFileHandle, rawBuffer, fileLength); FileClose(iFileHandle); //strip whitespace and punctuation from text theBuffer = new char[fileLength+1]; bufSize = 0; char *c=rawBuffer, c1; for(int i=0; i<fileLength; i++){ c1 = *c++; if((c1 >= 'A') && (c1 <= 'Z')){ theBuffer[bufSize++] = c1; } else if(((c1 >= 'a') && (c1 <= 'z'))){ theBuffer[bufSize++] = c1-32; } } this->Caption = (Application->Title + " - " + fName); textPosition = 0; }__finally{ if(rawBuffer){ delete [] rawBuffer; } Screen->Cursor = crDefault; RedrawBitmap(); } }
//--------------------------------------------------------------------------- //当前格子写入修改文件中 void __fastcall TFrmMain::N4Click(TObject* Sender) { if (!thOpen) return; int iFileHandle; //文件句柄 char buf[4]; int iVal; float fVal; FILE* stream; /* if ((stream = fopen(CurrentOpenFile.c_str(), "r+")) == NULL) { ShowMessage("打开文件出错"); return; } */ iFileHandle = FileOpen(CurrentOpenFile, fmOpenRead | fmOpenWrite); //打开文件 switch (thOpen->ColType[sgEdit->Col]) { case 0: //整型值 //for(int i=0;i<sgEdit->RowCount-1;i++){ /* fseek(stream, 0x14+((sgEdit->Row-1)*(sgEdit->ColCount-1)+(sgEdit->Col-1))*4, 0); iVal=StrToInt(sgEdit->Cells[sgEdit->Col][sgEdit->Row]); memcpy(buf, &iVal, 4); for(int i=0;i<4;i++) fwrite(buf+i, 1, 1, stream); */ iVal = StrToInt(sgEdit->Cells[sgEdit->Col][sgEdit->Row]); memcpy(buf, &iVal, 4); FileSeek(iFileHandle, 0x14 + ((sgEdit->Row - 1) * (sgEdit->ColCount - 1) + (sgEdit->Col - 1)) * 4, 0); FileWrite(iFileHandle, buf, 4); //} break; case 1: //浮点值 //fseek(stream, 0x14+((sgEdit->Row-1)*(sgEdit->ColCount-1)+(sgEdit->Col-1))*4, 0); //fVal=StrToFloat(sgEdit->Cells[sgEdit->Col][sgEdit->Row]); //fwrite(&fVal, 4, 1, stream); fVal = StrToFloat(sgEdit->Cells[sgEdit->Col][sgEdit->Row]); memcpy(buf, &fVal, 4); FileSeek(iFileHandle, 0x14 + ((sgEdit->Row - 1) * (sgEdit->ColCount - 1) + (sgEdit->Col - 1)) * 4, 0); FileWrite(iFileHandle, buf, 4); break; case 2: //文本不写入 break; } // fclose(stream); FileClose(iFileHandle); }
int FileCopy( const char *pFileIn, const char *pFileOut, int (*pfnCallback)( const char *p1, const char *p2, unsigned int nCopied, unsigned int nTotal ) ) { FHANDLE fh_in, fh_out; u8 chunk[CHUNK_SIZE]; int read; u64 copied, total; fh_in = FileOpen( pFileIn, O_RDONLY ); if( fh_in.fh == -1 ) return 0; fh_out = FileOpen( pFileOut, O_RDWR | O_CREAT | O_TRUNC ); if( fh_out.fh == -1 ) return 0; total = FileSeek( fh_in, 0, SEEK_END ); FileSeek( fh_in, 0, SEEK_SET ); // copy file in chunks copied = 0; while(1) { read = FileRead( fh_in, chunk, sizeof(chunk) ); if( read <= 0 ) break; FileWrite( fh_out, chunk, read ); copied += read; if( pfnCallback ) { if( !pfnCallback( pFileIn, pFileOut, copied, total ) ) { FileClose(fh_in); FileClose(fh_out); return 0; } } } FileClose(fh_in); FileClose(fh_out); return 1; }
int BDATreader::printlist() { int res,laststate,n; long lastpos; string tname; datablockinfo db,saveddb; if(state>0) { laststate=state; lastpos=pos; saveddb=lastinfo; state=1;pos=8; FileSeek(f,pos,SEEK_SET); res=getnextblockinfo(db); printf("BDAT binary data file information:\n - name %s\n - size: %ld\n",filename.c_str(),size); n=1; while(res==0) { printf(" - data block %d\n - name: %s\n - ID: %d\n - type: (%08X) ",n,lastinfo.name.c_str(),lastinfo.ID,lastinfo.type); switch (lastinfo.type) { case DT_bit:tname="bits";break; case DT_byte:tname="byte";break; case DT_ubyte:tname="unsigned byte";break; case DT_word:tname="word/2-byte integer";break; case DT_uword:tname="unsigned word/2-byte integer";break; case DT_int:tname="int/4-byte integer";break; case DT_uint:tname="unsigned int/4-byte integer";break; case DT_long:tname="long int/8-byte integer";break; case DT_ulong:tname="unsigned long int/8-byte integer";break; case DT_single:tname="float/4-byte floating point number";break; case DT_double:tname="double/8-byte floating point number";break; case DT_quad:tname="quad/16-byte floating point number";break; case DT_csingle:tname="complex float/two 4-byte floating point numbers";break; case DT_cdouble:tname="complex double/two 8-byte floating point numbers";break; case DT_cquad:tname="complex quad/two 16-byte floating point number";break; case DT_user:tname="user defined format";break; default:tname="unknown";break; } printf("%s\n - records: %d\n - record size: %d bytes\n - data size: %d bytes\n",tname.c_str(),lastinfo.records,lastinfo.recordsize,lastinfo.records*lastinfo.recordsize); res=skipblock(); if(res==0) res=getnextblockinfo(db); n++; } state=laststate; pos=lastpos; lastinfo=saveddb; FileSeek(f,pos,SEEK_SET); } else return -1; //file needs to be open return 0; };
/* * mdread() -- Read the specified block from a relation. */ void mdread(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer) { off_t seekpos; int nbytes; MdfdVec *v; TRACE_POSTGRESQL_SMGR_MD_READ_START(forknum, blocknum, reln->smgr_rnode.spcNode, reln->smgr_rnode.dbNode, reln->smgr_rnode.relNode); v = _mdfd_getseg(reln, forknum, blocknum, false, EXTENSION_FAIL); seekpos = (off_t) BLCKSZ *(blocknum % ((BlockNumber) RELSEG_SIZE)); Assert(seekpos < (off_t) BLCKSZ * RELSEG_SIZE); if (FileSeek(v->mdfd_vfd, seekpos, SEEK_SET) != seekpos) ereport(ERROR, (errcode_for_file_access(), errmsg("could not seek to block %u in file \"%s\": %m", blocknum, FilePathName(v->mdfd_vfd)))); nbytes = FileRead(v->mdfd_vfd, buffer, BLCKSZ); TRACE_POSTGRESQL_SMGR_MD_READ_DONE(forknum, blocknum, reln->smgr_rnode.spcNode, reln->smgr_rnode.dbNode, reln->smgr_rnode.relNode, nbytes, BLCKSZ); if (nbytes != BLCKSZ) { if (nbytes < 0) ereport(ERROR, (errcode_for_file_access(), errmsg("could not read block %u in file \"%s\": %m", blocknum, FilePathName(v->mdfd_vfd)))); /* * Short read: we are at or past EOF, or we read a partial block at * EOF. Normally this is an error; upper levels should never try to * read a nonexistent block. However, if zero_damaged_pages is ON or * we are InRecovery, we should instead return zeroes without * complaining. This allows, for example, the case of trying to * update a block that was later truncated away. */ if (zero_damaged_pages || InRecovery) MemSet(buffer, 0, BLCKSZ); else ereport(ERROR, (errcode(ERRCODE_DATA_CORRUPTED), errmsg("could not read block %u in file \"%s\": read only %d of %d bytes", blocknum, FilePathName(v->mdfd_vfd), nbytes, BLCKSZ))); } }
FileHandle(const wchar_t* path, uint64 offset) { hFileSrc = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr); if (offset != 0) FileSeek(hFileSrc, offset); }
bool File::Seek(int p, int from) { switch(srctype) { case MODE_MYFILE: case MODE_EXTFILE: return FileSeek(file,p,from); break; case MODE_MYDATA: case MODE_EXTDATA: switch (from) { //case SEEK_CUR: case FILESEEKCURRENT: if(datapos + p >= datasize || datapos + p < 0) return false; datapos += p; break; //case SEEK_SET: case FILESEEKSTART: if(p >= datasize || p < 0) return false; datapos = p; break; //case SEEK_END: case FILESEEKEND: if(datasize + p < 0 || p > 0) return false; datapos = datasize + p; break; } } return true; }
uint_fast8_t tmdLoadHeader(tmd_data *data, wchar_t *path) { //validate and load tmd header File fil; size_t offset, size; tmd_data data_tmp; tmd_content_chunk *content_chunk_tmp; uint_fast16_t content_count; uint8_t hash[SHA_256_SIZE]; if (!FileOpen(&fil, path, 0) || ( (FileRead2(&fil, &data_tmp.sig_type, sizeof(data_tmp.sig_type)) != sizeof(data_tmp.sig_type) || (offset = tmdHeaderOffset(data_tmp.sig_type)) == 0 || !FileSeek(&fil, offset) || FileRead2(&fil, &data_tmp.header, sizeof(data_tmp.header) + sizeof(data_tmp.content_info)) != sizeof(data_tmp.header) + sizeof(data_tmp.content_info)) && (FileClose(&fil) || 1) )) return 0; sha(hash, &data_tmp.content_info, sizeof(data_tmp.content_info), SHA_256_MODE); if (memcmp(hash, data_tmp.header.content_info_hash, sizeof(hash))) return FileClose(&fil) && 0; size = (content_count = __builtin_bswap16(data_tmp.header.content_count)) * sizeof(tmd_content_chunk); content_chunk_tmp = __builtin_alloca(size); if (FileRead2(&fil, content_chunk_tmp, size) != size) return FileClose(&fil) && 0; FileClose(&fil); for (uint_fast16_t info_index = 0, chunk_index = 0, chunk_count; chunk_index < content_count; info_index++, chunk_index += chunk_count) { if (info_index >= sizeof(data_tmp.content_info)/sizeof(tmd_content_info) || (chunk_count = __builtin_bswap16(data_tmp.content_info[info_index].content_command_count)) == 0 ) return 0; sha(hash, &content_chunk_tmp[chunk_index], chunk_count * sizeof(tmd_content_chunk), SHA_256_MODE); if (memcmp(hash, data_tmp.content_info[chunk_index].content_chunk_hash, sizeof(hash))) return 0; } *data = data_tmp; return 1; }
size_t tmdPreloadChunk(tmd_data *data, wchar_t *path, uint_fast16_t content_index) { //loads tmd chunk records and returns total chunks size of content index type on success FIL fil; size_t offset, size = 0; uint_fast16_t content_count, chunk_count; uint8_t hash[SHA_256_SIZE]; if (FileOpen(&fil, path, 0) && (offset = tmdHeaderOffset(data->sig_type))) { size = (content_count = __builtin_bswap16(data->header.content_count)) * sizeof(tmd_content_chunk); if (!data->content_chunk) data->content_chunk = __builtin_alloca(size); if (FileSeek(&fil, offset + sizeof(data->header) + sizeof(data->content_info)) && FileRead2(&fil, data->content_chunk, size) == size) { size = 0; for (uint_fast16_t info_index = 0, chunk_index = 0; chunk_index < content_count; info_index++) { chunk_count = __builtin_bswap16(data->content_info[info_index].content_command_count); sha(hash, &data->content_chunk[chunk_index], chunk_count * sizeof(tmd_content_chunk), SHA_256_MODE); if (memcmp(hash, data->content_info[chunk_index].content_chunk_hash, sizeof(hash))) { size = 0; break; } for (; chunk_count > 0; chunk_index++, chunk_count--) { if (content_index == CONTENT_INDEX_ALL || content_index == data->content_chunk[chunk_index].content_index) size += __builtin_bswap32(data->content_chunk[chunk_index].content_size_lo); } } } else size = 0; FileClose(&fil); } return size; }
//! Read a complete line from a file and return it as a string, treats CR, LF, CRLF and LFCR as valid line ends std::string FileGets(FileHandle File) { std::string Line; if(!FileValid(File)) return Line; while(!FileEof(File)) { char c = static_cast<char>(FileGetc(File)); // Enf of line? if((c == '\n') || (c == '\r')) { // If not the last item byte in the file... if(!FileEof(File)) { // Check the next byte, and if it is something we should not have read, push it back by seeking back UInt64 Pos = FileTell(File); char c2 = static_cast<char>(FileGetc(File)); // We only discard a second character if it is also a \n or \r, but not a duplicate of the first if(((c2 != '\n') && (c2 != '\r')) || (c2 == c)) FileSeek(File, Pos); } break; } Line += c; } return Line; }
/* * windowSwap - swap a windows data */ void static windowSwap( wind *w ) { int i, size; long pos; pos = (long)w->id * buffSize(); FileSeek( swapHandle, pos ); size = w->width * w->height; i = write( swapHandle, w->overlap, size ); if( i != size ) { return; } i = write( swapHandle, w->whooverlapping, size ); if( i != size ) { return; } i = write( swapHandle, w->text, size * sizeof( char_info ) ); if( i != sizeof( char_info ) * size ) { return; } MemFree2( &w->text ); MemFree2( &w->whooverlapping ); MemFree2( &w->overlap ); w->isswapped = TRUE; } /* windowSwap */
/*============================================================================ Description: See documentation for standard C library fseek ==========================================================================*/ unsigned short palm_fseek( PALM_FILE* io_pSF, long in_lOffset, short in_nOrigin ) { FileOriginEnum PalmOrigin = fileOriginCurrent; /* #define SEEK_CUR 1 #define SEEK_END 2 #define SEEK_SET 0 */ ChASSERT(io_pSF->volRef == ((UInt16)-1)); switch( in_nOrigin ) { case 0: PalmOrigin = fileOriginBeginning; break; case 1: PalmOrigin = fileOriginCurrent; break; case 2: PalmOrigin = fileOriginEnd; break; default: // error !! return 0xFFFF; break; } return FileSeek( io_pSF->file.fh, in_lOffset, PalmOrigin ); }
/* * BufFileLoadBuffer * * Load some data into buffer, if possible, starting from curOffset. * At call, must have dirty = false, pos and nbytes = 0. * On exit, nbytes is number of bytes loaded. */ static int BufFileLoadBuffer(BufFile *file, void* buffer, size_t bufsize) { int nb; /* * May need to reposition physical file. */ if (FileSeek(file->file, file->offset, SEEK_SET) != file->offset) { elog(ERROR, "could not seek in temporary file: %m"); } /* * Read whatever we can get, up to a full bufferload. */ nb = FileRead(file->file, buffer, (int)bufsize); if (nb < 0) { elog(ERROR, "could not read from temporary file: %m"); } /* we choose not to advance curOffset here */ return nb; }
void __fastcall TForm1::Button2Click(TObject *Sender) { if(this->OpenDialog1->Execute()) { AnsiString FileName = OpenDialog1->FileName ; int File = FileOpen(FileName,fmOpenRead); //获取文件内容真正的大小 int length = GetCompressedFileSize(FileName.c_str(),NULL); byte *buf = (byte *)malloc(length); char *content = (char *)malloc(length); //read file to buf FileRead(File,buf,length); FileSeek(File,0,0); FileRead(File,content,length); FileClose(File); this->MemoLeftData->Text = AnsiString(content); //store global data embeddata = BinaryToString(buf,length); embedSize = length * 8 ; free(content); free(buf); } }
/* * mdwrite() -- Write the supplied block at the appropriate location. */ bool mdwrite(SMgrRelation reln, BlockNumber blocknum, char *buffer, bool isTemp) { long seekpos; MdfdVec *v; v = _mdfd_getseg(reln, blocknum, false); #ifndef LET_OS_MANAGE_FILESIZE seekpos = (long) (BLCKSZ * (blocknum % ((BlockNumber) RELSEG_SIZE))); Assert(seekpos < BLCKSZ * RELSEG_SIZE); #else seekpos = (long) (BLCKSZ * (blocknum)); #endif if (FileSeek(v->mdfd_vfd, seekpos, SEEK_SET) != seekpos) return false; if (FileWrite(v->mdfd_vfd, buffer, BLCKSZ) != BLCKSZ) return false; if (!isTemp) { if (!register_dirty_segment(reln, v)) return false; } return true; }
/* * mdwrite() -- Write the supplied block at the appropriate location. * * This is to be used only for updating already-existing blocks of a * relation (ie, those before the current EOF). To extend a relation, * use mdextend(). */ void mdwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer, bool isTemp) { off_t seekpos; int nbytes; MdfdVec *v; /* This assert is too expensive to have on normally ... */ #ifdef CHECK_WRITE_VS_EXTEND Assert(blocknum < mdnblocks(reln, forknum)); #endif TRACE_POSTGRESQL_SMGR_MD_WRITE_START(forknum, blocknum, reln->smgr_rnode.spcNode, reln->smgr_rnode.dbNode, reln->smgr_rnode.relNode); v = _mdfd_getseg(reln, forknum, blocknum, isTemp, EXTENSION_FAIL); seekpos = (off_t) BLCKSZ *(blocknum % ((BlockNumber) RELSEG_SIZE)); Assert(seekpos < (off_t) BLCKSZ * RELSEG_SIZE); if (FileSeek(v->mdfd_vfd, seekpos, SEEK_SET) != seekpos) ereport(ERROR, (errcode_for_file_access(), errmsg("could not seek to block %u in file \"%s\": %m", blocknum, FilePathName(v->mdfd_vfd)))); nbytes = FileWrite(v->mdfd_vfd, buffer, BLCKSZ); TRACE_POSTGRESQL_SMGR_MD_WRITE_DONE(forknum, blocknum, reln->smgr_rnode.spcNode, reln->smgr_rnode.dbNode, reln->smgr_rnode.relNode, nbytes, BLCKSZ); if (nbytes != BLCKSZ) { if (nbytes < 0) ereport(ERROR, (errcode_for_file_access(), errmsg("could not write block %u in file \"%s\": %m", blocknum, FilePathName(v->mdfd_vfd)))); /* short write: complain appropriately */ ereport(ERROR, (errcode(ERRCODE_DISK_FULL), errmsg("could not write block %u in file \"%s\": wrote only %d of %d bytes", blocknum, FilePathName(v->mdfd_vfd), nbytes, BLCKSZ), errhint("Check free disk space."))); } if (!isTemp) register_dirty_segment(reln, forknum, v); }
abyss_bool ConnWriteFromFile(TConn * const connectionP, TFile * const fileP, uint64_t const start, uint64_t const last, void * const buffer, uint32_t const buffersize, uint32_t const rate) { /*---------------------------------------------------------------------------- Write the contents of the file stream *fileP, from offset 'start' up through 'last', to the HTTP connection *connectionP. Meter the reading so as not to read more than 'rate' bytes per second. Use the 'bufferSize' bytes at 'buffer' as an internal buffer for this. -----------------------------------------------------------------------------*/ abyss_bool retval; uint32_t waittime; abyss_bool success; uint32_t readChunkSize; if (rate > 0) { readChunkSize = MIN(buffersize, rate); /* One second's worth */ waittime = (1000 * buffersize) / rate; } else { readChunkSize = buffersize; waittime = 0; } success = FileSeek(fileP, start, SEEK_SET); if (!success) retval = FALSE; else { uint64_t const totalBytesToRead = last - start + 1; uint64_t bytesread; bytesread = 0; /* initial value */ while (bytesread < totalBytesToRead) { uint64_t const bytesLeft = totalBytesToRead - bytesread; uint64_t const bytesToRead = MIN(readChunkSize, bytesLeft); uint64_t bytesReadThisTime; bytesReadThisTime = FileRead(fileP, buffer, bytesToRead); bytesread += bytesReadThisTime; if (bytesReadThisTime > 0) ConnWrite(connectionP, buffer, bytesReadThisTime); else break; if (waittime > 0) xmlrpc_millisecond_sleep(waittime); } retval = (bytesread >= totalBytesToRead); } return retval; }
aalSBool ChunkFile::Restart() { offset = 0; if (FileSeek(file, 0, SEEK_SET)) return AAL_SFALSE; return AAL_STRUE; }
// Skip! // aalSBool ChunkFile::Skip(const aalULong & size) { if (FileSeek(file, size, SEEK_CUR)) return AAL_SFALSE; if (offset) offset -= size; return AAL_STRUE; }
// Return AAL_OK if chunk is found // aalSBool ChunkFile::Find(const char * id) { aalUByte cc[4]; FileSeek(file, offset, SEEK_CUR); while (FileRead(cc, 4, 1, file)) { if (!FileRead(&offset, 4, 1, file)) return AAL_SFALSE; if (!memcmp(cc, id, 4)) return AAL_STRUE; if (FileSeek(file, offset, SEEK_CUR)) return AAL_SFALSE; } return AAL_SFALSE; }
aalError CodecRAW::SetPosition(const aalULong & _position) { if (FileSeek(stream, _position, SEEK_CUR)) return AAL_ERROR_FILEIO; cursor = _position; return AAL_OK; }
static void FindPage (DviWidget dw) { int i; long file_position; if (dw->dvi.requested_page < 1) dw->dvi.requested_page = 1; if (dw->dvi.last_page != 0 && dw->dvi.requested_page > dw->dvi.last_page) dw->dvi.requested_page = dw->dvi.last_page; file_position = SearchPagePosition (dw, dw->dvi.requested_page); if (file_position != -1) { FileSeek(dw, file_position); dw->dvi.current_page = dw->dvi.requested_page; } else { for (i=dw->dvi.requested_page; i > 0; i--) { file_position = SearchPagePosition (dw, i); if (file_position != -1) break; } if (file_position == -1) file_position = 0; FileSeek (dw, file_position); dw->dvi.current_page = i; dw->dvi.display_enable = 0; while (dw->dvi.current_page != dw->dvi.requested_page) { dw->dvi.current_page = ParseInput (dw); /* * at EOF, seek back to the beginning of this page. */ if (!dw->dvi.readingTmp && feof (dw->dvi.file)) { file_position = SearchPagePosition (dw, dw->dvi.current_page); if (file_position != -1) FileSeek (dw, file_position); dw->dvi.requested_page = dw->dvi.current_page; break; } } } }
int BDATreader::createTOC() { int res,laststate,n; long lastpos,p; datablockinfo db,saveddb; if(hasTOC) deleteTOC(); if(state>0) { laststate=state; lastpos=pos; saveddb=lastinfo; state=1;pos=8; FileSeek(f,pos,SEEK_SET); res=getnextblockinfo(db); while(res==0) { TOCN++; res=skipblock(); if(res==0) res=getnextblockinfo(db); } if(TOCN>0) { hasTOC=true; TOCpos=new long[TOCN]; TOCID=new unsigned int[TOCN]; TOCnames=new string[TOCN]; state=1;p=pos=8; FileSeek(f,pos,SEEK_SET); res=getnextblockinfo(db); n=0; while(res==0) { res=skipblock(); TOCpos[n]=p; TOCID[n]=lastinfo.ID; TOCnames[n]=lastinfo.name; p=pos; if(res==0) res=getnextblockinfo(db); n++; } } state=laststate; pos=lastpos; lastinfo=saveddb; FileSeek(f,pos,SEEK_SET); } else return -1; return 0; };
HANDLE FileAppend(LPCTSTR FileName) { HANDLE hFile; hFile = CreateFile( FileName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); FileSeek(hFile, 0, FILE_END ); return hFile; }
/* * Finish the open by positioning the next read and saving information. * * file - The open file. * filePathName - name of the segment file to open. * version - AO table format version the file is in. * logicalEof - snapshot version of the EOF value to use as the read end * of the segment file. */ static void AppendOnlyStorageRead_FinishOpenFile(AppendOnlyStorageRead *storageRead, File file, char *filePathName, int version, int64 logicalEof) { int64 seekResult; MemoryContext oldMemoryContext; int segmentFileNameLen; AORelationVersion_CheckValid(version); /* * Seek to the beginning of the file. */ seekResult = FileSeek(file, 0, SEEK_SET); if (seekResult != 0) { FileClose(file); ereport(ERROR, (errcode(ERRCODE_IO_ERROR), errmsg("Append-only Storage Read error on segment file '%s' for relation '%s'. FileSeek offset = 0. Error code = %d (%s)", filePathName, storageRead->relationName, (int) seekResult, strerror((int) seekResult)))); } storageRead->file = file; storageRead->formatVersion = version; /* * When reading multiple segment files, we throw away the old segment file * name strings. */ oldMemoryContext = MemoryContextSwitchTo(storageRead->memoryContext); if (storageRead->segmentFileName != NULL) pfree(storageRead->segmentFileName); segmentFileNameLen = strlen(filePathName); storageRead->segmentFileName = (char *) palloc(segmentFileNameLen + 1); memcpy(storageRead->segmentFileName, filePathName, segmentFileNameLen + 1); /* Allocation is done. Go back to caller memory-context. */ MemoryContextSwitchTo(oldMemoryContext); storageRead->logicalEof = logicalEof; BufferedReadSetFile( &storageRead->bufferedRead, storageRead->file, storageRead->segmentFileName, logicalEof); }
acoral_u32 acoral_lseek(acoral_32 fd,acoral_32 offset,acoral_u8 whence) { acoral_u32 ret,fs_ret; ret=acoral_mutex_pend(fs_mutex,0); if(ret!=MUTEX_SUCCED) return -1; fs_ret=FileSeek(fd,offset,whence); acoral_mutex_post(fs_mutex); return fs_ret; }
/* * Get number of blocks present in a single disk file */ static BlockNumber _mdnblocks(File file, Size blcksz) { long len; len = FileSeek(file, 0L, SEEK_END); if (len < 0) return 0; /* on failure, assume file is empty */ return (BlockNumber) (len / blcksz); }