tags.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* block-level tags for passing html blocks through the blender
  2. */
  3. #include "config.h"
  4. #define __WITHOUT_AMALLOC 1
  5. #include "cstring.h"
  6. #include "tags.h"
  7. STRING(struct kw) extratags;
  8. /* the standard collection of tags are built and sorted when
  9. * discount is configured, so all we need to do is pull them
  10. * in and use them.
  11. *
  12. * Additional tags still need to be allocated, sorted, and deallocated.
  13. */
  14. #include "blocktags"
  15. /* define an additional html block tag
  16. */
  17. void
  18. mkd_define_tag(char *id, int selfclose)
  19. {
  20. struct kw *p;
  21. /* only add the new tag if it doesn't exist in
  22. * either the standard or extra tag tables.
  23. */
  24. if ( !(p = mkd_search_tags(id, strlen(id))) ) {
  25. /* extratags could be deallocated */
  26. if ( S(extratags) == 0 )
  27. CREATE(extratags);
  28. p = &EXPAND(extratags);
  29. p->id = id;
  30. p->size = strlen(id);
  31. p->selfclose = selfclose;
  32. }
  33. }
  34. /* case insensitive string sort (for qsort() and bsearch() of block tags)
  35. */
  36. static int
  37. casort(struct kw *a, struct kw *b)
  38. {
  39. if ( a->size != b->size )
  40. return a->size - b->size;
  41. return strncasecmp(a->id, b->id, b->size);
  42. }
  43. /* stupid cast to make gcc shut up about the function types being
  44. * passed into qsort() and bsearch()
  45. */
  46. typedef int (*stfu)(const void*,const void*);
  47. /* sort the list of extra html block tags for later searching
  48. */
  49. void
  50. mkd_sort_tags()
  51. {
  52. qsort(T(extratags), S(extratags), sizeof(struct kw), (stfu)casort);
  53. }
  54. /* look for a token in the html block tag list
  55. */
  56. struct kw*
  57. mkd_search_tags(char *pat, int len)
  58. {
  59. struct kw key;
  60. struct kw *ret;
  61. key.id = pat;
  62. key.size = len;
  63. if ( (ret=bsearch(&key,blocktags,NR_blocktags,sizeof key,(stfu)casort)) )
  64. return ret;
  65. if ( S(extratags) )
  66. return bsearch(&key,T(extratags),S(extratags),sizeof key,(stfu)casort);
  67. return 0;
  68. }
  69. /* destroy the extratags list (for shared libraries)
  70. */
  71. void
  72. mkd_deallocate_tags()
  73. {
  74. if ( S(extratags) > 0 )
  75. DELETE(extratags);
  76. } /* mkd_deallocate_tags */