void SC_StringBuffer::vappendf(const char* fmt, va_list ap)
{
	va_list ap2;
	size_t remaining = getRemaining();

	// Calling vsnprintf may invalidate vargs, so keep a copy
#ifdef __va_copy
	__va_copy(ap2, ap);
#else
	ap2 = ap;
#endif

	// NOTE: This only works since glibc 2.0.6!
	int size = vsnprintf(mPtr, remaining, fmt, ap);
	va_end(ap);

	// size returned excludes trailing \0
	if (size++ > 0) {
		if ((size_t)size > remaining) {
			growBy(size - remaining);
			vsnprintf(mPtr, size, fmt, ap2);
		}
		mPtr += size-1; // omit trailing \0
	}

	va_end(ap2);
}
//=============================================================================
// METHOD : SPELLcompression::readMoreBytes
//=============================================================================
unsigned int SPELLcompression::readMoreBytes()
{
	unsigned int numRead = -1;
	unsigned int remainingData = getRemaining();
	//CDEBUG("read more bytes (remaining: " << remainingData << ")");

	// If the read position is exactly at the end
	if ( remainingData == 0 )
	{
		//CDEBUG("  no remaining data");
		return 0;
	}
	// If the remaining bytes in the input data are less size than the chunk
	else if ( remainingData  <  COMPRESSION_CHUNK_SIZE )
	{
		// Return the remaining
		numRead = remainingData;
	}
	// If the remaining data is greather than a chunk
	else
	{
		// Just read a chunk
		numRead = COMPRESSION_CHUNK_SIZE;
	}
	// Note that start of source is positioned by m_totalReadBytes
	memcpy( m_processInput, m_inputData.c_str() + m_totalReadBytes, numRead );
	// Shift the read position the amount of bytes read
	m_totalReadBytes += numRead;
	//CDEBUG("  bytes read so far   :" << m_totalReadBytes);
	//CDEBUG("  remaining data now  :" << getRemaining());
	//CDEBUG("read bytes            :" << numRead);
	return numRead;
}
//=============================================================================
// METHOD : SPELLcompression::compress
//=============================================================================
std::string SPELLcompression::compress()
{
	int ret, flush;

	//CDEBUG("Original data: " << SPELLutils::dumpString(m_inputData) );
	//CDEBUG("initializing deflate");
	//CDEBUG("total input " << m_totalInputLen);
	//CDEBUG("remaining   " << getRemaining());

	// Initialize the deflate algorithm
	int windowBits = 15 + 16;
	ret = deflateInit2( &m_stream, Z_BEST_SPEED, Z_DEFLATED, windowBits, 8, Z_DEFAULT_STRATEGY );
	if (ret != Z_OK)
	{
		THROW_FATAL_EXCEPTION("Unable to compress", "Cannot initialize deflate algorithm", SPELL_ERROR_UTILS);
	}

	// Compress until the end of the input data
	do
	{
		// Copy the next bytes on the input buffer and returns the number of read bytes
		m_stream.avail_in = readMoreBytes();

		flush = (getRemaining()==0) ? Z_FINISH : Z_NO_FLUSH;
		m_stream.next_in = (Bytef*) m_processInput;

		// Run deflate on input until output buffer not full, finish compression
		// if all of source has been read in
		do
		{
			m_stream.avail_out = COMPRESSION_CHUNK_SIZE;
			m_stream.next_out = (Bytef*) m_processOutput;
        	//CDEBUG("calling deflate");
			ret = deflate( &m_stream, flush );
        	//CDEBUG("deflate result " << ret);
			assert( ret != Z_STREAM_ERROR );
			unsigned int numCompressed = COMPRESSION_CHUNK_SIZE - m_stream.avail_out;
        	//CDEBUG("number of deflated bytes " << numCompressed);
			writeNextBytes(numCompressed);
		}
		while( m_stream.avail_out == 0 );
		assert(m_stream.avail_in == 0);

    	//CDEBUG("deflate cycle done");

	}
	while( flush != Z_FINISH );
	assert( ret == Z_STREAM_END );


	deflateEnd(&m_stream);

	//CDEBUG("Compressed data: " << SPELLutils::dumpString(m_outputData.str()));
	//CDEBUG("Total written " << m_totalWriteBytes);

	return m_outputData.str();
}
Exemple #4
0
bool XML::writeDataToXML(MAUtil::String filename)
{
	MAHandle tfile;
	int dataLen = 256; //we set the dataLen to a fixed size for now.
	byte *data;
	//int fileSize;

	tfile = maFileOpen(filename.c_str(), MA_ACCESS_READ_WRITE);

	if(!maFileExists(tfile))
	{
		lprintfln("File does not exist: %s", filename.c_str());
		return false;
	}


	data = new byte[dataLen];

	while(getRemaining(tfile))
	{
		int rwResult;

		if(getRemaining(tfile) < 256)
		{
			dataLen = getRemaining(tfile);
		}

		rwResult = maFileRead(tfile, data, dataLen);
		rwResult += maFileWrite(file, data, dataLen);

		if(rwResult != 0)
		{
			return false;
		}

		memset(data, 0 , 256);
	}

	delete data;

	maFileClose(tfile);

	return true;
}
Exemple #5
0
void
CharBuffer::put(const char * src, size_t n)
{
    if (n > getRemaining()) {
        resize(_pos + (n * 2));
    }
    char * dst = &_buffer[_pos];
    memcpy(dst, src, n);
    _pos += n;
}
void SC_StringBuffer::append(const char* src, size_t size)
{
	if (size > 0) {
		size_t remaining = getRemaining();
		if (size > remaining) {
			growBy(size - remaining);
		}
		memcpy(mPtr, src, size);
		mPtr += size;
	}
}
void PhaseDecoder::decode() {
	makeColor();
	makePhase();
	memcpy( wrappedPhase, phase, sizeof(float) * width * height);
	for (int pass = 0; pass < maxPasses; pass++) {
		unwrapPhase();
		if (minRemaining != 0 && getRemaining() < minRemaining)
			break;
	}
	if (phasePersistence)
		memcpy(lastPhase, phase, sizeof(float) * width * height);
	makeDepth();
}
Exemple #8
0
/* move in the direction 'dir' */	
void move(char dir, int undo, int redo) {
	int dirX = 0, dirY = 0;
	if (dir == PUSH_RIGHT || dir == RIGHT) dirX = 1;
	else if (dir == PUSH_LEFT || dir == LEFT) dirX = -1;
	else if (dir == PUSH_DOWN || dir == DOWN) dirY = 1;
	else if (dir == PUSH_UP || dir == UP) dirY = -1;
	if (!playing) return;
	if (!undo && !redo) {
		reset_undone_ptr();
		undone_length = 0;
	}
	int posX = getX(), posY = getY();
	int valid = validMove(posX, posY, posX+dirX, posY+dirY);
	if (!valid) play_sound(HIT_WALL);
	else {
		int box = moveTo(posX, posY, posX+dirX, posY+dirY);
		if (!undo && !replaying) updatePath(dirX, dirY, box);
	}
	
	if (getRemaining() == 0) playing = 0;
}
Exemple #9
0
		// Returns a friendly string for the total size of files remaining to download in torrent
		inline std::string getTextRemaining()
		{
			return getFileSizeString(getRemaining());
		}
