示例#1
0
uint64_t FileData::Size() const
{
#ifdef OMIM_OS_TIZEN
  Tizen::Io::FileAttributes attr;
  result const error = Tizen::Io::File::GetAttributes(m_FileName.c_str(), attr);
  if (IsFailed(error))
    MYTHROW(Reader::SizeException, (m_FileName, m_Op, error));
  return attr.GetFileSize();
#else
  int64_t const pos = ftell64(m_File);
  if (pos == INVALID_POS)
    MYTHROW(Reader::SizeException, (GetErrorProlog(), pos));

  if (fseek64(m_File, 0, SEEK_END))
    MYTHROW(Reader::SizeException, (GetErrorProlog()));

  int64_t const size = ftell64(m_File);
  if (size == INVALID_POS)
    MYTHROW(Reader::SizeException, (GetErrorProlog(), size));

  if (fseek64(m_File, pos, SEEK_SET))
    MYTHROW(Reader::SizeException, (GetErrorProlog(), pos));

  ASSERT_GREATER_OR_EQUAL(size, 0, ());
  return static_cast<uint64_t>(size);
#endif
}
示例#2
0
int64_t UCIParser<NumType, LabelType>::GetFilePosition()
{
    int64_t position = ftell64(m_pFile);
    if (position == -1L)
        RuntimeError("UCIParser::GetFilePosition - error retrieving file position in file");
    return position;
}
示例#3
0
// TODO: Remove this ASAP. Rewrite the decompression routines to allow easy decompression of multiple patch file versions. To future me: Consider using a C++ class for decompression.
bool ZPatcher::FileDecompress_Version_1(CLzma2Dec* decoder, FILE* sourceFile, FILE* destFile)
{
	ELzmaStatus status;

	const SizeT buffer_size = 1 << 16;

	Byte sourceBuffer[buffer_size];
	Byte destBuffer[buffer_size];
	SizeT sourceLen = 0;
	SizeT destLen = buffer_size;
	int64_t sourceFilePos = ftell64(sourceFile);

	// We must reinitialize every time we want a decode a new file.
	Lzma2Dec_Init(decoder);

	while (true)
	{
		sourceLen = fread(sourceBuffer, 1, buffer_size, sourceFile);

		SRes res = Lzma2Dec_DecodeToBuf(decoder, destBuffer, &destLen, sourceBuffer, &sourceLen, LZMA_FINISH_ANY, &status);
		assert(res == SZ_OK);

		fwrite(destBuffer, 1, destLen, destFile);

		sourceFilePos += sourceLen;
		res = fseek64(sourceFile, sourceFilePos, SEEK_SET);
		assert(res == 0);

		if (res == SZ_OK && status == LZMA_STATUS_FINISHED_WITH_MARK)
			break;
	}

	return true;
}
示例#4
0
uint64_t File::Cursor() {
	if (_pFile == NULL) {
		WARN("File not opened");
		return 0;
	}
	return (uint64_t) ftell64(_pFile);
}
示例#5
0
// returns the file size for the specified filename
off_type GetFileSize(const char* filename) {

	FILE* FILEHANDLE  = NULL;
	off_type fileSize = 0;


	if(fopen_s(&FILEHANDLE, filename, "rb") != 0) {
		cout << "ERROR: Unable to open file (" << filename << ") when getting file size." << endl;
		exit(1);
	}

	if(FILEHANDLE) {

		if(fseek64(FILEHANDLE, 0, SEEK_END) != 0) {
			cout << "ERROR: Unable to go to the end of the file (" << filename << ")" << endl;
			exit(1);
		}

		fileSize = ftell64(FILEHANDLE);
		fclose(FILEHANDLE);
	}

	if ( FILEHANDLE != NULL )
		fclose( FILEHANDLE );

	return fileSize;
}
示例#6
0
uint64_t FileHandle::Pos() const
{
  int64_t const pos = ftell64(m_file);
  if (pos == INVALID_POS)
    THROWEX(FileException, (E2S()));

  ASSERT(pos >= 0, ());
  return static_cast<uint64_t>(pos);
}
示例#7
0
SMB_OFF_T sys_ftell(FILE *fp)
{
#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FTELL64)
	return (SMB_OFF_T)ftell64(fp);
#elif defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FTELLO64)
	return (SMB_OFF_T)ftello64(fp);
#else
	return (SMB_OFF_T)ftell(fp);
#endif
}
示例#8
0
uint64_t FileHandle::Size() const
{
  int64_t const pos = ftell64(m_file);
  if (pos == INVALID_POS)
    THROWEX(FileException, (E2S(), pos));

  if (fseek64(m_file, 0, SEEK_END))
    THROWEX(FileException, (E2S()));

  int64_t const size = ftell64(m_file);
  if (size == INVALID_POS)
    THROWEX(FileException, (E2S(), size));

  if (fseek64(m_file, pos, SEEK_SET))
    THROWEX(FileException, (E2S(), pos));

  ASSERT(size >= 0, ());
  return static_cast<uint64_t>(size);
}
示例#9
0
/* A portable ftell() function
   Return -1 on failure with errno set appropriately, current file
   position on success */
