initial commit
[goodguy/history.git] / cinelerra-5.0 / cinelerra / channeldb.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  * 
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * 
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  * 
20  */
21
22 #include "channel.h"
23 #include "channeldb.h"
24 #include "filesystem.h"
25 #include "filexml.h"
26 #include "preferences.inc"
27
28
29 #include <stdio.h>
30
31 ChannelDB::ChannelDB()
32 {
33 }
34
35 ChannelDB::~ChannelDB()
36 {
37         channels.remove_all_objects();
38 }
39
40 char* ChannelDB::prefix_to_path(char *path, const char *filename)
41 {
42         FileSystem fs;
43         char directory[BCTEXTLEN];
44         sprintf(directory, BCASTDIR);
45         fs.complete_path(directory);
46         fs.join_names(path, directory, filename);
47         return path;
48 }
49
50 void ChannelDB::load(const char *filename)
51 {
52         if( !filename ) return;
53
54         FileXML file;
55         char path[BCTEXTLEN];
56
57         prefix_to_path(path, filename);
58         channels.remove_all_objects();
59
60         int done = file.read_from_file(path, 1);
61
62         channels.remove_all_objects();
63 // Load channels
64         while(!done)
65         {
66                 Channel *channel = new Channel;
67                 if(!(done = channel->load(&file)))
68                         channels.append(channel);
69                 else
70                 {
71                         delete channel;
72                 }
73         }
74 }
75
76 void ChannelDB::save(const char *filename)
77 {
78         if( !filename ) return;
79         char path[BCTEXTLEN];
80         FileXML file;
81
82         prefix_to_path(path, filename);
83
84         if(channels.total)
85         {
86                 for(int i = 0; i < channels.total; i++)
87                 {
88 // Save channel here
89                         channels.values[i]->save(&file);
90                 }
91                 file.terminate_string();
92                 file.write_to_file(path);
93         }
94 }
95
96 Channel* ChannelDB::get(int number)
97 {
98         if(number >= 0 && number < channels.total)
99                 return channels.values[number];
100         else
101                 return 0;
102 }
103
104 void ChannelDB::copy_from(ChannelDB *src)
105 {
106         clear();
107         for(int i = 0; i < src->size(); i++)
108         {
109                 Channel *dst = new Channel;
110                 channels.append(dst);
111                 dst->copy_settings(src->get(i));
112         }
113 }
114
115 int ChannelDB::number_of(Channel *channel)
116 {
117         return channels.number_of(channel);
118 }
119
120 int ChannelDB::size()
121 {
122         return channels.total;
123 }
124
125 void ChannelDB::clear()
126 {
127         channels.remove_all_objects();
128 }
129
130 void ChannelDB::append(Channel *channel)
131 {
132         channels.append(channel);
133 }
134
135 void ChannelDB::remove_number(int number)
136 {
137         channels.remove_number(number);
138 }
139
140 void ChannelDB::set(int number, Channel *ptr)
141 {
142         channels.values[number] = ptr;
143 }