Exemple #10
0
std::string gt::Torrent::getTextRemaining()
{
	return getFileSizeString(getRemaining());
}
Exemple #11
0
void AdjKeys::recombine()
{
	int currKey = startKey;
	offSpring[0] = currKey;
	deleteTakenKey(currKey);

	int i;
	// produce offspring based on adjacent keys

	for(i = 1; i < nof; i++)
	{
		int flag = false;
		int tempKeys[4] = {-1, -1, -1, -1};
		int w = 0;
		for(int j = 0; j < 4; j++)
		{
			int t = aKeys[currKey][j];
			if(t != -1 && hasSharing(t))
			{
				currKey = t;
				flag = true;
				break;
			}
			else
			{
				if(t != -1)
					tempKeys[w++] = t;
				continue;
			}
		}
		if(!flag)
		{
			if(w != 0)
			{
				int tk = tempKeys[0];
				int moreAdjLeft = getRemaining(tk);
				currKey = tk;
				for(int v = 1; v < w; v++)
				{
					tk = tempKeys[v];
					int adjLeft = getRemaining(tk);
					if(adjLeft > moreAdjLeft)
					{
						moreAdjLeft = adjLeft;
						currKey = tk;
					}
				}
			}
			else
				break;
		}
		offSpring[i] = currKey;
		deleteTakenKey(currKey);
	}	

	if(i < nof)  // handle special case
	{
		for(int x = 0; x < nof; x++) 
		{
			if(getRemaining(x) != 0)
			{
				for(int y = 0; y < 4; y++)
				{
					if(aKeys[x][y] != -1)
					{
						offSpring[i++] = aKeys[x][y];
						deleteTakenKey(aKeys[x][y]);
					}
				}
			}
		}
	}
}