my_off_t my_ftell (FILE* fp) {
#if defined(HAVE_FTELLO) && SIZEOF_OFF_T >= 8
    return ftello (fp);
#elif defined(HAVE_FTELL64)
    return ftell64 (fp);
#elif SIZEOF_FPOS_T >= 8
    fpos_t pos;

    if (fgetpos (fp, &pos) != 0)
        return -1;

    return pos;
#else
	#error "Large file support, but no way to ftell."
#endif
}
示例#10
0
bool File::Initialize(string path, FILE_OPEN_MODE mode) {
	Close();
	_path = path;
	string openMode = "";
	switch (mode) {
		case FILE_OPEN_MODE_READ:
		{
			openMode = "rb";
			break;
		}
		case FILE_OPEN_MODE_TRUNCATE:
		{
			openMode = "w+b";
			break;
		}
		case FILE_OPEN_MODE_APPEND:
		{
			openMode = "a+b";
			break;
		}
		default:
		{
			FATAL("Invalid open mode");
			return false;
		}
	}

	_pFile = fopen(STR(_path), STR(openMode));

	if (_pFile == NULL) {
		int err = errno;
		FATAL("Unable to open file %s with mode `%s`. Error was: %s (%d)",
				STR(_path), STR(openMode), strerror(err), err);
		return false;
	}

	if (!SeekEnd())
		return false;

	_size = ftell64(_pFile);

	if (!SeekBegin())
		return false;

	return true;
}
示例#11
0
uint64_t FileData::Pos() const
{
#ifdef OMIM_OS_TIZEN
  int const pos = m_File->Tell();
  result const error = GetLastResult();
  if (IsFailed(error))
    MYTHROW(Writer::PosException, (m_FileName, m_Op, error, pos));
  return pos;
#else
  int64_t const pos = ftell64(m_File);
  if (pos == INVALID_POS)
    MYTHROW(Writer::PosException, (GetErrorProlog(), pos));

  ASSERT_GREATER_OR_EQUAL(pos, 0, ());
  return static_cast<uint64_t>(pos);
#endif
}
static uint64_t flush_node (FILE *fp, int features, struct memnode *mp, uint64_t *cntp)
{
    uint64_t curpos, refpos, pos, cnt, tcnt;
    struct memnode *tp;

    if (mp == NULL) return 0;

    tp  = mp;

    while (tp) {
        if (tp->child) {
            tp->childpos = flush_node (fp, features, tp->child, &(tp->subcnt));
            tp->child    = NULL;
        }
        tp   = tp->sib;
    }

    curpos = ftell64 (fp);
    pos    = curpos;
    refpos = pos;
    cnt    = 0;

    while (mp) {
        tcnt = mp->subcnt;
        if (mp->end) tcnt++;

        trie_node_write (fp, features, &refpos, &pos, tcnt, mp->len, mp->end, mp->childpos, mp->dat);

        cnt += tcnt;

        tp   = mp;        
        mp   = mp->sib;
        delnode (tp);
    }
    trie_node_write (fp, features, NULL, NULL, 0, 0, 0, 0, NULL);

    *cntp = cnt;

    return curpos;
}
示例#13
0
long long FTELL64(FILE *stream)
{
    long long ret=-1;
    if (stream==NULL)
        programmer_error("NULL file pointer passed to FTELL64.\n");
#if defined(darwin)
    // ftell is 64-bt ready on darwin
    ret=(long long)ftell(stream);
#elif defined(irix)
    ret=(long long)ftell64(stream);
#elif defined(cygwin)
     /* Although there is a man page for ftello64 on Windows cygwin,
      * it appears that 64 bit ops are nont yet supported there */
    ret=(long long)ftello(stream);
#elif defined(mingw)
    /* MinGW points to the Microsoft ftello64 */
    ret = (long long)ftello64(stream);
#else
    ret=ftello64(stream);
#endif
    if (ret==-1)
        programmer_error("Stream passed to FTELL64 is not seekable.\n");
    return ret;
}
示例#14
0
                for (int32_t y = 0; y < m_height; y++) {
                    for (int32_t x = 0; x < m_width; x++) {
                        *dst_ptr++ = *src_ptr >> 8;
#ifdef MONO_DATA_TIGHTLY_PACKED
                        src_ptr++;
#else
                        src_ptr += 3;
#endif
                    }

                    dst_ptr += m_line_gap;
                }
            }
        }
    }

    // Write image data to file
    m_last_frame_pos = ftell64(mp_avi_file);  // Grab position of last file
    fwrite_error_check(buffer , 1 , (m_width * m_bytes_per_pixel + m_line_gap) * m_height, mp_avi_file);

    // Tidy up after write failures
    if (m_file_write_error) {
        fclose(mp_avi_file);
        m_open = false;
    }

    bool ret = m_file_write_error;
    m_file_write_error = false;
    return ret;
}
示例#15
0
	// opens the alignment archive
	void CAlignmentReader::Open(const string& filename) {

		if(mIsOpen) {
			cout << "ERROR: An attempt was made to open an already open alignment archive." << endl;
			exit(1);
		}

		mInputFilename = filename;

		mInStream = NULL;
		if(fopen_s(&mInStream, filename.c_str(), "rb") != 0) {
			cout << "ERROR: Could not open the compressed alignment archive (" << mInputFilename << ") for reading." << endl;
			exit(1);
		}

		mIsOpen = true;
		
		// ===============
		// read the header
		// ===============

		// MOSAIK_SIGNATURE[6]	   0  -  5
		// STATUS[1]               6  -  6
		// SEQUENCE_TECHNOLOGY[2]  7  -  8
		// ARCHIVE_DATE[8]		   9  - 16
		// NUM_REFERENCE_SEQS[4]   17 - 20
		// NUM_READ_GROUPS[4]      21 - 24
		// NUM_READS[8]            25 - 32
		// NUM_BASES[8]            33 - 40
		// REFERENCES_OFFSET[8]    41 - 48
		// REFERENCE_GAP_OFFSET[8] 49 - 57
		// INDEX_OFFSET[8]         58 - 63
		// NUM_READ_GROUP_TAGS[1]  64 - 64
		// READ_GROUPS[*]

		// check the MOSAIK signature
		char signature[SIGNATURE_LENGTH + 1];
		signature[SIGNATURE_LENGTH] = 0;
		fread( signature, SIGNATURE_LENGTH, 1, mInStream );

		// check if the read signatures match
		//if(strncmp(signature, MOSAIK_SIGNATURE, 5) != 0) {
		if ( ( strncmp( signature, ALIGNER_SIGNATURE, SIGNATURE_LENGTH - 1 ) != 0 ) && ( strncmp( signature, SORT_SIGNATURE, SIGNATURE_LENGTH - 1 ) != 0 ) ) {
		//if(strncmp(signature, ALIGNER_SIGNATURE, 5) != 0) {
			printf("ERROR: It seems that the input file (%s) is not in the MOSAIK alignment format.\n", 
				filename.c_str());
			exit(1);
		}

		//if(MOSAIK_SIGNATURE[5] != signature[5]) {
		if ( ( signature[5] != ALIGNER_SIGNATURE[5] ) && ( signature[5] != ALIGNER_SIGNATURE5[5] ) && ( signature[5] != SORT_SIGNATURE[5] ) ) {
		//if ( ( signature[5] != ALIGNER_SIGNATURE[5] ) && ( signature[5] != ALIGNER_SIGNATURE5[5] ) ) {
			//char version = ( strncmp( signature, ALIGNER_SIGNATURE, SIGNATURE_LENGTH - 1 ) == 0 ) ? ALIGNER_SIGNATURE[5] : SORT_SIGNATURE[5];
			//printf("ERROR: It seems that the input file (%s) was created in another version of MosaikAligner. "
			//	"This version of MOSAIK expected to find an alignment archive using version: %hu, but the "
			//	"alignment archive uses version: %hu. A new alignment archive is required.\n", 
			//	filename.c_str(), version, signature[5]);
			
			printf("ERROR: It seems that the input file (%s) was created in another version of MosaikAligner. "
				"This version of MOSAIK expected to find an alignment archive using version: 4 or 5, but the "
				"alignment archive uses version: %hu. A new alignment archive is required.\n", 
				filename.c_str(), signature[5]);
			exit(1);
		}

		MosaikSignature = new char [ SIGNATURE_LENGTH + 1 ];
		memcpy( MosaikSignature, signature, SIGNATURE_LENGTH );
		MosaikSignature[ SIGNATURE_LENGTH ] = 0;

		// retrieve the alignment file status
		mStatus = (AlignmentStatus)fgetc(mInStream);

		// retrieve the sequencing technology
		fread((char*)&mSeqTech, SIZEOF_SHORT, 1, mInStream);

		// skip the archive date
		fseek64(mInStream, SIZEOF_UINT64, SEEK_CUR);

		// retrieve the number of reference sequences
		fread((char*)&mNumRefSeqs, SIZEOF_INT, 1, mInStream);

		// retrieve the number of read groups
		unsigned int numReadGroups;
		fread((char*)&numReadGroups, SIZEOF_INT, 1, mInStream);

		// retrieve the number of reads
		fread((char*)&mNumReads, SIZEOF_UINT64, 1, mInStream);

		if(mNumReads == 0) {
			printf("ERROR: The alignment archive header indicates that no reads are contained in\n");
			printf("       this file. This might happen when the file was not closed properly -\n");
			printf("       usually from a killed process or a crash. Your only recourse is to\n");
			printf("       realign this data set.\n");
			printf("       filename: [%s]\n", filename.c_str());
			exit(1);
		}

		// retrieve the number of bases
		fread((char*)&mNumBases, SIZEOF_UINT64, 1, mInStream);

		// retrieve the references offset
		off_type referencesOffset = 0;
		fread((char*)&referencesOffset, SIZEOF_OFF_TYPE, 1, mInStream);

		// retrieve the reference gaps offset
		fread((char*)&mReferenceGapOffset, SIZEOF_OFF_TYPE, 1, mInStream);

		// retrieve the index offset
		fread((char*)&mIndexOffset, SIZEOF_OFF_TYPE, 1, mInStream);

		// retrieve the number of header tags
		const unsigned char numHeaderTags = (unsigned char)fgetc(mInStream);

		if(numHeaderTags != 0) {
			for(unsigned char j = 0; j < numHeaderTags; j++) {
				Tag tag;
				ReadTag(tag);
				mHeaderTags[tag.ID] = tag;
			}
		}

		// DEBUG
		//cout << "mStatus:             " << (short)mStatus << endl;
		//cout << "mSeqTech:            " << mSeqTech << endl;
		//cout << "mNumRefSeqs:         " << mNumRefSeqs << endl;
		//cout << "numReadGroups:       " << numReadGroups << endl;
		//cout << "mNumReads:           " << mNumReads << endl;
		//cout << "mNumBases:           " << mNumBases << endl;
		//cout << "referencesOffset:    " << referencesOffset << endl;
		//cout << "mReferenceGapOffset: " << mReferenceGapOffset << endl;
		//cout << "mIndexOffset:        " << mIndexOffset << endl;
		//cout << "numHeaderTags:       " << (unsigned short)numHeaderTags << endl << endl;

		// retrieve the read groups
		mReadGroups.resize(numReadGroups);

		vector<ReadGroup>::iterator rgIter;
		for(rgIter = mReadGroups.begin(); rgIter != mReadGroups.end(); ++rgIter) {

			// read the metadata string lengths
			const unsigned char centerNameLen   = (unsigned char)fgetc(mInStream);
			const unsigned char libraryNameLen  = (unsigned char)fgetc(mInStream);
			const unsigned char platformUnitLen = (unsigned char)fgetc(mInStream);
			const unsigned char readGroupIDLen  = (unsigned char)fgetc(mInStream);
			const unsigned char sampleNameLen   = (unsigned char)fgetc(mInStream);

			unsigned short descriptionLen = 0;
			fread((char*)&descriptionLen, SIZEOF_SHORT, 1, mInStream);
			fread((char*)&rgIter->SequencingTechnology, SIZEOF_SHORT, 1, mInStream);
			fread((char*)&rgIter->MedianFragmentLength, SIZEOF_INT, 1, mInStream);

			rgIter->CenterName.resize(centerNameLen);
			rgIter->LibraryName.resize(libraryNameLen);
			rgIter->PlatformUnit.resize(platformUnitLen);
			rgIter->ReadGroupID.resize(readGroupIDLen);
			rgIter->SampleName.resize(sampleNameLen);
			rgIter->Description.resize(descriptionLen);

			// read the metadata strings
			fread((void*)rgIter->CenterName.data(),   centerNameLen,   1, mInStream);
			fread((void*)rgIter->Description.data(),  descriptionLen,  1, mInStream);
			fread((void*)rgIter->LibraryName.data(),  libraryNameLen,  1, mInStream);
			fread((void*)rgIter->PlatformUnit.data(), platformUnitLen, 1, mInStream);
			fread((void*)rgIter->ReadGroupID.data(),  readGroupIDLen,  1, mInStream);
			fread((void*)rgIter->SampleName.data(),   sampleNameLen,   1, mInStream);
			
			// set the read group code
			rgIter->ReadGroupCode = ReadGroup::GetCode(*rgIter);

			// add the read group to our LUT
			mReadGroupLUT[rgIter->ReadGroupCode] = *rgIter;

			// retrieve the number of read group tags
			const unsigned char numReadGroupTags = (unsigned char)fgetc(mInStream);

			if(numReadGroupTags != 0) {
				printf("ERROR: Found %u read group tags, but support for read group tags has not been implemented yet.\n", numReadGroupTags);
				exit(1);
			}

			//// DEBUG
			//cout << "center name:            " << rgIter->CenterName << endl;
			//cout << "description:            " << rgIter->Description << endl;
			//cout << "library name:           " << rgIter->LibraryName << endl;
			//cout << "platform unit:          " << rgIter->PlatformUnit << endl;
			//cout << "read group ID:          " << rgIter->ReadGroupID << endl;
			//cout << "sample name:            " << rgIter->SampleName << endl;
			//cout << "sequencing technology:  " << rgIter->SequencingTechnology << endl;
			//cout << "median fragment length: " << rgIter->MedianFragmentLength << endl << endl;
		}

		// store the reads offset
		mReadsOffset = ftell64(mInStream);

		// ============================
		// read the reference sequences
		// ============================

		// jump to the reference sequence section
		fseek64(mInStream, referencesOffset, SEEK_SET);

		mReferenceSequences.resize(mNumRefSeqs);
		//mRefSeqLUT = new char*[mNumRefSeqs];
		mRefSeqLUT.resize( mNumRefSeqs );

		unsigned int currentRefSeq = 0;
		vector<ReferenceSequence>::iterator rsIter;
		for(rsIter = mReferenceSequences.begin(); rsIter != mReferenceSequences.end(); ++rsIter, ++currentRefSeq) {

			// REFERENCE_SEQ_NAME_LEN[1]                0 -  0 
			// REFERENCE_SEQ_SPECIES_LEN[1]             1 -  1
			// REFERENCE_SEQ_GENOME_ASSEMBLY_ID_LEN[1]  2 -  2
			// REFERENCE_SEQ_URI_LEN[1]                 3 -  3
			// REFERENCE_SEQ_NUM_BASES[4]               4 -  7
			// REFERENCE_SEQ_SEQ_OFFSET[8]              8 - 15
			// REFERENCE_SEQ_MD5[16]                   16 - 31
			// REFERENCE_SEQ_NAME[X]                   32 - XX
			// REFERENCE_SEQ_SPECIES[X]
			// REFERENCE_SEQ_GENOME_ASSEMBLY_ID[X]
			// REFERENCE_SEQ_URI[X]

			// read the name length
			const unsigned char nameLen = fgetc(mInStream);

			// read the species length
			const unsigned char speciesLen = fgetc(mInStream);

			// read the genome assembly id length
			const unsigned char genomeAssemblyIDLen = fgetc(mInStream);

			// read the uri length
			const unsigned char uriLen = fgetc(mInStream);

			// read the number of bases
			fread((char*)&rsIter->NumBases, SIZEOF_INT, 1, mInStream);

			// write the number of aligned reads
			fread((char*)&rsIter->NumAligned, SIZEOF_UINT64, 1, mInStream);

			// read the MD5 checksum
			rsIter->MD5.resize(32);
			char* pBuffer = (char*)rsIter->MD5.data();
			fread(pBuffer, 32, 1, mInStream);

			// read the reference name
			rsIter->Name.resize(nameLen);
			pBuffer = (char*)rsIter->Name.data();
			fread(pBuffer, nameLen, 1, mInStream);

			//mRefSeqLUT[currentRefSeq] = new char[nameLen + 1];
			//mRefSeqLUT[currentRefSeq].resize( nameLen + 1 );
			//memcpy(mRefSeqLUT[currentRefSeq], pBuffer, nameLen);
			mRefSeqLUT[currentRefSeq].insert( 0, pBuffer, nameLen );
			//mRefSeqLUT[currentRefSeq][nameLen] = 0;
			mRefSeqLUT[currentRefSeq].push_back(0);

			// read the species name
			if(speciesLen > 0) {
				rsIter->Species.resize(speciesLen);
				pBuffer = (char*)rsIter->Species.data();
				fread(pBuffer, speciesLen, 1, mInStream);
			}

			// read the genome assembly ID
			if(genomeAssemblyIDLen > 0) {
				rsIter->GenomeAssemblyID.resize(genomeAssemblyIDLen);
				pBuffer = (char*)rsIter->GenomeAssemblyID.data();
				fread(pBuffer, genomeAssemblyIDLen, 1, mInStream);
			}

			// read the URI
			if(uriLen > 0) {
				rsIter->URI.resize(uriLen);
				pBuffer = (char*)rsIter->URI.data();
				fread(pBuffer, uriLen, 1, mInStream);
			}

			// retrieve the number of reference sequence tags
			const unsigned char numReferenceSequenceTags = (unsigned char)fgetc(mInStream);

			if(numReferenceSequenceTags != 0) {
				printf("ERROR: Found reference sequence tags, but support for reference sequence tags has not been implemented yet.\n");
				exit(1);
			}

			//// DEBUG
			//cout << "# bases:                " << rsIter->NumBases << endl;
			//cout << "md5:                    " << rsIter->MD5 << endl;
			//cout << "name:                   " << rsIter->Name << endl;
			//cout << "species:                " << rsIter->Species << endl;
			//cout << "genome assembly ID:     " << rsIter->GenomeAssemblyID << endl;
			//cout << "URI:                    " << rsIter->URI << endl;
		}

		// ================================
		// read the reference sequence gaps
		// ================================

		CFastLZIO fio;
		if(mReferenceGapOffset != 0) {

			// jump to the reference gap location
			fseek64(mInStream, mReferenceGapOffset, SEEK_SET);

			// read the reference gaps vector
			fio.Read(mBuffer, mBufferLen, mInStream);

			unsigned int bufferOffset = 0;
			vector<GapInfo>::iterator gvIter;
			vector<vector<GapInfo> >::iterator rsgIter;

			mRefSeqGaps.resize(mNumRefSeqs);
			for(rsgIter = mRefSeqGaps.begin(); rsgIter != mRefSeqGaps.end(); ++rsgIter) {

				// retrieve the number of gaps for this reference sequence
				unsigned int numGaps = 0;
				memcpy((char*)&numGaps, mBuffer + bufferOffset, SIZEOF_INT);
				bufferOffset += SIZEOF_INT;

				// pre-allocate the reference gap vector
				rsgIter->resize(numGaps);

				for(gvIter = rsgIter->begin(); gvIter != rsgIter->end(); ++gvIter) {

					// retrieve the reference gap position
					memcpy((char*)&gvIter->Position, mBuffer + bufferOffset, SIZEOF_INT);
					bufferOffset += SIZEOF_INT;

					// retrieve the reference gap length
					memcpy((char*)&gvIter->Length, mBuffer + bufferOffset, SIZEOF_SHORT);
					bufferOffset += SIZEOF_SHORT;
				}
			}
		}

		// restore our file position
		Rewind();
	}
