get_attached_files.c
changeset 0 ae7b3fa753dc
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/get_attached_files.c	Thu Nov 16 08:30:26 2006 +0000
     1.3 @@ -0,0 +1,148 @@
     1.4 +/*
     1.5 + * This program parses the XML data provided by sourceforge to extract the
     1.6 + * list of "attached files" (which are not themselves included in the XML
     1.7 + * stream and therefore need to be seperately downloaded for archiving).
     1.8 + */
     1.9 +
    1.10 +#include <stdlib.h>
    1.11 +#include <stdio.h>
    1.12 +#include <string.h>
    1.13 +#include <libxml/xmlmemory.h>
    1.14 +#include <libxml/parser.h>
    1.15 +
    1.16 +struct artifact_type {
    1.17 +    char *name;
    1.18 +    int id;
    1.19 +} artifact_types[] = {
    1.20 +    "Bugs",		109746,
    1.21 +    "Support Requests",	209746,
    1.22 +    "Patches",		309746,
    1.23 +    "Feature Requests",	359746,
    1.24 +};
    1.25 +
    1.26 +#ifndef NO_ELEMS
    1.27 +#define NO_ELEMS(array)	(sizeof(array)/sizeof(*(array)))
    1.28 +#endif
    1.29 +
    1.30 +void parse_history(xmlDocPtr doc,const xmlChar *artifact_id,
    1.31 +  const xmlChar *artifact_type,xmlNodePtr node)
    1.32 +{
    1.33 +    xmlNodePtr field,cur;
    1.34 +    xmlAttrPtr attr;
    1.35 +    xmlChar *name=NULL;
    1.36 +    xmlChar *field_name=NULL;
    1.37 +    xmlChar *text;
    1.38 +    int file_id,i;
    1.39 +    for(field=node->xmlChildrenNode;field;field=field->next)
    1.40 +	if (!xmlStrcmp(field->name,(const xmlChar *)"field"))
    1.41 +	{
    1.42 +	    for(attr=field->properties;attr;attr=attr->next)
    1.43 +		if (!xmlStrcmp(attr->name,(const xmlChar *)"name"))
    1.44 +		{
    1.45 +		    name=xmlNodeListGetString(doc,attr->children,1);
    1.46 +		    if (!xmlStrcmp(name,(const xmlChar *)"field_name"))
    1.47 +			field_name=xmlNodeListGetString(doc,field->children,1);
    1.48 +		    if (!xmlStrcmp(field_name,(const xmlChar *)"File Added") &&
    1.49 +		      !xmlStrcmp(name,(const xmlChar *)"old_value"))
    1.50 +		    {
    1.51 +			text=xmlNodeListGetString(doc,field->children,1);
    1.52 +			if (sscanf(text,"%d",&file_id)==1)
    1.53 +			{
    1.54 +			    for(i=NO_ELEMS(artifact_types)-1;i>=0;i--)
    1.55 +				if (!strcmp(artifact_types[i].name,
    1.56 +				  (const char *)artifact_type))
    1.57 +				    break;
    1.58 +			    if (i>=0)
    1.59 +				printf("%d %s %d\n",
    1.60 +				  artifact_types[i].id,artifact_id,file_id);
    1.61 +			    else
    1.62 +				fprintf(stderr,
    1.63 +				  "Warning: Unknown artifact type \"%s\" - "
    1.64 +				  "attached file %s ignored\n",
    1.65 +				  artifact_type,text);
    1.66 +			}
    1.67 +			xmlFree(text);
    1.68 +		    }
    1.69 +		    xmlFree(name);
    1.70 +		}
    1.71 +	}
    1.72 +    if (field_name)
    1.73 +	xmlFree(field_name);
    1.74 +}
    1.75 +
    1.76 +void parse_artifact(xmlDocPtr doc,xmlNodePtr node)
    1.77 +{
    1.78 +    xmlNodePtr field,cur;
    1.79 +    xmlAttrPtr attr;
    1.80 +    xmlChar *name=NULL;
    1.81 +    xmlChar *id=NULL;
    1.82 +    xmlChar *type=NULL;
    1.83 +    xmlChar *text;
    1.84 +    for(field=node->xmlChildrenNode;field;field=field->next)
    1.85 +	if (!xmlStrcmp(field->name,(const xmlChar *)"field"))
    1.86 +	{
    1.87 +	    for(attr=field->properties;attr;attr=attr->next)
    1.88 +		if (!xmlStrcmp(attr->name,(const xmlChar *)"name"))
    1.89 +		{
    1.90 +		    name=xmlNodeListGetString(doc,attr->children,1);
    1.91 +		    if (!xmlStrcmp(name,(const xmlChar *)"artifact_id"))
    1.92 +			id=xmlNodeListGetString(doc,field->children,1);
    1.93 +		    if (!xmlStrcmp(name,(const xmlChar *)"artifact_type"))
    1.94 +			type=xmlNodeListGetString(doc,field->children,1);
    1.95 +		}
    1.96 +	    if (!xmlStrcmp(name,(const xmlChar *)"artifact_history"))
    1.97 +		for(cur=field->xmlChildrenNode;cur;cur=cur->next)
    1.98 +		    if (!xmlStrcmp(cur->name,"history"))
    1.99 +			parse_history(doc,id,type,cur);
   1.100 +	    if (name)
   1.101 +	    {
   1.102 +		xmlFree(name);
   1.103 +		name=NULL;
   1.104 +	    }
   1.105 +	}
   1.106 +    if (id)
   1.107 +	xmlFree(id);
   1.108 +    if (type)
   1.109 +	xmlFree(type);
   1.110 +}
   1.111 +
   1.112 +int main(int argc,char **argv)
   1.113 +{
   1.114 +    xmlDocPtr doc;
   1.115 +    xmlNodePtr cur;
   1.116 +    doc=xmlParseFile(argv[1]);
   1.117 +    if (!doc)
   1.118 +    {
   1.119 +	fprintf(stderr,"Document not parsed successfully.\n");
   1.120 +	exit(1);
   1.121 +    }
   1.122 +    cur=xmlDocGetRootElement(doc);
   1.123 +    if (!cur)
   1.124 +    {
   1.125 +	fprintf(stderr,"Empty document.\n");
   1.126 +	xmlFreeDoc(doc);
   1.127 +	exit(1);
   1.128 +    }
   1.129 +    if (xmlStrcmp(cur->name,(const xmlChar *)"project_export"))
   1.130 +    {
   1.131 +	fprintf(stderr,"%s does not appear to be a project export document.\n",
   1.132 +	  argv[1]);
   1.133 +	xmlFreeDoc(doc);
   1.134 +	exit(1);
   1.135 +    }
   1.136 +    for(cur=cur->xmlChildrenNode;cur;cur=cur->next)
   1.137 +	if (!xmlStrcmp(cur->name,(const xmlChar *)"artifacts"))
   1.138 +	    break;
   1.139 +    if (!cur)
   1.140 +    {
   1.141 +	fprintf(stderr,"%s does not appear to contain an artifacts node.\n",
   1.142 +	  argv[1]);
   1.143 +	xmlFreeDoc(doc);
   1.144 +	exit(1);
   1.145 +    }
   1.146 +    for(cur=cur->xmlChildrenNode;cur;cur=cur->next)
   1.147 +	if (!xmlStrcmp(cur->name,(const xmlChar *)"artifact"))
   1.148 +	    parse_artifact(doc,cur);
   1.149 +    xmlFreeDoc(doc);
   1.150 +    exit(0);
   1.151 +}