15 memback(void *contents, size_t size, size_t nmemb, void *userp)
17 size_t realsize = size * nmemb;
18 struct MemoryStruct *mem = (struct MemoryStruct *)userp;
20 char *ptr = realloc(mem->memory, mem->size + realsize + 1);
23 printf("not enough memory (realloc returned NULL)\n");
28 memcpy(&(mem->memory[mem->size]), contents, realsize);
29 mem->size += realsize;
30 mem->memory[mem->size] = 0;
36 main(int argc, char *argv[])
39 CURL *curl = curl_easy_init();
43 fprintf(stderr, "ERROR: Failed to initialize cURL\n");
47 struct MemoryStruct chunk;
48 chunk.memory = malloc(1); // will be grown as needed by the realloc above
49 chunk.size = 0; // no data at this point
51 curl_easy_setopt(curl, CURLOPT_URL,
52 "https://nobilis.nobles.edu/skyworld/castlemenu.php");
53 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, memback);
54 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
55 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
57 printf("INFO: Fetching wepage\n");
58 res = curl_easy_perform(curl);
60 if (res != CURLE_OK) {
61 fprintf(stderr,"ERROR: Could not fetch webpage\n%s\n",
62 curl_easy_strerror(res));
66 curl_easy_cleanup(curl);
69 printf("INFO: Parsing HTML\n");
71 char *outp = (char *) malloc(strlen(chunk.memory) + 1); // TODO: Realloc?
74 // Extract text from between HTML tags
75 for (int i = 345; i < strlen(chunk.memory); i++) {
76 char c = chunk.memory[i];
77 if (c == '<') intag = true;
78 if (!intag) strncat(outp, &c, 1);
79 if (c == '>') intag = false;
82 // Strip empty newlines
83 char *nl = (char *) malloc(strlen(outp));
85 for (int i = 0; i < strlen(outp) - 1; i++) {
86 if (outp[i] == '\n' && outp[i+1] == '\n') i+=4;
87 strncat(nl, &outp[i], 1);
96 XtSetLanguageProc (NULL, NULL, NULL);
97 toplevel = XtVaOpenApplication (&app, "Castle Menu", NULL, 0, &argc, argv,
98 NULL, sessionShellWidgetClass, NULL);
100 // Set textbox settings
102 XtSetArg(args[n], XmNvalue, nl); n++;
103 XtSetArg(args[n], XmNeditable, False); n++;
104 XtSetArg(args[n], XmNcolumns, 80); n++;
105 XtSetArg(args[n], XmNcursorPositionVisible, False); n++;
106 text_w = XmCreateScrolledText(toplevel, "text", args, n);
110 // Display everything
111 XtManageChild(text_w);
112 XtRealizeWidget(toplevel);