This section describes how the sample application Downloader downloads data from multiple URLs and stores it in multiple files using the URL Access Manager function
URLDownload
. Downloader obtains the URLs to be downloaded by reading a text file in which they have been stored.
Listing 2-11 illustrates how Downloader's main function sets up the main event loop and calls the function
getURL
to obtain a URL from a file of URLs.
Listing 2-11 The Downloader application's main function
#include <Events.h> #include <stdio.h> #include "URLAccess.h" #include "string.h" #include "Memory.h" void main (void){ OSStatus err = noErr; char url[255]; int count, fileCount = 0; EventRecord ev; // Call MaxApplZone, MoreMasters. // Initialize graphics port, fonts, menus, cursor, and dialogs. // Clear the screen. while ( url != NULL ) { // Handle Events through each loop WaitNextEvent(everyEvent, &ev, 0, NULL); eventHandler( NULL, &ev ); // Obtain a URL from the file of URLs result = getURL(url); // getURL function not shown if ( result == eofErr ) { // Handle error condition. } // Call Download function. result = DoDownload( url ); if ( result != noErr) { // Handle error condition. } { printf("\n All of the URLs have been downloaded.\n"); }
The
DoDownload
function shown in Listing 2-12 does the actual work of downloading data from the URL. It creates a file specification for the data that is to be downloaded and a URL reference. It specifies the mask kURLReplaceExistingFlag in the
openFlags
parameter to replace an existing file (if any) with the downloaded data and to display a progress indicator during the download. Lastly, it calls the function
URLDownload
to download the data.
Listing 2-12 Downloader's
void DoDownload (void){ URLReference urlRef; FSSpec dest, *destPtr = NULL; destPtr = &dest; Handle destHandle = NULL; int openFlags = kURLReplaceExistingFlag + kURLDisplayProgressFlag; Str255 newFile; // Create the file specification for the download. sprintf((char*)newFile, "File %d", fileCount); c2pstr((char*)newFile); fileCount++; err = FSMakeFSSpec(0, 0, newFile, &dest); // Create the URLReference. err = URLNewReference( theURL, &urlRef ); if (err != noErr) printf("URLNewReference failed\n"); // Download the data. err = URLDownload( urlRef, destPtr, destHandle, openFlags, &eventHandler, (void*)&fileCount ); if (err != noErr) printf("URLDownload failed\n"); // Clean up. err = URLDisposeReference( urlRef ); if (err != noErr) printf("URLDisposeReference failed\n"); return err; }DoDownload
function
Listing 2-10 illustrates Downloader's general
event handling function eventHandler
. This function handles system events that might occur during calls to the functions
URLSimpleDownload
,
URLDownload
,
URLSimpleUpload
, and
URLUpload
. The
userContext
parameter can be used to associate any particular call of
URLDownload
with any particular call of the system event callback function. In this context, it is an integer.
Listing 2-13 Downloader's system event callback function
pascal long eventHandler( void * userContext, EventRecord* eventPtr ) { EventRecord* ev; int what = 0; int context = 0; int* intPtr = NULL; // Convert the event pointer into an event record. ev = (EventRecord*)eventPtr; what = ev->what; // Convert the void* to an integer. intPtr = (int*)userContext; context = *intPtr; if (context < 0 || context > 99) context = -1; // Unknown context switch (what) { case 0 : // Null Event break; case mouseDown: printf("Handler Called: mouseDown User Context: %d\n", context); // Call function to handle event. break; case updateEvt: printf("Handler Called: updateEvt User Context: %d\n", context); // Call function to handle event. break; case activateEvt: printf("Handler Called: activateEvt User Context: %d\n", context); // Call function to handle event. break; case keyDown: printf("Handler Called: keyDown User Context: %d\n", context); // Call function to handle event. break; default: printf("Handler Called: Default User Context: %d\n", context); break; } return NULL; }