Debian Codegraph @

examples/src/common.c

raw
1 #include <stddef.h>
2 #include <stdio.h>
3 #include <string.h>
4
5 #if defined WIN32 || defined _WIN32 || defined MSDOS || defined __CYGWIN__ || defined __EMX__ || defined OS2
6 #include <io.h>
7 #include <fcntl.h>
8 #endif
9
10 #include <ogg/ogg.h>
11
12 #include "common.h"
13
14 /*
15 This function returns the next packet, reading data as necessary
16 */
17
18 int get_packet(ogg_sync_state *oy,ogg_stream_state *os,int *init,ogg_packet *op)
19 {
20 char *buffer;
21 size_t bytes;
22 ogg_page og;
23
24 /* try to get a packet from the stream */
25 if (*init && ogg_stream_packetout(os,op)) return 0;
26
27 /* read data and feed the pages to ogg */
28 buffer=ogg_sync_buffer(oy,4096);
29 bytes=fread(buffer,1,4096,stdin);
30 if (bytes==0) return 1; /* we're done */
31 ogg_sync_wrote(oy,bytes);
32 while (ogg_sync_pageout(oy,&og)>0) {
33 if (!*init && ogg_page_bos(&og)) {
34 ogg_stream_init(os,ogg_page_serialno(&og));
35 }
36 ogg_stream_pagein(os,&og);
37 if (!*init && ogg_page_bos(&og)) {
38 ogg_packet op;
39 ogg_stream_packetpeek(os,&op);
40 if (op.bytes>=8 && !memcmp(op.packet,"\200kate\0\0\0",8)) {
41 *init=1;
42 }
43 else {
44 ogg_stream_clear(os);
45 }
46 }
47 }
48
49 /* try again with the new data */
50 return get_packet(oy,os,init,op);
51 }
52
53 void set_binary_file(FILE *f)
54 {
55 #if defined WIN32 || defined _WIN32
56 _setmode(_fileno(f),_O_BINARY);
57 #else
58 #if defined MSDOS || defined __CYGWIN__ || defined __EMX__ || defined OS2 || defined __BORLANDC__
59 setmode(fileno(f),_O_BINARY);
60 #endif
61 #endif
62 }
63