Add Russian translation provided by Валерий Крувялис <valkru@mail.ru>
[xiph-mirror.git] / theora-old / lib / comment.c
blobd9d87185467a12d6c7d21d6e3d498fdfd47fc5b3
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-2003 *
9 * by the Xiph.Org Foundation http://www.xiph.org/ *
10 * *
11 ********************************************************************
13 function: read/write and client interface for comment header packet
14 last mod: $Id$
16 ********************************************************************/
18 #include <stdlib.h>
19 #include <string.h>
20 #include <ctype.h>
21 #include "codec_internal.h"
23 void theora_comment_init(theora_comment *tc){
24 memset(tc,0,sizeof(*tc));
27 void theora_comment_add(theora_comment *tc,char *comment){
28 tc->user_comments=_ogg_realloc(tc->user_comments,
29 (tc->comments+2)*sizeof(*tc->user_comments));
30 tc->comment_lengths=_ogg_realloc(tc->comment_lengths,
31 (tc->comments+2)*sizeof(*tc->comment_lengths));
32 tc->comment_lengths[tc->comments]=strlen(comment);
33 tc->user_comments[tc->comments]=_ogg_malloc(tc->comment_lengths[tc->comments]+1);
34 strcpy(tc->user_comments[tc->comments], comment);
35 tc->comments++;
36 tc->user_comments[tc->comments]=NULL;
39 void theora_comment_add_tag(theora_comment *tc, char *tag, char *value){
40 char *comment=_ogg_malloc(strlen(tag)+strlen(value)+2); /* +2 for = and \0 */
41 strcpy(comment, tag);
42 strcat(comment, "=");
43 strcat(comment, value);
44 theora_comment_add(tc, comment);
45 _ogg_free(comment);
48 /* This is more or less the same as strncasecmp - but that doesn't exist
49 * everywhere, and this is a fairly trivial function, so we include it */
50 static int tagcompare(const char *s1, const char *s2, int n){
51 int c=0;
52 while(c < n){
53 if(toupper(s1[c]) != toupper(s2[c]))
54 return !0;
55 c++;
57 return 0;
60 char *theora_comment_query(theora_comment *tc, char *tag, int count){
61 long i;
62 int found = 0;
63 int taglen = strlen(tag)+1; /* +1 for the = we append */
64 char *fulltag = _ogg_malloc(taglen+ 1);
66 strcpy(fulltag, tag);
67 strcat(fulltag, "=");
69 for(i=0;i<tc->comments;i++){
70 if(!tagcompare(tc->user_comments[i], fulltag, taglen)){
71 if(count == found){
72 _ogg_free(fulltag);
73 /* We return a pointer to the data, not a copy */
74 return tc->user_comments[i] + taglen;
76 else
77 found++;
80 _ogg_free(fulltag);
81 return NULL; /* didn't find anything */
84 int theora_comment_query_count(theora_comment *tc, char *tag){
85 int i,count=0;
86 int taglen = strlen(tag)+1; /* +1 for the = we append */
87 char *fulltag = _ogg_malloc(taglen+1);
88 strcpy(fulltag,tag);
89 strcat(fulltag, "=");
91 for(i=0;i<tc->comments;i++){
92 if(!tagcompare(tc->user_comments[i], fulltag, taglen))
93 count++;
95 _ogg_free(fulltag);
96 return count;
99 void theora_comment_clear(theora_comment *tc){
100 if(tc){
101 long i;
102 for(i=0;i<tc->comments;i++)
103 if(tc->user_comments[i])_ogg_free(tc->user_comments[i]);
104 if(tc->user_comments)_ogg_free(tc->user_comments);
105 if(tc->comment_lengths)_ogg_free(tc->comment_lengths);
106 if(tc->vendor)_ogg_free(tc->vendor);
107 memset(tc,0,sizeof(*tc));