Parse and display the list of disks.

This commit is contained in:
Stephen Heumann 2019-04-13 23:25:02 -05:00
parent 72411079f9
commit 9f245a3f79
3 changed files with 72 additions and 11 deletions

View File

@ -11,6 +11,8 @@
#include <lineedit.h> #include <lineedit.h>
#include <list.h> #include <list.h>
#include <desk.h> #include <desk.h>
#include <quickdraw.h>
#include <qdaux.h>
#include <gsos.h> #include <gsos.h>
#include <orca.h> #include <orca.h>
#include <finder.h> #include <finder.h>
@ -22,6 +24,8 @@
#include "http.h" #include "http.h"
#include "readtcp.h" #include "readtcp.h"
#include "tcpconnection.h" #include "tcpconnection.h"
#include "json.h"
#include "jsonutil.h"
static char menuTitle[] = "\pArchive.org Disk Browser\xC9"; static char menuTitle[] = "\pArchive.org Disk Browser\xC9";
static char windowTitle[] = "\p Archive.org Disk Browser "; static char windowTitle[] = "\p Archive.org Disk Browser ";
@ -63,10 +67,13 @@ boolean gsDisksOnly = true;
struct diskListEntry { struct diskListEntry {
char *memPtr; char *memPtr;
Byte memFlag; Byte memFlag;
char *idPtr;
}; };
struct diskListEntry diskList[DISK_LIST_LENGTH]; struct diskListEntry diskList[DISK_LIST_LENGTH];
int diskListPos = 0;
static struct MountURLRec mountURLRec = {sizeof(struct MountURLRec)}; static struct MountURLRec mountURLRec = {sizeof(struct MountURLRec)};
Session sess = {0}; Session sess = {0};
@ -75,6 +82,11 @@ Session sess = {0};
/* See Prog. Ref. for System 6.0, page 20. */ /* See Prog. Ref. for System 6.0, page 20. */
static NDASysWindRecord sysWindRecord; static NDASysWindRecord sysWindRecord;
/* Buffer for data received from network */
char *netBuf = NULL;
json_value *json = NULL;
void InstallMenuItem(void) { void InstallMenuItem(void) {
static MenuItemTemplate menuItem = { static MenuItemTemplate menuItem = {
/* .version = */ 0x8000, /* show dividing line */ /* .version = */ 0x8000, /* show dividing line */
@ -127,6 +139,14 @@ void CloseBrowserWindow(void) {
CloseResourceFile(resourceFileID); CloseResourceFile(resourceFileID);
resourceFileOpened = false; resourceFileOpened = false;
} }
free(netBuf);
netBuf = NULL;
if (json) {
json_value_free(json);
json = NULL;
}
} }
#pragma databank 0 #pragma databank 0
@ -162,11 +182,28 @@ boolean DoLEEdit (int editAction) {
return ((id == searchLine) || (id == pageNumberLine)); return ((id == searchLine) || (id == pageNumberLine));
} }
boolean processDoc(json_value *docObj) {
if (diskListPos >= DISK_LIST_LENGTH)
return false;
if (docObj == NULL || docObj->type != json_object)
return false;
json_value *id = findEntryInObject(docObj, "identifier", json_string);
json_value *title = findEntryInObject(docObj, "title", json_string);
if (id == NULL || title == NULL)
return true;
diskList[diskListPos].idPtr = id->u.string.ptr;
diskList[diskListPos++].memPtr = title->u.string.ptr;
return true;
}
/* Do a search */ /* Do a search */
void DoSearch(void) { void DoSearch(void) {
static char searchURL[] = "http://archive.org/advancedsearch.php?q=emulator%3Aapple3&fl%5B%5D=identifier&fl%5B%5D=title&rows=3&page=1&output=json"; static char searchURL[] = "http://archive.org/advancedsearch.php?q=emulator%3Aapple3&fl%5B%5D=identifier&fl%5B%5D=title&rows=3&page=1&output=json";
enum NetDiskError result; enum NetDiskError result;
WaitCursor();
result = SetURL(&sess, searchURL, FALSE, FALSE); result = SetURL(&sess, searchURL, FALSE, FALSE);
//TODO enable this once we have real code to build the URL //TODO enable this once we have real code to build the URL
//if (result != OPERATION_SUCCESSFUL) //if (result != OPERATION_SUCCESSFUL)
@ -180,18 +217,30 @@ void DoSearch(void) {
if (sess.contentLength == 0 || sess.contentLength > 0xffff) if (sess.contentLength == 0 || sess.contentLength > 0xffff)
sess.contentLength = 0xffff; sess.contentLength = 0xffff;
char *buf = malloc(sess.contentLength + 1); free(netBuf);
if (buf == NULL) netBuf = malloc(sess.contentLength + 1);
if (netBuf == NULL)
goto errorReturn; goto errorReturn;
InitReadTCP(&sess, sess.contentLength, buf); InitReadTCP(&sess, sess.contentLength, netBuf);
while (TryReadTCP(&sess) == rsWaiting) while (TryReadTCP(&sess) == rsWaiting)
/* keep reading */ ; /* keep reading */ ;
*(buf + (sess.contentLength - sess.readCount)) = 0; sess.contentLength -= sess.readCount;
*(netBuf + (sess.contentLength)) = 0;
json = json_parse(netBuf, sess.contentLength);
if (json == NULL)
goto errorReturn;
json_value *response = findEntryInObject(json, "response", json_object);
if (response == NULL)
goto errorReturn;
diskListPos = 0;
json_value *docs = findEntryInObject(response, "docs", json_array);
processArray(docs, json_object, processDoc);
//TODO
for (int i = 0; i < DISK_LIST_LENGTH; i++) { for (int i = 0; i < DISK_LIST_LENGTH; i++) {
diskList[i].memPtr = buf;
diskList[i].memFlag = 0; diskList[i].memFlag = 0;
} }
@ -199,7 +248,7 @@ void DoSearch(void) {
CtlRecHndl disksListHandle = GetCtlHandleFromID(window, disksList); CtlRecHndl disksListHandle = GetCtlHandleFromID(window, disksList);
HiliteControl(noHilite, disksListHandle); HiliteControl(noHilite, disksListHandle);
NewList2(NULL, 1, (Ref) diskList, refIsPointer, NewList2(NULL, 1, (Ref) diskList, refIsPointer,
DISK_LIST_LENGTH, (Handle)disksListHandle); diskListPos, (Handle)disksListHandle);
SetCtlMoreFlags( SetCtlMoreFlags(
GetCtlMoreFlags(disksListHandle) | fCtlCanBeTarget | fCtlWantEvents, GetCtlMoreFlags(disksListHandle) | fCtlCanBeTarget | fCtlWantEvents,
@ -213,12 +262,14 @@ void DoSearch(void) {
ShowControl(GetCtlHandleFromID(window, nextPageButton)); ShowControl(GetCtlHandleFromID(window, nextPageButton));
EndTCPConnection(&sess); EndTCPConnection(&sess);
InitCursor();
return; return;
errorReturn: errorReturn:
EndTCPConnection(&sess); EndTCPConnection(&sess);
InitCursor();
// TODO show error message // TODO show error message
//SysBeep(); SysBeep();
} }
/* Handle an event after TaskMasterDA processing */ /* Handle an event after TaskMasterDA processing */

View File

@ -133,7 +133,7 @@ resource rControlTemplate (disksList) {
$0000, /* List Type */ $0000, /* List Type */
0, /* List Start */ 0, /* List Start */
10, /* ListMemHeight */ 10, /* ListMemHeight */
5, /* List Mem Size */ 9, /* List Mem Size */
0, /* List Ref */ 0, /* List Ref */
0 /* Color Ref */ 0 /* Color Ref */
}}; }};

View File

@ -70,7 +70,17 @@ int main(int argc, char **argv) {
while (TryReadTCP(&sess) == rsWaiting) while (TryReadTCP(&sess) == rsWaiting)
/* keep reading */ ; /* keep reading */ ;
printf("Read %lu bytes\n", sess.contentLength - sess.readCount); //printf("sess.contentLength = %lu\n", sess.contentLength);
//printf("sess.readCount = %lu\n", sess.readCount);
sess.contentLength -= sess.readCount;
printf("Read %lu bytes:\n", sess.contentLength);
for (unsigned long i = 0; i < sess.contentLength; i++) {
putchar(buf[i]);
}
putchar('\n');
cleanup: cleanup:
EndTCPConnection(&sess); EndTCPConnection(&sess);