1 #include "mpeg3private.h"
2 #include "mpeg3protos.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);
17 int mpeg3_delete_fs(mpeg3_fs_t *fs)
19 mpeg3_delete_css(fs->css);
24 int mpeg3_copy_fs(mpeg3_fs_t *dst, mpeg3_fs_t *src)
26 strcpy(dst->path, src->path);
27 dst->current_byte = 0;
31 long mpeg3io_get_total_bytes(mpeg3_fs_t *fs)
35 * if(stat(fs->path, &st) < 0) return 0;
36 * return (long)st.st_size;
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)
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")))
59 perror("mpeg3io_open_file");
63 fs->total_bytes = mpeg3io_get_total_bytes(fs);
74 int mpeg3io_close_file(mpeg3_fs_t *fs)
76 if(fs->fd) fclose(fs->fd);
81 int mpeg3io_read_data(unsigned char *buffer, long bytes, mpeg3_fs_t *fs)
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;
95 if(stat(path, &file_st) < 0)
97 perror("mpeg3io_device");
101 fp = setmntent(MOUNTED, "r");
102 while(fp && (mnt = getmntent(fp)))
104 if(stat(mnt->mnt_fsname, &device_st) < 0) continue;
105 if(device_st.st_rdev == file_st.st_dev)
107 strncpy(device, mnt->mnt_fsname, MPEG3_STRLEN);
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)
132 char current_dir[MPEG3_STRLEN];
133 getcwd(current_dir, MPEG3_STRLEN);
134 sprintf(complete_path, "%s/%s", current_dir, path);
137 strcpy(complete_path, path);
140 void mpeg3io_get_directory(char *directory, char *path)
142 char *ptr = strrchr(path, '/');
146 for(i = 0; i < ptr - path; i++)
148 directory[i] = path[i];
154 void mpeg3io_get_filename(char *filename, char *path)
156 char *ptr = strrchr(path, '/');
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)
175 while(header != code &&
180 header |= mpeg3io_read_char(fs);
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)
191 //printf("mpeg3io_prev_code %08x %08x %d %x %x\n", header, code, mpeg3io_bof(fs), count, fs->current_byte);
192 while(header != code &&
196 mpeg3io_seek_relative(fs, -1);
198 header |= ((uint32_t)mpeg3io_read_char(fs)) << 24;
199 mpeg3io_seek_relative(fs, -1);
203 return mpeg3io_bof(fs) || count <= 0;