| 1 | #include <stdio.h> |
|---|
| 2 | #include <string.h> |
|---|
| 3 | #include <wizard/WizardsToolkit.h> |
|---|
| 4 | |
|---|
| 5 | int main(int argc, char **argv) |
|---|
| 6 | { |
|---|
| 7 | char |
|---|
| 8 | *xml; |
|---|
| 9 | |
|---|
| 10 | const char |
|---|
| 11 | *acronym, |
|---|
| 12 | *data_type, |
|---|
| 13 | *description, |
|---|
| 14 | *endian, |
|---|
| 15 | *mask, |
|---|
| 16 | *mime_type, |
|---|
| 17 | *offset, |
|---|
| 18 | *pattern, |
|---|
| 19 | *priority, |
|---|
| 20 | *match_type, |
|---|
| 21 | *value; |
|---|
| 22 | |
|---|
| 23 | ExceptionInfo |
|---|
| 24 | *exception; |
|---|
| 25 | |
|---|
| 26 | size_t |
|---|
| 27 | length; |
|---|
| 28 | |
|---|
| 29 | static char |
|---|
| 30 | *doc_type = |
|---|
| 31 | { |
|---|
| 32 | "<!DOCTYPE mimemap [\n" |
|---|
| 33 | " <!ELEMENT mimemap (mime+)>\n" |
|---|
| 34 | " <!ELEMENT mime (#PCDATA)>\n" |
|---|
| 35 | " <!ATTLIST mime type CDATA #REQUIRED>\n" |
|---|
| 36 | " <!ATTLIST mime acronym CDATA #IMPLIED>\n" |
|---|
| 37 | " <!ATTLIST mime description CDATA #IMPLIED>\n" |
|---|
| 38 | " <!ATTLIST mime pattern CDATA #IMPLIED>\n" |
|---|
| 39 | " <!ATTLIST mime offset CDATA #IMPLIED>\n" |
|---|
| 40 | " <!ATTLIST mime data-type (string|byte|short|long) #IMPLIED>\n" |
|---|
| 41 | " <!ATTLIST mime endian (lsb|msb) #IMPLIED>\n" |
|---|
| 42 | " <!ATTLIST mime magic CDATA #IMPLIED>\n" |
|---|
| 43 | " <!ATTLIST mime mask CDATA #IMPLIED>\n" |
|---|
| 44 | " <!ATTLIST mime priority CDATA #IMPLIED>\n" |
|---|
| 45 | "]>" |
|---|
| 46 | }; |
|---|
| 47 | |
|---|
| 48 | XMLTreeInfo |
|---|
| 49 | *comment, |
|---|
| 50 | *expanded_acronym, |
|---|
| 51 | *glob, |
|---|
| 52 | *magic, |
|---|
| 53 | *match, |
|---|
| 54 | *type, |
|---|
| 55 | *xml_info; |
|---|
| 56 | |
|---|
| 57 | if (argc != 2) |
|---|
| 58 | return(fprintf(stderr,"usage: %s xml-file\n", argv[0])); |
|---|
| 59 | exception=AcquireExceptionInfo(); |
|---|
| 60 | xml=(char *) FileToBlob(argv[1],~0UL,&length,exception); |
|---|
| 61 | if (xml == (char *) NULL) |
|---|
| 62 | return(fprintf(stderr,"%s: unable to read file\n",argv[1])); |
|---|
| 63 | xml_info=NewXMLTree(xml,exception); |
|---|
| 64 | xml=(char *) RelinquishWizardMemory(xml); |
|---|
| 65 | if (xml_info == (XMLTreeInfo *) NULL) |
|---|
| 66 | return(fprintf(stderr,"%s: unable to parse xml-file\n",argv[1])); |
|---|
| 67 | (void) printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); |
|---|
| 68 | (void) printf("%s\n",doc_type); |
|---|
| 69 | (void) printf("<mimemap>\n"); |
|---|
| 70 | type=GetXMLTreeChild(xml_info,"mime-type"); |
|---|
| 71 | while (type != (XMLTreeInfo *) NULL) |
|---|
| 72 | { |
|---|
| 73 | mime_type=GetXMLTreeAttribute(type,"type"); |
|---|
| 74 | acronym=(const char *) NULL; |
|---|
| 75 | expanded_acronym=GetXMLTreeChild(type,"acronym"); |
|---|
| 76 | if (expanded_acronym != (XMLTreeInfo *) NULL) |
|---|
| 77 | acronym=GetXMLTreeContent(expanded_acronym); |
|---|
| 78 | expanded_acronym=GetXMLTreeChild(type,"expanded-acronym"); |
|---|
| 79 | description=(const char *) NULL; |
|---|
| 80 | comment=GetXMLTreeChild(type,"comment"); |
|---|
| 81 | if (comment != (XMLTreeInfo *) NULL) |
|---|
| 82 | description=GetXMLTreeContent(comment); |
|---|
| 83 | if (expanded_acronym != (XMLTreeInfo *) NULL) |
|---|
| 84 | description=GetXMLTreeContent(expanded_acronym); |
|---|
| 85 | magic=GetXMLTreeChild(type,"magic"); |
|---|
| 86 | priority=(char *) NULL; |
|---|
| 87 | match=(XMLTreeInfo *) NULL; |
|---|
| 88 | if (magic != (XMLTreeInfo *) NULL) |
|---|
| 89 | { |
|---|
| 90 | priority=GetXMLTreeAttribute(magic,"priority"); |
|---|
| 91 | match=GetXMLTreeChild(magic,"match"); |
|---|
| 92 | } |
|---|
| 93 | while (match != (XMLTreeInfo *) NULL) |
|---|
| 94 | { |
|---|
| 95 | value=(char *) NULL; |
|---|
| 96 | match_type=(char *) NULL; |
|---|
| 97 | mask=(char *) NULL; |
|---|
| 98 | offset=(char *) NULL; |
|---|
| 99 | if (match != (XMLTreeInfo *) NULL) |
|---|
| 100 | { |
|---|
| 101 | value=GetXMLTreeAttribute(match,"value"); |
|---|
| 102 | match_type=GetXMLTreeAttribute(match,"type"); |
|---|
| 103 | offset=GetXMLTreeAttribute(match,"offset"); |
|---|
| 104 | mask=GetXMLTreeAttribute(match,"mask"); |
|---|
| 105 | } |
|---|
| 106 | (void) printf(" <mime"); |
|---|
| 107 | if (mime_type != (const char *) NULL) |
|---|
| 108 | (void) printf(" type=\"%s\"",mime_type); |
|---|
| 109 | if (description != (const char *) NULL) |
|---|
| 110 | (void) printf(" description=\"%s\"",description); |
|---|
| 111 | if (match_type != (const char *) NULL) |
|---|
| 112 | { |
|---|
| 113 | data_type="string"; |
|---|
| 114 | endian="undefined"; |
|---|
| 115 | if (strncmp(match_type,"little",6) == 0) |
|---|
| 116 | endian="LSB"; |
|---|
| 117 | if (strncmp(match_type,"big",3) == 0) |
|---|
| 118 | endian="MSB"; |
|---|
| 119 | if (strcmp(match_type,"byte") == 0) |
|---|
| 120 | data_type="byte"; |
|---|
| 121 | if (strcmp(match_type+strlen(match_type)-2,"16") == 0) |
|---|
| 122 | data_type="short"; |
|---|
| 123 | if (strcmp(match_type+strlen(match_type)-2,"32") == 0) |
|---|
| 124 | data_type="long"; |
|---|
| 125 | (void) printf(" data-type=\"%s\"",data_type); |
|---|
| 126 | if (strcmp(endian,"undefined") != 0) |
|---|
| 127 | (void) printf(" endian=\"%s\"",endian); |
|---|
| 128 | } |
|---|
| 129 | if (offset != (const char *) NULL) |
|---|
| 130 | (void) printf(" offset=\"%s\"",offset); |
|---|
| 131 | if (mask != (const char *) NULL) |
|---|
| 132 | (void) printf(" mask=\"%s\"",mask); |
|---|
| 133 | if (value != (const char *) NULL) |
|---|
| 134 | { |
|---|
| 135 | char |
|---|
| 136 | *magic; |
|---|
| 137 | |
|---|
| 138 | magic=AcquireString(value); |
|---|
| 139 | SubstituteString(&magic,"<","<"); |
|---|
| 140 | SubstituteString(&magic,">",">"); |
|---|
| 141 | SubstituteString(&magic,"\"","""); |
|---|
| 142 | (void) printf(" magic=\"%s\"",magic); |
|---|
| 143 | magic=(char *) RelinquishWizardMemory(magic); |
|---|
| 144 | } |
|---|
| 145 | if (priority != (const char *) NULL) |
|---|
| 146 | (void) printf(" priority=\"%s\"",priority); |
|---|
| 147 | (void) printf(" />\n"); |
|---|
| 148 | match=GetNextXMLTreeTag(match); |
|---|
| 149 | } |
|---|
| 150 | glob=GetXMLTreeChild(type,"glob"); |
|---|
| 151 | while (glob != (XMLTreeInfo *) NULL) |
|---|
| 152 | { |
|---|
| 153 | pattern=GetXMLTreeAttribute(glob,"pattern"); |
|---|
| 154 | value=(char *) NULL; |
|---|
| 155 | if (match) |
|---|
| 156 | value=GetXMLTreeAttribute(match,"value"); |
|---|
| 157 | (void) printf(" <mime"); |
|---|
| 158 | if (mime_type != (const char *) NULL) |
|---|
| 159 | (void) printf(" type=\"%s\"",mime_type); |
|---|
| 160 | if (acronym != (const char *) NULL) |
|---|
| 161 | (void) printf(" acronym=\"%s\"",acronym); |
|---|
| 162 | if (description != (const char *) NULL) |
|---|
| 163 | (void) printf(" description=\"%s\"",description); |
|---|
| 164 | (void) printf(" priority=\"100\""); |
|---|
| 165 | if (pattern != (const char *) NULL) |
|---|
| 166 | (void) printf(" pattern=\"%s\"",pattern); |
|---|
| 167 | (void) printf(" />\n"); |
|---|
| 168 | glob=GetNextXMLTreeTag(glob); |
|---|
| 169 | } |
|---|
| 170 | type=GetNextXMLTreeTag(type); |
|---|
| 171 | } |
|---|
| 172 | (void) printf("</mimemap>\n"); |
|---|
| 173 | xml=XMLTreeInfoToXML(xml_info); |
|---|
| 174 | (void) fprintf(stderr,"%s\n",xml); |
|---|
| 175 | xml=(char *) RelinquishWizardMemory(xml); |
|---|
| 176 | DestroyXMLTree(xml_info); |
|---|
| 177 | exception=DestroyExceptionInfo(exception); |
|---|
| 178 | return(0); |
|---|
| 179 | } |
|---|