Credit Andrew - fix vorbis audio which was scratchy and ensure aging plugin does...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / adcuts.C
1 /*
2  *  CINELERRA
3  * Copyright (C) 2016-2020 William Morrow
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * by the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  */
19
20
21 #include "adcuts.h"
22 #include "filexml.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdint.h>
27 #include <unistd.h>
28 #include <string.h>
29
30 AdCuts::
31 AdCuts(int pid, int fd, const char *fn)
32 {
33         this->pid = pid;
34         this->fd = fd;
35         memset(this->filename,0,sizeof(this->filename));
36         if( fn ) strcpy(this->filename,fn);
37 }
38
39 AdCuts::
40 ~AdCuts()
41 {
42         if( fd < 0 ) return;
43         ::close(fd);
44         if( first ) {
45                 char cut_filename[BCTEXTLEN];
46                 strcpy(cut_filename, filename);
47                 strcpy(strrchr(cut_filename, '.'),".cut");
48                 write_cuts(cut_filename);
49                 printf(_("cuts to %s complete\n"),cut_filename);
50         }
51         else
52                 ::remove(filename);
53 }
54
55
56 int AdCuts::
57 load(FileXML &xml)
58 {
59         for(;;) {
60                 if( xml.read_tag() != 0 ) return 1;
61                 if( !xml.tag.title_is("CUT") ) break;
62                 double time = xml.tag.get_property("TIME", (double)0.0);
63                 int action = xml.tag.get_property("ACTION", (int)0);
64                 append(new AdCut(time, action));
65         }
66         return 0;
67 }
68
69 AdCuts *AdCuts::
70 read_cuts(const char *filename)
71 {
72         FileXML xml;
73         if( xml.read_from_file(filename, 1) ) return 0;
74         do {
75                 if( xml.read_tag() ) return 0;
76         } while( !xml.tag.title_is("CUTS") );
77
78         int pid = xml.tag.get_property("PID", (int)-1);
79         const char *file = xml.tag.get_property("FILE");
80         AdCuts *cuts = new AdCuts(pid, -1, file);
81         if( cuts->load(xml) || !xml.tag.title_is("/CUTS") ) {
82                  delete cuts; cuts = 0;
83         }
84         return cuts;
85 }
86
87 int AdCuts::
88 save(FileXML &xml)
89 {
90         xml.tag.set_title("CUTS");
91         xml.tag.set_property("PID", pid);
92         xml.tag.set_property("FILE", filename);
93         xml.append_tag();
94         xml.append_newline();
95
96         for( AdCut *cut=first; cut; cut=cut->next ) {
97                 xml.tag.set_title("CUT");
98                 xml.tag.set_property("TIME", cut->time);
99                 xml.tag.set_property("ACTION", cut->action);
100                 xml.append_tag();
101                 xml.tag.set_title("/CUT");
102                 xml.append_tag();
103                 xml.append_newline();
104         }
105
106         xml.tag.set_title("/CUTS");
107         xml.append_tag();
108         xml.append_newline();
109         return 0;
110 }
111
112 void AdCuts::
113 write_cuts(const char *filename)
114 {
115         FILE *fp = fopen(filename, "wb");
116         if( fp != 0 ) {
117                 FileXML xml;
118                 save(xml);
119                 xml.terminate_string();
120                 xml.write_to_file(fp);
121                 fclose(fp);
122         }
123 }
124