public
Description: Ruby library for reading/writing vorbis comments
Homepage: http://code.jeremyevans.net/doc/ruby-vorbis_comment/
Clone URL: git://github.com/jeremyevans/ruby-vorbis_comment.git
ruby-vorbis_comment / vorbis_comment_ext.c
100644 162 lines (141 sloc) 5.165 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* Copyright (C) 2007 Jeremy Evans (code at jeremyevans net)
* Copyright (C) 2006 Tilman Sauerbeck (tilman at code-monkey de)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, version 2.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
 
#include <ruby.h>
#include <st.h>
#include <stdio.h>
#include <stdbool.h>
#include <errno.h>
#include <assert.h>
#include <limits.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ogg/ogg.h>
#include <vorbis/codec.h>
 
#include "vcedit.h"
 
static VALUE eOpen, eInvalidData, eInvalidComment, eTempFile, eReopen;
 
/*
* call-seq:
* object.read_fields -> fields
*
* Reads the comments from the file into the appropriate data structure.
* Returns the fields (a precreated CICPHash). Do not call this directly.
*/
VALUE read_fields (VALUE self) {
    vcedit_state *state;
    vorbis_comment *vc;
    VALUE fields, filename;
    VALUE k, v;
    int add_to_fields;
    int i;
 
    filename = rb_iv_get(self, "@filename");
    state = vcedit_state_new(StringValuePtr(filename));
    if (!state)
        rb_raise (rb_eNoMemError, "Out of Memory");
 
    switch (vcedit_open (state)) {
        case VCEDIT_ERR_OPEN:
            vcedit_state_unref(state);
            rb_raise (eOpen, "Cannot open file");
        case VCEDIT_ERR_INVAL:
            vcedit_state_unref(state);
            rb_raise (eInvalidData, "Invalid data");
        default:
            break;
    }
 
    vc = vcedit_comments(state);
    
    fields = rb_iv_get(self, "@fields");
    rb_funcall(fields, rb_intern("clear"), 0);
    add_to_fields = rb_intern("add_to_fields");
 
    /* check whether all comments are well-formed */
    for (i = 0; i < vc->comments; i++) {
        char *ptr, *content = vc->user_comments[i];
 
        ptr = strchr (content, '=');
        if (!ptr || ptr == content) {
            rb_funcall(fields, rb_intern("clear"), 0);
            vcedit_state_unref(state);
            rb_raise (eInvalidComment, "invalid comment - %s", content);
        }
        
        k = rb_str_new (content, ptr - content);
        v = rb_str_new2 (ptr + 1);
        rb_funcall(self, add_to_fields, 2, k, v);
    }
    
    vcedit_state_unref(state);
    return fields;
}
 
/*
* call-seq:
* object.write_fields(comments) -> comments
*
* Writes the comments to the file. Do not call this directly.
*/
VALUE write_fields (VALUE self, VALUE comments) {
    vcedit_state *state;
    vorbis_comment *vc;
    struct RArray *items, *comment;
    VALUE filename;
    int i, j;
 
    filename = rb_iv_get(self, "@filename");
    state = vcedit_state_new(StringValuePtr(filename));
    if (!state)
        rb_raise (rb_eNoMemError, "Out of Memory");
 
    switch (vcedit_open (state)) {
        case VCEDIT_ERR_OPEN:
            vcedit_state_unref(state);
            rb_raise (eOpen, "Cannot open file");
        case VCEDIT_ERR_INVAL:
            vcedit_state_unref(state);
            rb_raise (eInvalidData, "Invalid data");
        default:
            break;
    }
 
    vc = vcedit_comments(state);
    vorbis_comment_clear(vc);
    vorbis_comment_init(vc);
 
    items = RARRAY(comments);
    for (i = 0; i < items->len; i++) {
        comment = RARRAY(items->ptr[i]);
        vorbis_comment_add_tag(vc, StringValuePtr(comment->ptr[0]), StringValuePtr(comment->ptr[1]));
    }
    
    switch (vcedit_write (state)) {
        case VCEDIT_ERR_INVAL:
            vcedit_state_unref(state);
            rb_raise (eInvalidData, "Invalid data");
        case VCEDIT_ERR_TMPFILE:
            vcedit_state_unref(state);
            rb_raise (eTempFile, "Cannot create temporary file");
        case VCEDIT_ERR_REOPEN:
            vcedit_state_unref(state);
            rb_raise (eReopen, "Cannot reopen file");
        default:
            break;
    }
    vcedit_state_unref(state);
    
    return comments;
}
 
void Init_vorbis_comment_ext(void) {
    VALUE VorbisComment;
    VorbisComment = rb_define_class("VorbisComment", rb_cObject);
    rb_define_private_method(VorbisComment, "read_fields", read_fields, 0);
    rb_define_private_method(VorbisComment, "write_fields", write_fields, 1);
    eOpen = rb_define_class_under (VorbisComment, "OpenError", rb_eStandardError);
    eInvalidData = rb_define_class_under (VorbisComment, "InvalidDataError", rb_eStandardError);
    eInvalidComment = rb_define_class_under (VorbisComment, "InvalidCommentError", rb_eStandardError);
    eTempFile = rb_define_class_under (VorbisComment, "TempFileError", rb_eStandardError);
    eReopen = rb_define_class_under (VorbisComment, "ReopenError", rb_eStandardError);
}