示例#16
0
// Returns true if we need to call again.
bool Response::SendBody(Socket *aSocket) {
  if (mode == ERROR_FILE_NOT_EXIST) {
    cout << "Sent (empty) body (" << parser.id << ")" << std::endl;
    return false;
  }

  int len = 1024;
  unsigned wait = 0;
  string rateStr;
  if (ContainsKey(parser.GetParams(), "rate")) {
    const map<string,string> params = parser.GetParams();
    rateStr = params.find("rate")->second;
    double rate = atof(rateStr.c_str());
    const double period = 0.1;
    if (rate <= 0.0) {
      len = 1024;
      wait = 0;
    } else {
      len = (unsigned)(rate * 1024 * period);
      wait = (unsigned)(period * 1000.0); // ms
    }
  }

  if (mode == GET_ENTIRE_FILE) {
    if (!file) {
      if (fopen_s(&file, path.c_str(), "rb")) {
        file = 0;
        return false;
      }
    }
    if (feof(file)) {
      // Transmitted entire file!
      fclose(file);
      file = 0;
      return false;
    }

    int64_t tell = ftell64(file);

    // Transmit the next segment.
    char* buf = new char[len];
    int x = (int)fread(buf, 1, len, file);
    int r = aSocket->Send(buf, x);
    delete buf;
    if (r < 0) {
      // Some kind of error.
      return false;
    }
      
    if (wait > 0) {
      Sleep(wait);
    }
    // Else we tranmitted that segment, we're ok.
    return true;

  } else if (mode == GET_FILE_RANGE) {
    if (!file) {
      if (fopen_s(&file, path.c_str(), "rb")) {
        file = 0;
        return false;
      }
      fseek64(file, rangeStart, SEEK_SET);
      offset = rangeStart;
      bytesRemaining = rangeEnd - rangeStart;
    }
    if (feof(file) || bytesRemaining == 0) {
      // Transmitted entire range.
      fclose(file);
      file = 0;
      return false;
    }

    // Transmit the next segment.
    char* buf = new char[len];

    len = (unsigned)MIN(bytesRemaining, len);
    size_t bytesSent = fread(buf, 1, len, file);
    bytesRemaining -= bytesSent;
    int r = aSocket->Send(buf, (int)bytesSent);
    delete buf;
    if (r < 0) {
      // Some kind of error.
      return false;
    }
    offset += bytesSent;
    assert(ftell64(file) == offset);

    if (wait > 0) {
      Sleep(wait);
    }

    // Else we tranmitted that segment, we're ok.
    return true;
  }
  else if (mode == DIR_LIST) {
    std::stringstream response;
    PathEnumerator *enumerator = PathEnumerator::getEnumerator(path);
    if (enumerator) {
      response << "<!DOCTYPE html>\n<ul>";
      string href;
      while (enumerator->next(href)) {
        if (href == "." || path == "." && href == "..") {
          continue;
        }
        response << "<li><a href=\"" << path + "/" + href;
        if (!rateStr.empty()) {
          response << "?rate=" + rateStr;
        }
        response << "\">" << href << "</a></li>";
      }
      response << "</ul>";
      delete enumerator;
    }
    string _r = response.str();
    aSocket->Send(_r.c_str(), (int)_r.size());
    return false;
  }

  return false;
}
示例#17
0
main(int argc, char **argv)
{
    int n1,n2,n3,o1,o2,o3,d1,d2,d3;
    int i1,i2,i3,n123,n1o,n2o,n3o;
    int dd,d0,dsort;
    float *trace,*traceo;
    FILE *infp=stdin,*outfp=stdout;
    long long m1, m2, m3;
    off_t lofset;

    /* get parameters */
    initargs(argc,argv);
    askdoc(1);

    file2g(infp);

    if (!getparint("n1",&n1)) err("Must specify n1!\n");
    if (!getparint("n2",&n2)) 
      {
	m1 = 0;
	bcopy(&m1,&lofset,8);
        fseek64(infp,lofset,2);
	lofset = ftell64(infp);
	bcopy(&lofset,&m1,8);
	m1 = m1/sizeof(float)/n1;
        n2 = m1;
	m1 = 0;
	bcopy(&m1,&lofset,8);
        fseek64(infp,lofset,0);
      }
    if (!getparint("n3",&n3)) 
      {
	m1 = 0;
	bcopy(&m1,&lofset,8);
        fseek64(infp,lofset,2);
	lofset = ftell64(infp);
	bcopy(&lofset,&m1,8);
        m1 = m1/sizeof(float)/(n1*n2);
	n3 = m1;
	m1 = 0;
	bcopy(&m1,&lofset,8);
        fseek64(infp,lofset,0);
      }
    if (!getparint("o1",&o1)) o1=1;
    if (!getparint("o2",&o2)) o2=1;
    if (!getparint("o3",&o3)) o3=1;
    if (!getparint("d1",&d1)) d1=1;
    if (!getparint("d2",&d2)) d2=1;
    if (!getparint("d3",&d3)) d3=1;
    if (!getparint("dsort",&dsort)) dsort=0;
    if (!getparint("d0",&d0)) d0=1;
    if (!getparint("dd",&dd)) dd=1;
    /* error checking */
    m1 = 0;
    bcopy(&m1,&lofset,8);
    fseek64(infp,lofset,2);
    lofset = ftell64(infp);
    bcopy(&lofset,&m1,8);
    m1 = m1/sizeof(float);
    n123 = m1;
    m1 = 0;
    bcopy(&m1,&lofset,8);
    fseek64(infp,lofset,0);
    if ( n123 < n1*n2*n3 ) err("check input n1*n2*n3 \n");
    if (o1<1) o1=1; 
    if (o1>n1) o1=n1; 
    if (d1<1) d1=1; 
    if (d1>n1) d1=n1;
    if (o2<1) o2=1;
    if (o2>n2) o2=n3; 
    if (d2<1) d2=1;
    if (d2>n2) d2=n2;
    if (o3<1) o3=1;
    if (o3>n3) o3=n3; 
    if (d3<1) d3=1; 
    if (d3>n3) d3=n3;
    if (!getparint("n1o",&n1o)) n1o = (n1-o1)/d1+1;
    if (n1o >(n1-o1)/d1+1) n1o = (n1-o1)/d1+1;   
    if (!getparint("n2o",&n2o)) n2o = (n2-o2)/d2+1;
    if (n2o >(n2-o2)/d2+1) n2o = (n2-o2)/d2+1;   
    if (!getparint("n3o",&n3o)) n3o = (n3-o3)/d3+1;
    if (n3o >(n3-o3)/d3+1) n3o = (n3-o3)/d3+1;   
    /* allocate space */
    trace = (float*)malloc(n1*sizeof(float));
    traceo = (float*)malloc(n1o*sizeof(float));
    
    /* subsampling input data */
    if (dsort == 0 )
    {
       for(i3=o3;i3<=o3+(n3o-1)*d3;i3=i3+d3)
          {
          m3 = (i3-1)*n2;
          for(i2=o2;i2<=o2+(n2o-1)*d2;i2=i2+d2)
	     {
             m2 = m3 + (i2-1);  
	     m1 = m2*n1*4;
	     bcopy(&m1,&lofset,8);
	     fseek64(infp,lofset,0);
             if (fread(trace,sizeof(float),n1,infp)!=n1)
             err("Error reading input\n");
	     for(i1=0;i1<n1o;i1++) traceo[i1] = trace[o1-1+i1*d1];
             /* write output data */
             fwrite(traceo,sizeof(float),n1o,outfp);
	     }
          }
     }
     else
     {
       for(i3=0;i3<n3;i3++)
          {
          m3 = i3*n2;
	  i2 = d0 + i3*dd;
          m2 = m3 + (i2-1);  
	  m1 = m2*n1*4;
	  if (m2 < n2*n3)
	     {
	     bcopy(&m1,&lofset,8);
	     fseek64(infp,lofset,0);
             if (fread(trace,sizeof(float),n1,infp)!=n1);
             /* write output data */
             fwrite(trace,sizeof(float),n1,outfp);
	     }
          }
     }
}
示例#18
0
	// opens the read archive
	void CReadReader::Open(const string& filename) {

		if(mIsOpen) {
			cout << "ERROR: An attempt was made to open an already open read archive." << endl;
			exit(1);
		}

		mInputFilename = filename;

		if(fopen_s(&mInStream, filename.c_str(), "rb") != 0) {
			cout << "ERROR: Could not open the compressed read archive (" << mInputFilename << ") for reading." << endl;
			exit(1);
		}

		mIsOpen = true;

		// ===============
		// read the header
		// ===============

		// MOSAIK_SIGNATURE[6]       0  -  5
		// STATUS[1]                 6  -  6
		// SEQUENCING_TECHNOLOGY[1]  7  -  7
		// ARCHIVE_DATE[8]           8  - 15
		// NUM_READS[8]              16 - 23
		// NUM_BASES[8]              24 - 31
		// MEDIAN_FRAGMENT_LENGTH[4] 32 - 35
		// CENTER_NAME_LEN[1]        36 - 36
		// LIBRARY_NAME_LEN[1]       37 - 37
		// PLATFORM_UNIT_LEN[1]      38 - 38
		// READ_GROUP_ID_LEN[1]      39 - 39
		// SAMPLE_NAME_LEN[1]        40 - 40
		// DESCRIPTION_LEN[2]        41 - 42
		// RESERVED[8]               43 - 50
		// CENTER_NAME[*]            51
		// DESCRIPTION[*]
		// LIBRARY_NAME[*]
		// PLATFORM_UNIT[*]
		// READ_GROUP_ID[*]
		// SAMPLE_NAME[*]

		// skip the MOSAIK signature
		const unsigned char SIGNATURE_LENGTH = 6;
		fseek64(mInStream, SIGNATURE_LENGTH, SEEK_SET);

		// read the read status (currently single end or paired end)
		mStatus = (ReadStatus)fgetc(mInStream);

		// read the sequencing technology
		mReadGroup.SequencingTechnology = (SequencingTechnologies)fgetc(mInStream);
		if(mReadGroup.SequencingTechnology == ST_SOLID) mIsSOLiD = true;

		// skip the archive date
		fseek64(mInStream, SIZEOF_UINT64, SEEK_CUR);

		// retrieve the number of reads
		fread((char*)&mNumReads, SIZEOF_UINT64, 1, mInStream);

		// retrieve the number of bases
		fread((char*)&mNumBases, SIZEOF_UINT64, 1, mInStream);

		// read the median fragment length
		fread((char*)&mReadGroup.MedianFragmentLength, SIZEOF_INT, 1, mInStream);

		// read the metadata string lengths
		const unsigned char centerNameLen   = (unsigned char)fgetc(mInStream);
		const unsigned char libraryNameLen  = (unsigned char)fgetc(mInStream);
		const unsigned char platformUnitLen = (unsigned char)fgetc(mInStream);
		const unsigned char readGroupIDLen  = (unsigned char)fgetc(mInStream);
		const unsigned char sampleNameLen   = (unsigned char)fgetc(mInStream);

		unsigned short descriptionLen = 0;
		fread((char*)&descriptionLen, SIZEOF_SHORT, 1, mInStream);

		mReadGroup.CenterName.resize(centerNameLen);
		mReadGroup.LibraryName.resize(libraryNameLen);
		mReadGroup.PlatformUnit.resize(platformUnitLen);
		mReadGroup.ReadGroupID.resize(readGroupIDLen);
		mReadGroup.SampleName.resize(sampleNameLen);
		mReadGroup.Description.resize(descriptionLen);

		// skip the reserved bytes
		fseek64(mInStream, SIZEOF_UINT64, SEEK_CUR);

		// read the metadata strings
		if(centerNameLen > 0)   fread((void*)mReadGroup.CenterName.data(),   centerNameLen,   1, mInStream);
		if(descriptionLen > 0)  fread((void*)mReadGroup.Description.data(),  descriptionLen,  1, mInStream);
		if(libraryNameLen > 0)  fread((void*)mReadGroup.LibraryName.data(),  libraryNameLen,  1, mInStream);
		if(platformUnitLen > 0) fread((void*)mReadGroup.PlatformUnit.data(), platformUnitLen, 1, mInStream);

		fread((void*)mReadGroup.ReadGroupID.data(),  readGroupIDLen,  1, mInStream);
		fread((void*)mReadGroup.SampleName.data(),   sampleNameLen,   1, mInStream);

		// create our read group code
		mReadGroup.ReadGroupCode = ReadGroup::GetCode(mReadGroup);

		// get the reads offset
		mReadsOffset = ftell64(mInStream);

		//// DEBUG
		//cout << "# reads:                " << mNumReads << endl;
		//cout << "# bases:                " << mNumBases << endl;
		//cout << "median fragment length: " << mReadGroup.MedianFragmentLength << endl;
		//cout << "center name:            " << mReadGroup.CenterName << endl;
		//cout << "library name:           " << mReadGroup.LibraryName << endl;
		//cout << "platform unit:          " << mReadGroup.PlatformUnit << endl;
		//cout << "read group ID:          " << mReadGroup.ReadGroupID << endl;
		//cout << "sample name:            " << mReadGroup.SampleName << endl;
		//cout << "description:            " << mReadGroup.Description << endl;
		//exit(1);
	}