summaryrefslogtreecommitdiff
path: root/genindex/rmstream.c
blob: b77a7af4e8e1af0b240e9e0b95539a10ea8ca64c (plain)
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
/*
 * A VDR stream removing tool (C++)
 *
 * (C) 2005 Stefan Huelswitt <s.huelswitt@gmx.de>
 *
 * This code 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 code 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
 */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>

#include "pes.h"
#include "version.h"

// --- cRmStream ---------------------------------------------------------------

class cRmStream : public cPES {
private:
  int infile, outfile;
  unsigned char buff[KILOBYTE(256)];
protected:
  virtual int Output(const uchar *data, int len);
public:
  cRmStream(void);
  void Work(const char *inName, const char *outName);
  void Skip(int type);
  };

cRmStream::cRmStream(void)
{
  SetDefaultRule(prPass);
}

void cRmStream::Skip(int type)
{
  SetRule(type,prSkip);
}

void cRmStream::Work(const char *inName, const char *outName)
{
  infile=open(inName,O_RDONLY);
  if(infile>=0) {
    if(access(outName,F_OK)<0) {
      if(errno==ENOENT) {
        outfile=open(outName,O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
        if(outfile>=0) {
          while(1) {
            int c=read(infile,buff,sizeof(buff));
            if(c>0) {
              int u=Process(buff,c);
              if(c!=u) printf("bummer, count!=0\n");
              }
            else {
              if(c<0) printf("read failed: %s\n",strerror(errno));
              break;
              }
            }
          close(outfile);
          Statistics();
          }
        else printf("open failed: %s: %s\n",outName,strerror(errno));
        }
      else printf("access failed: %s: %s\n",outName,strerror(errno));
      }
    else printf("file exists: %s\n",outName);
    close(infile);
    }
  else printf("open failed: %s: %s\n",inName,strerror(errno));
}

int cRmStream::Output(const uchar *data, int len)
{
  int c=write(outfile,data,len);
  if(c<0) printf("write failed: %s\n",strerror(errno));
  return c;
}

// --- MAIN --------------------------------------------------------------------

void usage(void)
{
  printf("Usage: rmstream -d str [-o outfile] infile\n");
  exit(1);
}

int main(int argc, char *argv[])
{
  printf("This is %s %s (%s), (C) 2005 Stefan Hülswitt\n"
         "Released under the GNU Public License\n",
         "rmstream",PRG_VERSION,__DATE__);

  cRmStream gi;
  char *inName=0, *outName=0;
  int c, num=0;
  while((c=getopt(argc,argv,"d:o:"))!=-1) {
    switch(c) {
      case 'd':
        {
        int type=strtol(optarg,0,0);
        gi.Skip(type);
        printf("skipping stream 0x%x\n",type);
        num++;
        break;
        }
      case 'o':
        outName=optarg;
        break;
      case '?':
        usage();
      }
    }

  if(optind<argc) inName=argv[optind];
  else usage();

  if(num<1) usage();
  
  if(outName==0)
    asprintf(&outName,"%s.new",inName);

  gi.Work(inName,outName);
  return 0;
}