comments.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #define _GNU_SOURCE //asks stdio.h to include asprintf
  2. #include <stdio.h>
  3. #include <libxml2/libxml/xpath.h>
  4. #include <libxml2/libxml/xpathInternals.h>
  5. #include "stopif.h"
  6. #include "xmlbuff.h"
  7. #include "dict.h"
  8. static char* anonymizeAuthor(dictionary *authors, xmlChar const *authorName) {
  9. char *name = (char*)authorName;
  10. char *newName = (char*)dictionary_find(authors, name);
  11. if (newName)
  12. return newName;
  13. asprintf(&newName, "Author%d", authors->length+1);
  14. dictionary_add(authors, name, newName);
  15. return newName;
  16. }
  17. static void printAuthors(dictionary *authors) {
  18. for (int i=0; i<authors->length; i++)
  19. printf("\"%s\" is now \"%s\"\n", authors->pairs[i]->key, (char*)authors->pairs[i]->value);
  20. }
  21. int anonymizeComments(XMLBuff *infile) {
  22. dictionary *anonAuthors = dictionary_new();
  23. const xmlChar *authorPath = (xmlChar*)"//w:comment/@w:author";
  24. xmlDocPtr doc = xmlReadMemory(infile->data, infile->size, infile->name, NULL, 0);
  25. Stopif(!doc, return -1, "Error: unable to parse file \"%s\"\n", infile->name);
  26. xmlXPathContextPtr context = xmlXPathNewContext(doc);
  27. Stopif(!context, return -1, "Error: unable to create new XPath context\n");
  28. const xmlChar* prefix = (xmlChar*)"w";
  29. const xmlChar* ns = (xmlChar*)"http://schemas.openxmlformats.org/wordprocessingml/2006/main";
  30. Stopif(xmlXPathRegisterNs(context, prefix, ns), return -1, "Error: Can't add namespace!\n");
  31. xmlXPathObjectPtr authors = xmlXPathEvalExpression(authorPath, context);
  32. Stopif(!authors, return -1, "Something is wrong with XPATH %s\n", authorPath);
  33. xmlChar *authorName = (xmlChar*)"";
  34. for (int i=0; i < authors->nodesetval->nodeNr; i++){
  35. authorName = xmlNodeGetContent(authors->nodesetval->nodeTab[i]);
  36. char *anonAuthor = anonymizeAuthor(anonAuthors, authorName);
  37. xmlNodeSetContent(authors->nodesetval->nodeTab[i], (xmlChar*)anonAuthor);
  38. }
  39. xmlChar *buf;
  40. xmlDocDumpMemoryEnc(doc, &buf, &infile->size, "UTF-8");
  41. infile->data = (char*)buf;
  42. Stopif(!infile->size, return -1, "Errors: unable to save file %s\n", infile->name);
  43. xmlXPathFreeObject(authors);
  44. xmlXPathFreeContext(context);
  45. xmlFreeDoc(doc);
  46. xmlCleanupParser();
  47. printAuthors(anonAuthors);
  48. xmlFree(authorName);
  49. dictionary_free(anonAuthors);
  50. return 0;
  51. }