This example program demonstrates how a CMML file that is given through a file uri and optionally contains a fragment offset can be interpreted. The example can be extended to other schemes such as http and to cover uri queries, too.
The procedure is illustrated in cmml-uri-file.c, which opens a file given through a file uri, and optionally seeks to an offset given the uri fragment specifier. It then prints out the descriptions of all the following clips:
00001 00031 #include <stdio.h> 00032 00033 #include <cmml.h> 00034 #include <string.h> 00035 00046 #define BUFSIZE 100000 00047 00049 typedef struct { 00050 char *scheme; 00051 char *authority; 00052 char *path; 00053 char *querystr; 00054 char *fragstr; 00055 } URI; 00056 00057 00066 static URI * 00067 parse_file_uri (const char *uri_string) 00068 { 00069 const char *location; 00070 const char *locbegin; 00071 int length; 00072 URI *result; 00073 locbegin = uri_string; 00074 result = (URI*) calloc(sizeof(URI), sizeof(char)); 00075 00076 /* ignore file:// and authority parts to get path */ 00077 location = strstr (locbegin, "://"); 00078 locbegin = location+3; 00079 length = strlen(locbegin); 00080 location = strchr(locbegin, '#'); /* XXX: ignore queries for the moment */ 00081 if (location != NULL) length = location - locbegin; 00082 result->path = (char*) calloc (length+1, sizeof(char)); 00083 result->path = strncpy(result->path, locbegin, length); 00084 result->path[length] = '\0'; 00085 00086 if (location != NULL) { 00087 /* fragment given */ 00088 length = strlen(location); 00089 result->fragstr = NULL; 00090 result->fragstr = (char*) calloc (length, sizeof(char)); 00091 result->fragstr = strncpy(result->fragstr, location+1, length); 00092 } else { 00093 result->fragstr = NULL; 00094 } 00095 return result; 00096 } 00097 00108 static int 00109 read_clip (CMML * cmml, const CMML_Clip * clip, void * user_data) { 00110 puts(clip->desc_text); 00111 return 0; 00112 } 00113 00125 int main(int argc, char *argv[]) 00126 { 00127 char *uri_string = NULL; 00128 URI * uri; 00129 CMML * doc; 00130 long n = 0; 00131 00132 if (argc < 2) { 00133 fprintf (stderr, "Usage: %s <file://filename#fragment>\n", argv[0]); 00134 exit (1); 00135 } 00136 uri_string = argv[1]; 00137 00138 uri = parse_file_uri(uri_string); 00139 00140 doc = cmml_open(uri->path); 00141 00142 /* if fragment given, forward to that */ 00143 if (uri->fragstr != NULL) cmml_skip_to_offset(doc, uri->fragstr); 00144 00145 cmml_set_read_callbacks (doc, NULL, NULL, read_clip, NULL); 00146 00147 while (((n = cmml_read (doc, BUFSIZE)) > 0)); 00148 00149 cmml_close(doc); 00150 00151 exit(0); 00152 }