Credit Andrew - improve in-tree documentation
[goodguy/cinelerra.git] / cinelerra / adcuts.C
1
2 #include "adcuts.h"
3 #include "filexml.h"
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdint.h>
8 #include <unistd.h>
9 #include <string.h>
10
11 AdCuts::
12 AdCuts(int pid, int fd, const char *fn)
13 {
14         this->pid = pid;
15         this->fd = fd;
16         memset(this->filename,0,sizeof(this->filename));
17         if( fn ) strcpy(this->filename,fn);
18 }
19
20 AdCuts::
21 ~AdCuts()
22 {
23         if( fd < 0 ) return;
24         ::close(fd);
25         if( first ) {
26                 char cut_filename[BCTEXTLEN];
27                 strcpy(cut_filename, filename);
28                 strcpy(strrchr(cut_filename, '.'),".cut");
29                 write_cuts(cut_filename);
30                 printf(_("cuts to %s complete\n"),cut_filename);
31         }
32         else
33                 ::remove(filename);
34 }
35
36
37 int AdCuts::
38 load(FileXML &xml)
39 {
40         for(;;) {
41                 if( xml.read_tag() != 0 ) return 1;
42                 if( !xml.tag.title_is("CUT") ) break;
43                 double time = xml.tag.get_property("TIME", (double)0.0);
44                 int action = xml.tag.get_property("ACTION", (int)0);
45                 append(new AdCut(time, action));
46         }
47         return 0;
48 }
49
50 AdCuts *AdCuts::
51 read_cuts(const char *filename)
52 {
53         FileXML xml;
54         if( xml.read_from_file(filename, 1) ) return 0;
55         do {
56                 if( xml.read_tag() ) return 0;
57         } while( !xml.tag.title_is("CUTS") );
58
59         int pid = xml.tag.get_property("PID", (int)-1);
60         const char *file = xml.tag.get_property("FILE");
61         AdCuts *cuts = new AdCuts(pid, -1, file);
62         if( cuts->load(xml) || !xml.tag.title_is("/CUTS") ) {
63                  delete cuts; cuts = 0;
64         }
65         return cuts;
66 }
67
68 int AdCuts::
69 save(FileXML &xml)
70 {
71         xml.tag.set_title("CUTS");
72         xml.tag.set_property("PID", pid);
73         xml.tag.set_property("FILE", filename);
74         xml.append_tag();
75         xml.append_newline();
76
77         for( AdCut *cut=first; cut; cut=cut->next ) {
78                 xml.tag.set_title("CUT");
79                 xml.tag.set_property("TIME", cut->time);
80                 xml.tag.set_property("ACTION", cut->action);
81                 xml.append_tag();
82                 xml.tag.set_title("/CUT");
83                 xml.append_tag();
84                 xml.append_newline();
85         }
86
87         xml.tag.set_title("/CUTS");
88         xml.append_tag();
89         xml.append_newline();
90         return 0;
91 }
92
93 void AdCuts::
94 write_cuts(const char *filename)
95 {
96         FILE *fp = fopen(filename, "wb");
97         if( fp != 0 ) {
98                 FileXML xml;
99                 save(xml);
100                 xml.terminate_string();
101                 xml.write_to_file(fp);
102                 fclose(fp);
103         }
104 }
105