r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / libmpeg3 / mpeg3io.c.unbuffered
blob3b107153088256e8e75cf44d5bf119ce9be40818
1 #include "mpeg3private.h"
2 #include "mpeg3protos.h"
4 #include <mntent.h>
5 #include <sys/stat.h>
6 #include <stdlib.h>
7 #include <string.h>
9 mpeg3_fs_t* mpeg3_new_fs(char *path)
11         mpeg3_fs_t *fs = calloc(1, sizeof(mpeg3_fs_t));
12         fs->css = mpeg3_new_css();
13         strcpy(fs->path, path);
14         return fs;
17 int mpeg3_delete_fs(mpeg3_fs_t *fs)
19         mpeg3_delete_css(fs->css);
20         free(fs);
21         return 0;
24 int mpeg3_copy_fs(mpeg3_fs_t *dst, mpeg3_fs_t *src)
26         strcpy(dst->path, src->path);
27         dst->current_byte = 0;
28         return 0;
31 long mpeg3io_get_total_bytes(mpeg3_fs_t *fs)
34  *      struct stat st;
35  *      if(stat(fs->path, &st) < 0) return 0;
36  *      return (long)st.st_size;
37  */
39         fseek(fs->fd, 0, SEEK_END);
40         fs->total_bytes = ftell(fs->fd);
41         fseek(fs->fd, 0, SEEK_SET);
42         return fs->total_bytes;
45 long mpeg3io_path_total_bytes(char *path)
47         struct stat st;
48         if(stat(path, &st) < 0) return 0;
49         return (long)st.st_size;
52 int mpeg3io_open_file(mpeg3_fs_t *fs)
54 /* Need to perform authentication before reading a single byte. */
55         mpeg3_get_keys(fs->css, fs->path);
57         if(!(fs->fd = fopen(fs->path, "rb")))
58         {
59                 perror("mpeg3io_open_file");
60                 return 1;
61         }
63         fs->total_bytes = mpeg3io_get_total_bytes(fs);
64         
65         if(!fs->total_bytes)
66         {
67                 fclose(fs->fd);
68                 return 1;
69         }
70         fs->current_byte = 0;
71         return 0;
74 int mpeg3io_close_file(mpeg3_fs_t *fs)
76         if(fs->fd) fclose(fs->fd);
77         fs->fd = 0;
78         return 0;
81 int mpeg3io_read_data(unsigned char *buffer, long bytes, mpeg3_fs_t *fs)
83         int result = 0;
84         result = !fread(buffer, 1, bytes, fs->fd);
85         fs->current_byte += bytes;
86         return (result && bytes);
89 int mpeg3io_device(char *path, char *device)
91         struct stat file_st, device_st;
92     struct mntent *mnt;
93         FILE *fp;
95         if(stat(path, &file_st) < 0)
96         {
97                 perror("mpeg3io_device");
98                 return 1;
99         }
101         fp = setmntent(MOUNTED, "r");
102     while(fp && (mnt = getmntent(fp)))
103         {
104                 if(stat(mnt->mnt_fsname, &device_st) < 0) continue;
105                 if(device_st.st_rdev == file_st.st_dev)
106                 {
107                         strncpy(device, mnt->mnt_fsname, MPEG3_STRLEN);
108                         break;
109                 }
110         }
111         endmntent(fp);
113         return 0;
116 int mpeg3io_seek(mpeg3_fs_t *fs, long byte)
118         fs->current_byte = byte;
119         return fseek(fs->fd, byte, SEEK_SET);
122 int mpeg3io_seek_relative(mpeg3_fs_t *fs, long bytes)
124         fs->current_byte += bytes;
125         return fseek(fs->fd, fs->current_byte, SEEK_SET);
128 void mpeg3io_complete_path(char *complete_path, char *path)
130         if(path[0] != '/')
131         {
132                 char current_dir[MPEG3_STRLEN];
133                 getcwd(current_dir, MPEG3_STRLEN);
134                 sprintf(complete_path, "%s/%s", current_dir, path);
135         }
136         else
137                 strcpy(complete_path, path);
140 void mpeg3io_get_directory(char *directory, char *path)
142         char *ptr = strrchr(path, '/');
143         if(ptr)
144         {
145                 int i;
146                 for(i = 0; i < ptr - path; i++)
147                 {
148                         directory[i] = path[i];
149                 }
150                 directory[i] = 0;
151         }
154 void mpeg3io_get_filename(char *filename, char *path)
156         char *ptr = strrchr(path, '/');
157         if(!ptr) 
158                 ptr = path;
159         else
160                 ptr++;
162         strcpy(filename, ptr);
165 void mpeg3io_joinpath(char *title_path, char *directory, char *new_filename)
167         sprintf(title_path, "%s/%s", directory, new_filename);
170 /* Find end of next 4 byte code */
171 int mpeg3io_next_code(mpeg3_fs_t *fs, uint32_t code, int count)
173         uint32_t header = 0;
175         while(header != code &&
176                 !mpeg3io_eof(fs) &&
177                 count > 0)
178         {
179                 header <<= 8;
180                 header |= mpeg3io_read_char(fs);
181                 count--;
182         }
184         return mpeg3io_eof(fs) || count <= 0;
187 /* Find start of previous 4 byte code */
188 int mpeg3io_prev_code(mpeg3_fs_t *fs, uint32_t code, int count)
190         uint32_t header = 0;
191 //printf("mpeg3io_prev_code %08x %08x %d %x %x\n", header, code, mpeg3io_bof(fs), count, fs->current_byte);
192         while(header != code &&
193                 !mpeg3io_bof(fs) &&
194                 count > 0)
195         {
196                 mpeg3io_seek_relative(fs, -1);
197                 header >>= 8;
198                 header |= ((uint32_t)mpeg3io_read_char(fs)) << 24;
199                 mpeg3io_seek_relative(fs, -1);
200                 count--;
201         }
202         
203         return mpeg3io_bof(fs) || count <= 0;