File: in_file.c

package info (click to toggle)
gmerlin-avdecoder 2.0.0~svn6298~dfsg0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,112 kB
  • sloc: ansic: 94,580; sh: 705; makefile: 705; awk: 43; sed: 16
file content (147 lines) | stat: -rw-r--r-- 3,928 bytes parent folder | download | duplicates (3)
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
/*****************************************************************
 * gmerlin-avdecoder - a general purpose multimedia decoding library
 *
 * Copyright (c) 2001 - 2012 Members of the Gmerlin project
 * gmerlin-general@lists.sourceforge.net
 * http://gmerlin.sourceforge.net
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * *****************************************************************/

#include <avdec_private.h>
#include <md5.h>


#define LOG_DOMAIN "in_file"
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdio.h>

/* stat */
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>


#ifdef _WIN32
#define BGAV_FSEEK(a,b,c) fseeko64(a,b,c)
#define BGAV_FTELL(a) ftello64(a)
#else
#ifdef HAVE_FSEEKO
#define BGAV_FSEEK fseeko
#else
#define BGAV_FSEEK fseek
#endif

#ifdef HAVE_FTELLO
#define BGAV_FTELL ftello
#else
#define BGAV_FTELL ftell
#endif

#endif

static int open_file(bgav_input_context_t * ctx, const char * url, char ** r)
  {
  gavl_dictionary_t * dict;
  FILE * f;
  struct stat st;
  uint8_t md5sum[16];
  if(!strncmp(url, "file://", 7))
    url += 7;
  
  f = fopen(url, "rb");
  if(!f)
    {
    gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN, "Cannot open %s: %s",
             url, strerror(errno));
    return 0;
    }
  ctx->priv = f;

  fstat(fileno(f), &st);
  dict = gavl_dictionary_get_src_nc(&ctx->m, GAVL_META_SRC, 0);

  gavl_dictionary_set_long(dict, GAVL_META_MTIME, st.st_mtime);
  
  
  BGAV_FSEEK((FILE*)(ctx->priv), 0, SEEK_END);
  ctx->total_bytes = BGAV_FTELL((FILE*)(ctx->priv));

  gavl_dictionary_set_long(dict, GAVL_META_TOTAL_BYTES, ctx->total_bytes);
  
  BGAV_FSEEK((FILE*)(ctx->priv), 0, SEEK_SET);
  
  ctx->filename = gavl_strdup(url);
  
  bgav_md5_buffer(ctx->filename, strlen(ctx->filename),
                  md5sum);
  
  ctx->index_file = bgav_sprintf("%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
                                 md5sum[0], md5sum[1], md5sum[2], md5sum[3], 
                                 md5sum[4], md5sum[5], md5sum[6], md5sum[7], 
                                 md5sum[8], md5sum[9], md5sum[10], md5sum[11], 
                                 md5sum[12], md5sum[13], md5sum[14], md5sum[15]);
  ctx->flags |= BGAV_INPUT_CAN_PAUSE;
  return 1;
  }

static int     read_file(bgav_input_context_t* ctx,
                         uint8_t * buffer, int len)
  {
  return fread(buffer, 1, len, (FILE*)(ctx->priv)); 
  }

static int64_t seek_byte_file(bgav_input_context_t * ctx,
                              int64_t pos, int whence)
  {
  BGAV_FSEEK((FILE*)(ctx->priv), ctx->position, SEEK_SET);
  return BGAV_FTELL((FILE*)(ctx->priv));
  }

static void close_file(bgav_input_context_t * ctx)
  {
  if(ctx->priv)
    fclose((FILE*)(ctx->priv));
  }

static int open_stdin(bgav_input_context_t * ctx, const char * url, char ** r)
  {
  ctx->priv = stdin;
  return 1;
  }

static void close_stdin(bgav_input_context_t * ctx)
  {
  /* Nothing to do here */
  }

const bgav_input_t bgav_input_file =
  {
    .name =      "file",
    .open =      open_file,
    .read =      read_file,
    .seek_byte = seek_byte_file,
    .close =     close_file
  };

const bgav_input_t bgav_input_stdin =
  {
    .name =      "stdin",
    .open =      open_stdin,
    .read =      read_file,
    .close =     close_stdin
  };