/* A little utility to fetch a web page and send the raw data * to the provided FILE pointer. This ptr could be "stdout" to * display on the console. * * Example: * TA_WebFetch( "www.yahoo.com", stdout ); * or * TA_WebFetch( "http://www.yahoo.com/mt", myFile ); */ TA_RetCode TA_WebFetch( TA_Libc *libHandle, const char *url, FILE *out ) { TA_RetCode retCode; TA_WebPage *webPage; const char *stringStart; const char *webSitePage; char *allocString; unsigned int webSiteLength; if( !url ) return TA_BAD_PARAM; allocString = NULL; /* Skip the http:// if specified. */ stringStart = url; if( strncmp( url, "http://", 7 ) == 0 ) stringStart += 7; /* Find if there is a specifc web page specified. */ webSitePage = strchr( stringStart, '/' ); if( webSitePage ) { webSitePage++; if( *webSitePage == '\0' ) { webSitePage = NULL; } else { webSiteLength = webSitePage - stringStart - 1; allocString = (char *)TA_Malloc( libHandle, webSiteLength+1 ); if( !allocString ) return TA_ALLOC_ERR; strncpy( allocString, stringStart, webSiteLength ); allocString[webSiteLength] = '\0'; stringStart = allocString; } } retCode = TA_WebPageAlloc( libHandle, stringStart, webSitePage, NULL, NULL, &webPage, 2 ); if( allocString ) TA_Free( libHandle, allocString ); if( retCode == TA_SUCCESS ) { retCode = TA_StreamToFile( webPage->content, out ); TA_WebPageFree( webPage ); } return retCode; }
TA_RetCode TA_WebPageAllocFromYahooName( TA_Libc *libHandle, const TA_DecodingParam *decodingParam, const char *yahooName, TA_WebPage **allocatedWebPage ) { TA_PROLOG; TA_RetCode retCode; char webSitePage[300]; unsigned int prefixLength, suffixLength, symbolLength, i; const char *webSiteAddr; const char *uirPrefix, *uirSuffix; TA_WebPage *webPage; TA_TRACE_BEGIN( libHandle, TA_WebPageAllocFromYahooName ); retCode = TA_INTERNAL_ERROR(117); if( !decodingParam || !yahooName || !allocatedWebPage ) { TA_TRACE_RETURN( TA_BAD_PARAM ); } webSiteAddr = decodingParam->webSiteServer; TA_ASSERT( libHandle, webSiteAddr != NULL ); uirPrefix = decodingParam->uirPrefix; TA_ASSERT( libHandle, uirPrefix != NULL ); prefixLength = strlen( uirPrefix ); uirSuffix = decodingParam->uirSuffix; TA_ASSERT( libHandle, uirSuffix != NULL ); suffixLength = strlen( uirSuffix ); symbolLength = strlen( yahooName ); if( (symbolLength + suffixLength + prefixLength) >= 299 ) { TA_TRACE_RETURN( TA_INVALID_SECURITY_EXCHANGE ); } sprintf( webSitePage, "%s%s%s", uirPrefix, yahooName, uirSuffix ); /* Get the Web Page */ for( i=0; i < 10; i++ ) { retCode = TA_WebPageAlloc( libHandle, webSiteAddr, webSitePage, NULL, NULL, &webPage, 10 ); if( retCode == TA_SUCCESS ) break; else { /* Yahoo! is may be slow, let's sleep 1 minute */ TA_Sleep( 60 ); } } if( retCode != TA_SUCCESS ) { TA_TRACE_RETURN( retCode ); } *allocatedWebPage = webPage; TA_TRACE_RETURN( TA_SUCCESS ); }