info.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
  5. * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  6. * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  7. * *
  8. * THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009 *
  9. * by the Xiph.Org Foundation and contributors http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function:
  13. last mod: $Id$
  14. ********************************************************************/
  15. #include <stdlib.h>
  16. #include <ctype.h>
  17. #include <string.h>
  18. #include "internal.h"
  19. /*This is more or less the same as strncasecmp, but that doesn't exist
  20. everywhere, and this is a fairly trivial function, so we include it.
  21. Note: We take advantage of the fact that we know _n is less than or equal to
  22. the length of at least one of the strings.*/
  23. static int oc_tagcompare(const char *_s1,const char *_s2,int _n){
  24. int c;
  25. for(c=0;c<_n;c++){
  26. if(toupper(_s1[c])!=toupper(_s2[c]))return !0;
  27. }
  28. return _s1[c]!='=';
  29. }
  30. void th_info_init(th_info *_info){
  31. memset(_info,0,sizeof(*_info));
  32. _info->version_major=TH_VERSION_MAJOR;
  33. _info->version_minor=TH_VERSION_MINOR;
  34. _info->version_subminor=TH_VERSION_SUB;
  35. _info->keyframe_granule_shift=6;
  36. }
  37. void th_info_clear(th_info *_info){
  38. memset(_info,0,sizeof(*_info));
  39. }
  40. void th_comment_init(th_comment *_tc){
  41. memset(_tc,0,sizeof(*_tc));
  42. }
  43. void th_comment_add(th_comment *_tc,const char *_comment){
  44. char **user_comments;
  45. int *comment_lengths;
  46. int comment_len;
  47. user_comments=_ogg_realloc(_tc->user_comments,
  48. (_tc->comments+2)*sizeof(*_tc->user_comments));
  49. if(user_comments==NULL)return;
  50. _tc->user_comments=user_comments;
  51. comment_lengths=_ogg_realloc(_tc->comment_lengths,
  52. (_tc->comments+2)*sizeof(*_tc->comment_lengths));
  53. if(comment_lengths==NULL)return;
  54. _tc->comment_lengths=comment_lengths;
  55. comment_len=strlen(_comment);
  56. comment_lengths[_tc->comments]=comment_len;
  57. user_comments[_tc->comments]=_ogg_malloc(comment_len+1);
  58. if(user_comments[_tc->comments]==NULL)return;
  59. memcpy(_tc->user_comments[_tc->comments],_comment,comment_len+1);
  60. _tc->comments++;
  61. _tc->user_comments[_tc->comments]=NULL;
  62. }
  63. void th_comment_add_tag(th_comment *_tc,const char *_tag,const char *_val){
  64. char *comment;
  65. int tag_len;
  66. int val_len;
  67. tag_len=strlen(_tag);
  68. val_len=strlen(_val);
  69. /*+2 for '=' and '\0'.*/
  70. comment=_ogg_malloc(tag_len+val_len+2);
  71. if(comment==NULL)return;
  72. memcpy(comment,_tag,tag_len);
  73. comment[tag_len]='=';
  74. memcpy(comment+tag_len+1,_val,val_len+1);
  75. th_comment_add(_tc,comment);
  76. _ogg_free(comment);
  77. }
  78. char *th_comment_query(th_comment *_tc,const char *_tag,int _count){
  79. long i;
  80. int found;
  81. int tag_len;
  82. tag_len=strlen(_tag);
  83. found=0;
  84. for(i=0;i<_tc->comments;i++){
  85. if(!oc_tagcompare(_tc->user_comments[i],_tag,tag_len)){
  86. /*We return a pointer to the data, not a copy.*/
  87. if(_count==found++)return _tc->user_comments[i]+tag_len+1;
  88. }
  89. }
  90. /*Didn't find anything.*/
  91. return NULL;
  92. }
  93. int th_comment_query_count(th_comment *_tc,const char *_tag){
  94. long i;
  95. int tag_len;
  96. int count;
  97. tag_len=strlen(_tag);
  98. count=0;
  99. for(i=0;i<_tc->comments;i++){
  100. if(!oc_tagcompare(_tc->user_comments[i],_tag,tag_len))count++;
  101. }
  102. return count;
  103. }
  104. void th_comment_clear(th_comment *_tc){
  105. if(_tc!=NULL){
  106. long i;
  107. for(i=0;i<_tc->comments;i++)_ogg_free(_tc->user_comments[i]);
  108. _ogg_free(_tc->user_comments);
  109. _ogg_free(_tc->comment_lengths);
  110. _ogg_free(_tc->vendor);
  111. memset(_tc,0,sizeof(*_tc));
  112. }
  113. }