teaching machines

CS 330 Lecture 19 – Abstracting a GUI

March 9, 2012 by . Filed under cs330, lectures, spring 2012.

Agenda

TODO

Code

parse_xml.c

#include <expat.h>
#include <stdio.h>
#include <stdlib.h>

/* typedef parser_t * XML_Parser; */

/* ------------------------------------------------------------------------- */

void start(void *data,
           const char *tag,
           const char **attributes) {

  printf("data: %d\n", *((int *) data));

  printf("tag: %s\n", tag);
}

/* ------------------------------------------------------------------------- */

void end(void *data,
         const char *tag) {
  printf("tag: %s\n", tag);
}

/* ------------------------------------------------------------------------- */

int main(int argc, char **argv) {

  int foo = 17;

  XML_Parser parser = XML_ParserCreate(NULL);
  XML_SetElementHandler(parser, start, end);
  XML_SetUserData(parser, &foo);

  FILE *xml_file = fopen(argv[1], "r");
  int done = 0;

  const int BUFFER_SIZE = 8192;
  char buffer[BUFFER_SIZE];

  while (!done) {
    int length = fread(buffer, sizeof(char), BUFFER_SIZE, xml_file);
    done = feof(xml_file);
    XML_Parse(parser, buffer, length, done);
  }

  fclose(xml_file);
  XML_ParserFree(parser);

  return 0;
}

emailclient.xml

<?xml version="1.0" encoding="UTF-8"?>
<panel orientation="vertical">
 <panel orientation="horizontal">
  <label text="To:" />
  <textbox name="toBox" />
 </panel>
 <panel orientation="horizontal">
  <label text="Subject:" />
  <textbox name="subjectBox" />
 </panel>
 <textarea name="bodyBox" />
 <button text="Send" />
</panel>

swingify.c

#include <expat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int coreyNeedsToThinkOfAName = -1;

/* ------------------------------------------------------------------------- */

void start(void *data,
           const char *tag,
           const char **attributes) {
  /* printf("open %s\n", tag); */
  /* for (int i = 0; attributes[i] != NULL; i += 2) { */
    /* printf("%s -> %s\n", attributes[i], attributes[i + 1]); */
  /* } */

  if (strcmp(tag, "panel") == 0) {
    ++coreyNeedsToThinkOfAName;
    printf("JPanel panel%d = new JPanel();\n", coreyNeedsToThinkOfAName);
    printf("panel%d.setLayout(new BoxLayout(panel%d, BoxLayout.%s));\n", coreyNeedsToThinkOfAName, coreyNeedsToThinkOfAName, strcmp(attributes[1], "vertical") == 0 ? "PAGE_AXIS" : "LINE_AXIS");
  } else if (strcmp(tag, "label") == 0) {
    printf("panel%d.add(new JLabel(\"%s\"));\n", coreyNeedsToThinkOfAName, attributes[1]);
  } else if (strcmp(tag, "textbox") == 0) {
    printf("panel%d.add(new JTextField());\n", coreyNeedsToThinkOfAName);
  }
}

/* ------------------------------------------------------------------------- */

void end(void *data,
         const char *tag) {
  /* printf("close %s\n", tag); */
}

/* ------------------------------------------------------------------------- */

int main(int argc, char **argv) {
  XML_Parser parser = XML_ParserCreate(NULL);
  XML_SetElementHandler(parser, start, end);

  FILE *xml_file = fopen(argv[1], "r");
  int done = 0;

  const int BUFFER_SIZE = 8192;
  char buffer[BUFFER_SIZE];

  while (!done) {
    int length = fread(buffer, sizeof(char), BUFFER_SIZE, xml_file);
    done = feof(xml_file);
    XML_Parse(parser, buffer, length, done);
  }

  fclose(xml_file);
  XML_ParserFree(parser);

  return 0;
}

Haiku

Gramps says life was tough.
Read and write XML much?
That’s uphill both ways.