char *join( char **inStrings, int inNumParts, char *inGlue ) { StringBufferOutputStream *outStream = new StringBufferOutputStream(); for( int i=0; i<inNumParts - 1; i++ ) { outStream->writeString( inStrings[i] ); outStream->writeString( inGlue ); } // no glue after last string outStream->writeString( inStrings[ inNumParts - 1 ] ); char *returnString = outStream->getString(); delete outStream; return returnString; }
WebRequest::WebRequest( const char *inMethod, const char *inURL, const char *inBody, const char *inProxy ) : mError( false ), mURL( stringDuplicate( inURL ) ), mRequest( NULL ), mRequestPosition( -1 ), mResultReady( false ), mResult( NULL ), mSock( NULL ) { const char *startString = "http://"; char *urlCopy = stringDuplicate( inURL ); char *urlStart = stringLocateIgnoreCase( urlCopy, startString ); char *serverStart; if( urlStart == NULL ) { // no http:// at start of URL serverStart = urlCopy; } else { serverStart = &( urlStart[ strlen( startString ) ] ); } // find the first occurrence of "/", which is the end of the // server name char *serverNameCopy; char *requestHostNameCopy = stringDuplicate( serverStart ); if( inProxy != NULL ) { serverNameCopy = stringDuplicate( inProxy ); } else { // will be same as parsed, request host name copy later serverNameCopy = NULL; } char *getPath; if( inProxy != NULL ) { // for proxy, pass entire URL as method target getPath = (char*)inURL; } else { // for direct connection, pass only file sub-path from URL getPath = strstr( serverStart, "/" ); } char *hostNameEnd = strstr( requestHostNameCopy, "/" ); if( hostNameEnd == NULL ) { hostNameEnd = &( requestHostNameCopy[ strlen( requestHostNameCopy ) ] ); if( inProxy == NULL ) { getPath = (char *)"/"; } } // terminate the url here to extract the host name and port hostNameEnd[0] = '\0'; if( inProxy == NULL ) { serverNameCopy = stringDuplicate( requestHostNameCopy ); } int portNumber = 80; // look for a port number char *colon = strstr( serverNameCopy, ":" ); if( colon != NULL ) { char *portNumberString = &( colon[1] ); int numRead = sscanf( portNumberString, "%d", & portNumber ); if( numRead != 1 ) { portNumber = 80; } // terminate the name here so port isn't taken as part // of the address colon[0] = '\0'; } mSuppliedAddress = new HostAddress( stringDuplicate( serverNameCopy ), portNumber ); mNumericalAddress = NULL; mLookupThread = NULL; // launch right into name lookup mLookupThread = new LookupThread( mSuppliedAddress ); mSock = NULL; // compose the request into a buffered stream StringBufferOutputStream tempStream; tempStream.writeString( inMethod ); tempStream.writeString( " " ); tempStream.writeString( getPath ); tempStream.writeString( " HTTP/1.0\r\n" ); tempStream.writeString( "Host: " ); tempStream.writeString( requestHostNameCopy ); tempStream.writeString( "\r\n" ); if( inBody != NULL ) { char *lengthString = autoSprintf( "Content-Length: %d\r\n", strlen( inBody ) ); tempStream.writeString( lengthString ); delete [] lengthString; tempStream.writeString( "Content-Type: application/x-www-form-urlencoded\r\n\r\n" ); tempStream.writeString( inBody ); } else { tempStream.writeString( "\r\n" ); } mRequest = tempStream.getString(); mRequestPosition = 0; delete [] serverNameCopy; delete [] requestHostNameCopy; delete [] urlCopy; }
unsigned char *getAIFFHeader( int inNumChannels, int inSampleSizeInBits, int inSampleRateInHertz, int inNumSampleFrames, int *outHeaderLength ) { /* Header Information 32 bits 'FORM' 32 bits ckSize 32 bits 'AIFF' 32 bits 'COMM' 32 bits ckSize '18' 16 bits numChannels 32 bits num SampleFrames 16 bits sampleSize '16' 80 bits sampleRate { 16 bits = '16398' 16 bits = '44100' remaining 48 bits = '0' } 32 bits 'SSND' 32 bits ckSize 32 bits offset '0' 32 bits block size '0' FINALLY: sound data #bytes in 'FORM' chunk = bytes in sound data + 46 #bytes in 'SSND' chunk = bytes in sound data + 8 */ int soundSizeInBytes = ( inNumChannels * inNumSampleFrames * inSampleSizeInBits ) / 8; StringBufferOutputStream *headerStream = new StringBufferOutputStream(); headerStream->writeString( "FORM" ); // form chunk ID headerStream->writeLong( 46 + soundSizeInBytes ); // size of form chunk headerStream->writeString( "AIFF" ); // form type headerStream->writeString( "COMM" ); // common chunk ID headerStream->writeLong( 18 ); // common chunk size headerStream->writeShort( inNumChannels ); headerStream->writeLong( inNumSampleFrames ); // number of frames in sound // data headerStream->writeShort( inSampleSizeInBits ); // size of each sample headerStream->writeLong( inSampleRateInHertz | 16398<<16 ); // sample rate in Hz plus // mysterious most significant // bits headerStream->writeLong( 0 ); // 48 bits of 0 padding headerStream->writeShort( 0 ); headerStream->writeString( "SSND" ); // Sound chunk ID headerStream->writeLong( 8 + soundSizeInBytes ); // size of sound chunk headerStream->writeLong( 0 ); // offset headerStream->writeLong( 0 ); // block size unsigned char *returnBuffer = headerStream->getBytes( outHeaderLength ); delete headerStream; return returnBuffer; }