home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-06-28 | 8.0 KB | 329 lines | [TEXT/CWIE] |
- // --------------------------------------------------------------------------------------
- // FetchParseURL.c
- //
- // Written by Don Arbow and Marc A. Raiser, EveryDay Objects, Inc.
- // in one day - June 26, 1997
- // --------------------------------------------------------------------------------------
-
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
-
- #include "Subwoofer.h"
-
- #include "FetchParseURL.h"
- #include "HackWindows.h"
-
- #include <Folders.h>
- #include <Files.h>
- #include <Threads.h>
-
- #define DEBUG 0
-
- void PathNameFromDirID(long dirID, short vRefNum, StringPtr fullPathName);
- void PathNameFromWD(long vRefNum, StringPtr pathName);
- void pstrcat(StringPtr dst, StringPtr src);
- void pstrinsert(StringPtr dst, StringPtr src);
- void ParseFile(void);
-
- // GLOBALS
- Str255 uniqueName;
- char baseURL[256];
- FSSpec gFSSpec;
-
- void QueryURL(Str255 url)
- {
- OSErr err = 0;
- char urlName[255];
- short vRefNum;
- long dirID;
- unsigned long secs;
-
- BlockMoveData(url + 1, baseURL, url[0]);
- baseURL[url[0]] = '\0';
-
- // Define the URLReference
- BlockMoveData(url + 1, urlName, url[0]);
- urlName[url[0]] = '\0';
- #if DEBUG
- err = FindFolder(kOnSystemDisk, kDesktopFolderType, true, &vRefNum, &dirID);
- #else
- err = FindFolder(kOnSystemDisk, kTemporaryFolderType, true, &vRefNum, &dirID);
-
- #endif
- GetDateTime(&secs);
- NumToString(secs, uniqueName);
- FSMakeFSSpec(vRefNum, dirID, uniqueName, &gFSSpec);
- err = URLDownload(urlName, &gFSSpec, kURLDisplayProgressFlag + kURLDisplayAuthFlag);
-
- if (err == noErr) {
- ClearList();
- ParseFile();
- } else {
-
- ShowError(err);
- }
- #if !DEBUG
- FSpDelete(&gFSSpec);
-
- #endif
- }
-
- void ParseFile(void) {
-
- FILE *ref;
- Str255 pathName;
- char c;
- char tagBuf[1000], *p;
- char urlBuf[1000], *q, *z;
- Boolean searchTag;
- char absURL[256];
-
- strcpy(absURL, baseURL);
- PathNameFromDirID(gFSSpec.parID, gFSSpec.vRefNum, pathName);
- pstrcat(pathName, gFSSpec.name);
- p2cstr(pathName);
- // open
- ref = fopen((char*)pathName, "r");
-
- while (!feof(ref)) {
-
- // skip to begin of tag '<'
- while ((c = getc(ref)) != '<' && !feof(ref)) {
- }
-
- p = tagBuf;
- // skip to begin of tag '<'
- while ((c = getc(ref)) != '>' && !feof(ref)) {
- *p++ = tolower(c);
- }
- *p = '\0';
-
- p = tagBuf;
- searchTag = true;
- while (searchTag) {
- char term = '\0';
- while ((*p == ' ' || *p == '\t') && *p != '\0') p++; // skip whitespace
-
- if ((strncmp(p, "href", 4) == 0) || (strncmp(p, "src", 3) == 0)) {
-
- if (strncmp(p, "src", 3) == 0)
- p += 3;
- else
- p += 4;
-
- while (*p != ' ' && *p != '\t' && *p != '=') p++; // skip non-whitespace
-
- while (*p == ' ' || *p == '\t') p++; // skip whitespace
- if (*p++ == '=') {
- while (*p == ' ' || *p == '\t') p++; // skip whitespace
- if (*p == '\"') {
- term = '\"';
- p++;
- } else {
- term = ' ';
- }
- // copy until term
- q = urlBuf;
- while (*p != term)
- *q++ = *p++;
- *q = '\0';
- q = urlBuf;
-
- if ((q = strpbrk(urlBuf, "#")) != NULL)
- *q = '\0';
-
- if (urlBuf[0] == '/') {
- strncpy(absURL + strlen(baseURL), urlBuf, strlen(urlBuf) + 1);
- }
- // add to list
- c2pstr(urlBuf);
- AddListItem((unsigned char*)urlBuf);
- }
- }
- term = '\0';
- while (*p != ' ' && *p != '\t') {
- if (*p == term) {
- searchTag = false;
- break;
- }
- p++; // skip non-whitespace
- }
- }
- }
- fclose(ref);
- }
-
- void ShowError(OSErr err)
- {
- Str63 theString;
-
- switch ( err )
- {
- case noErr:
- return;
- break;
-
- case kURLInvalidURLReferenceError:
- ParamText("\pThat URL is not a valid link.", "\p", "\p", "\p");
- break;
- case kURLInvalidParameterError:
- ParamText("\p kURLInvalidParameterError", "\p", "\p", "\p");
- break;
- case kURLProgressAlreadyDisplayedError:
- ParamText("\p kURLProgressAlreadyDisplayedError", "\p", "\p", "\p");
- break;
- case kURLFileExistsError:
- ParamText("\pThat file already exists", "\p", "\p", "\p");
- break;
- case kURLInvalidURLError:
- ParamText("\pThat is not a valid URL.", "\p", "\p", "\p");
- break;
- case kURLUnsupportedSchemeError:
- ParamText("\pSorry, that internet protocol is not supported.", "\p", "\p", "\p");
- break;
- case kURLServerBusyError:
- ParamText("\pThe server is busy, please try later.", "\p", "\p", "\p");
- break;
- case kURLAuthenticationError:
- ParamText("\pCannot find that domain.", "\p", "\p", "\p");
- break;
- case kURLUnkownPropertyError:
- ParamText("\p kURLUnkownPropertyError", "\p", "\p", "\p");
- break;
- case kURLPropertyBufferToSmallError:
- ParamText("\p kURLPropertyBufferToSmallError", "\p", "\p", "\p");
- break;
- case kURLUnsetablePropertyError:
- ParamText("\p kURLUnsetablePropertyError", "\p", "\p", "\p");
- break;
- case kURLInvalidCallError:
- ParamText("\p kURLInvalidCallError", "\p", "\p", "\p");
- break;
- case kURLNoInternetConfigError:
- ParamText("\pYou must have Internet Config to run this hack.", "\p", "\p", "\p");
- break;
- case kURLUserCancelledError:
- ParamText("\pDownload has been aborted per your request.", "\p", "\p", "\p");
- break;
- case kURLFileEmptyError:
- ParamText("\pThe resource you requested is empty", "\p", "\p", "\p");
- break;
- case kURLResourceNotFoundError:
- ParamText("\pThe resource you requested was not found", "\p", "\p", "\p");
- break;
- #if 0
- // unknown at this time
- case kURLExtensionFailureError:
- ParamText("\p kURLExtensionFailureError", "\p", "\p", "\p");
- break;
- #endif
-
- default :
- NumToString(err, theString);
- ParamText("\pUnknown error: ", theString, "\p", "\p");
- break;
- }
-
- StopAlert(128, nil);
- }
-
- // Assumes inclusion of <MacHeaders>
- #define haveAUX() 0
-
- /*
- * Pascal string utilities
- */
- /*
- * pstrcat - add string 'src' to end of string 'dst'
- */
- void pstrcat(StringPtr dst, StringPtr src)
- {
- /* copy string in */
- BlockMove(src + 1, dst + *dst + 1, *src);
- /* adjust length byte */
- *dst += *src;
- }
-
- /*
- * pstrinsert - insert string 'src' at beginning of string 'dst'
- */
- void pstrinsert(StringPtr dst, StringPtr src)
- {
- /* make room for new string */
- BlockMove(dst + 1, dst + *src + 1, *dst);
- /* copy new string in */
- BlockMove(src + 1, dst + 1, *src);
- /* adjust length byte */
- *dst += *src;
- }
-
- void PathNameFromDirID(long dirID, short vRefNum, StringPtr fullPathName)
- {
- CInfoPBRec block;
- Str255 directoryName;
- OSErr err;
-
- memset(&block, '\0', sizeof(CInfoPBRec));
-
- fullPathName[0] = '\0';
-
- block.dirInfo.ioDrParID = dirID;
- block.dirInfo.ioNamePtr = directoryName;
- do {
- block.dirInfo.ioVRefNum = vRefNum;
- block.dirInfo.ioFDirIndex = -1;
- block.dirInfo.ioDrDirID = block.dirInfo.ioDrParID;
- err = PBGetCatInfoSync(&block);
- if (haveAUX()) {
- if (directoryName[1] != '/')
- /* If this isn't root (i.e. "/"), append a slash ('/') */
- pstrcat(directoryName, (StringPtr)"\p/");
- }
- else
- pstrcat(directoryName, (StringPtr)"\p:");
- pstrinsert(fullPathName, directoryName);
- } while (block.dirInfo.ioDrDirID != 2);
- }
-
- /*
- PathNameFromWD:
- Given an HFS working directory, this routine returns the full pathname that
- corresponds to it. It does this by calling PBGetWDInfo to get the VRefNum and
- DirID of the real directory. It then calls PathNameFromDirID, and returns its
- result.
-
- */
- void PathNameFromWD(long vRefNum, StringPtr pathName)
- {
- WDPBRec myBlock;
- OSErr err;
- /*
- PBGetWDInfo has a bug under A/UX 1.1. If vRefNum is a real
- vRefNum and not a wdRefNum, then it returns garbage.
- Since A/UX has only 1 volume (in the Macintosh sense) and
- only 1 root directory, this can occur only when a file has been
- selected in the root directory (/).
- So we look for this and hardcode the DirID and vRefNum.
- */
- if ((haveAUX()) && (vRefNum == -1))
- PathNameFromDirID(2, -1, pathName);
- else {
- myBlock.ioNamePtr = nil;
- myBlock.ioVRefNum = vRefNum;
- myBlock.ioWDIndex = 0;
- myBlock.ioWDProcID = 0;
- /*
- Change the Working Directory number in vRefnum into a real
- vRefnum and DirID. The real vRefnum is returned in ioVRefnum,
- and the real DirID is returned in ioWDDirID.
- */
- err = PBGetWDInfoSync(&myBlock);
- if (err != noErr)
- return;
- PathNameFromDirID(myBlock.ioWDDirID, myBlock.ioWDVRefNum,
- pathName);
- }
- }
-
-