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