Ejemplo n.º 1
0
bool DynamicBlockBuffer::atomicAppendNewBlock(BlockStreamBase* new_block){
	lock_.acquire();
	const bool ret=appendNewBlock(new_block);
	lock_.release();
	return ret;
}
static bool readNextBlock(BinaryFileReaderData * data, char ** lastChrom, int * lastStart, bool * pointByPoint) {
	BlockData * block = calloc(1, sizeof(BlockData));
	char ** chromPtr = block->chroms;
	int * startPtr = block->starts;
	int * finishPtr = block->finishes;
	double * valuePtr = block->values;
	char c;
	bool startSet = true;
	int32_t holder;
	float holder2;

	for (block->count = 0; block->count < BLOCK_LENGTH; block->count++) {
		// Check whether file finished
		if (fread(&c, 1, 1, data->file) == 0) {
			appendNewBlock(data, block);
			// Increment counter to push the reader into NULL block
			pthread_mutex_lock(&data->count_mutex);
			data->count++;
			pthread_mutex_unlock(&data->count_mutex);
			return true;
		}
		
		if (*pointByPoint)
			startSet = false;

		// Read header
		if (c == 1) {
			// Point by Point
			*pointByPoint = true;
			mustRead(&c, 1, 1, data->file);
			if (c) {
				*chromPtr = calloc(1000, 1);
				char * ptr = * chromPtr;
				int pos;
				for (pos = 0; pos < 1000 && c; pos++) {
					*ptr = c;
					mustRead(&c, 1, 1, data->file);
					ptr++;
				}
				while (c)
					mustRead(&c, 1, 1, data->file);
			}
			mustRead(&holder, sizeof(int32_t), 1, data->file);
			*startPtr = holder;
			startSet = true;
		} else if (c == 2) {
			// Normal
			*pointByPoint = false;
			mustRead(&c, 1, 1, data->file);
			if (c) {
				*chromPtr = calloc(1000, 1);
				char * ptr = * chromPtr;
				int pos;
				for (pos = 0; pos < 1000 && c; pos++) {
					*ptr = c;
					mustRead(&c, 1, 1, data->file);
					ptr++;
				}
				while (c)
					mustRead(&c, 1, 1, data->file);
			}
		}

		// Set chromosome if unset in header
		if (*chromPtr == NULL) 
			*chromPtr = *lastChrom;

		// Read coords
		if (*pointByPoint) {
			if (!startSet)
				*startPtr = *lastStart + 1;
			*finishPtr = *startPtr + 1;
		} else {
			mustRead(&holder, sizeof(int32_t), 1, data->file);
			*startPtr = holder;
			mustRead(&holder, sizeof(int32_t), 1, data->file);
			*finishPtr = holder;
		}

		// Read value
		mustRead(&holder2, sizeof(float), 1, data->file);
		*valuePtr = holder2;

		// Record stuff
		*lastChrom = *chromPtr;
		*lastStart = *startPtr;

		// Step ahead
		chromPtr++;
		startPtr++;
		finishPtr++;
		valuePtr++;
	}
	
	return appendNewBlock(data, block);
}