/** * dfun.c * Some experimental fun with dbus (or at least dbus-style xml). */ // +-------+---------------------------------------------------------- // | Notes | // +-------+ /* This is essentially me just playing with how I might parse the XML to find out things about the interface. */ // +---------+-------------------------------------------------------- // | Headers | // +---------+ #include #include #include #include // +-----------+------------------------------------------------------ // | Constants | // +-----------+ /** * The default size of buffers. */ #define DEFAULT_BUFFER_SIZE 32 // +-------+---------------------------------------------------------- // | Types | // +-------+ /** * */ // +---------+-------------------------------------------------------- // | Helpers | // +---------+ /** * Find the next tag in a file. Returns a string that represents * the tag. That string may change in future calls to xml_file_next_tag, * so you should make sure to copy it into a new structure if you need * it in the future. * Returns NULL if no tag is found. */ char * xml_file_next_tag (FILE *fp) { static gchar *buffer = NULL; // Buffer used for reading strings static gint bufsize = 0; // Size of that buffer. static gchar ch; // Character read static gint i = 0; // Index into buffer // Make sure that we have a buffer if (buffer == NULL) { buffer = g_try_malloc (sizeof (gchar) * DEFAULT_BUFFER_SIZE); if (buffer == NULL) return NULL; bufsize = DEFAULT_BUFFER_SIZE; } // Read until we hit the opening '<' or end of file while (((ch = getc (fp)) != '\0') && (ch != '<')) ; // If we've hit the end of file, give up if (ch == '\0') return NULL; // Read the remaining characters // FORTHCOMING return buffer; } // xml_file_next_tag /** * Find the next tag in a string. Sets startp and endp to the * beginning and end of that tag (if found). Returns 1 if * found, and 0 if not found. * * Hack: Assumes no > signs appear within the tag. */ char * xml_str_next_tag (const gchar *str, char **startp, char **endp) { // Find the start of the tag. char *start = strchr (str, '<'); if (start == NULL) return 0; // Find the end of the tag char *end = strchr (start, ">"); if (end == 0) return 0; // Return the information *startp = start; *endp = end; return 1; } // xml_str_next_tag // +------+----------------------------------------------------------- // | Main | // +------+ int main (int argc, char **argv) { char *contents; gsize length; GError *error; // Read the file if (! g_file_get_contents ("summary.xml", &contents, &length, &error)) { g_fprintf (stderr, "Error! %s.\n", error->message); return 1; } // Print it out g_printf ("%s", contents); // And we're done return 0; } // main